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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3