error message return if image processing fails
[sgn.git] / cgi-bin / chado / ajax_ontology_browser.pl
blobd2ba525615c74b92edd03dada5a2b5984f083f7e
2 use strict;
3 use warnings;
5 # a new version of the ontology browser by Lukas.
7 use CXGN::Scrap::AjaxPage;
8 use CXGN::DB::Connection;
9 use CXGN::Chado::Ontology;
10 use CXGN::Chado::Cvterm;
11 use JSON;
14 my $json = JSON->new();
15 my %res=();
17 my $s = CXGN::Scrap::AjaxPage->new();
18 my $dbh = CXGN::DB::Connection->new();
20 my ( $node, $action, $db_name, $term_name ) =
21 $s->get_encoded_arguments( "node", "action", "db_name", "term_name" );
23 my $cvterm = CXGN::Chado::Cvterm->new_with_accession( $dbh, $node );
25 my $empty_cvterm = CXGN::Chado::Cvterm->new($dbh);
26 my @response_nodes = ();
27 my $error = "";
28 if ( $action eq "children" ) {
29 @response_nodes = $cvterm->get_children();
31 elsif ( $action eq "parents" ) {
32 @response_nodes = $cvterm->get_recursive_parents();
34 elsif ( $action eq "roots" ) {
35 my @namespaces = ( 'GO', 'PO', 'SP', 'SO', 'PATO' );
36 my @roots = ();
37 foreach (@namespaces) {
38 push @roots, CXGN::Chado::Cvterm::get_roots( $dbh, $_ );
40 foreach (@roots) { push @response_nodes, [ $_, $empty_cvterm ] }
43 } elsif ( $action eq "match" ) {
44 my $synonym_query = $dbh->prepare(
45 "SELECT distinct(cvterm.dbxref_id), cv.name, cvterm.name, dbxref.accession, synonym
46 FROM public.cvterm
47 JOIN public.cv USING (cv_id)
48 LEFT JOIN public.cvtermsynonym USING (cvterm_id)
49 JOIN public.dbxref USING (dbxref_id)
50 JOIN public.db USING (db_id)
51 WHERE cvterm.is_obsolete= 0 AND
52 db.name=? AND
53 cvtermsynonym.synonym ilike '%$term_name%'
57 my $ontology_query = $dbh->prepare(
58 "SELECT distinct(cvterm.dbxref_id), cv.name, cvterm.name, dbxref.accession,
59 count(synonym)
60 FROM public.cvterm
61 JOIN public.cv USING (cv_id)
62 LEFT JOIN public.cvtermsynonym USING (cvterm_id)
63 JOIN public.dbxref USING (dbxref_id)
64 JOIN public.db USING (db_id)
65 WHERE cvterm.is_obsolete= 0 AND
66 db.name=? AND
67 (cvterm.name ilike '%$term_name%'
68 OR cvterm.definition ilike '%$term_name%'
70 GROUP BY cvterm.dbxref_id, cvterm.name, dbxref.accession, cv.name
71 ORDER BY cv.name, cvterm.name
74 $ontology_query->execute($db_name);
75 my %terms;
76 my ( $dbxref_id, $cv_name, $cvterm_name, $accession, $synonym ) =
77 $ontology_query->fetchrow_array();
79 while ($cvterm_name) {
80 $terms{$cv_name}{"$dbxref_id*$cv_name--$db_name:$accession--"} =
81 $cvterm_name;
82 ( $dbxref_id, $cv_name, $cvterm_name, $accession, $synonym ) =
83 $ontology_query->fetchrow_array();
86 $synonym_query->execute($db_name);
87 my @synonym_terms;
88 while ( my ( $dbxref_id, $cv_name, $cvterm_name, $accession, $synonym ) =
89 $synonym_query->fetchrow_array() )
91 if ( $terms{$cv_name}{"$dbxref_id*$cv_name--$db_name:$accession--"} ) {
92 $terms{$cv_name}{"$dbxref_id*$cv_name--$db_name:$accession--"} .=
93 " ($synonym)";
95 else {
96 $terms{$cv_name}{"$dbxref_id*$cv_name--$db_name:$accession--"} =
97 $cvterm_name . " ($synonym)";
101 #sort the hash of hashes by keys(cv_name) and then by values (term names)
102 my $print_string;
104 foreach my $cv_name ( sort ( keys %terms ) ) {
105 foreach my $key (
106 sort { $terms{$cv_name}{$a} cmp $terms{$cv_name}{$b} }
107 keys %{ $terms{$cv_name} }
110 $print_string .= $key . $terms{$cv_name}{$key};
111 $print_string .= "|";
114 $res{response} = $print_string;
116 } else { $res{error} = "ERROR. The action parameter is required."; }
119 my @response_list = ();
121 if (@response_nodes) {
122 foreach my $n (@response_nodes) {
123 my $has_children = 0;
124 if ( $n->[0]->count_children() > 0 ) { $has_children = 1; }
125 push @response_list,
126 ( $n->[0]->get_full_accession() ) . "\*"
127 . ( $n->[0]->get_cvterm_name() . "\*"
128 . ( $n->[0]->get_cvterm_id() )
129 . "\*$has_children" . "\*"
130 . $n->[1]->get_cvterm_name() );
132 $res{response} = join "#", @response_list;
136 $s->send_http_header();
137 print $json->encode(\%res);