[Bio] / FigWebServices / index.cgi Repository:
ViewVC logotype

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.130 - (view) (download)

1 : olson 1.93 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : overbeek 1.49 ### start
19 :    
20 : overbeek 1.120 use FIGRules;
21 :     my $file = &FIGRules::GetTempFileName;
22 :    
23 : efrank 1.1 use FIG;
24 : olson 1.89 use FIG_CGI;
25 : efrank 1.1
26 : mkubal 1.109 #use strict;
27 : parrello 1.78 use Tracer;
28 : golsen 1.97 use FIGjs qw( toolTipScript );
29 :     use GenoGraphics qw( render );
30 :     use gjoparseblast qw( next_blast_hsp );
31 : golsen 1.65
32 : efrank 1.92 use URI::Escape; # uri_escape
33 : olson 1.36 use POSIX;
34 : efrank 1.1 use HTML;
35 : golsen 1.65
36 : olson 1.89 my($fig, $cgi, $user);
37 : olson 1.90 my $this_script = "index.cgi";
38 : efrank 1.1
39 : olson 1.54 eval {
40 : olson 1.89 ($fig, $cgi, $user) = FIG_CGI::init(debug_save => 0,
41 :     debug_load => 0,
42 :     print_params => 0);
43 : olson 1.54 };
44 :    
45 :     if ($@ ne "")
46 :     {
47 :     my $err = $@;
48 :    
49 :     my(@html);
50 :    
51 :     push(@html, $cgi->p("Error connecting to SEED database."));
52 :     if ($err =~ /Could not connect to DBI:.*could not connect to server/)
53 :     {
54 : parrello 1.75 push(@html, $cgi->p("Could not connect to relational database of type $FIG_Config::dbms named $FIG_Config::db on port $FIG_Config::dbport."));
55 : olson 1.54 }
56 :     else
57 :     {
58 : parrello 1.75 push(@html, $cgi->pre($err));
59 : olson 1.54 }
60 :     &HTML::show_page($cgi, \@html, 1);
61 :     exit;
62 :     }
63 : olson 1.89
64 : parrello 1.78 Trace("Connected to FIG.") if T(2);
65 : overbeek 1.28 my($map,@orgs,$user,$map,$org,$made_by,$from_func,$to_func);
66 : efrank 1.1
67 : overbeek 1.85 #for my $k (sort keys %ENV)
68 :     #{
69 :     # warn "$k=$ENV{$k}\n";
70 :     #}
71 : overbeek 1.81
72 : efrank 1.1 $ENV{"PATH"} = "$FIG_Config::bin:$FIG_Config::ext_bin:" . $ENV{"PATH"};
73 :    
74 : overbeek 1.120 if (0)
75 :     {
76 : overbeek 1.125 my $VAR1;
77 :     eval(join("",`cat /tmp/index_parms`));
78 :     $cgi = $VAR1;
79 :     # print STDERR &Dumper($cgi);
80 :     }
81 :    
82 :     if (0)
83 :     {
84 : overbeek 1.120 print $cgi->header;
85 :     my @params = $cgi->param;
86 :     print "<pre>\n";
87 :     foreach $_ (@params)
88 :     {
89 :     print "$_\t:",join(",",$cgi->param($_)),":\n";
90 :     }
91 :    
92 :     if (0)
93 :     {
94 : overbeek 1.125 if (open(TMP,">/tmp/index_parms"))
95 : overbeek 1.120 {
96 :     print TMP &Dumper($cgi);
97 :     close(TMP);
98 :     }
99 :     }
100 :     exit;
101 :     }
102 :    
103 : efrank 1.1 my $html = [];
104 : golsen 1.23
105 : overbeek 1.51 my($pattern,$seq_pat,$tool,$ids,$subsearch);
106 : redwards 1.80
107 : overbeek 1.87 my $user = $cgi->param('user');
108 :    
109 : overbeek 1.28 if ($cgi->param('Search for Genes Matching an Occurrence Profile or Common to a Set of Organisms'))
110 : efrank 1.1 {
111 : parrello 1.78 Trace("Gene search chosen.") if T(2);
112 : golsen 1.23 unshift @$html, "<TITLE>The SEED: Phylogenetic Signatures</TITLE>\n";
113 : efrank 1.1 $ENV{"REQUEST_METHOD"} = "GET";
114 :     $ENV{"QUERY_STRING"} = "user=$user";
115 :     my @out = `./sigs.cgi`;
116 : overbeek 1.49 print @out;
117 :     exit;
118 :     }
119 : overbeek 1.128 elsif ($cgi->param('Search for Genes in Cluster, but Not Subsystems'))
120 :     {
121 :     $ENV{"REQUEST_METHOD"} = "GET";
122 :     $ENV{"QUERY_STRING"} = "user=$user";
123 :     my @out = `./clust_ss.cgi`;
124 :     print @out;
125 :     exit;
126 :     }
127 :    
128 : overbeek 1.49 #-----------------------------------------------------------------------
129 :     # Statistics for a single organism
130 :     #-----------------------------------------------------------------------
131 :     elsif ($cgi->param('statistics'))
132 :     {
133 : parrello 1.78 Trace("Statistics chosen.") if T(2);
134 : overbeek 1.49 @orgs = $cgi->param('korgs');
135 : olson 1.124 @orgs = map { $_ =~ /(\d+\.\d+)/; $1 } @orgs;
136 : overbeek 1.49 if (@orgs != 1)
137 :     {
138 : golsen 1.72 unshift @$html, "<TITLE>The SEED Statistics Page</TITLE>\n";
139 : overbeek 1.49 push(@$html,$cgi->h1('Please select a single organism to get statistcs'));
140 :     }
141 :     else
142 :     {
143 : parrello 1.75 $ENV{"REQUEST_METHOD"} = "GET";
144 :     $ENV{"QUERY_STRING"} = "user=$user&genome=$orgs[0]";
145 :     my @out = `./genome_statistics.cgi`;
146 :     print @out;
147 :     exit;
148 : overbeek 1.49 }
149 : efrank 1.1 }
150 : overbeek 1.17 #-----------------------------------------------------------------------
151 : overbeek 1.51 # Locate PEGs in Subsystems
152 :     #-----------------------------------------------------------------------
153 :     elsif ($cgi->param('Find PEGs') && ($subsearch = $cgi->param('subsearch')))
154 :     {
155 : parrello 1.78 Trace("PEG find chosen.") if T(2);
156 : overbeek 1.51 my $genome = $cgi->param('genome');
157 :     my (@pegs,$peg);
158 : overbeek 1.52
159 :     my @poss = $fig->by_alias($subsearch);
160 :     if (@poss > 0) { $subsearch = $poss[0] }
161 :    
162 : overbeek 1.51 if ($subsearch =~ /(fig\|\d+\.\d+\.peg\.\d+)/)
163 :     {
164 : parrello 1.75 # handle searching for homologs that occur in subsystems
165 :     $peg = $1;
166 :     @pegs = ($peg);
167 :     push(@pegs,map { $_->id2 } $fig->sims( $peg, 500, 1.0e-10, "fig"));
168 :     if ($genome)
169 :     {
170 :     my $genomeQ = quotemeta $genome;
171 :     @pegs = grep { $_ =~ /^fig\|$genomeQ/ } @pegs;
172 :     }
173 : overbeek 1.51 }
174 :     else
175 :     {
176 : parrello 1.75 # handle searching for PEGs with functional role in subsystems
177 :     @pegs = $fig->seqs_with_role($subsearch,"master",$genome);
178 : overbeek 1.51 }
179 :    
180 :     print $cgi->header;
181 :     if (@pegs == 0)
182 :     {
183 : parrello 1.75 print $cgi->h1("Sorry, could not even find PEGs to check");
184 : overbeek 1.51 }
185 :     else
186 :     {
187 : parrello 1.75 my(@pairs,$pair,@sub);
188 :     @pairs = map { $peg = $_;
189 :     @sub = $fig->peg_to_subsystems($peg);
190 :     map { [$peg,$_] } @sub } @pegs;
191 :     if (@pairs == 0)
192 :     {
193 :     print $cgi->h1("Sorry, could not map any PEGs to subsystems");
194 :     }
195 :     else
196 :     {
197 :     my($uni,$uni_func);
198 :     my $col_hdrs = ["PEG","Genome","Function","UniProt","UniProt Function","Subsystem"];
199 :     my $tab = [ map { $pair = $_; $uni = $fig->to_alias($pair->[0],"uni");
200 :     ($uni,$uni_func) = $uni ? (&HTML::uni_link($cgi,$uni),scalar $fig->function_of($uni)) : ("","");
201 :     [&HTML::fid_link($cgi,$pair->[0]),
202 :     $fig->org_of($pair->[0]),
203 :     scalar $fig->function_of($pair->[0]),
204 :     $uni,$uni_func,
205 :     &HTML::sub_link($cgi,$pair->[1])] } @pairs];
206 :     print &HTML::make_table($col_hdrs,$tab,"PEGs that Occur in Subsystems");
207 :     }
208 : overbeek 1.51 }
209 :     exit;
210 :     }
211 :     #-----------------------------------------------------------------------
212 : overbeek 1.31 # Align Sequences
213 :     #-----------------------------------------------------------------------
214 :     elsif ($cgi->param('Align Sequences'))
215 :     {
216 : parrello 1.78 Trace("Sequence alignment chosen.");
217 : overbeek 1.31 my $seqs = $cgi->param('seqids');
218 :     $seqs =~ s/^\s+//;
219 :     $seqs =~ s/\s+$//;
220 :     my @seq_ids = split(/[ \t,;]+/,$seqs);
221 :     if (@seq_ids < 2)
222 :     {
223 : parrello 1.75 print $cgi->header;
224 :     print $cgi->h1("Sorry, you need to specify at least two sequence IDs");
225 : overbeek 1.31 }
226 :     else
227 :     {
228 : parrello 1.75 $ENV{"REQUEST_METHOD"} = "GET";
229 :     $_ = join('&checked=',@seq_ids);
230 :     $ENV{"QUERY_STRING"} = "user=$user&align=1&checked=" . $_;
231 :     my @out = `./fid_checked.cgi`;
232 :     print join("",@out);
233 : overbeek 1.31 }
234 :     exit;
235 :     }
236 :     #-----------------------------------------------------------------------
237 : overbeek 1.17 # Search (text) || Find Genes in Org that Might Play the Role
238 :     #-----------------------------------------------------------------------
239 : golsen 1.59 elsif ( ( $pattern = $cgi->param('pattern') )
240 :     && ( $cgi->param('Search')
241 :     || $cgi->param('Search genome selected below')
242 : redwards 1.63 || $cgi->param('Search Selected Organisms')
243 : golsen 1.59 || $cgi->param('Find Genes in Org that Might Play the Role')
244 :     )
245 :     )
246 : efrank 1.1 {
247 : parrello 1.78 Trace("Pattern search chosen.") if T(2);
248 : overbeek 1.17 # Remove leading and trailing spaces from pattern -- GJO:
249 :     $pattern =~ s/^\s+//;
250 :     $pattern =~ s/\s+$//;
251 : efrank 1.1 if ($cgi->param('Find Genes in Org that Might Play the Role') &&
252 : parrello 1.75 (@orgs = $cgi->param('korgs')) && (@orgs == 1))
253 : efrank 1.1 {
254 : parrello 1.75 unshift @$html, "<TITLE>The SEED: Genes in that Might Play Specific Role</TITLE>\n";
255 : olson 1.124 @orgs = map { $_ =~ /(\d+\.\d+)/; $1 } @orgs;
256 : parrello 1.75 $ENV{"REQUEST_METHOD"} = "GET";
257 :     $ENV{"QUERY_STRING"} = "user=$user&request=find_in_org&role=$pattern&org=$orgs[0]";
258 :     my @out = `./pom.cgi`;
259 :     print join("",@out);
260 :     exit;
261 : efrank 1.1 }
262 :     else
263 :     {
264 : parrello 1.75 unshift @$html, "<TITLE>The SEED: Search Results</TITLE>\n";
265 :     &show_indexed_objects($fig, $cgi, $html, $pattern);
266 : efrank 1.1 }
267 :     }
268 : overbeek 1.17 #-----------------------------------------------------------------------
269 :     # Metabolic Overview
270 :     #-----------------------------------------------------------------------
271 : efrank 1.1 elsif (($map = $cgi->param('kmap')) && $cgi->param('Metabolic Overview'))
272 :     {
273 : parrello 1.78 Trace("Metabolic overview chosen.") if T(2);
274 : olson 1.38 if ($map =~ /\(([^)]*)\)$/)
275 :     {
276 : parrello 1.75 $map = $1;
277 : olson 1.38 }
278 :     else
279 :     {
280 : parrello 1.75 # ??? Gary ???
281 : olson 1.38 }
282 :    
283 :     #$map =~ s/^.*\((MAP\d+)\).*$/$1/;
284 : efrank 1.1 @orgs = $cgi->param('korgs');
285 : olson 1.124 @orgs = map { $_ =~ /(\d+\.\d+)/; $1 } @orgs;
286 : efrank 1.1 $ENV{"REQUEST_METHOD"} = "GET";
287 :     if (@orgs > 0)
288 :     {
289 : parrello 1.75 $ENV{"QUERY_STRING"} = "user=$user&map=$map&org=$orgs[0]";
290 : efrank 1.1 }
291 :     else
292 :     {
293 : parrello 1.75 $ENV{"QUERY_STRING"} = "user=$user&map=$map";
294 : efrank 1.1 }
295 :    
296 : golsen 1.23 unshift @$html, "<TITLE>The SEED: Metabolic Overview</TITLE>\n";
297 : olson 1.38 my @out = `./show_map.cgi`;
298 : efrank 1.1 &HTML::trim_output(\@out);
299 : golsen 1.23 push( @$html, "<br>\n", @out );
300 : efrank 1.1 }
301 : overbeek 1.17
302 :     #-----------------------------------------------------------------------
303 :     # Search for Matches (sequence or pattern)
304 :     #-----------------------------------------------------------------------
305 : efrank 1.1 elsif (($seq_pat = $cgi->param('seq_pat')) &&
306 :     ($tool = $cgi->param('Tool')) &&
307 :     $cgi->param('Search for Matches'))
308 :     {
309 : parrello 1.78 Trace("Match search chosen.") if T(2);
310 : overbeek 1.30 @orgs = $cgi->param('korgs');
311 :     if (@orgs > 0)
312 :     {
313 : olson 1.124 @orgs = map { $_ =~ /(\d+\.\d+)/; $1 } @orgs;
314 : overbeek 1.30 }
315 :     else
316 :     {
317 : parrello 1.75 @orgs = ("");
318 : overbeek 1.30 }
319 :    
320 : efrank 1.1 if ($tool =~ /blast/)
321 :     {
322 : parrello 1.75 unshift @$html, "<TITLE>The SEED: BLAST Search Results</TITLE>\n";
323 :     &run_blast($fig,$cgi,$html,$orgs[0],$tool,$seq_pat);
324 : efrank 1.1 }
325 :     elsif ($tool =~ /Protein scan_for_matches/)
326 :     {
327 : parrello 1.75 unshift @$html, "<TITLE>The SEED: Protein Pattern Match Results</TITLE>\n";
328 :     &run_prot_scan_for_matches($fig,$cgi,$html,$orgs[0],$seq_pat);
329 : efrank 1.1 }
330 :     elsif ($tool =~ /DNA scan_for_matches/)
331 :     {
332 : parrello 1.75 unshift @$html, "<TITLE>The SEED: Nucleotide Pattern Match Results</TITLE>\n";
333 :     &run_dna_scan_for_matches($fig,$cgi,$html,$orgs[0],$seq_pat);
334 : efrank 1.1 }
335 :     }
336 : overbeek 1.7 elsif (($made_by = $cgi->param('made_by')) && $cgi->param('Extract Assignments'))
337 :     {
338 : parrello 1.78 Trace("Assignment export chosen.") if T(2);
339 : overbeek 1.7 &export_assignments($fig,$cgi,$html,$made_by);
340 :     }
341 : overbeek 1.28 elsif ($cgi->param('Generate Assignments via Translation') &&
342 :     ($from_func = $cgi->param('from_func')) &&
343 :     ($to_func = $cgi->param('to_func')))
344 :     {
345 : parrello 1.78 Trace("Assignment translate chosen.") if T(2);
346 : overbeek 1.28 &translate_assignments($fig,$cgi,$html,$from_func,$to_func);
347 :     }
348 : golsen 1.96
349 : overbeek 1.30 elsif ($cgi->param('Extract Matched Sequences') && ($ids = $cgi->param('ids')))
350 :     {
351 : parrello 1.78 Trace("Matched sequence extract chosen.") if T(2);
352 : overbeek 1.30 my @ids = split(/,/,$ids);
353 : golsen 1.96
354 :     # Truncate the list if requested:
355 :    
356 : overbeek 1.30 my($list_to,$i);
357 :     if ($list_to = $cgi->param('list_to'))
358 :     {
359 : parrello 1.75 for ($i=0; ($i < @ids) && ($ids[$i] ne $list_to); $i++) {}
360 :     if ($i < @ids)
361 :     {
362 :     $#ids = $i;
363 :     }
364 : overbeek 1.30 }
365 :    
366 : golsen 1.96 # Print the sequences:
367 :     # Add organisms -- GJO
368 : overbeek 1.30
369 : golsen 1.96 my( $id, $seq, $desc, $func, $org );
370 :     push( @$html, $cgi->pre );
371 : overbeek 1.30 foreach $id (@ids)
372 :     {
373 : parrello 1.75 if ($seq = $fig->get_translation($id))
374 :     {
375 : golsen 1.96 $desc = $id;
376 :     if ( $func = $fig->function_of( $id ) )
377 :     {
378 :     $desc .= " $func";
379 :     }
380 :     if ( $org = $fig->genus_species( $fig->genome_of( $id ) ) )
381 :     {
382 :     $desc .= " [$org]" if $org;
383 :     }
384 :     push( @$html, ">$desc\n" );
385 : parrello 1.75 for ($i=0; ($i < length($seq)); $i += 60)
386 :     {
387 : golsen 1.96 # substr does not mind a request for more than length
388 :     push( @$html, substr( $seq, $i, 60 ) . "\n" );
389 : parrello 1.75 }
390 :     }
391 : overbeek 1.30 }
392 :     push(@$html,$cgi->end_pre);
393 :     }
394 : overbeek 1.17
395 :     #-----------------------------------------------------------------------
396 :     # Initial search page
397 :     #-----------------------------------------------------------------------
398 : efrank 1.1 else
399 :     {
400 : parrello 1.78 Trace("SEED Entry page chosen.") if T(2);
401 : golsen 1.23 unshift @$html, "<TITLE>The SEED: Entry Page</TITLE>\n";
402 : efrank 1.1 &show_initial($fig,$cgi,$html);
403 :     }
404 : parrello 1.115 Trace("Showing page.") if T(3);
405 : efrank 1.1 &HTML::show_page($cgi,$html,1);
406 : parrello 1.115 Trace("Page shown.") if T(3);
407 : overbeek 1.49 exit;
408 : efrank 1.1
409 : overbeek 1.17
410 :     #==============================================================================
411 :     # Initial page (alias search)
412 :     #==============================================================================
413 :    
414 : efrank 1.1 sub show_initial {
415 :     my($fig,$cgi,$html) = @_;
416 :     my($map,$name,$olrg,$gs);
417 :    
418 : overbeek 1.83
419 :     #
420 :     # Display the message of the day, if present.
421 :     #
422 :    
423 :     show_motd($fig, $cgi, $html);
424 :    
425 : golsen 1.113 # The original $a and $b conflicted with explicit sort variables (ouch):
426 :     # "Can't use "my $a" in sort comparison" -- GJO
427 :    
428 :     my( $at, $bt, $et, $v, $envt ) = $fig->genome_counts;
429 : parrello 1.115 push(@$html,$cgi->h2("Contains $at archaeal, $bt bacterial, $et eukaryal, $v viral and $envt environmental genomes"));
430 : golsen 1.113 my( $ac, $bc, $ec ) = $fig->genome_counts("complete");
431 :     push(@$html,$cgi->h2("Of these, $ac archaeal, $bc bacterial and $ec eukaryal genomes are more-or-less complete"),$cgi->hr);
432 : efrank 1.1
433 :     push(@$html,
434 : parrello 1.75 $cgi->h2('Work on Subsystems'),
435 : overbeek 1.46
436 : parrello 1.75 # $cgi->start_form(-action => "ssa2.cgi"),
437 :     # "Enter user: ",
438 :     # $cgi->textfield(-name => "user", -size => 20),
439 :     # $cgi->submit('Work on Subsystems'),
440 :     # $cgi->end_form,
441 :    
442 :     # $cgi->h2('Work on Subsystems Using New, Experimental Code'),
443 : overbeek 1.111 # "This is the <i>new</i> subsystems code, and is now officially released.",
444 : parrello 1.75 $cgi->start_form(-action => "subsys.cgi"),
445 :     "Enter user: ",
446 :     $cgi->textfield(-name => "user", -size => 20),
447 :     $cgi->submit('Work on Subsystems'),
448 :     $cgi->end_form,
449 :     $cgi->hr,
450 : overbeek 1.114 $cgi->start_form(-action => "ff.cgi"),
451 :     "Enter user: ",
452 :     $cgi->textfield(-name => "user", -size => 20),
453 :     $cgi->submit('Work on FIGfams'),
454 :     $cgi->end_form,
455 :     $cgi->hr,
456 : olson 1.127 "On the annotator seed only (currently): <a href='SubsysEditor.cgi'>Use the new Subsystem Editor</a>\n",
457 :     $cgi->hr,
458 : parrello 1.75 );
459 : olson 1.42
460 : golsen 1.95 push( @$html,
461 :     $cgi->start_form(-action => $this_script),
462 :     "<table>\n",
463 :     "<tr>",
464 : golsen 1.100 "<td colspan=2>", $cgi->h2('Searching for Genes or Functional Roles Using Text'), "</td>",
465 :     "<td align=right><a href='sdk_uniprot_search.cgi'>UniProt WebService Search</a></td>",
466 :     "</tr>\n",
467 :     "<tr>",
468 : golsen 1.95 "<td>Search Pattern: </td>",
469 :     "<td>", $cgi->textfield(-name => "pattern", -size => 65), "</td>",
470 :     "<td>", "Search <select name=search_kind>
471 :     <option value=DIRECT >Directly</option>
472 :     <option value=GO >Via Gene Ontology</option>
473 :     <option value=HUGO >Via HUGO Gene Nomenclature Committee</option>
474 :     </select></td>",
475 :     "</tr>\n",
476 :     "<tr>",
477 :     "<td>User ID:</td>",
478 :     "<td>",
479 :     $cgi->textfield(-name => "user", -size => 20), " [optional] &nbsp; &nbsp; ",
480 :     "Max Genes: ", $cgi->textfield(-name => "maxpeg", -size => 6, -value => 100), "&nbsp; &nbsp; ",
481 :     "Max Roles: ", $cgi->textfield(-name => "maxrole", -size => 6, -value => 100), "</td>",
482 :     "<td>", $cgi->checkbox(-name => "substring_match", -label => 'Allow substring match'), "</td>",
483 :     "</tr>\n",
484 :     "</table>\n",
485 :     $cgi->submit('Search'),
486 :     $cgi->submit('Search genome selected below'),
487 :     $cgi->reset('Clear'),
488 :     $cgi->hr
489 :     );
490 : olson 1.41
491 : golsen 1.113 #---------------------------------------------------------------------------
492 :     # Build the list of genomes from which the user can pick:
493 :     #---------------------------------------------------------------------------
494 :    
495 :     my $link;
496 :     ( $link = $cgi->url(-relative => 1) ) =~ s/index\.cgi/show_log.cgi/;
497 :    
498 :     push( @$html, $cgi->h2('If You Need to Pick a Genome for Options Below'),"&nbsp;[<a href=$link>Log</a>]");
499 :    
500 :     my @display = ( 'All', 'Archaea', 'Bacteria', 'Eucarya', 'Plasmids', 'Viruses', 'Environmental samples' );
501 : golsen 1.47
502 :     # Canonical names must match the keywords used in the DBMS. They are
503 :     # defined in compute_genome_counts.pl
504 : golsen 1.113
505 : golsen 1.47 my %canonical = (
506 :     'All' => undef,
507 :     'Archaea' => 'Archaea',
508 :     'Bacteria' => 'Bacteria',
509 :     'Eucarya' => 'Eukaryota',
510 : golsen 1.113 'Plasmids' => 'Plasmid',
511 : golsen 1.47 'Viruses' => 'Virus',
512 :     'Environmental samples' => 'Environmental Sample'
513 :     );
514 :    
515 :     my $req_dom = $cgi->param( 'domain' ) || 'All';
516 :     my @domains = $cgi->radio_group( -name => 'domain',
517 :     -default => $req_dom,
518 :     -override => 1,
519 :     -values => [ @display ]
520 :     );
521 :    
522 :     my $n_domain = 0;
523 : golsen 1.113 my %dom_num = map { ( $_ => $n_domain++ ) } @display;
524 : golsen 1.47 my $req_dom_num = $dom_num{ $req_dom } || 0;
525 :    
526 : golsen 1.113 # Plasmids, Viruses and Environmental samples must have completeness
527 :     # = All (that is how they are in the database). Otherwise, default is
528 :     # Only "complete".
529 :    
530 : golsen 1.47 my $req_comp = ( $req_dom_num > $dom_num{ 'Eucarya' } ) ? 'All'
531 :     : $cgi->param( 'complete' ) || 'Only "complete"';
532 :     my @complete = $cgi->radio_group( -name => 'complete',
533 :     -default => $req_comp,
534 :     -override => 1,
535 :     -values => [ 'All', 'Only "complete"' ]
536 :     );
537 : golsen 1.113
538 : golsen 1.47 # Use $fig->genomes( complete, restricted, domain ) to get org list:
539 : golsen 1.113
540 : golsen 1.47 my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
541 : golsen 1.113
542 : overbeek 1.107 my @orgs;
543 : olson 1.123 my %org_labels;
544 : overbeek 1.107 foreach my $org ($fig->genomes( $complete, undef, $canonical{ $req_dom } ))
545 :     {
546 : olson 1.123 my $label = compute_genome_label($fig, $org);
547 :     $org_labels{$org} = $label;
548 :     push(@orgs, $org);
549 : overbeek 1.107 }
550 : golsen 1.113
551 :     # Make the sort case independent -- GJO
552 :    
553 :     # @orgs = sort { $a cmp $b } @orgs;
554 : olson 1.123 @orgs = sort { lc( $org_labels{$a} ) cmp lc( $org_labels{$b} ) } @orgs;
555 : overbeek 1.107
556 : olson 1.123 my $n_genomes = @orgs;
557 : overbeek 1.107
558 : olson 1.123 #
559 :     # Make a list of the org names for the code that doesn't use the
560 :     # name/value separation in the scrolling list.
561 :     #
562 : golsen 1.47
563 : olson 1.123 my @org_names = map { $org_labels{$_} } @orgs;
564 : golsen 1.47
565 : golsen 1.113 push( @$html, "<TABLE>\n",
566 :     " <TR VAlign=top>\n",
567 :     " <TD>",
568 : parrello 1.75 $cgi->scrolling_list( -name => 'korgs',
569 : golsen 1.47 -values => [ @orgs ],
570 : olson 1.123 -labels => \%org_labels,
571 : redwards 1.63 -size => 10,
572 : golsen 1.60 ), $cgi->br,
573 : golsen 1.47 "$n_genomes genomes shown ",
574 : golsen 1.60 $cgi->submit( 'Update List' ), $cgi->reset, $cgi->br,
575 : parrello 1.75 "Show some ", $cgi->submit('statistics')," of the selected genome",
576 : golsen 1.113 " </TD>",
577 :    
578 :     " <TD><b>Domain(s) to show:</b>\n",
579 :     " <TABLE>\n",
580 :     " <TR VAlign=bottom>\n",
581 :     " <TD>", join( "<br>", @domains[0..3]), "</TD>\n",
582 :     " <TD>&nbsp;&nbsp;&nbsp;</TD>\n",
583 :     " <TD>", join( "<br>", @domains[4..$#domains]), "</TD>\n",
584 :     " </TR>\n",
585 :     " </TABLE>\n",
586 :     " ", join( "<br>", "<b>Completeness?</b>", @complete), "\n",
587 :     " </TD>",
588 :     " </TR>\n",
589 : golsen 1.47 "</TABLE>\n",
590 : golsen 1.60 $cgi->hr
591 :     );
592 : overbeek 1.49
593 : overbeek 1.112
594 : golsen 1.47 push( @$html, $cgi->h2('Finding Candidates for a Functional Role'),
595 : parrello 1.75 "Make sure that you type the functional role you want to search for in the Search Pattern above",
596 :     $cgi->br,
597 :     $cgi->submit('Find Genes in Org that Might Play the Role'),
598 :     $cgi->hr);
599 : overbeek 1.17
600 : golsen 1.60 my @maps = sort map { $map = $_; $name = $fig->map_name($map); "$name ($map)" } $fig->all_maps;
601 :    
602 : golsen 1.47 push( @$html, $cgi->h2('Metabolic Overviews and Subsystem Maps (via KEGG & SEED) - Choose Map'),
603 : parrello 1.75 $cgi->submit('Metabolic Overview'),
604 :     $cgi->br,
605 :     $cgi->br,
606 :     $cgi->scrolling_list(-name => 'kmap',
607 : efrank 1.1 -values => [@maps],
608 : parrello 1.75 -size => 10
609 : efrank 1.1 ),
610 : parrello 1.75 $cgi->hr);
611 : overbeek 1.17
612 : golsen 1.47 push( @$html, $cgi->h2('Searching DNA or Protein Sequences (in a selected organism)'),
613 : parrello 1.75 "<TABLE>\n",
614 :     " <TR>\n",
615 :     " <TD>Sequence/Pattern: </TD>",
616 :     " <TD Colspan=3>", $cgi->textarea(-name => 'seq_pat', -rows => 10, -cols => 70), "</TD>\n",
617 :     " </TR>\n",
618 :     " <TR>\n",
619 :     " <TD>Search Program: </TD>",
620 :     " <TD>", $cgi->popup_menu(-name => 'Tool', -values => ['blastp', 'blastx', 'blastn', 'tblastn', 'blastp against complete genomes', 'Protein scan_for_matches', 'DNA scan_for_matches'], -default => 'blastp'), " </TD>",
621 :     " <TD> Program Options:</TD>",
622 :     " <TD>", $cgi->textfield( -name => "blast_options", -size => 27 ), "</TD>",
623 :     " </TR>\n",
624 :     "</TABLE>\n",
625 :     $cgi->submit('Search for Matches'),
626 :     $cgi->hr);
627 : overbeek 1.17
628 : olson 1.41 #
629 :     # Make assignment export tbl.
630 :     #
631 :    
632 :     my @atbl;
633 : golsen 1.64 push(@atbl, [ "Extract assignments made by ",
634 : parrello 1.75 $cgi->textfield(-name => "made_by", -size => 25) . " (do not prefix with <b>master:</b>)" ]);
635 : golsen 1.64 push(@atbl, [ "Save as user: ",
636 : parrello 1.75 $cgi->textfield(-name => "save_user", -size => 25) . " (do not prefix with <b>master:</b>)" ] );
637 : golsen 1.64 push(@atbl, [ "After date (MM/DD/YYYY) ",
638 : parrello 1.75 $cgi->textfield(-name => "after_date", -size => 15)]);
639 : olson 1.41
640 :     push(@$html,
641 : overbeek 1.84 $cgi->h2($cgi->a({name => "exporting_assignments"}, 'Exporting Assignments')),
642 : parrello 1.75 &HTML::make_table(undef, \@atbl, '', border => 0),
643 :     $cgi->checkbox(-label => 'Tab-delimited Spreadsheet', -name => 'tabs', -value => 1),
644 :     $cgi->br,
645 :     $cgi->checkbox(-label => 'Save Assignments', -name => 'save_assignments', -value => 1),
646 :     $cgi->br,
647 :     $cgi->submit('Extract Assignments'),
648 :     $cgi->br, $cgi->br, $cgi->br,
649 :     "Alternatively, you can generate a set of assignments as translations of existing assignments. ",
650 :     "To do so, you need to make sure that you fill in the <b>Save as user</b> field just above. You ",
651 :     "should use something like <b>RossO</b> (leave out the <b>master:</b>). When you look at the assignments (and decide which ",
652 :     "to actually install), they will be made available under that name (but, when you access them, ",
653 :     "you will normally be using something like <b>master:RossO</b>)",
654 :     $cgi->br,$cgi->br,
655 :     "From: ",
656 :     $cgi->textarea(-name => 'from_func', -rows => 4, -cols => 100),
657 :     $cgi->br,$cgi->br,
658 :     "To:&nbsp;&nbsp;&nbsp;&nbsp; ",$cgi->textfield(-name => "to_func", -size => 100),
659 :     $cgi->br,
660 : golsen 1.103 "<TABLE Width=100%><TR><TD>",
661 :     $cgi->submit('Generate Assignments via Translation'),
662 :     "</TD><TD NoWrap Width=1%>",
663 : overbeek 1.76 $cgi->a({class=>"help", target=>"help", href=>"Html/seedtips.html#replace_names"}, "Help with generate assignments via translation"),
664 : golsen 1.103 "</TD></TR></TABLE>\n"
665 : golsen 1.95 );
666 :    
667 :     push(@$html,
668 : parrello 1.75 $cgi->hr,
669 :     $cgi->h2('Searching for Interesting Genes'),
670 :     $cgi->submit('Search for Genes Matching an Occurrence Profile or Common to a Set of Organisms'),
671 : overbeek 1.128 $cgi->submit('Search for Genes in Cluster, but Not Subsystems'),
672 : overbeek 1.17 $cgi->end_form
673 : parrello 1.75 );
674 : overbeek 1.14
675 :     push(@$html,
676 : parrello 1.75 $cgi->hr,
677 :     $cgi->h2('Process Saved Assignments Sets'),
678 :     $cgi->start_form(-action => "assignments.cgi"),
679 :     "Here you should include the <b>master:</b>. Thus use something like <b>master:RossO</b>",$cgi->br,
680 :     $cgi->br,
681 :     "Enter user: ",
682 :     $cgi->textfield(-name => "user", -size => 20),
683 :     $cgi->submit('Process Assignment Sets'),
684 : overbeek 1.17 $cgi->end_form
685 : parrello 1.75 );
686 : efrank 1.1
687 : overbeek 1.19 push(@$html,
688 : parrello 1.75 $cgi->hr,
689 : overbeek 1.112 $cgi->h2('Locate clustered genes not in subsystems'),
690 :     $cgi->start_form(-action => "find_ss_genes.cgi"),
691 :     $cgi->br,
692 :     "Enter user: ",
693 :     $cgi->textfield(-name => "user", -size => 20),
694 :     $cgi->submit('Find Clustered Genes'),
695 :     $cgi->end_form
696 :     );
697 :    
698 :     push(@$html,
699 :     $cgi->hr,
700 : parrello 1.75 $cgi->h2('Align Sequences'),
701 : olson 1.89 $cgi->start_form(-action => $this_script),
702 : parrello 1.75 "Enter user: ",
703 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
704 :     $cgi->submit('Align Sequences'),": ",
705 :     $cgi->textfield(-name => "seqids", -size => 100),
706 : overbeek 1.31 $cgi->end_form
707 : parrello 1.75 );
708 : overbeek 1.51
709 :     push(@$html,
710 : parrello 1.75 $cgi->hr,
711 :     $cgi->h2('Locate PEGs in Subsystems'),
712 :     "If you wish to locate PEGs in subsystems, you have two approaches supported. You can
713 : overbeek 1.56 give a FIG id, and you will get a list of all homologs in the designated genome that occur in subsystems.
714 :     Alternatively, you can specify a functional role, and all PEGs in the genome that match that role will be shown.",
715 : olson 1.89 $cgi->start_form(-action => $this_script),
716 : parrello 1.75 "Enter user: ",
717 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
718 :     $cgi->br,"Genome: ",$cgi->textfield(-name => "genome", -size => 15),$cgi->br,
719 :     "Search: ",$cgi->textfield(-name => "subsearch", -size => 100),$cgi->br,
720 :     $cgi->submit('Find PEGs'),": ",
721 : overbeek 1.51 $cgi->end_form
722 : parrello 1.75 );
723 : overbeek 1.116 push(@$html,
724 :     $cgi->hr,
725 :     $cgi->h2('Compare Metabolic Reconstructions'),
726 :     "If you wish to compare the reconstructions for two distinct genomes, use this tool.
727 :     You should specify two genomes, or a P1K server output directory (as genome1) and a second genome (which
728 :     must be a valid genome ID that exists in this SEED). You can ask for functional roles/subsystems that the
729 :     genomes have in common, those that exist in genome1 only, or those that exist in only genome2.",
730 :     $cgi->start_form(-action => 'comp_MR.cgi'),
731 :     "Enter user: ",
732 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
733 :     $cgi->br,"Genome1: ",$cgi->textfield(-name => "genome1", -size => 40),$cgi->br,
734 :     $cgi->br,"Genome2: ",$cgi->textfield(-name => "genome2", -size => 15),
735 :     $cgi->scrolling_list( -name => 'request',
736 :     -values => [ 'common', 'in1_not2','in2_not1' ],
737 :     -size => 3,
738 :     ), $cgi->br,
739 :     $cgi->submit('Compare Reconstructions'),": ",
740 :     $cgi->end_form
741 :     );
742 : overbeek 1.117
743 :     push(@$html,
744 :     $cgi->hr,
745 :     $cgi->h2('Compare Genomes'),
746 :     "If you wish to compare the contents of several genomes, you can use this tool.
747 :     Choose a set of genomes (at least two).<br><br> ",
748 :     $cgi->start_form(-action => 'comp_genomes.cgi'),
749 :     "Enter user: ",
750 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
751 :     $cgi->scrolling_list( -name => 'comp_orgs',
752 : overbeek 1.129 -values => [ @orgs ],
753 :     -labels => \%org_labels,
754 : overbeek 1.117 -size => 10,
755 :     -multiple => 1,
756 :     ), $cgi->br,$cgi->br,
757 : overbeek 1.118 "<br><br>",
758 : overbeek 1.117 "Optionally, you can select a PEG and window size to limit the comparison:<br>",
759 :     "PEG: ", $cgi->textfield(-name => "peg", -size => 20), $cgi->br,
760 :     "Window Size: ", $cgi->textfield(-name => "sz", -size => 8, -value => 20000), $cgi->br,
761 :    
762 : overbeek 1.119 $cgi->submit('Compare Genomes'),
763 :     $cgi->submit('Update Functions in MouseOvers'),"<br>",
764 : overbeek 1.117 $cgi->end_form
765 :     );
766 : overbeek 1.120
767 :     push(@$html,
768 :     $cgi->hr,
769 :     $cgi->h2('New Pattern Matching'),
770 :     "The new pattern location tool.<br><br> ",
771 :     $cgi->start_form(-action => 'locate_patterns.cgi'),
772 :     "Enter user (optional): ",
773 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
774 :     $cgi->scrolling_list( -name => 'comp_orgs',
775 : olson 1.123 -values => [ @org_names ],
776 : overbeek 1.120 -size => 10,
777 :     -multiple => 1,
778 :     ), $cgi->br,$cgi->br,
779 :     "<br><br>",
780 :     "Pattern: ", $cgi->textfield(-name => "pattern", -size => 60), $cgi->br,
781 :     $cgi->popup_menu(-name => 'Tool',
782 :     -values => ['Protein scan_for_matches', 'DNA scan_for_matches'],
783 :     -default => 'Protein scan_for_matches'),
784 :     $cgi->submit('Scan For Matches'),
785 :     $cgi->end_form
786 :     );
787 : efrank 1.1 }
788 :    
789 : olson 1.123 sub compute_genome_label
790 :     {
791 :     my($fig, $org) = @_;
792 :    
793 :     my $gs = $fig->genus_species($org);
794 :     if ($fig->genome_domain($org) ne "Environmental Sample")
795 :     {
796 :     my $gc=$fig->number_of_contigs($org);
797 :     $label = "$gs ($org) [$gc contigs]";
798 :     }
799 :     else
800 :     {
801 :     $label = "$gs ($org)";
802 :     }
803 :     return $label;
804 :     }
805 :    
806 : overbeek 1.83 #
807 :     # Show a message of the day if it's present.
808 :     #
809 :     sub show_motd
810 :     {
811 :     my($fig, $cgi, $html) = @_;
812 :    
813 :     my $motd_file = "$FIG_Config::fig_disk/config/motd";
814 :    
815 :     if (open(F, "<$motd_file"))
816 :     {
817 :     push(@$html, "<p>\n");
818 :     while (<F>)
819 :     {
820 :     push(@$html, $_);
821 :     }
822 :     close(F);
823 :     push(@$html, "<hr>\n");
824 :     }
825 :     }
826 : overbeek 1.17
827 :     #==============================================================================
828 :     # Indexed objects (text search)
829 :     #==============================================================================
830 :    
831 : efrank 1.1 sub show_indexed_objects {
832 : golsen 1.22 my($fig, $cgi, $html, $pattern) = @_;
833 :     my($msg, $i);
834 : efrank 1.1
835 :     if ($pattern =~ /^\s*(fig\|\d+\.\d+\.peg\.\d+)\s*$/)
836 :     {
837 : parrello 1.75 my $peg = $1;
838 :     my $user = $cgi->param('user');
839 :     $user = $user ? $user : "";
840 : paczian 1.102 my @prot_out;
841 :     if (defined($cgi->param('fromframe'))) {
842 :     $ENV{'REQUEST_METHOD'} = "GET";
843 :     $ENV{"QUERY_STRING"} = "prot=$peg\&user=$user\&action=proteinpage";
844 :     $ENV{"REQUEST_URI"} =~ s/$this_script/frame.cgi/;
845 : olson 1.122 $ENV{"SCRIPT_NAME"} =~ s/$this_script/frame.cgi/;
846 : paczian 1.102 @prot_out = TICK("./frame.cgi");
847 :     } else {
848 :     $ENV{'REQUEST_METHOD'} = "GET";
849 :     $ENV{"QUERY_STRING"} = "prot=$peg\&user=$user";
850 :     $ENV{"REQUEST_URI"} =~ s/$this_script/protein.cgi/;
851 : olson 1.122 $ENV{"SCRIPT_NAME"} =~ s/$this_script/protein.cgi/;
852 : paczian 1.102 @prot_out = TICK("./protein.cgi");
853 :     }
854 : parrello 1.75 print @prot_out;
855 :     exit;
856 : efrank 1.1 }
857 : overbeek 1.71 $pattern =~ s/([a-zA-Z0-9])\|([a-zA-Z0-9])/$1\\\|$2/ig;
858 : efrank 1.92
859 :     my $search_kind = $cgi->param("search_kind");
860 :     if ( $search_kind && ! ($search_kind eq "DIRECT") ) {
861 :     #otherwise $search_kind is name of controlled vocab
862 :     find_pegs_by_cv($fig, $cgi, $html, $user, $pattern, $search_kind);
863 :     return;
864 :     }
865 :    
866 : overbeek 1.17 push( @$html, $cgi->br );
867 : olson 1.70 my( $peg_index_data, $role_index_data ) = $fig->search_index($pattern, $cgi->param("substring_match") eq "on");
868 : overbeek 1.17 my $maxpeg = defined( $cgi->param("maxpeg") ) ? $cgi->param("maxpeg") : 100;
869 :     my $maxrole = defined( $cgi->param("maxrole") ) ? $cgi->param("maxrole") : 100;
870 : mkubal 1.109 my $output_file = "$FIG_Config::temp/search_results.txt";
871 : parrello 1.115 Trace("Producing search output file $output_file") if T(3);
872 : mkubal 1.109 open(OUT,">$output_file");
873 : overbeek 1.17
874 : redwards 1.53 # RAE added lines to allow searching within a single organism
875 : golsen 1.59 # if ($cgi->param('korgs'))
876 :     # {
877 :     # $cgi->param('korgs') =~ /\((\d+\.*\d*)\)/;
878 :     # $org=$1; # this should be undef if korgs is not defined
879 :    
880 :     # push (@$html, $cgi->br, "Matches found in ",$cgi->param('korgs'), $cgi->p);
881 :     # my @clean_data; my @clean_index;
882 :     # while (@$peg_index_data)
883 :     # {
884 :     # my ($data, $index)=(shift @$peg_index_data, shift @$role_index_data);
885 :     # next unless (${$data}[0] =~ /^fig\|$org\.peg/);
886 :     # push @clean_data, $data;
887 :     # push @clean_index, $index;
888 :     # }
889 :    
890 :     # @$peg_index_data=@clean_data;
891 :     # @$role_index_data=@clean_index;
892 :     # }
893 :     ## End of added lines
894 : redwards 1.53
895 : mkubal 1.99 # RAE version with separate submit buttoxns and more than one org in korg
896 : redwards 1.63 # this is used by organisms.cgi for group specific searches
897 :     if ( $cgi->param('korgs') && $cgi->param('Search Selected Organisms')
898 :     )
899 :     {
900 :     my @temp;
901 :     foreach my $org ($cgi->param('korgs'))
902 :     {
903 :     push @temp, grep { $_->[0] =~ /^fig\|$org/ } @$peg_index_data;
904 :     }
905 :     @$peg_index_data = @temp;
906 :     }
907 :    
908 : golsen 1.59 # GJO version with separate submit buttons
909 : redwards 1.53
910 : olson 1.123 if ( $cgi->param('korgs') && $cgi->param('korgs') =~ /(\d+\.\d+)/
911 : golsen 1.59 && $cgi->param('Search genome selected below')
912 :     )
913 :     {
914 : parrello 1.75 my $org = $1;
915 : olson 1.123 my $label = compute_genome_label($fig, $org);
916 :     push @$html, $cgi->br, "Matches found in $label", $cgi->p;
917 : mkubal 1.79 @$peg_index_data = grep { $_->[0] =~ /^fig\|$org\.*/ } @$peg_index_data;
918 : redwards 1.53 }
919 : parrello 1.115 Trace("Initial push.") if T(3);
920 : golsen 1.59 if ( ( $maxpeg > 0 ) && @$peg_index_data )
921 : overbeek 1.17 {
922 : parrello 1.75 # RAE: Added javascript buttons see below. Only two things are needed.
923 :     # The form must have a name parameter, and the one line of code for the
924 :     # buttons. Everything else is automatic
925 :    
926 :     push( @$html, $cgi->start_form( -method => 'post',
927 :     -target => "window$$",
928 :     -action => 'fid_checked.cgi',
929 :     -name => 'found_pegs'
930 :     ),
931 :     $cgi->hidden(-name => 'user', -value => $user),
932 :     "For Selected (checked) sequences: ",
933 :     $cgi->submit('get sequences'),
934 :     $cgi->submit('view annotations'),
935 :     $cgi->submit('assign/annotate'),
936 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
937 :     $cgi->br, $cgi->br
938 :     );
939 : efrank 1.1
940 : redwards 1.63 # RAE Add the check all/uncheck all boxes.
941 :     push (@$html, $cgi->br, &HTML::java_buttons("found_pegs", "checked"), $cgi->br);
942 : parrello 1.75
943 :     my $n = @$peg_index_data;
944 :     if ($n > $maxpeg)
945 :     {
946 :     $msg = "Showing first $maxpeg out of $n protein genes";
947 :     $#{$peg_index_data} = $maxpeg-1;
948 :     }
949 :     else
950 :     {
951 : mkubal 1.79 $msg = "Showing $n FEATURES";
952 : parrello 1.75 }
953 :    
954 : parrello 1.115 my $col_hdrs = ["Sel","FEATURE","Organism","Aliases","Functions","Who","Attributes"];
955 : overbeek 1.106 my $tab = [ map { format_peg_entry( $fig, $cgi, $_ ) } sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
956 : mkubal 1.109
957 :     my $tab2 = [ sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
958 : parrello 1.115 Trace("Final html push.") if T(3);
959 : mkubal 1.109 push( @$html,$cgi->br,
960 :     "<a href=$FIG_Config::temp_url/search_results.txt>Download_Search_Results</a>",
961 :     &HTML::make_table($col_hdrs,$tab,$msg),
962 : parrello 1.75 $cgi->br,
963 :     "For SELECTed (checked) sequences: ",
964 :     $cgi->submit('get sequences'),
965 :     $cgi->submit('view annotations'),
966 :     $cgi->submit('assign/annotate'),
967 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
968 :     $cgi->br,
969 :     $cgi->end_form
970 : mkubal 1.109 );
971 :    
972 :     foreach my $t (@$tab2){
973 :     my $string = join("\t",@$t);
974 :     print OUT "$string\n";
975 :     }
976 :    
977 : efrank 1.1 }
978 : golsen 1.59 elsif ( $maxpeg > 0 )
979 :     {
980 : parrello 1.75 push @$html, $cgi->h3('No matching protein genes');
981 : golsen 1.59 }
982 : overbeek 1.17
983 : golsen 1.59 if ( ( $maxrole > 0 ) && @$role_index_data )
984 : efrank 1.1 {
985 : parrello 1.75 my $n = @$role_index_data;
986 :     if ($n > $maxrole)
987 :     {
988 :     $msg = "Showing first $maxrole out of $n Roles";
989 :     $#{$role_index_data} = $maxrole - 1;
990 :     }
991 :     else
992 :     {
993 :     $msg = "Showing $n Roles";
994 :     }
995 :    
996 :     if ( $maxpeg > 0 ) { push( @$html, $cgi->hr ) }
997 :     my $col_hdrs = ["Role"];
998 :     my $tab = [ map { &format_role_entry($fig,$cgi,$_) } @$role_index_data ];
999 :     push( @$html, &HTML::make_table($col_hdrs,$tab,$msg) );
1000 : efrank 1.1 }
1001 : golsen 1.59 elsif ( $maxrole > 0 )
1002 :     {
1003 : parrello 1.75 push @$html, $cgi->h3('No matching roles');
1004 : golsen 1.59 }
1005 : parrello 1.115 Trace("Show-indexed-objects method complete.") if T(3);
1006 : efrank 1.1 }
1007 :    
1008 : golsen 1.59
1009 : efrank 1.1 sub format_peg_entry {
1010 : golsen 1.67 my( $fig, $cgi, $entry ) = @_;
1011 : efrank 1.1
1012 : parrello 1.115 my($peg,$gs,$aliases,$function,$who,$attribute) = @$entry;
1013 : overbeek 1.17
1014 : golsen 1.21 $gs =~ s/\s+\d+$//; # Org name comes with taxon_id appended (why?) -- GJO
1015 : efrank 1.1
1016 : golsen 1.67 my $box = "<input type=checkbox name=checked value=\"$peg\">";
1017 : overbeek 1.17 return [ $box, &HTML::fid_link($cgi,$peg), $gs, $aliases, $function, $who ];
1018 : efrank 1.1 }
1019 :    
1020 :     sub format_role_entry {
1021 :     my($fig,$cgi,$entry) = @_;
1022 :    
1023 :     return [&HTML::role_link($cgi,$entry)];
1024 :     }
1025 :    
1026 :     sub run_prot_scan_for_matches {
1027 :     my($fig,$cgi,$html,$org,$pat) = @_;
1028 :     my($string,$peg,$beg,$end,$user,$col_hdrs,$tab,$i);
1029 :    
1030 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
1031 :     open(PAT,">$tmp_pat")
1032 : parrello 1.75 || die "could not open $tmp_pat";
1033 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
1034 :     print PAT "$pat\n";
1035 :     close(PAT);
1036 :     my @out = `$FIG_Config::ext_bin/scan_for_matches -p $tmp_pat < $FIG_Config::organisms/$org/Features/peg/fasta`;
1037 :     if (@out < 1)
1038 :     {
1039 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
1040 : efrank 1.1 }
1041 :     else
1042 :     {
1043 : parrello 1.75 if (@out > 2000)
1044 :     {
1045 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
1046 :     $#out = 1999;
1047 :     }
1048 :    
1049 :     push(@$html,$cgi->pre);
1050 :     $user = $cgi->param('user');
1051 :     $col_hdrs = ["peg","begin","end","string","function of peg"];
1052 :     for ($i=0; ($i < @out); $i += 2)
1053 :     {
1054 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
1055 :     {
1056 :     $peg = $1;
1057 :     $beg = $2;
1058 :     $end = $3;
1059 :     $string = $out[$i+1];
1060 :     chomp $string;
1061 :     push( @$tab, [ &HTML::fid_link($cgi,$peg,1),
1062 :     $beg,
1063 :     $end,
1064 :     $string,
1065 :     scalar $fig->function_of( $peg, $user )
1066 :     ]
1067 :     );
1068 :     }
1069 :     }
1070 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
1071 :     push(@$html,$cgi->end_pre);
1072 : efrank 1.1 }
1073 :     unlink($tmp_pat);
1074 :     }
1075 :    
1076 : overbeek 1.17 #==============================================================================
1077 :     # Scan for matches
1078 :     #==============================================================================
1079 :    
1080 : efrank 1.1 sub run_dna_scan_for_matches {
1081 :     my($fig,$cgi,$html,$org,$pat) = @_;
1082 :     my($string,$contig,$beg,$end,$col_hdrs,$tab,$i);
1083 :    
1084 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
1085 :     open(PAT,">$tmp_pat")
1086 : parrello 1.75 || die "could not open $tmp_pat";
1087 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
1088 :     print PAT "$pat\n";
1089 :     close(PAT);
1090 :     my @out = `cat $FIG_Config::organisms/$org/contigs | $FIG_Config::ext_bin/scan_for_matches -c $tmp_pat`;
1091 :     if (@out < 1)
1092 :     {
1093 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
1094 : efrank 1.1 }
1095 :     else
1096 :     {
1097 : parrello 1.75 if (@out > 2000)
1098 :     {
1099 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
1100 :     $#out = 1999;
1101 :     }
1102 :    
1103 :     push(@$html,$cgi->pre);
1104 :     $col_hdrs = ["contig","begin","end","string"];
1105 :     for ($i=0; ($i < @out); $i += 2)
1106 :     {
1107 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
1108 :     {
1109 :     $contig = $1;
1110 :     $beg = $2;
1111 :     $end = $3;
1112 :     $string = $out[$i+1];
1113 :     chomp $string;
1114 :     push(@$tab,[$contig,$beg,$end,$string]);
1115 :     }
1116 :     }
1117 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
1118 :     push(@$html,$cgi->end_pre);
1119 : efrank 1.1 }
1120 :     unlink($tmp_pat);
1121 :     }
1122 :    
1123 : overbeek 1.17 #==============================================================================
1124 :     # BLAST search
1125 :     #==============================================================================
1126 :    
1127 : efrank 1.1 sub run_blast {
1128 : golsen 1.45 my( $fig, $cgi, $html, $org, $tool, $seq ) = @_;
1129 :     my( $query, @out );
1130 : efrank 1.1
1131 : golsen 1.45 my $tmp_seq = "$FIG_Config::temp/run_blast_tmp$$.seq";
1132 : efrank 1.1
1133 : overbeek 1.17 #--------------------------------------------------------------------------
1134 : golsen 1.97 # Does the request require a defined genome? We never check that the
1135 :     # database build works, so the least we can do is some up-front tests.
1136 :     # -- GJO
1137 :     #--------------------------------------------------------------------------
1138 :    
1139 :     if ( $tool !~ /complete genomes/ )
1140 :     {
1141 :     if ( ! $org || ! -d "$FIG_Config::organisms/$org" )
1142 :     {
1143 :     push @$html, $cgi->h2("Sorry, $tool requires selecting a genome." );
1144 :     return;
1145 :     }
1146 :    
1147 :     if ( ( $tool =~ /blastn/ ) || ( $tool =~ /tblastx/ ) )
1148 :     {
1149 :     if ( ! -f "$FIG_Config::organisms/$org/contigs" )
1150 :     {
1151 :     push @$html, $cgi->h2("Sorry, cannot find DNA data for genome $org." );
1152 :     return;
1153 :     }
1154 :     }
1155 :     else
1156 :     {
1157 :     if ( ! -f "$FIG_Config::organisms/$org/Features/peg/fasta" )
1158 :     {
1159 :     push @$html, $cgi->h2("Sorry, cannot find protein data for genome $org." );
1160 :     return;
1161 :     }
1162 :     }
1163 :     }
1164 :    
1165 :     #--------------------------------------------------------------------------
1166 : overbeek 1.17 # Is the request for an id? Get the sequence
1167 :     #--------------------------------------------------------------------------
1168 : golsen 1.97
1169 : golsen 1.91 if ( ( $query ) = $seq =~ /^\s*([a-zA-Z]{2,4}\|\S+)/ )
1170 : efrank 1.1 {
1171 : parrello 1.75 # Replaced $id with $query so that output inherits label -- GJO
1172 : golsen 1.91 # Found ugly fairure to build correct query sequence for
1173 :     # 'blastp against complete genomes'. Can't figure out
1174 :     # why it ever worked with and id -- GJO
1175 :    
1176 : parrello 1.75 $seq = "";
1177 : golsen 1.91 if ( ($tool eq "blastp") || ($tool eq "tblastn")
1178 :     || ($tool eq 'blastp against complete genomes')
1179 :     )
1180 : parrello 1.75 {
1181 :     $seq = $fig->get_translation($query);
1182 : golsen 1.97 my $func = $fig->function_of( $query, $user );
1183 :     $query .= " $func" if $func;
1184 : parrello 1.75 }
1185 :     elsif ($query =~ /^fig/)
1186 :     {
1187 :     my @locs;
1188 :     if ((@locs = $fig->feature_location($query)) && (@locs > 0))
1189 :     {
1190 :     $seq = $fig->dna_seq($fig->genome_of($query),@locs);
1191 :     }
1192 :     }
1193 :     if (! $seq)
1194 :     {
1195 :     push(@$html,$cgi->h1("Sorry, could not get sequence for $query"));
1196 :     return;
1197 :     }
1198 : efrank 1.1 }
1199 : golsen 1.45
1200 : overbeek 1.17 #--------------------------------------------------------------------------
1201 :     # Is it a fasta format? Get the query name
1202 :     #--------------------------------------------------------------------------
1203 : golsen 1.45
1204 :     elsif ( $seq =~ s/^>\s*(\S+[^\n\012\015]*)// ) # more flexible match -- GJO
1205 : efrank 1.1 {
1206 : parrello 1.75 $query = $1;
1207 : efrank 1.1 }
1208 : golsen 1.45
1209 : overbeek 1.17 #--------------------------------------------------------------------------
1210 :     # Take it as plain text
1211 :     #--------------------------------------------------------------------------
1212 : golsen 1.45
1213 : efrank 1.1 else
1214 :     {
1215 : parrello 1.75 $query = "query";
1216 : efrank 1.1 }
1217 : golsen 1.45
1218 :     #
1219 :     # The rest is taken as the sequence
1220 :     #
1221 :    
1222 : golsen 1.23 $seq =~ s/\s+//g;
1223 : golsen 1.45 open( SEQ, ">$tmp_seq" ) || die "run_blast could not open $tmp_seq";
1224 : efrank 1.1 print SEQ ">$query\n$seq\n";
1225 : golsen 1.45 close( SEQ );
1226 : efrank 1.1
1227 :     if (! $ENV{"BLASTMAT"}) { $ENV{"BLASTMAT"} = "$FIG_Config::blastmat" }
1228 : golsen 1.88 my $blast_opt = $cgi->param( 'blast_options' ) || '';
1229 : efrank 1.1
1230 : golsen 1.45 if ( $tool eq "blastp" )
1231 : efrank 1.1 {
1232 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1233 :     &verify_db( $db, "p" );
1234 : golsen 1.126 @out = map { &HTML::set_prot_links($cgi,$_) }
1235 :     remove_deleted_fids( $fig, execute_blastall( 'blastp', $tmp_seq, $db, $blast_opt ) );
1236 : efrank 1.1 }
1237 : golsen 1.45
1238 :     elsif ( $tool eq "blastx" )
1239 : efrank 1.1 {
1240 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1241 :     &verify_db( $db, "p" );
1242 : golsen 1.126 @out = map { &HTML::set_prot_links($cgi,$_) }
1243 :     remove_deleted_fids( $fig, execute_blastall( 'blastx', $tmp_seq, $db, $blast_opt ) );
1244 : efrank 1.1 }
1245 : golsen 1.45
1246 :     elsif ( $tool eq "blastn" )
1247 : efrank 1.1 {
1248 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1249 :     &verify_db( $db, "n" ); ### fix to get all contigs
1250 : golsen 1.88 @out = execute_blastall( 'blastn', $tmp_seq, $db, "-r 1 -q -1 " . $blast_opt );
1251 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1252 : efrank 1.1 }
1253 : golsen 1.45
1254 :     elsif ( $tool eq "tblastn" )
1255 : efrank 1.1 {
1256 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1257 :     &verify_db( $db, "n" ); ### fix to get all contigs
1258 : golsen 1.88 @out = execute_blastall( 'tblastn', $tmp_seq, $db, $blast_opt );
1259 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1260 : efrank 1.1 }
1261 : golsen 1.45
1262 :     elsif ( $tool eq 'blastp against complete genomes' ) ### this tool gets nonstandard treatment: RAO
1263 : overbeek 1.30 {
1264 : golsen 1.103 &blast_complete( $fig, $cgi, $html, $tmp_seq, $query, $seq );
1265 : golsen 1.91 unlink( $tmp_seq );
1266 :     return;
1267 : overbeek 1.30 }
1268 : golsen 1.45
1269 : overbeek 1.17 if (@out < 1) # This is really a bigger problem than no hits (GJO)
1270 : efrank 1.1 {
1271 : golsen 1.88 push @$html, $cgi->h1( "Sorry, no blast output" );
1272 : efrank 1.1 }
1273 :     else
1274 :     {
1275 : golsen 1.88 push @$html, $cgi->pre, @out, $cgi->end_pre;
1276 : efrank 1.1 }
1277 : golsen 1.45 unlink( $tmp_seq );
1278 : efrank 1.1 }
1279 :    
1280 : golsen 1.126 #
1281 :     # @blast_text = remove_deleted_fids( $fig, @blast_text )
1282 :     #
1283 :     # The blast datebases include all the proteins, including those deleted.
1284 :     # This is a text filter to remove those deleted. Requires 3 states:
1285 :     #
1286 :     # $delete
1287 :     # 0 Pass the line
1288 :     # 1 Delete current line
1289 :     # 2 Delete until next subject sequence
1290 :     #
1291 :     sub remove_deleted_fids
1292 :     {
1293 :     my $fig = shift;
1294 :     my $delete = 0;
1295 :    
1296 :     grep { if ( /^(fig\|\d+\.\d+\.[^.]+\.\d+)/ )
1297 :     {
1298 :     $delete = $fig->is_deleted_fid( $1 ) ? 1 : 0;
1299 :     }
1300 :     elsif ( /^>(fig\|\d+\.\d+\.[^.]+\.\d+)/ )
1301 :     {
1302 :     $delete = $fig->is_deleted_fid( $1 ) ? 2 : 0;
1303 :     }
1304 :     elsif ( $delete == 1 ) # Deleted 1 previous line
1305 :     {
1306 :     $delete = 0;
1307 :     }
1308 :     elsif ( /^ +Database: / ) # No more subject sequences
1309 :     {
1310 :     $delete = 0;
1311 :     }
1312 :     ! $delete # If we don't want to delete, pass the line
1313 :     } @_;
1314 :     }
1315 : golsen 1.45
1316 : golsen 1.88 # `$blastall -p $prog -i $tmp_seq -d $db $blast_opt`
1317 :     # execute_blastall( $prog, $input_file, $db, $options )
1318 :    
1319 :     sub execute_blastall
1320 :     {
1321 :     my( $prog, $input, $db, $options ) = @_;
1322 :    
1323 :     my $blastall = "$FIG_Config::ext_bin/blastall";
1324 :     my @args = ( '-p', $prog, '-i', $input, '-d', $db, split(/\s+/, $options) );
1325 :    
1326 :     my $bfh;
1327 :     my $pid = open( $bfh, "-|" );
1328 :     if ( $pid == 0 )
1329 :     {
1330 :     exec( $blastall, @args );
1331 :     die join( " ", $blastall, @args, "failed: $!" );
1332 :     }
1333 :    
1334 : golsen 1.91 <$bfh>
1335 : golsen 1.88 }
1336 :    
1337 :    
1338 : golsen 1.91 # Changed to:
1339 :     # Include low complexity filter in blast search.
1340 :     # Remove all but first match to a given database sequence.
1341 :     # Sort by bit-score, not E-value (which becomes equal for all strong matches).
1342 :     # Limit to 1000 matches.
1343 :     # -- GJO
1344 : golsen 1.126 #
1345 :     # Removal of deleted fids is in format_sims (really belongs here).
1346 :     # Function to remove duplicates is duplicated in format_sims.
1347 :     # Limit of output to 1000 is also in format_sims.
1348 : golsen 1.91
1349 :     sub blast_complete
1350 :     {
1351 : golsen 1.103 my( $fig, $cgi, $html, $seqfile, $query, $seq ) = @_;
1352 : golsen 1.88 my( $genome, @sims );
1353 :    
1354 : overbeek 1.30 @sims = ();
1355 : golsen 1.91 foreach $genome ( $fig->genomes("complete") )
1356 : overbeek 1.30 {
1357 : parrello 1.75 my $db = "$FIG_Config::organisms/$genome/Features/peg/fasta";
1358 :     next if (! -s $db);
1359 : overbeek 1.30
1360 : parrello 1.75 &verify_db($db,"p");
1361 :     my $sim;
1362 : golsen 1.91 my %seen = ();
1363 :     push @sims, map { chomp;
1364 :     $sim = [ split /\t/ ];
1365 :     $sim->[10] =~ s/^e-/1.0e-/;
1366 :     $seen{ $sim->[1] }++ ? () : $sim
1367 :     }
1368 :     execute_blastall( 'blastp', $seqfile, $db, '-m 8 -F T -e 1e-5' );
1369 : overbeek 1.30 }
1370 : golsen 1.91
1371 :     @sims = sort { $b->[11] <=> $a->[11] } @sims;
1372 :     if ( @sims > 1000 ) { @sims = @sims[0 .. 999] }
1373 : golsen 1.103 &format_sims( $fig, $cgi, $html, \@sims, $query, $seq );
1374 : overbeek 1.30 }
1375 :    
1376 : golsen 1.65
1377 :     #------------------------------------------------------------------------------
1378 : golsen 1.97 # Graphically display search results on contigs
1379 : golsen 1.65 #
1380 :     # use FIGjs qw( toolTipScript );
1381 :     # use GenoGraphics qw( render );
1382 :     #------------------------------------------------------------------------------
1383 :     #
1384 : golsen 1.97 # Fields produced by next_blast_hsp:
1385 :     #
1386 :     # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1387 :     # qid qdef qlen sid sdef slen scr e_val p_n p_val n_mat n_id n_pos n_gap dir q1 q2 qseq s1 s2 sseq
1388 : golsen 1.65 #------------------------------------------------------------------------------
1389 :    
1390 :     sub blast_graphics {
1391 :     my ( $fig_or_sprout, $cgi, $genome, $out, $tool ) = @_;
1392 :    
1393 :     my $e_min = 0.1;
1394 :     my $gg = [];
1395 : golsen 1.97 my @html = ();
1396 : golsen 1.65
1397 : golsen 1.97 # Changed to use standalone parsing function, not shell script -- GJO
1398 : golsen 1.65
1399 : golsen 1.97 my $outcopy = [ @$out ];
1400 :     while ( $_ = &gjoparseblast::next_blast_hsp( $outcopy ) )
1401 : golsen 1.65 {
1402 : golsen 1.97 my ( $qid, $qlen, $contig, $slen ) = @$_[0, 2, 3, 5 ];
1403 :     my ( $e_val, $n_mat, $n_id, $q1, $q2, $s1, $s2 ) = @$_[ 7, 10, 11, 15, 16, 18, 19 ];
1404 :     next if $e_val > $e_min;
1405 :     my ( $genes, $min, $max ) = hsp_context( $fig_or_sprout, $cgi, $genome,
1406 :     $e_val, 100 * $n_id / $n_mat,
1407 :     $qid, $q1, $q2, $qlen,
1408 :     $contig, $s1, $s2, $slen
1409 :     );
1410 :     if ($min && $max)
1411 : golsen 1.65 {
1412 : golsen 1.97 push @$gg, [ substr( $contig, 0, 18 ), $min, $max, $genes ];
1413 : golsen 1.65 }
1414 : golsen 1.97 }
1415 : golsen 1.65
1416 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1417 :     # $genes = [ $gene, $gene, ... ];
1418 :     # $map = [ $label, $min_coord, $max_coord, $genes ];
1419 :     # $gg = [ $map, $map, ... ];
1420 :     # render( $gg, $width, $obj_half_heigth, $save, $img_index_number )
1421 :    
1422 :     if ( @$gg )
1423 :     {
1424 :     # print STDERR Dumper( $gg );
1425 :     my $gs = $fig_or_sprout->genus_species( $genome );
1426 :     my $space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1427 :     my $legend = "<TABLE>\n"
1428 :     . " <TR>\n"
1429 :     . " <TD>Q = Query sequence$space</TD>\n"
1430 :     . " <TD Bgcolor='#FF0000'>$space</TD><TD>Frame 1 translation$space</TD>\n"
1431 :     . " <TD Bgcolor='#00FF00'>$space</TD><TD>Frame 2 translation$space</TD>\n"
1432 :     . " <TD Bgcolor='#0000FF'>$space</TD><TD>Frame 3 translation$space</TD>\n"
1433 :     . " <TD Bgcolor='#808080'>$space</TD><TD>Untranslated feature</TD>\n"
1434 :     . " </TR>\n"
1435 :     . "</TABLE><P />";
1436 :    
1437 :     push @html, "\n", FIGjs::toolTipScript(), "\n",
1438 :     $cgi->h2( "Results of $tool search of contigs from $gs\n"),
1439 :     $legend,
1440 :     @{ GenoGraphics::render( $gg, 600, 4, 0, 1 ) },
1441 :     $cgi->hr, "\n";
1442 : golsen 1.65 }
1443 :    
1444 :     return @html;
1445 :     }
1446 :    
1447 :    
1448 :     sub hsp_context {
1449 :     my( $fig_or_sprout, $cgi, $genome, $e_val, $pct_id,
1450 :     $qid, $q1, $q2, $qlen,
1451 : parrello 1.75 $contig, $s1, $s2, $slen ) = @_;
1452 : golsen 1.65 my $half_sz = 5000;
1453 :    
1454 :     my( $from, $to, $features, $fid, $beg, $end );
1455 :     my( $link, $lbl, $isprot, $function, $uniprot, $info, $prot_query );
1456 :    
1457 :     my $user = $cgi->param( 'user' ) || "";
1458 :     my $sprout = $cgi->param( 'SPROUT' ) ? '&SPROUT=1' : '';
1459 :    
1460 :     my @genes = ();
1461 :    
1462 :     # Based on the match position of the query, select the context region:
1463 :    
1464 :     ( $from, $to ) = ( $s1 <= $s2 ) ? ( $s1 - $half_sz, $s2 + $half_sz )
1465 :     : ( $s2 - $half_sz, $s1 + $half_sz );
1466 :     $from = 1 if ( $from < 1 );
1467 :     $to = $slen if ( $to > $slen );
1468 :    
1469 :     # Get the genes in the region, and adjust the ends to include whole genes:
1470 :    
1471 :     ( $features, $from, $to ) = genes_in_region( $fig_or_sprout, $cgi, $genome, $contig, $from, $to );
1472 :    
1473 : golsen 1.103 # Fix the end points if features have moved them to exclude query:
1474 :    
1475 :     if ( $s1 < $s2 ) { $from = $s1 if $s1 < $from; $to = $s2 if $s2 > $to }
1476 :     else { $from = $s2 if $s2 < $from; $to = $s1 if $s1 > $to }
1477 : golsen 1.65
1478 :     # Add the other features:
1479 :    
1480 :     foreach $fid ( @$features )
1481 :     {
1482 : parrello 1.75 my $contig1;
1483 :     ( $contig1, $beg, $end ) = boundaries_of( $fig_or_sprout, feature_locationS( $fig_or_sprout, $fid ) );
1484 :     next if $contig1 ne $contig;
1485 :    
1486 :     $link = "";
1487 :     if ( ( $lbl ) = $fid =~ /peg\.(\d+)$/ ) {
1488 : overbeek 1.82 ( $link = $cgi->url(-relative => 1) ) =~ s/index\.cgi/protein.cgi/;
1489 : parrello 1.75 $link .= "?prot=$fid&user=$user$sprout";
1490 :     $isprot = 1;
1491 :     } elsif ( ( $lbl ) = $fid =~ /\.([a-z]+)\.\d+$/ ) {
1492 :     $lbl = uc $lbl;
1493 :     $isprot = 0;
1494 :     } else {
1495 :     $lbl = "";
1496 :     $isprot = 0;
1497 :     }
1498 :    
1499 :     $function = function_ofS( $fig_or_sprout, $fid );
1500 :    
1501 :     $uniprot = join ", ", grep { /^uni\|/ } feature_aliasesL( $fig_or_sprout, $fid);
1502 :    
1503 :     $info = join( '<br />', "<b>Feature:</b> $fid",
1504 :     "<b>Contig:</b> $contig",
1505 :     "<b>Begin:</b> $beg",
1506 :     "<b>End:</b> $end",
1507 :     $function ? "<b>Function:</b> $function" : '',
1508 :     $uniprot ? "<b>Uniprot ID:</b> $uniprot" : ''
1509 :     );
1510 :    
1511 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1512 :    
1513 : parrello 1.75 push @genes, [ feature_graphic( $beg, $end, $isprot ),
1514 :     $lbl, $link, $info,
1515 :     $isprot ? () : ( undef, "Feature information" )
1516 :     ];
1517 : golsen 1.65 }
1518 :    
1519 :     # Draw the query. The subject coordinates are always DNA. If the query
1520 :     # is protein, it is about 3 times shorter than the matching contig DNA.
1521 :     # Splitting the difference, if 1.7 times the query length is still less
1522 :     # than the subject length, we will call it a protein query (and reading
1523 :     # frame in the contig coordinates has meaning). If it is nucleotides,
1524 :     # there is no defined frame.
1525 :    
1526 :     $info = join( '<br />', $qid ne 'query ' ? "<b>Query:</b> $qid" : (),
1527 :     "<b>Length:</b> $qlen",
1528 :     "<b>E-value:</b> $e_val",
1529 :     "<b>% identity:</b> " . sprintf( "%.1f", $pct_id ),
1530 :     "<b>Region of similarity:</b> $q1 &#150; $q2"
1531 :     );
1532 :     $prot_query = ( 1.7 * abs( $q2 - $q1 ) < abs( $s2 - $s1 ) ) ? 1 : 0;
1533 :    
1534 : golsen 1.104 if ( $user && $prot_query )
1535 : golsen 1.97 {
1536 :     $link = $cgi->url(-relative => 1);
1537 :     $link =~ s/index\.cgi/propose_new_peg.cgi/;
1538 :     $link .= "?user=$user&genome=$genome&covering=${contig}_${s1}_${s2}";
1539 :     }
1540 :     else
1541 :     {
1542 :     $link = undef;
1543 :     }
1544 :    
1545 : golsen 1.65 push @genes, [ feature_graphic( $s1, $s2, $prot_query ),
1546 : golsen 1.97 'Q', $link, $info, undef, 'Query and match information'
1547 : golsen 1.65 ];
1548 :    
1549 :     return \@genes, $from, $to;
1550 :     }
1551 :    
1552 :    
1553 :     sub feature_graphic {
1554 :     my ( $beg, $end, $isprot ) = @_;
1555 :     my ( $min, $max, $symb, $color );
1556 :    
1557 :     ( $min, $max, $symb ) = ( $beg <= $end ) ? ( $beg, $end, "rightArrow" )
1558 :     : ( $end, $beg, "leftArrow" );
1559 :    
1560 :     # Color proteins by translation frame
1561 :    
1562 :     $color = $isprot ? qw( blue red green )[ $beg % 3 ] : 'grey';
1563 :    
1564 :     ( $min, $max, $symb, $color );
1565 :     }
1566 :    
1567 :    
1568 :     sub genes_in_region {
1569 :     my( $fig_or_sprout, $cgi, $genome, $contig, $min, $max ) = @_;
1570 :    
1571 :     if ( $cgi->param( 'SPROUT' ) )
1572 :     {
1573 : parrello 1.75 my( $x, $feature_id );
1574 :     my( $feat, $min, $max ) = $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1575 :     my @tmp = sort { ($a->[1] cmp $b->[1]) or
1576 :     (($a->[2]+$a->[3]) <=> ($b->[2]+$b->[3]))
1577 :     }
1578 :     map { $feature_id = $_;
1579 :     $x = feature_locationS( $fig_or_sprout, $feature_id );
1580 :     $x ? [ $feature_id, boundaries_of( $fig_or_sprout, $x )] : ()
1581 :     }
1582 :     @$feat;
1583 :     return ( [map { $_->[0] } @tmp ], $min, $max );
1584 : golsen 1.65 }
1585 :     else
1586 :     {
1587 : parrello 1.75 return $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1588 : golsen 1.65 }
1589 :     }
1590 :    
1591 :    
1592 :     sub feature_locationS {
1593 :     my ( $fig_or_sprout, $peg ) = @_;
1594 :     scalar $fig_or_sprout->feature_location( $peg );
1595 :     }
1596 :    
1597 :    
1598 :     sub boundaries_of {
1599 :     my( $fig_or_sprout, $loc ) = @_;
1600 :     $fig_or_sprout->boundaries_of( $loc );
1601 :     }
1602 :    
1603 :    
1604 :     sub function_ofS {
1605 :     my( $fig_or_sprout, $peg, $user ) = @_;
1606 :     scalar $fig_or_sprout->function_of( $peg, $user );
1607 :     }
1608 :    
1609 :    
1610 :     sub feature_aliasesL {
1611 :     my( $fig_or_sprout, $fid ) = @_;
1612 :     my @tmp = $fig_or_sprout->feature_aliases( $fid );
1613 :     @tmp
1614 :     }
1615 :    
1616 :    
1617 : overbeek 1.30 sub format_sims {
1618 : golsen 1.103 my( $fig, $cgi, $html, $sims, $query, $seq ) = @_;
1619 :     my( $col_hdrs, $table, @ids, $ids, $sim, %seen, $n, $fid );
1620 : overbeek 1.30
1621 :     $col_hdrs = [ "Select up to here",
1622 : parrello 1.75 "Similar sequence",
1623 :     "E-val",
1624 :     "Function",
1625 :     "Organism",
1626 :     "Aliases"
1627 :     ];
1628 : overbeek 1.30
1629 :     $table = [];
1630 :     @ids = ();
1631 : golsen 1.103 $n = 0; # Count reported sequences
1632 :     foreach $sim ( @$sims )
1633 : overbeek 1.30 {
1634 : golsen 1.103 $fid = $sim->[1];
1635 :     next if $seen{ $fid }++; # One hit per sequence
1636 :     next if $fig->is_deleted_fid( $fid ); # Hide deleted sequences
1637 :     my $alii = scalar $fig->feature_aliases( $fid );
1638 :     $alii =~ s/,/, /g;
1639 :     push( @$table, [ $cgi->checkbox( -name => 'list_to',
1640 :     -value => $fid,
1641 :     -override => 1,
1642 :     -checked => 0,
1643 :     -label => ""
1644 :     ),
1645 :     &HTML::fid_link( $cgi, $fid ),
1646 :     [ $sim->[10], "TD NoWrap" ],
1647 :     scalar $fig->function_of( $fid ),
1648 :     $fig->genus_species( $fig->genome_of( $fid ) ),
1649 :     $alii
1650 :     ]
1651 :     );
1652 :     push( @ids, $fid );
1653 :     last if ++$n >= 1000; # Stop after 1000
1654 : overbeek 1.30 }
1655 : golsen 1.103
1656 : overbeek 1.30 $ids = join(",",@ids);
1657 :     my $target = "window$$";
1658 : golsen 1.103 push( @$html, $cgi->start_form( -method => 'post',
1659 : parrello 1.75 -target => $target,
1660 : olson 1.89 -action => $this_script
1661 : parrello 1.75 ),
1662 : golsen 1.103 $cgi->hidden(-name => 'ids', -value => $ids),
1663 :     $cgi->hidden(-name => 'qid', -value => $query),
1664 :     $cgi->hidden(-name => 'qseq', -value => $seq),
1665 :     $cgi->submit('Extract Matched Sequences'),
1666 :     # $cgi->submit('Align Matched Sequences'),
1667 :     &HTML::make_table($col_hdrs,$table,"Best Hits"),
1668 :     $cgi->submit('Extract Matched Sequences'),
1669 :     # $cgi->submit('Align Matched Sequences'),
1670 :     $cgi->end_form
1671 :     );
1672 : overbeek 1.30 }
1673 : overbeek 1.17
1674 : golsen 1.103
1675 : efrank 1.1 sub verify_db {
1676 :     my($db,$type) = @_;
1677 :    
1678 : overbeek 1.17 if ($type =~ /^p/i)
1679 : efrank 1.1 {
1680 : parrello 1.75 if ((! -s "$db.psq") || (-M "$db.psq" > -M $db))
1681 :     {
1682 :     system "$FIG_Config::ext_bin/formatdb -p T -i $db";
1683 :     }
1684 : efrank 1.1 }
1685 :     else
1686 :     {
1687 : parrello 1.75 if ((! -s "$db.nsq") || (-M "$db.nsq" > -M $db))
1688 :     {
1689 :     system "$FIG_Config::ext_bin/formatdb -p F -i $db";
1690 :     }
1691 : efrank 1.1 }
1692 : parrello 1.75 }
1693 : overbeek 1.7
1694 :     sub export_assignments {
1695 :     my($fig,$cgi,$html,$who) = @_;
1696 :     my($genome,$x);
1697 :    
1698 : olson 1.124 my @genomes = map { $_ =~ /(\d+\.\d+)/; $1 } $cgi->param('korgs');
1699 : overbeek 1.7
1700 :     if (@genomes == 0)
1701 :     {
1702 : parrello 1.75 @genomes = $fig->genomes;
1703 : overbeek 1.7 }
1704 :    
1705 : overbeek 1.10 my @assignments = $fig->assignments_made(\@genomes,$who,$cgi->param('after_date'));
1706 : overbeek 1.7 if (@assignments == 0)
1707 :     {
1708 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no assignments where made by $who"));
1709 : overbeek 1.7 }
1710 :     else
1711 :     {
1712 : parrello 1.75 my $col_hdrs = ["FIG id", "External ID", "Genus/Species","Assignment"];
1713 :     my $tab = [];
1714 :     my($x,$peg,$func);
1715 :     foreach $x (@assignments)
1716 :     {
1717 :     ( $peg, $func ) = @$x;
1718 :     push( @$tab,[ HTML::set_prot_links( $cgi, $peg ),
1719 :     HTML::set_prot_links( $cgi, ext_id( $fig, $peg ) ),
1720 :     $fig->genus_species($fig->genome_of($peg)),
1721 :     $func
1722 :     ] );
1723 :     }
1724 :    
1725 :     if ($cgi->param('save_assignments'))
1726 :     {
1727 :     my $user = $cgi->param('save_user');
1728 :     if ($user)
1729 :     {
1730 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1731 :     my $file = &FIG::epoch_to_readable(time) . ":$who:exported_from_local_SEED";
1732 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1733 :     {
1734 :     print TMP join("",map { join("\t",@$_) . "\n" } map { [$_->[0],$_->[3]] } @$tab);
1735 :     close(TMP);
1736 :     }
1737 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1738 :     }
1739 :     else
1740 :     {
1741 :     push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1742 :     }
1743 :     }
1744 :    
1745 :     if ($cgi->param('tabs'))
1746 :     {
1747 :     print $cgi->header;
1748 :     print "<pre>\n";
1749 :     print join("",map { join("\t",@$_) . "\n" } @$tab);
1750 :     print "</pre>\n";
1751 :     exit;
1752 :     }
1753 :     else
1754 :     {
1755 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Assignments Made by $who"));
1756 :     }
1757 : overbeek 1.7 }
1758 :     }
1759 :    
1760 :     sub ext_id {
1761 :     my($fig,$peg) = @_;
1762 :    
1763 :     my @mapped = grep { $_ !~ /^fig/ } map { $_->[0] } $fig->mapped_prot_ids($peg);
1764 :     if (@mapped == 0)
1765 :     {
1766 : parrello 1.75 return $peg;
1767 : overbeek 1.7 }
1768 :    
1769 :     my @tmp = ();
1770 :     if ((@tmp = grep { $_ =~ /^sp/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1771 :     if ((@tmp = grep { $_ =~ /^pir/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1772 :     if ((@tmp = grep { $_ =~ /^gi/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1773 :     if ((@tmp = grep { $_ =~ /^tr/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1774 :     if ((@tmp = grep { $_ =~ /^tn/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1775 :     if ((@tmp = grep { $_ =~ /^kegg/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1776 :    
1777 :     return $peg;
1778 :     }
1779 :    
1780 : overbeek 1.28 sub translate_assignments {
1781 :     my($fig,$cgi,$html,$from_func,$to_func) = @_;
1782 :    
1783 : overbeek 1.56 my @funcs = grep { $_ =~ /^\S.*\S$/ } split(/[\012\015]+/,$from_func);
1784 :    
1785 : overbeek 1.28 my $user = $cgi->param('save_user');
1786 :     if ($user)
1787 :     {
1788 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1789 :     my $file = &FIG::epoch_to_readable(time) . ":$user:translation";
1790 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1791 :     {
1792 : overbeek 1.55 my($peg,$func);
1793 : overbeek 1.28
1794 : overbeek 1.56 foreach $from_func (@funcs)
1795 : overbeek 1.28 {
1796 : overbeek 1.57 my $from_funcQ = quotemeta $from_func;
1797 :    
1798 : overbeek 1.56 foreach $peg ($fig->seqs_with_role($from_func))
1799 : overbeek 1.28 {
1800 : overbeek 1.56 if ($peg =~ /^fig\|/)
1801 : overbeek 1.28 {
1802 : overbeek 1.56 $func = $fig->function_of($peg);
1803 : overbeek 1.125 my $comment = "";
1804 :     if ($func =~ /^([^\#]*)(\#.*)$/)
1805 :     {
1806 :     $comment = $2;
1807 :     $func = $1;
1808 :     $func =~ s/\s+$//;
1809 :     $comment = $comment ? " $comment" : "";
1810 :     }
1811 :    
1812 : overbeek 1.56 if ($func eq $from_func)
1813 :     {
1814 : overbeek 1.125 print TMP "$peg\t$to_func$comment\n";
1815 : overbeek 1.56 }
1816 : overbeek 1.73 else
1817 : overbeek 1.57 {
1818 : overbeek 1.73 my @pieces = grep { $_ } split(/(\s+[\/@]\s+)|(\s*;\s+)/,$func);
1819 :     if (@pieces > 1)
1820 :     {
1821 :     my $func1 = join("",map { $_ =~ s/^$from_funcQ$/$to_func/; $_ } @pieces);
1822 :     if ($func ne $func1)
1823 :     {
1824 : overbeek 1.125 print TMP "$peg\t$func1$comment\n";
1825 : overbeek 1.73 }
1826 :     }
1827 : overbeek 1.57 }
1828 : overbeek 1.28 }
1829 :     }
1830 :     }
1831 :     close(TMP);
1832 :     }
1833 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1834 :     }
1835 :     else
1836 :     {
1837 : parrello 1.75 push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1838 : overbeek 1.28 }
1839 :     }
1840 : efrank 1.92
1841 :     sub find_pegs_by_cv1 {
1842 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1843 :    
1844 :     # Remember kind of search that got us hear so we can call back
1845 :     # with same kind
1846 :     my $search = "Search";
1847 :     if ($cgi->param('Search genome selected below')) {
1848 :     $search=uri_escape('Search genome selected below');
1849 :     } elsif ( $cgi->param('Search Selected Organisms') ) {
1850 :     $search = uri_escape('Search Selected Organisms');
1851 :     } elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1852 :     $search = uri_escape('Find Genes in Org that Might Play the Role');
1853 :     }
1854 :    
1855 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1856 :    
1857 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1858 :     my $find_table_rows;
1859 :     my $counter = 0;
1860 :     for my $r (@$search_results)
1861 :     {
1862 :     my @temp = split("\t",$r);
1863 :     my $row = [];
1864 :     my $id= $temp[1];
1865 :     my $term = $temp[2];
1866 :     my $id_and_term = $id."; ".$term;
1867 :     my $pattern=uri_escape("$id; $term");
1868 :    
1869 :     my $link = "index.cgi?pattern=$pattern&Search=1&user=$user";
1870 :     my $cb = "<a href=$link>Find PEGs</a>";
1871 :    
1872 :     #feh my $cb = $cgi->submit(-name=>'$search', -value=>'Find PEGs');
1873 :     #my $cb_value = $cv."split_here".$id."; ".$term;
1874 :     #my $cb ="<input type=checkbox name=find_checked_$counter value='$cb_value'>" ;
1875 :     push(@$row,$cb);
1876 :     push(@$row,$cv);
1877 :     push(@$row,$id_and_term);
1878 :     push(@$find_table_rows,$row);
1879 :     $counter = $counter + 1;
1880 :     }
1881 :    
1882 :     my $find_terms_button="";
1883 :     if ($counter > 0) {
1884 :     $find_terms_button= $cgi->submit(-name=>'$search', -value=>'$search');
1885 :     }
1886 :    
1887 :     # build the page
1888 :     push @$html,
1889 :     $cgi->start_form(),
1890 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1891 :     $cgi->br,
1892 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1893 :     $cgi->hr,
1894 :     "<h4>Terms Matching Your Criteria </h4>\n",
1895 :     $cgi->br,
1896 :     &HTML::make_table($find_col_hdrs,$find_table_rows),
1897 :     $cgi->br,
1898 :     $find_terms_button,
1899 :     $cgi->end_form;
1900 :    
1901 :     return $html;
1902 :     }
1903 :    
1904 :     sub find_pegs_by_cv {
1905 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1906 :    
1907 :     # Remember kind of search that got us hear so we can call back
1908 :     # with same kind (not working so force to simple Search)
1909 :    
1910 :     my $search = "Search";
1911 :    
1912 :     #if ($cgi->param('Search genome selected below')) {
1913 :     # $search='Search genome selected below';
1914 :     #} elsif ( $cgi->param('Search Selected Organisms') ) {
1915 :     # $search = 'Search Selected Organisms';
1916 :     #} elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1917 :     # $search = 'Find Genes in Org that Might Play the Role';
1918 :     #}
1919 :    
1920 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1921 :    
1922 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1923 :     my @patterns=();
1924 :     for my $r (@$search_results)
1925 :     {
1926 :     my @temp = split("\t",$r);
1927 :     my $id= $temp[1];
1928 :     my $term = $temp[2];
1929 :     my $pattern="$id; $term";
1930 :    
1931 :     push(@patterns,$pattern);
1932 :     }
1933 :    
1934 :     my @pattern_radio;
1935 :     if ($#patterns + 1) {
1936 :     @pattern_radio = $cgi->radio_group( -name => 'pattern',
1937 :     -values => [ @patterns ]
1938 :     );
1939 :     } else {
1940 :     @pattern_radio = ("Nothing found");
1941 :     }
1942 :    
1943 :     my $find_terms_button= $cgi->submit(-name=>"Search", -value=>"Search");
1944 :    
1945 :     # build the page
1946 :     push @$html,
1947 :     $cgi->start_form(),
1948 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1949 :     $cgi->br,
1950 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1951 :     $cgi->hr,
1952 :     "<h4>$cv Terms Matching Your Criteria </h4>\n",
1953 :     $cgi->br,
1954 :     $find_terms_button,
1955 :     $cgi->br,
1956 :     $cgi->br,
1957 :     join( "<br>", @pattern_radio),
1958 :     # &HTML::make_table($find_col_hdrs,$find_table_rows),
1959 :     $cgi->br,
1960 :     $find_terms_button,
1961 :     $cgi->end_form;
1962 :    
1963 :     return $html;
1964 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3