clean
[sgn.git] / lib / SGN / Controller / AJAX / Expression.pm
blobd0fa28eb458b1a5dcd53df259739459898bf8d55
2 package SGN::Controller::AJAX::Expression;
4 use Moose;
6 use strict;
7 use warnings;
9 use CXGN::DB::DBICFactory;
10 use CXGN::GEM::Schema;
11 use CXGN::GEM::Template;
12 use CXGN::GEM::Expression;
13 # use Data::Dumper;
15 use CXGN::DB::Connection;
16 use CXGN::GEM::Schema::GeTemplate;
17 use CXGN::Chado::Dbxref::DbxrefI;
19 use CXGN::Page::FormattingHelpers qw| page_title_html info_table_html hierarchical_selectboxes_html |;
20 use CXGN::Page::UserPrefs;
22 BEGIN { extends 'Catalyst::Controller::REST' }
24 __PACKAGE__->config(
25 default => 'application/json',
26 stash_key => 'rest',
27 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
30 our %urlencode;
33 sub get_data :Path('/tools/expression/result') :Args(0) {
34 my ($self, $c) = @_;
36 # declare variables
37 my @all_data;
38 my %exp_design;
39 my $template_id;
40 my $query_gene = $c->req->param("gene");
41 $query_gene =~ s/\s+//g;
43 # to store errors as they happen
44 my @errors;
47 # get all data from the query gene
48 my @schema_list = ('gem', 'biosource', 'metadata', 'public');
49 my $schema = CXGN::DB::DBICFactory->open_schema( 'CXGN::GEM::Schema', search_path => \@schema_list, );
50 my @template_array = CXGN::GEM::Template->new_by_name($schema, $query_gene);
54 ## Add expression object
55 foreach my $template (@template_array) {
56 if (!defined $template || !defined $template->get_template_id()) {
57 push (@errors, "The query gene is not available in our database. Please try again\n");
58 } else {
59 $template_id = $template->get_template_id();
60 print "id: $template_id\n";
62 # get the experiment data from each experiment
63 my ($exp_data, $design_name, $design_desc, $exp_dbxref,$exp_pub) = get_expression_data($template_id,$schema);
65 $exp_design{"name"} = $design_name;
66 $exp_design{"description"} = $design_desc;
67 $exp_design{"dbxref"} = $exp_dbxref;
68 $exp_design{"pub"} = $exp_pub;
69 $exp_design{"data"} = $exp_data;
71 # save the final structure of data; An array containing one hash for each experiment
72 push(@all_data, \%exp_design);
76 # print STDERR "my data: ".Dumper(@all_data)."\n";
77 # print STDERR "my data: ".Dumper($all_data[0]{"data"})."\n";
80 # print STDERR "final design:".Dumper(@all_design_data)."\n";
82 # return errors if needed
83 if (scalar (@errors) > 0){
84 my $user_errors = join("<br />", @errors);
85 $c->stash->{rest} = {error => $user_errors};
86 return;
89 # return dat to mason view
90 $c->stash->{rest} = {
91 gene_id => $template_id,
92 gene_name => $query_gene,
93 all_exp_design =>\@all_data,
100 sub get_expression_data {
101 my $template_id = shift;
102 my $schema = shift;
103 my @one_design;
104 my @one_experiment;
105 my $experiment;
107 my $title;
108 my $journal;
109 my $pyear;
111 #----------------------------------
112 my %one_exp;
113 my @all_exp;
114 my @all_data;
115 #----------------------------------
117 my @exp_exp_values_rows = $schema->resultset('GeExpressionByExperiment')
118 ->search( { template_id => $template_id } );
120 # print STDERR "rows: ".Dumper(@exp_exp_values_rows)."\n";
122 foreach my $expexp_value_row (@exp_exp_values_rows) {
123 my %exp_values = $expexp_value_row->get_columns();
125 my $exp_id = $exp_values{'experiment_id'};
126 $one_experiment[0] = $exp_id;
128 $experiment = CXGN::GEM::Experiment->new($schema, $exp_id);
130 if (defined $experiment && defined $experiment->get_experiment_id() ) {
132 my $exp_name = $experiment->get_experiment_name();
133 # print STDERR "\nEXP_NAME: $exp_name\n";
135 my $exp_rep = $exp_values{'replicates_used'} || 'NA';
137 my $fpkm = Math::BigFloat->new( $exp_values{'mean'})
138 ->ffround(-2)
139 ->bstr();
141 # my $median = Math::BigFloat->new( $exp_values{'median'} )
142 # ->ffround(-2)
143 # ->bstr();
145 my $fpkm_lo = Math::BigFloat->new($exp_values{'standard_desviation'})
146 ->ffround(-2)
147 ->bstr();
149 my $fpkm_hi = Math::BigFloat->new($exp_values{'coefficient_of_variance'})
150 ->ffround(-2)
151 ->bstr();
153 $one_exp{"name"} = $exp_name;
154 $one_exp{"id"} = $exp_id;
155 $one_exp{"replicates"} = $exp_rep;
156 $one_exp{"fpkm"} = $fpkm;
157 $one_exp{"fpkm_lo"} = $fpkm_lo;
158 $one_exp{"fpkm_hi"} = $fpkm_hi;
159 $one_exp{"exp_description"} = $experiment->get_description();
163 ## Get the external links for experiment conditions
164 my @exp_dbxref_ids = $experiment->get_dbxref_list();
165 my $exp_dbxref_html = "";
167 foreach my $dbxref_id (@exp_dbxref_ids) {
168 $exp_dbxref_html = "$exp_dbxref_html ".get_dbxref_html($dbxref_id, $schema);
171 print STDERR "SRA dbxref: $exp_dbxref_html\n";
172 $one_exp{"exp_links"} = $exp_dbxref_html;
175 # my @target_list = $experiment->get_target_list();
176 # my $tissue = set_tissue($target_list[0]);
179 # print STDERR "one exp:".Dumper(@one_experiment)."\n";
180 my %tmp_hash = %one_exp;
181 push (@all_exp, \%tmp_hash);
182 # %one_exp;
185 # push (@all_exp, %one_exp);
186 # %one_exp;
189 my $expdesign = $experiment->get_experimental_design();
190 my $expdesign_name = $expdesign->get_experimental_design_name();
191 my $expdesign_desc = $expdesign->get_description();
192 my $pub_html = '';
193 my $expdesign_dbxref_html = '';
195 #print STDERR "$expdesign_name: experiment replicates: $exp_rep\n\n";
197 # get exp_design publications
198 my @pub_id_list = $expdesign->get_publication_list();
199 # my @pub_title_list = $expdesign->get_publication_list('title');
200 # my @pub_journal_list = $expdesign->get_publication_list('series_name');
201 # my @pub_year_list = $expdesign->get_publication_list('pyear');
205 my @dbxref_id_list = $expdesign->get_dbxref_list();
207 for (my $i = 0; $i < scalar(@pub_id_list); $i++) {
208 if ($pub_id_list[$i]) {
210 $expdesign_dbxref_html = "$expdesign_dbxref_html ".get_dbxref_html($dbxref_id_list[$i], $schema);
213 my ($pub_obj) = $schema->resultset('Pub::Pub')
214 ->search({ pub_id => $pub_id_list[$i]});
217 $title = $pub_obj->get_column('title');
218 $journal = $pub_obj->get_column('series_name');
219 $pyear = $pub_obj->get_column('pyear');
223 if ($title && $journal && $pyear) {
224 $pub_html = "$pub_html $title $journal $pyear.<br/>\n";
226 elsif ($title) {
227 $pub_html = "$pub_html $title<br/>\n";
231 # print STDERR "$expdesign_dbxref_html\n";
233 # my $exp_design_description = "<b>$expdesign_name:</b> $expdesign_desc.";
235 # if ($expdesign_dbxref_html ne '') {
236 # $exp_design_description = "$exp_design_description\n<br/><b>External links:</b> $expdesign_dbxref_html\n";
237 # # $exp_design_description = "$exp_design_description\n<b>External link:</b> $pub_dbxref_html\n";
239 # if ($pub_dbxref_html ne '') {
240 # $exp_design_description = "$exp_design_description\n<br/><b>Publications:</b> $pub_dbxref_html\n";
241 # # $exp_design_description = "$exp_design_description\n<b>External link:</b> $pub_dbxref_html\n";
244 # push (@all_data, %exp_design);
246 return (\@all_exp,$expdesign_name,$expdesign_desc,$expdesign_dbxref_html,$pub_html);
247 # return (\@one_design,$expdesign_name);
250 sub get_dbxref_html {
251 my $dbxref_id = shift;
252 my $schema = shift;
253 my $dbxref_html;
255 my ($dbxref_obj) = $schema->resultset('General::Dbxref')
256 ->search({ dbxref_id => $dbxref_id });
258 if (defined $dbxref_obj) {
259 my ($db_obj) = $schema->resultset('General::Db')
260 ->search({ db_id => $dbxref_obj->get_column('db_id') });
262 my $dbxref_url = $db_obj->get_column('urlprefix').$db_obj->get_column('url').$dbxref_obj->get_column('accession');
263 $dbxref_html = "<a href='".$dbxref_url."' target='_blank'>".$db_obj->get_column('name').":".$dbxref_obj->get_column('accession')."</a>";
266 return $dbxref_html;
270 sub set_tissue {
271 my $first_target = shift;
272 my $dbh = CXGN::DB::Connection->new();
273 my $tissue;
274 my @sample_list = $first_target->get_sample_list();
276 foreach my $sample (@sample_list) {
277 my %po = $sample_list[0]->get_dbxref_related('PO');
279 foreach my $sample_dbxref_id (keys %po) {
280 my %dbxref_po = %{ $po{$sample_dbxref_id} };
281 my $po_name = $dbxref_po{'cvterm.name'};
283 my $cvterm = CXGN::Chado::Cvterm->new( $dbh, $dbxref_po{'cvterm.cvterm_id'} );
284 my $accession = $cvterm->get_accession;
286 # print STDERR "$accession: $po_name\n";
289 return $tissue;