BrAPI study POST call
[sgn.git] / cgi-bin / search / all_unig_for_annot.pl
blobc97e47999aa3cae87423c87712e3268466b809cf
1 use strict;
2 use CXGN::Page;
3 use CXGN::DB::Connection;
5 my $dbh = CXGN::DB::Connection->new();
7 #currently, we only search annotation based on clones
8 #combining annotation targets creates problems for determining
9 #the proper join path to pull in unigene info
11 my $manual_annot_matches_q = $dbh->prepare("select t.type_description, ma.annotation_text, a.first_name || ' ' || a.last_name, ma.last_modified from manual_annotations as ma left join sgn_people.sp_person as a on (ma.author_id=a.sp_person_id) left join annotation_target_type as t on (ma.annotation_target_type_id=t.annotation_target_type_id) where ma.manual_annotations_id=? and ma.annotation_target_type_id='1'");
13 my $manual_clone_unig_link_q = $dbh->prepare("select u.unigene_id, g.comment, ub.build_nr, ub.build_date, ub.unigene_build_id from manual_annotations as ma left join seqread as s on (ma.annotation_target_id=s.clone_id) left join est using (read_id) left join unigene_member using (est_id) left join unigene as u using (unigene_id) left join unigene_build as ub using (unigene_build_id) left join groups as g on (ub.organism_group_id=g.group_id) where ub.status='C' and ma.manual_annotations_id=? and ma.annotation_target_type_id='1'");
16 my $blast_matches_q = $dbh->prepare ("select bt.db_name, bd.defline, bt.blast_program from blast_defline as bd left join blast_targets as bt using (blast_target_id) where bd.defline_id=?");
18 my $blast_unig_link_q = $dbh->prepare("select u.unigene_id, g.comment, bh.score, bh.evalue, bh.identity_percentage, bh.apply_start, bh.apply_end from blast_defline as bd left join blast_hits as bh on (bd.defline_id=bh.defline_id) left join blast_annotations as ba using (blast_annotation_id) left join unigene as u on (ba.apply_id=u.unigene_id) left join unigene_build as ub using (unigene_build_id) left join groups as g on (ub.organism_group_id=g.group_id) where apply_type='15' and ub.status='C' and bd.defline_id=?");
20 my $page = CXGN::Page->new( "Annotation Search Results", "Dan");
22 my $desc_colour='#EEEEEE';
23 my $unig_link='/search/unigene.pl?unigene_id=';
26 #get all the relevant info for performing the search
27 my ($search_type, $match_id) = $page->get_arguments("search_type", "match_id");
29 unless ($match_id) {
30 null_request();
34 #execute the necessary SQL commands to get data
35 #do only search types requested
36 my @match_detail;
37 my @unigene_list;
39 if ($search_type eq 'manual_search'){
41 #get the text search match
43 #data returned by $manual_annot_matches_q is:
44 #t.type_description, ma.annotation_text, a.author_name, ma.last_modified
46 $manual_annot_matches_q->execute($match_id) or $page->error_page("Couldn't get manual match info\n");
48 if ($manual_annot_matches_q->rows == 0) {
49 no_matches($match_id);
52 if ($manual_annot_matches_q->rows > 1) {
53 too_many_matches($match_id);
56 my ($type_desc, $annot_text, $author_name, $last_updated) = $manual_annot_matches_q->fetchrow_array();
58 my $annot_target_desc = "$type_desc by $author_name on $last_updated";
59 @match_detail = ($annot_target_desc, $annot_text);
62 #get the unigene links
64 #data returned by $manual_clone_unig_link_q is:
65 #u.unigene_id, g.comment, ub.build_nr, ub.build_date, ub.unigene_build_id
67 $manual_clone_unig_link_q->execute($match_id) or $page->error_page("Couldn't run manual_clone_unig_link_q\n");
69 while (my ($unig_id, $build_desc, $build_nr, $build_date, $unig_build_id) = $manual_clone_unig_link_q->fetchrow_array()){
71 my $unig_desc="<tr><td></td><td align=\"left\" nowrap=\"nowrap\"><a href=\"$unig_link$unig_id\">SGN-U$unig_id</a></td><td align=\"left\" nowrap=\"nowrap\">$build_desc build $build_nr from $build_date</td><td></td></tr>";
73 push @unigene_list, [$unig_desc, $unig_build_id];
80 elsif($search_type eq 'blast_search'){
83 #data returned by $blast_matches_q is:
84 #bt.db_name, bd.defline, bt.blast_program
86 $blast_matches_q->execute($match_id) or $page->error_page("Couldn't get blast matches\n");
88 if ($blast_matches_q->rows == 0){
89 no_matches($match_id);
92 if ($blast_matches_q->rows > 1) {
93 too_many_matches($match_id);
96 my ($blast_target_db, $defline, $blast_program) = $blast_matches_q->fetchrow_array();
97 my $annot_target_desc = "Unigene <b>$blast_program</b> search against <b>$blast_target_db</b>";
98 @match_detail = ($annot_target_desc, $defline);
101 #data returned by $blast_unig_link_q is:
102 #u.unigene_id, g.comment, bh.score, bh.evalue, bh.identity_percentage, bh.apply_start, bh.apply_end
104 $blast_unig_link_q->execute($match_id) or $page->error_page("Couldn't get blast unigene links\n");
106 while (my ($unig_id, $build_desc, $blast_score, $evalue, $identity_pct, $span_start, $span_end)=$blast_unig_link_q->fetchrow_array()){
107 my $span_ln=abs($span_end - $span_start);
108 $identity_pct=sprintf "%7.2f", $identity_pct;
109 my $unig_desc="<tr><td></td><td align=\"left\" nowrap=\"nowrap\"><a href=\"$unig_link$unig_id\">SGN-U$unig_id</a></td><td align=\"left\" nowrap=\"nowrap\">$build_desc;</td><td align=\"left\" nowrap=\"nowrap\"> matched with $identity_pct% identity over ${span_ln}bp (e-value $evalue)</td></tr>";
110 push @unigene_list, [$unig_desc, $blast_score];
116 #get the data ready for display
117 my @results;
119 #match data is:
120 #[annotated_data_description, annotation_text];
122 my $match_text=$match_detail[1];
124 #strip <br /> tags.
125 #this is for the current specific version of manual annotation
126 #it is a kludge, there should be a text only searcheable field
127 # and a separate html enhanced text display field in the db
129 $match_text =~ s/\<br\>/ /g;
131 #insert <br /> in front of genbank id tags to break up long deflines
132 $match_text =~ s/(gi\|)/<br \/>$1/g;
134 my $web_format="
135 <tr><td bgcolor=\"$desc_colour\" align=\"left\" nowrap=\"nowrap\">$match_detail[0]</td></tr>
136 <tr><td colspan=\"2\">$match_text</td></tr>";
138 if (@unigene_list){
139 $web_format .= "
140 <tr><td colspan=\"2\" align=\"left\" nowrap=\"nowrap\"><br />Unigenes containing this annotation:</td></tr>
141 <tr><td colspan=\"2\" align=\"left\" nowrap=\"nowrap\">
142 <table align=\"left\" cellspacing=\"2\" cellpadding=\"0\" border=\"0\">
146 #unigene data is:
147 #[unigene_line, sorting_value]
148 # the unigene line has 4 columns
150 foreach (sort{$$b[1] <=> $$a[1]} @unigene_list){
151 $web_format .= "$$_[0]";
153 $web_format .="</table></td></tr>";
156 $web_format .= "<tr><td colspan=\"2\">&nbsp;</td></tr>";
157 push @results, $web_format;
161 #start printing the page
162 $page->header();
164 print<<EOF
165 <table align="center" cellspacing="0" cellpadding="2" border="0" width="100%">
166 <tr><td colspan="2" bgcolor="#FFFFFF"><br /></td></tr>
167 @results
168 <tr><td colspan="2" bgcolor="#FFFFFF"><br /></td></tr>
169 </table>
173 $page->footer();
179 sub no_matches {
180 my ($match_id) = @_;
182 $page->header();
184 print <<EOF;
186 <p>ERROR: No matches; should return at least one match for $match_id</p>
189 $page->footer();
191 exit 0;
194 sub too_many_matches {
195 my ($match_id) = @_;
197 $page->header();
199 print <<EOF;
201 <p>ERROR: Too many matches: should only return one match for $match_id</p>
205 $page->footer();
207 exit 0;
210 sub null_request {
212 $page->header();
214 print <<EOF;
216 <p><b>No match_id was found</b></p>
220 $page->footer();
222 exit 0;