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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3