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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3