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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.109 - (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 :     "This is the <i>new</i> subsystems code, and is now officially released.",
398 :     $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 : golsen 1.47 push( @$html, $cgi->h2('Finding Candidates for a Functional Role'),
526 : parrello 1.75 "Make sure that you type the functional role you want to search for in the Search Pattern above",
527 :     $cgi->br,
528 :     $cgi->submit('Find Genes in Org that Might Play the Role'),
529 :     $cgi->hr);
530 : overbeek 1.17
531 : golsen 1.60 my @maps = sort map { $map = $_; $name = $fig->map_name($map); "$name ($map)" } $fig->all_maps;
532 :    
533 : golsen 1.47 push( @$html, $cgi->h2('Metabolic Overviews and Subsystem Maps (via KEGG & SEED) - Choose Map'),
534 : parrello 1.75 $cgi->submit('Metabolic Overview'),
535 :     $cgi->br,
536 :     $cgi->br,
537 :     $cgi->scrolling_list(-name => 'kmap',
538 : efrank 1.1 -values => [@maps],
539 : parrello 1.75 -size => 10
540 : efrank 1.1 ),
541 : parrello 1.75 $cgi->hr);
542 : overbeek 1.17
543 : golsen 1.47 push( @$html, $cgi->h2('Searching DNA or Protein Sequences (in a selected organism)'),
544 : parrello 1.75 "<TABLE>\n",
545 :     " <TR>\n",
546 :     " <TD>Sequence/Pattern: </TD>",
547 :     " <TD Colspan=3>", $cgi->textarea(-name => 'seq_pat', -rows => 10, -cols => 70), "</TD>\n",
548 :     " </TR>\n",
549 :     " <TR>\n",
550 :     " <TD>Search Program: </TD>",
551 :     " <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>",
552 :     " <TD> Program Options:</TD>",
553 :     " <TD>", $cgi->textfield( -name => "blast_options", -size => 27 ), "</TD>",
554 :     " </TR>\n",
555 :     "</TABLE>\n",
556 :     $cgi->submit('Search for Matches'),
557 :     $cgi->hr);
558 : overbeek 1.17
559 : olson 1.41 #
560 :     # Make assignment export tbl.
561 :     #
562 :    
563 :     my @atbl;
564 : golsen 1.64 push(@atbl, [ "Extract assignments made by ",
565 : parrello 1.75 $cgi->textfield(-name => "made_by", -size => 25) . " (do not prefix with <b>master:</b>)" ]);
566 : golsen 1.64 push(@atbl, [ "Save as user: ",
567 : parrello 1.75 $cgi->textfield(-name => "save_user", -size => 25) . " (do not prefix with <b>master:</b>)" ] );
568 : golsen 1.64 push(@atbl, [ "After date (MM/DD/YYYY) ",
569 : parrello 1.75 $cgi->textfield(-name => "after_date", -size => 15)]);
570 : olson 1.41
571 :     push(@$html,
572 : overbeek 1.84 $cgi->h2($cgi->a({name => "exporting_assignments"}, 'Exporting Assignments')),
573 : parrello 1.75 &HTML::make_table(undef, \@atbl, '', border => 0),
574 :     $cgi->checkbox(-label => 'Tab-delimited Spreadsheet', -name => 'tabs', -value => 1),
575 :     $cgi->br,
576 :     $cgi->checkbox(-label => 'Save Assignments', -name => 'save_assignments', -value => 1),
577 :     $cgi->br,
578 :     $cgi->submit('Extract Assignments'),
579 :     $cgi->br, $cgi->br, $cgi->br,
580 :     "Alternatively, you can generate a set of assignments as translations of existing assignments. ",
581 :     "To do so, you need to make sure that you fill in the <b>Save as user</b> field just above. You ",
582 :     "should use something like <b>RossO</b> (leave out the <b>master:</b>). When you look at the assignments (and decide which ",
583 :     "to actually install), they will be made available under that name (but, when you access them, ",
584 :     "you will normally be using something like <b>master:RossO</b>)",
585 :     $cgi->br,$cgi->br,
586 :     "From: ",
587 :     $cgi->textarea(-name => 'from_func', -rows => 4, -cols => 100),
588 :     $cgi->br,$cgi->br,
589 :     "To:&nbsp;&nbsp;&nbsp;&nbsp; ",$cgi->textfield(-name => "to_func", -size => 100),
590 :     $cgi->br,
591 : golsen 1.103 "<TABLE Width=100%><TR><TD>",
592 :     $cgi->submit('Generate Assignments via Translation'),
593 :     "</TD><TD NoWrap Width=1%>",
594 : overbeek 1.76 $cgi->a({class=>"help", target=>"help", href=>"Html/seedtips.html#replace_names"}, "Help with generate assignments via translation"),
595 : golsen 1.103 "</TD></TR></TABLE>\n"
596 : golsen 1.95 );
597 :    
598 :     push(@$html,
599 : parrello 1.75 $cgi->hr,
600 :     $cgi->h2('Searching for Interesting Genes'),
601 :     $cgi->submit('Search for Genes Matching an Occurrence Profile or Common to a Set of Organisms'),
602 : overbeek 1.17 $cgi->end_form
603 : parrello 1.75 );
604 : overbeek 1.14
605 :     push(@$html,
606 : parrello 1.75 $cgi->hr,
607 :     $cgi->h2('Process Saved Assignments Sets'),
608 :     $cgi->start_form(-action => "assignments.cgi"),
609 :     "Here you should include the <b>master:</b>. Thus use something like <b>master:RossO</b>",$cgi->br,
610 :     $cgi->br,
611 :     "Enter user: ",
612 :     $cgi->textfield(-name => "user", -size => 20),
613 :     $cgi->submit('Process Assignment Sets'),
614 : overbeek 1.17 $cgi->end_form
615 : parrello 1.75 );
616 : efrank 1.1
617 : overbeek 1.19 push(@$html,
618 : parrello 1.75 $cgi->hr,
619 :     $cgi->h2('Align Sequences'),
620 : olson 1.89 $cgi->start_form(-action => $this_script),
621 : parrello 1.75 "Enter user: ",
622 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
623 :     $cgi->submit('Align Sequences'),": ",
624 :     $cgi->textfield(-name => "seqids", -size => 100),
625 : overbeek 1.31 $cgi->end_form
626 : parrello 1.75 );
627 : overbeek 1.51
628 :     push(@$html,
629 : parrello 1.75 $cgi->hr,
630 :     $cgi->h2('Locate PEGs in Subsystems'),
631 :     "If you wish to locate PEGs in subsystems, you have two approaches supported. You can
632 : overbeek 1.56 give a FIG id, and you will get a list of all homologs in the designated genome that occur in subsystems.
633 :     Alternatively, you can specify a functional role, and all PEGs in the genome that match that role will be shown.",
634 : olson 1.89 $cgi->start_form(-action => $this_script),
635 : parrello 1.75 "Enter user: ",
636 :     $cgi->textfield(-name => "user", -size => 20), $cgi->br,
637 :     $cgi->br,"Genome: ",$cgi->textfield(-name => "genome", -size => 15),$cgi->br,
638 :     "Search: ",$cgi->textfield(-name => "subsearch", -size => 100),$cgi->br,
639 :     $cgi->submit('Find PEGs'),": ",
640 : overbeek 1.51 $cgi->end_form
641 : parrello 1.75 );
642 : efrank 1.1 }
643 :    
644 : overbeek 1.83 #
645 :     # Show a message of the day if it's present.
646 :     #
647 :     sub show_motd
648 :     {
649 :     my($fig, $cgi, $html) = @_;
650 :    
651 :     my $motd_file = "$FIG_Config::fig_disk/config/motd";
652 :    
653 :     if (open(F, "<$motd_file"))
654 :     {
655 :     push(@$html, "<p>\n");
656 :     while (<F>)
657 :     {
658 :     push(@$html, $_);
659 :     }
660 :     close(F);
661 :     push(@$html, "<hr>\n");
662 :     }
663 :     }
664 : overbeek 1.17
665 :     #==============================================================================
666 :     # Indexed objects (text search)
667 :     #==============================================================================
668 :    
669 : efrank 1.1 sub show_indexed_objects {
670 : golsen 1.22 my($fig, $cgi, $html, $pattern) = @_;
671 :     my($msg, $i);
672 : efrank 1.1
673 :     if ($pattern =~ /^\s*(fig\|\d+\.\d+\.peg\.\d+)\s*$/)
674 :     {
675 : parrello 1.75 my $peg = $1;
676 :     my $user = $cgi->param('user');
677 :     $user = $user ? $user : "";
678 : paczian 1.102 my @prot_out;
679 :     if (defined($cgi->param('fromframe'))) {
680 :     $ENV{'REQUEST_METHOD'} = "GET";
681 :     $ENV{"QUERY_STRING"} = "prot=$peg\&user=$user\&action=proteinpage";
682 :     $ENV{"REQUEST_URI"} =~ s/$this_script/frame.cgi/;
683 :     @prot_out = TICK("./frame.cgi");
684 :     } else {
685 :     $ENV{'REQUEST_METHOD'} = "GET";
686 :     $ENV{"QUERY_STRING"} = "prot=$peg\&user=$user";
687 :     $ENV{"REQUEST_URI"} =~ s/$this_script/protein.cgi/;
688 :     @prot_out = TICK("./protein.cgi");
689 :     }
690 : parrello 1.75 print @prot_out;
691 :     exit;
692 : efrank 1.1 }
693 : overbeek 1.71 $pattern =~ s/([a-zA-Z0-9])\|([a-zA-Z0-9])/$1\\\|$2/ig;
694 : efrank 1.92
695 :     my $search_kind = $cgi->param("search_kind");
696 :     if ( $search_kind && ! ($search_kind eq "DIRECT") ) {
697 :     #otherwise $search_kind is name of controlled vocab
698 :     find_pegs_by_cv($fig, $cgi, $html, $user, $pattern, $search_kind);
699 :     return;
700 :     }
701 :    
702 : overbeek 1.17 push( @$html, $cgi->br );
703 : olson 1.70 my( $peg_index_data, $role_index_data ) = $fig->search_index($pattern, $cgi->param("substring_match") eq "on");
704 : overbeek 1.17 my $maxpeg = defined( $cgi->param("maxpeg") ) ? $cgi->param("maxpeg") : 100;
705 :     my $maxrole = defined( $cgi->param("maxrole") ) ? $cgi->param("maxrole") : 100;
706 : mkubal 1.109
707 :     my $output_file = "$FIG_Config::temp/search_results.txt";
708 :     open(OUT,">$output_file");
709 :    
710 :     #foreach my $peg (map { $_->[0] } @$peg_index_data)
711 :     #{
712 :     # my($p,$t,$v,$l);
713 :     # my $function = $fig->function_of($peg);
714 :     # my @aliases = $fig->feature_aliases($peg);
715 :     # my $gs = $fig->genus_species($peg);
716 :     # my @rets = $fig->get_attributes($peg,$pattern);
717 :     # foreach my $ret (@rets){
718 :     # ($p,$t,$v,$l) = @$ret;
719 :     # }
720 :     # my $alias_string = join("_",@aliases);
721 :     # print OUT "$peg\t$gs\t$function\t$alias_string\t$pattern\t$v\n";
722 :     # }
723 :    
724 : overbeek 1.17
725 : redwards 1.53 # RAE added lines to allow searching within a single organism
726 : golsen 1.59 # if ($cgi->param('korgs'))
727 :     # {
728 :     # $cgi->param('korgs') =~ /\((\d+\.*\d*)\)/;
729 :     # $org=$1; # this should be undef if korgs is not defined
730 :    
731 :     # push (@$html, $cgi->br, "Matches found in ",$cgi->param('korgs'), $cgi->p);
732 :     # my @clean_data; my @clean_index;
733 :     # while (@$peg_index_data)
734 :     # {
735 :     # my ($data, $index)=(shift @$peg_index_data, shift @$role_index_data);
736 :     # next unless (${$data}[0] =~ /^fig\|$org\.peg/);
737 :     # push @clean_data, $data;
738 :     # push @clean_index, $index;
739 :     # }
740 :    
741 :     # @$peg_index_data=@clean_data;
742 :     # @$role_index_data=@clean_index;
743 :     # }
744 :     ## End of added lines
745 : redwards 1.53
746 : mkubal 1.99 # RAE version with separate submit buttoxns and more than one org in korg
747 : redwards 1.63 # this is used by organisms.cgi for group specific searches
748 :     if ( $cgi->param('korgs') && $cgi->param('Search Selected Organisms')
749 :     )
750 :     {
751 :     my @temp;
752 :     foreach my $org ($cgi->param('korgs'))
753 :     {
754 :     push @temp, grep { $_->[0] =~ /^fig\|$org/ } @$peg_index_data;
755 :     }
756 :     @$peg_index_data = @temp;
757 :     }
758 :    
759 : golsen 1.59 # GJO version with separate submit buttons
760 : redwards 1.53
761 : golsen 1.59 if ( $cgi->param('korgs') && $cgi->param('korgs') =~ /\((\d+\.*\d*)\)/
762 :     && $cgi->param('Search genome selected below')
763 :     )
764 :     {
765 : parrello 1.75 my $org = $1;
766 :     push @$html, $cgi->br, "Matches found in ",$cgi->param('korgs'), $cgi->p;
767 : mkubal 1.79 @$peg_index_data = grep { $_->[0] =~ /^fig\|$org\.*/ } @$peg_index_data;
768 : redwards 1.53 }
769 :    
770 : golsen 1.59 if ( ( $maxpeg > 0 ) && @$peg_index_data )
771 : overbeek 1.17 {
772 : parrello 1.75 # RAE: Added javascript buttons see below. Only two things are needed.
773 :     # The form must have a name parameter, and the one line of code for the
774 :     # buttons. Everything else is automatic
775 :    
776 :     push( @$html, $cgi->start_form( -method => 'post',
777 :     -target => "window$$",
778 :     -action => 'fid_checked.cgi',
779 :     -name => 'found_pegs'
780 :     ),
781 :     $cgi->hidden(-name => 'user', -value => $user),
782 :     "For Selected (checked) sequences: ",
783 :     $cgi->submit('get sequences'),
784 :     $cgi->submit('view annotations'),
785 :     $cgi->submit('assign/annotate'),
786 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
787 :     $cgi->br, $cgi->br
788 :     );
789 : efrank 1.1
790 : redwards 1.63 # RAE Add the check all/uncheck all boxes.
791 :     push (@$html, $cgi->br, &HTML::java_buttons("found_pegs", "checked"), $cgi->br);
792 : parrello 1.75
793 :     my $n = @$peg_index_data;
794 :     if ($n > $maxpeg)
795 :     {
796 :     $msg = "Showing first $maxpeg out of $n protein genes";
797 :     $#{$peg_index_data} = $maxpeg-1;
798 :     }
799 :     else
800 :     {
801 : mkubal 1.79 $msg = "Showing $n FEATURES";
802 : parrello 1.75 }
803 :    
804 : mkubal 1.79 my $col_hdrs = ["Sel","FEATURE","Organism","Aliases","Function","Who"];
805 : overbeek 1.106 my $tab = [ map { format_peg_entry( $fig, $cgi, $_ ) } sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
806 : mkubal 1.109
807 :     my @att_values;
808 :     foreach my $peg (map { $_->[0] } @$peg_index_data){
809 :     my($p,$t,$v,$l);
810 :     if($fig->get_attributes($peg,$pattern)){
811 :     my @rets = $fig->get_attributes($peg,$pattern);
812 :     foreach my $ret (@rets){
813 :     ($p,$t,$v,$l) = @$ret;
814 :     push(@att_values,$v);
815 :     }
816 :     }
817 :     else{push(@att_values,"no_attribute_value")}
818 :     }
819 :    
820 :     my $tab2 = [ sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
821 :    
822 :     push( @$html,$cgi->br,
823 :     "<a href=$FIG_Config::temp_url/search_results.txt>Download_Search_Results</a>",
824 :     &HTML::make_table($col_hdrs,$tab,$msg),
825 : parrello 1.75 $cgi->br,
826 :     "For SELECTed (checked) sequences: ",
827 :     $cgi->submit('get sequences'),
828 :     $cgi->submit('view annotations'),
829 :     $cgi->submit('assign/annotate'),
830 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
831 :     $cgi->br,
832 :     $cgi->end_form
833 : mkubal 1.109 );
834 :    
835 :     my $result_counter = 0;
836 :     foreach my $t (@$tab2){
837 :     my $att_value = $att_values[$result_counter];
838 :     my $string = join("\t",@$t);
839 :     $string = $string."\t".$att_value;
840 :     print OUT "$string\n";
841 :     $result_counter = $result_counter + 1;
842 :     }
843 :    
844 : efrank 1.1 }
845 : golsen 1.59 elsif ( $maxpeg > 0 )
846 :     {
847 : parrello 1.75 push @$html, $cgi->h3('No matching protein genes');
848 : golsen 1.59 }
849 : overbeek 1.17
850 : golsen 1.59 if ( ( $maxrole > 0 ) && @$role_index_data )
851 : efrank 1.1 {
852 : parrello 1.75 my $n = @$role_index_data;
853 :     if ($n > $maxrole)
854 :     {
855 :     $msg = "Showing first $maxrole out of $n Roles";
856 :     $#{$role_index_data} = $maxrole - 1;
857 :     }
858 :     else
859 :     {
860 :     $msg = "Showing $n Roles";
861 :     }
862 :    
863 :     if ( $maxpeg > 0 ) { push( @$html, $cgi->hr ) }
864 :     my $col_hdrs = ["Role"];
865 :     my $tab = [ map { &format_role_entry($fig,$cgi,$_) } @$role_index_data ];
866 :     push( @$html, &HTML::make_table($col_hdrs,$tab,$msg) );
867 : efrank 1.1 }
868 : golsen 1.59 elsif ( $maxrole > 0 )
869 :     {
870 : parrello 1.75 push @$html, $cgi->h3('No matching roles');
871 : golsen 1.59 }
872 : efrank 1.1 }
873 :    
874 : golsen 1.59
875 : efrank 1.1 sub format_peg_entry {
876 : golsen 1.67 my( $fig, $cgi, $entry ) = @_;
877 : efrank 1.1 my($i,$function,$who);
878 :    
879 :     my($peg,$gs,$aliases,@funcs) = @$entry;
880 : overbeek 1.17
881 : golsen 1.21 $gs =~ s/\s+\d+$//; # Org name comes with taxon_id appended (why?) -- GJO
882 : efrank 1.1
883 :     @funcs = map { $_ =~ s/^function:\s*//; $_ } @funcs;
884 :    
885 :     if ($aliases)
886 :     {
887 : parrello 1.75 $aliases =~ s/^aliases://;
888 : efrank 1.1 }
889 :     else
890 :     {
891 : parrello 1.75 $aliases = "";
892 : efrank 1.1 }
893 :    
894 : golsen 1.21 my $user = $cgi->param('user');
895 :     $user = $user ? $user : "";
896 :    
897 : efrank 1.1 if ($user)
898 :     {
899 : parrello 1.75 for ($i=0; ($i < @funcs) && ($funcs[$i] !~ /\#$user/); $i++) {}
900 :     if ($i < @funcs)
901 :     {
902 :     ($function,$who) = split(/\#/,$funcs[$i]);
903 :     }
904 : efrank 1.1 }
905 : golsen 1.21
906 : efrank 1.1 if (! $function)
907 :     {
908 : parrello 1.75 for ($i=0; ($i < @funcs) && ($funcs[$i] !~ /\#master/); $i++) {}
909 :     if ($i < @funcs)
910 :     {
911 :     ($function,$who) = split(/\#/,$funcs[$i]);
912 :     }
913 : efrank 1.1 }
914 :    
915 :     if ((! $function) && (@funcs > 0))
916 :     {
917 : parrello 1.75 ($function,$who) = split(/\#/,$funcs[0]);
918 : efrank 1.1 }
919 : golsen 1.67 my $box = "<input type=checkbox name=checked value=\"$peg\">";
920 : overbeek 1.17 return [ $box, &HTML::fid_link($cgi,$peg), $gs, $aliases, $function, $who ];
921 : efrank 1.1 }
922 :    
923 :     sub format_role_entry {
924 :     my($fig,$cgi,$entry) = @_;
925 :    
926 :     return [&HTML::role_link($cgi,$entry)];
927 :     }
928 :    
929 :     sub run_prot_scan_for_matches {
930 :     my($fig,$cgi,$html,$org,$pat) = @_;
931 :     my($string,$peg,$beg,$end,$user,$col_hdrs,$tab,$i);
932 :    
933 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
934 :     open(PAT,">$tmp_pat")
935 : parrello 1.75 || die "could not open $tmp_pat";
936 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
937 :     print PAT "$pat\n";
938 :     close(PAT);
939 :     my @out = `$FIG_Config::ext_bin/scan_for_matches -p $tmp_pat < $FIG_Config::organisms/$org/Features/peg/fasta`;
940 :     if (@out < 1)
941 :     {
942 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
943 : efrank 1.1 }
944 :     else
945 :     {
946 : parrello 1.75 if (@out > 2000)
947 :     {
948 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
949 :     $#out = 1999;
950 :     }
951 :    
952 :     push(@$html,$cgi->pre);
953 :     $user = $cgi->param('user');
954 :     $col_hdrs = ["peg","begin","end","string","function of peg"];
955 :     for ($i=0; ($i < @out); $i += 2)
956 :     {
957 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
958 :     {
959 :     $peg = $1;
960 :     $beg = $2;
961 :     $end = $3;
962 :     $string = $out[$i+1];
963 :     chomp $string;
964 :     push( @$tab, [ &HTML::fid_link($cgi,$peg,1),
965 :     $beg,
966 :     $end,
967 :     $string,
968 :     scalar $fig->function_of( $peg, $user )
969 :     ]
970 :     );
971 :     }
972 :     }
973 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
974 :     push(@$html,$cgi->end_pre);
975 : efrank 1.1 }
976 :     unlink($tmp_pat);
977 :     }
978 :    
979 : overbeek 1.17 #==============================================================================
980 :     # Scan for matches
981 :     #==============================================================================
982 :    
983 : efrank 1.1 sub run_dna_scan_for_matches {
984 :     my($fig,$cgi,$html,$org,$pat) = @_;
985 :     my($string,$contig,$beg,$end,$col_hdrs,$tab,$i);
986 :    
987 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
988 :     open(PAT,">$tmp_pat")
989 : parrello 1.75 || die "could not open $tmp_pat";
990 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
991 :     print PAT "$pat\n";
992 :     close(PAT);
993 :     my @out = `cat $FIG_Config::organisms/$org/contigs | $FIG_Config::ext_bin/scan_for_matches -c $tmp_pat`;
994 :     if (@out < 1)
995 :     {
996 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
997 : efrank 1.1 }
998 :     else
999 :     {
1000 : parrello 1.75 if (@out > 2000)
1001 :     {
1002 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
1003 :     $#out = 1999;
1004 :     }
1005 :    
1006 :     push(@$html,$cgi->pre);
1007 :     $col_hdrs = ["contig","begin","end","string"];
1008 :     for ($i=0; ($i < @out); $i += 2)
1009 :     {
1010 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
1011 :     {
1012 :     $contig = $1;
1013 :     $beg = $2;
1014 :     $end = $3;
1015 :     $string = $out[$i+1];
1016 :     chomp $string;
1017 :     push(@$tab,[$contig,$beg,$end,$string]);
1018 :     }
1019 :     }
1020 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
1021 :     push(@$html,$cgi->end_pre);
1022 : efrank 1.1 }
1023 :     unlink($tmp_pat);
1024 :     }
1025 :    
1026 : overbeek 1.17 #==============================================================================
1027 :     # BLAST search
1028 :     #==============================================================================
1029 :    
1030 : efrank 1.1 sub run_blast {
1031 : golsen 1.45 my( $fig, $cgi, $html, $org, $tool, $seq ) = @_;
1032 :     my( $query, @out );
1033 : efrank 1.1
1034 : golsen 1.45 my $tmp_seq = "$FIG_Config::temp/run_blast_tmp$$.seq";
1035 : efrank 1.1
1036 : overbeek 1.17 #--------------------------------------------------------------------------
1037 : golsen 1.97 # Does the request require a defined genome? We never check that the
1038 :     # database build works, so the least we can do is some up-front tests.
1039 :     # -- GJO
1040 :     #--------------------------------------------------------------------------
1041 :    
1042 :     if ( $tool !~ /complete genomes/ )
1043 :     {
1044 :     if ( ! $org || ! -d "$FIG_Config::organisms/$org" )
1045 :     {
1046 :     push @$html, $cgi->h2("Sorry, $tool requires selecting a genome." );
1047 :     return;
1048 :     }
1049 :    
1050 :     if ( ( $tool =~ /blastn/ ) || ( $tool =~ /tblastx/ ) )
1051 :     {
1052 :     if ( ! -f "$FIG_Config::organisms/$org/contigs" )
1053 :     {
1054 :     push @$html, $cgi->h2("Sorry, cannot find DNA data for genome $org." );
1055 :     return;
1056 :     }
1057 :     }
1058 :     else
1059 :     {
1060 :     if ( ! -f "$FIG_Config::organisms/$org/Features/peg/fasta" )
1061 :     {
1062 :     push @$html, $cgi->h2("Sorry, cannot find protein data for genome $org." );
1063 :     return;
1064 :     }
1065 :     }
1066 :     }
1067 :    
1068 :     #--------------------------------------------------------------------------
1069 : overbeek 1.17 # Is the request for an id? Get the sequence
1070 :     #--------------------------------------------------------------------------
1071 : golsen 1.97
1072 : golsen 1.91 if ( ( $query ) = $seq =~ /^\s*([a-zA-Z]{2,4}\|\S+)/ )
1073 : efrank 1.1 {
1074 : parrello 1.75 # Replaced $id with $query so that output inherits label -- GJO
1075 : golsen 1.91 # Found ugly fairure to build correct query sequence for
1076 :     # 'blastp against complete genomes'. Can't figure out
1077 :     # why it ever worked with and id -- GJO
1078 :    
1079 : parrello 1.75 $seq = "";
1080 : golsen 1.91 if ( ($tool eq "blastp") || ($tool eq "tblastn")
1081 :     || ($tool eq 'blastp against complete genomes')
1082 :     )
1083 : parrello 1.75 {
1084 :     $seq = $fig->get_translation($query);
1085 : golsen 1.97 my $func = $fig->function_of( $query, $user );
1086 :     $query .= " $func" if $func;
1087 : parrello 1.75 }
1088 :     elsif ($query =~ /^fig/)
1089 :     {
1090 :     my @locs;
1091 :     if ((@locs = $fig->feature_location($query)) && (@locs > 0))
1092 :     {
1093 :     $seq = $fig->dna_seq($fig->genome_of($query),@locs);
1094 :     }
1095 :     }
1096 :     if (! $seq)
1097 :     {
1098 :     push(@$html,$cgi->h1("Sorry, could not get sequence for $query"));
1099 :     return;
1100 :     }
1101 : efrank 1.1 }
1102 : golsen 1.45
1103 : overbeek 1.17 #--------------------------------------------------------------------------
1104 :     # Is it a fasta format? Get the query name
1105 :     #--------------------------------------------------------------------------
1106 : golsen 1.45
1107 :     elsif ( $seq =~ s/^>\s*(\S+[^\n\012\015]*)// ) # more flexible match -- GJO
1108 : efrank 1.1 {
1109 : parrello 1.75 $query = $1;
1110 : efrank 1.1 }
1111 : golsen 1.45
1112 : overbeek 1.17 #--------------------------------------------------------------------------
1113 :     # Take it as plain text
1114 :     #--------------------------------------------------------------------------
1115 : golsen 1.45
1116 : efrank 1.1 else
1117 :     {
1118 : parrello 1.75 $query = "query";
1119 : efrank 1.1 }
1120 : golsen 1.45
1121 :     #
1122 :     # The rest is taken as the sequence
1123 :     #
1124 :    
1125 : golsen 1.23 $seq =~ s/\s+//g;
1126 : golsen 1.45 open( SEQ, ">$tmp_seq" ) || die "run_blast could not open $tmp_seq";
1127 : efrank 1.1 print SEQ ">$query\n$seq\n";
1128 : golsen 1.45 close( SEQ );
1129 : efrank 1.1
1130 :     if (! $ENV{"BLASTMAT"}) { $ENV{"BLASTMAT"} = "$FIG_Config::blastmat" }
1131 : golsen 1.88 my $blast_opt = $cgi->param( 'blast_options' ) || '';
1132 : efrank 1.1
1133 : golsen 1.45 if ( $tool eq "blastp" )
1134 : efrank 1.1 {
1135 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1136 :     &verify_db( $db, "p" );
1137 : golsen 1.88 @out = map { &HTML::set_prot_links($cgi,$_) } execute_blastall( 'blastp', $tmp_seq, $db, $blast_opt );
1138 : efrank 1.1 }
1139 : golsen 1.45
1140 :     elsif ( $tool eq "blastx" )
1141 : efrank 1.1 {
1142 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1143 :     &verify_db( $db, "p" );
1144 : golsen 1.88 @out = map { &HTML::set_prot_links($cgi,$_) } execute_blastall( 'blastx', $tmp_seq, $db, $blast_opt );
1145 : efrank 1.1 }
1146 : golsen 1.45
1147 :     elsif ( $tool eq "blastn" )
1148 : efrank 1.1 {
1149 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1150 :     &verify_db( $db, "n" ); ### fix to get all contigs
1151 : golsen 1.88 @out = execute_blastall( 'blastn', $tmp_seq, $db, "-r 1 -q -1 " . $blast_opt );
1152 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1153 : efrank 1.1 }
1154 : golsen 1.45
1155 :     elsif ( $tool eq "tblastn" )
1156 : efrank 1.1 {
1157 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1158 :     &verify_db( $db, "n" ); ### fix to get all contigs
1159 : golsen 1.88 @out = execute_blastall( 'tblastn', $tmp_seq, $db, $blast_opt );
1160 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1161 : efrank 1.1 }
1162 : golsen 1.45
1163 :     elsif ( $tool eq 'blastp against complete genomes' ) ### this tool gets nonstandard treatment: RAO
1164 : overbeek 1.30 {
1165 : golsen 1.103 &blast_complete( $fig, $cgi, $html, $tmp_seq, $query, $seq );
1166 : golsen 1.91 unlink( $tmp_seq );
1167 :     return;
1168 : overbeek 1.30 }
1169 : golsen 1.45
1170 : overbeek 1.17 if (@out < 1) # This is really a bigger problem than no hits (GJO)
1171 : efrank 1.1 {
1172 : golsen 1.88 push @$html, $cgi->h1( "Sorry, no blast output" );
1173 : efrank 1.1 }
1174 :     else
1175 :     {
1176 : golsen 1.88 push @$html, $cgi->pre, @out, $cgi->end_pre;
1177 : efrank 1.1 }
1178 : golsen 1.45 unlink( $tmp_seq );
1179 : efrank 1.1 }
1180 :    
1181 : golsen 1.45
1182 : golsen 1.88 # `$blastall -p $prog -i $tmp_seq -d $db $blast_opt`
1183 :     # execute_blastall( $prog, $input_file, $db, $options )
1184 :    
1185 :     sub execute_blastall
1186 :     {
1187 :     my( $prog, $input, $db, $options ) = @_;
1188 :    
1189 :     my $blastall = "$FIG_Config::ext_bin/blastall";
1190 :     my @args = ( '-p', $prog, '-i', $input, '-d', $db, split(/\s+/, $options) );
1191 :    
1192 :     my $bfh;
1193 :     my $pid = open( $bfh, "-|" );
1194 :     if ( $pid == 0 )
1195 :     {
1196 :     exec( $blastall, @args );
1197 :     die join( " ", $blastall, @args, "failed: $!" );
1198 :     }
1199 :    
1200 : golsen 1.91 <$bfh>
1201 : golsen 1.88 }
1202 :    
1203 :    
1204 : golsen 1.91 # Changed to:
1205 :     # Include low complexity filter in blast search.
1206 :     # Remove all but first match to a given database sequence.
1207 :     # Sort by bit-score, not E-value (which becomes equal for all strong matches).
1208 :     # Limit to 1000 matches.
1209 :     # -- GJO
1210 :    
1211 :     sub blast_complete
1212 :     {
1213 : golsen 1.103 my( $fig, $cgi, $html, $seqfile, $query, $seq ) = @_;
1214 : golsen 1.88 my( $genome, @sims );
1215 :    
1216 : overbeek 1.30 @sims = ();
1217 : golsen 1.91 foreach $genome ( $fig->genomes("complete") )
1218 : overbeek 1.30 {
1219 : parrello 1.75 my $db = "$FIG_Config::organisms/$genome/Features/peg/fasta";
1220 :     next if (! -s $db);
1221 : overbeek 1.30
1222 : parrello 1.75 &verify_db($db,"p");
1223 :     my $sim;
1224 : golsen 1.91 my %seen = ();
1225 :     push @sims, map { chomp;
1226 :     $sim = [ split /\t/ ];
1227 :     $sim->[10] =~ s/^e-/1.0e-/;
1228 :     $seen{ $sim->[1] }++ ? () : $sim
1229 :     }
1230 :     execute_blastall( 'blastp', $seqfile, $db, '-m 8 -F T -e 1e-5' );
1231 : overbeek 1.30 }
1232 : golsen 1.91
1233 :     @sims = sort { $b->[11] <=> $a->[11] } @sims;
1234 :     if ( @sims > 1000 ) { @sims = @sims[0 .. 999] }
1235 : golsen 1.103 &format_sims( $fig, $cgi, $html, \@sims, $query, $seq );
1236 : overbeek 1.30 }
1237 :    
1238 : golsen 1.65
1239 :     #------------------------------------------------------------------------------
1240 : golsen 1.97 # Graphically display search results on contigs
1241 : golsen 1.65 #
1242 :     # use FIGjs qw( toolTipScript );
1243 :     # use GenoGraphics qw( render );
1244 :     #------------------------------------------------------------------------------
1245 :     #
1246 : golsen 1.97 # Fields produced by next_blast_hsp:
1247 :     #
1248 :     # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1249 :     # 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
1250 : golsen 1.65 #------------------------------------------------------------------------------
1251 :    
1252 :     sub blast_graphics {
1253 :     my ( $fig_or_sprout, $cgi, $genome, $out, $tool ) = @_;
1254 :    
1255 :     my $e_min = 0.1;
1256 :     my $gg = [];
1257 : golsen 1.97 my @html = ();
1258 : golsen 1.65
1259 : golsen 1.97 # Changed to use standalone parsing function, not shell script -- GJO
1260 : golsen 1.65
1261 : golsen 1.97 my $outcopy = [ @$out ];
1262 :     while ( $_ = &gjoparseblast::next_blast_hsp( $outcopy ) )
1263 : golsen 1.65 {
1264 : golsen 1.97 my ( $qid, $qlen, $contig, $slen ) = @$_[0, 2, 3, 5 ];
1265 :     my ( $e_val, $n_mat, $n_id, $q1, $q2, $s1, $s2 ) = @$_[ 7, 10, 11, 15, 16, 18, 19 ];
1266 :     next if $e_val > $e_min;
1267 :     my ( $genes, $min, $max ) = hsp_context( $fig_or_sprout, $cgi, $genome,
1268 :     $e_val, 100 * $n_id / $n_mat,
1269 :     $qid, $q1, $q2, $qlen,
1270 :     $contig, $s1, $s2, $slen
1271 :     );
1272 :     if ($min && $max)
1273 : golsen 1.65 {
1274 : golsen 1.97 push @$gg, [ substr( $contig, 0, 18 ), $min, $max, $genes ];
1275 : golsen 1.65 }
1276 : golsen 1.97 }
1277 : golsen 1.65
1278 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1279 :     # $genes = [ $gene, $gene, ... ];
1280 :     # $map = [ $label, $min_coord, $max_coord, $genes ];
1281 :     # $gg = [ $map, $map, ... ];
1282 :     # render( $gg, $width, $obj_half_heigth, $save, $img_index_number )
1283 :    
1284 :     if ( @$gg )
1285 :     {
1286 :     # print STDERR Dumper( $gg );
1287 :     my $gs = $fig_or_sprout->genus_species( $genome );
1288 :     my $space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1289 :     my $legend = "<TABLE>\n"
1290 :     . " <TR>\n"
1291 :     . " <TD>Q = Query sequence$space</TD>\n"
1292 :     . " <TD Bgcolor='#FF0000'>$space</TD><TD>Frame 1 translation$space</TD>\n"
1293 :     . " <TD Bgcolor='#00FF00'>$space</TD><TD>Frame 2 translation$space</TD>\n"
1294 :     . " <TD Bgcolor='#0000FF'>$space</TD><TD>Frame 3 translation$space</TD>\n"
1295 :     . " <TD Bgcolor='#808080'>$space</TD><TD>Untranslated feature</TD>\n"
1296 :     . " </TR>\n"
1297 :     . "</TABLE><P />";
1298 :    
1299 :     push @html, "\n", FIGjs::toolTipScript(), "\n",
1300 :     $cgi->h2( "Results of $tool search of contigs from $gs\n"),
1301 :     $legend,
1302 :     @{ GenoGraphics::render( $gg, 600, 4, 0, 1 ) },
1303 :     $cgi->hr, "\n";
1304 : golsen 1.65 }
1305 :    
1306 :     return @html;
1307 :     }
1308 :    
1309 :    
1310 :     sub hsp_context {
1311 :     my( $fig_or_sprout, $cgi, $genome, $e_val, $pct_id,
1312 :     $qid, $q1, $q2, $qlen,
1313 : parrello 1.75 $contig, $s1, $s2, $slen ) = @_;
1314 : golsen 1.65 my $half_sz = 5000;
1315 :    
1316 :     my( $from, $to, $features, $fid, $beg, $end );
1317 :     my( $link, $lbl, $isprot, $function, $uniprot, $info, $prot_query );
1318 :    
1319 :     my $user = $cgi->param( 'user' ) || "";
1320 :     my $sprout = $cgi->param( 'SPROUT' ) ? '&SPROUT=1' : '';
1321 :    
1322 :     my @genes = ();
1323 :    
1324 :     # Based on the match position of the query, select the context region:
1325 :    
1326 :     ( $from, $to ) = ( $s1 <= $s2 ) ? ( $s1 - $half_sz, $s2 + $half_sz )
1327 :     : ( $s2 - $half_sz, $s1 + $half_sz );
1328 :     $from = 1 if ( $from < 1 );
1329 :     $to = $slen if ( $to > $slen );
1330 :    
1331 :     # Get the genes in the region, and adjust the ends to include whole genes:
1332 :    
1333 :     ( $features, $from, $to ) = genes_in_region( $fig_or_sprout, $cgi, $genome, $contig, $from, $to );
1334 :    
1335 : golsen 1.103 # Fix the end points if features have moved them to exclude query:
1336 :    
1337 :     if ( $s1 < $s2 ) { $from = $s1 if $s1 < $from; $to = $s2 if $s2 > $to }
1338 :     else { $from = $s2 if $s2 < $from; $to = $s1 if $s1 > $to }
1339 : golsen 1.65
1340 :     # Add the other features:
1341 :    
1342 :     foreach $fid ( @$features )
1343 :     {
1344 : parrello 1.75 my $contig1;
1345 :     ( $contig1, $beg, $end ) = boundaries_of( $fig_or_sprout, feature_locationS( $fig_or_sprout, $fid ) );
1346 :     next if $contig1 ne $contig;
1347 :    
1348 :     $link = "";
1349 :     if ( ( $lbl ) = $fid =~ /peg\.(\d+)$/ ) {
1350 : overbeek 1.82 ( $link = $cgi->url(-relative => 1) ) =~ s/index\.cgi/protein.cgi/;
1351 : parrello 1.75 $link .= "?prot=$fid&user=$user$sprout";
1352 :     $isprot = 1;
1353 :     } elsif ( ( $lbl ) = $fid =~ /\.([a-z]+)\.\d+$/ ) {
1354 :     $lbl = uc $lbl;
1355 :     $isprot = 0;
1356 :     } else {
1357 :     $lbl = "";
1358 :     $isprot = 0;
1359 :     }
1360 :    
1361 :     $function = function_ofS( $fig_or_sprout, $fid );
1362 :    
1363 :     $uniprot = join ", ", grep { /^uni\|/ } feature_aliasesL( $fig_or_sprout, $fid);
1364 :    
1365 :     $info = join( '<br />', "<b>Feature:</b> $fid",
1366 :     "<b>Contig:</b> $contig",
1367 :     "<b>Begin:</b> $beg",
1368 :     "<b>End:</b> $end",
1369 :     $function ? "<b>Function:</b> $function" : '',
1370 :     $uniprot ? "<b>Uniprot ID:</b> $uniprot" : ''
1371 :     );
1372 :    
1373 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1374 :    
1375 : parrello 1.75 push @genes, [ feature_graphic( $beg, $end, $isprot ),
1376 :     $lbl, $link, $info,
1377 :     $isprot ? () : ( undef, "Feature information" )
1378 :     ];
1379 : golsen 1.65 }
1380 :    
1381 :     # Draw the query. The subject coordinates are always DNA. If the query
1382 :     # is protein, it is about 3 times shorter than the matching contig DNA.
1383 :     # Splitting the difference, if 1.7 times the query length is still less
1384 :     # than the subject length, we will call it a protein query (and reading
1385 :     # frame in the contig coordinates has meaning). If it is nucleotides,
1386 :     # there is no defined frame.
1387 :    
1388 :     $info = join( '<br />', $qid ne 'query ' ? "<b>Query:</b> $qid" : (),
1389 :     "<b>Length:</b> $qlen",
1390 :     "<b>E-value:</b> $e_val",
1391 :     "<b>% identity:</b> " . sprintf( "%.1f", $pct_id ),
1392 :     "<b>Region of similarity:</b> $q1 &#150; $q2"
1393 :     );
1394 :     $prot_query = ( 1.7 * abs( $q2 - $q1 ) < abs( $s2 - $s1 ) ) ? 1 : 0;
1395 :    
1396 : golsen 1.104 if ( $user && $prot_query )
1397 : golsen 1.97 {
1398 :     $link = $cgi->url(-relative => 1);
1399 :     $link =~ s/index\.cgi/propose_new_peg.cgi/;
1400 :     $link .= "?user=$user&genome=$genome&covering=${contig}_${s1}_${s2}";
1401 :     }
1402 :     else
1403 :     {
1404 :     $link = undef;
1405 :     }
1406 :    
1407 : golsen 1.65 push @genes, [ feature_graphic( $s1, $s2, $prot_query ),
1408 : golsen 1.97 'Q', $link, $info, undef, 'Query and match information'
1409 : golsen 1.65 ];
1410 :    
1411 :     return \@genes, $from, $to;
1412 :     }
1413 :    
1414 :    
1415 :     sub feature_graphic {
1416 :     my ( $beg, $end, $isprot ) = @_;
1417 :     my ( $min, $max, $symb, $color );
1418 :    
1419 :     ( $min, $max, $symb ) = ( $beg <= $end ) ? ( $beg, $end, "rightArrow" )
1420 :     : ( $end, $beg, "leftArrow" );
1421 :    
1422 :     # Color proteins by translation frame
1423 :    
1424 :     $color = $isprot ? qw( blue red green )[ $beg % 3 ] : 'grey';
1425 :    
1426 :     ( $min, $max, $symb, $color );
1427 :     }
1428 :    
1429 :    
1430 :     sub genes_in_region {
1431 :     my( $fig_or_sprout, $cgi, $genome, $contig, $min, $max ) = @_;
1432 :    
1433 :     if ( $cgi->param( 'SPROUT' ) )
1434 :     {
1435 : parrello 1.75 my( $x, $feature_id );
1436 :     my( $feat, $min, $max ) = $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1437 :     my @tmp = sort { ($a->[1] cmp $b->[1]) or
1438 :     (($a->[2]+$a->[3]) <=> ($b->[2]+$b->[3]))
1439 :     }
1440 :     map { $feature_id = $_;
1441 :     $x = feature_locationS( $fig_or_sprout, $feature_id );
1442 :     $x ? [ $feature_id, boundaries_of( $fig_or_sprout, $x )] : ()
1443 :     }
1444 :     @$feat;
1445 :     return ( [map { $_->[0] } @tmp ], $min, $max );
1446 : golsen 1.65 }
1447 :     else
1448 :     {
1449 : parrello 1.75 return $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1450 : golsen 1.65 }
1451 :     }
1452 :    
1453 :    
1454 :     sub feature_locationS {
1455 :     my ( $fig_or_sprout, $peg ) = @_;
1456 :     scalar $fig_or_sprout->feature_location( $peg );
1457 :     }
1458 :    
1459 :    
1460 :     sub boundaries_of {
1461 :     my( $fig_or_sprout, $loc ) = @_;
1462 :     $fig_or_sprout->boundaries_of( $loc );
1463 :     }
1464 :    
1465 :    
1466 :     sub function_ofS {
1467 :     my( $fig_or_sprout, $peg, $user ) = @_;
1468 :     scalar $fig_or_sprout->function_of( $peg, $user );
1469 :     }
1470 :    
1471 :    
1472 :     sub feature_aliasesL {
1473 :     my( $fig_or_sprout, $fid ) = @_;
1474 :     my @tmp = $fig_or_sprout->feature_aliases( $fid );
1475 :     @tmp
1476 :     }
1477 :    
1478 :    
1479 : overbeek 1.30 sub format_sims {
1480 : golsen 1.103 my( $fig, $cgi, $html, $sims, $query, $seq ) = @_;
1481 :     my( $col_hdrs, $table, @ids, $ids, $sim, %seen, $n, $fid );
1482 : overbeek 1.30
1483 :     $col_hdrs = [ "Select up to here",
1484 : parrello 1.75 "Similar sequence",
1485 :     "E-val",
1486 :     "Function",
1487 :     "Organism",
1488 :     "Aliases"
1489 :     ];
1490 : overbeek 1.30
1491 :     $table = [];
1492 :     @ids = ();
1493 : golsen 1.103 $n = 0; # Count reported sequences
1494 :     foreach $sim ( @$sims )
1495 : overbeek 1.30 {
1496 : golsen 1.103 $fid = $sim->[1];
1497 :     next if $seen{ $fid }++; # One hit per sequence
1498 :     next if $fig->is_deleted_fid( $fid ); # Hide deleted sequences
1499 :     my $alii = scalar $fig->feature_aliases( $fid );
1500 :     $alii =~ s/,/, /g;
1501 :     push( @$table, [ $cgi->checkbox( -name => 'list_to',
1502 :     -value => $fid,
1503 :     -override => 1,
1504 :     -checked => 0,
1505 :     -label => ""
1506 :     ),
1507 :     &HTML::fid_link( $cgi, $fid ),
1508 :     [ $sim->[10], "TD NoWrap" ],
1509 :     scalar $fig->function_of( $fid ),
1510 :     $fig->genus_species( $fig->genome_of( $fid ) ),
1511 :     $alii
1512 :     ]
1513 :     );
1514 :     push( @ids, $fid );
1515 :     last if ++$n >= 1000; # Stop after 1000
1516 : overbeek 1.30 }
1517 : golsen 1.103
1518 : overbeek 1.30 $ids = join(",",@ids);
1519 :     my $target = "window$$";
1520 : golsen 1.103 push( @$html, $cgi->start_form( -method => 'post',
1521 : parrello 1.75 -target => $target,
1522 : olson 1.89 -action => $this_script
1523 : parrello 1.75 ),
1524 : golsen 1.103 $cgi->hidden(-name => 'ids', -value => $ids),
1525 :     $cgi->hidden(-name => 'qid', -value => $query),
1526 :     $cgi->hidden(-name => 'qseq', -value => $seq),
1527 :     $cgi->submit('Extract Matched Sequences'),
1528 :     # $cgi->submit('Align Matched Sequences'),
1529 :     &HTML::make_table($col_hdrs,$table,"Best Hits"),
1530 :     $cgi->submit('Extract Matched Sequences'),
1531 :     # $cgi->submit('Align Matched Sequences'),
1532 :     $cgi->end_form
1533 :     );
1534 : overbeek 1.30 }
1535 : overbeek 1.17
1536 : golsen 1.103
1537 : efrank 1.1 sub verify_db {
1538 :     my($db,$type) = @_;
1539 :    
1540 : overbeek 1.17 if ($type =~ /^p/i)
1541 : efrank 1.1 {
1542 : parrello 1.75 if ((! -s "$db.psq") || (-M "$db.psq" > -M $db))
1543 :     {
1544 :     system "$FIG_Config::ext_bin/formatdb -p T -i $db";
1545 :     }
1546 : efrank 1.1 }
1547 :     else
1548 :     {
1549 : parrello 1.75 if ((! -s "$db.nsq") || (-M "$db.nsq" > -M $db))
1550 :     {
1551 :     system "$FIG_Config::ext_bin/formatdb -p F -i $db";
1552 :     }
1553 : efrank 1.1 }
1554 : parrello 1.75 }
1555 : overbeek 1.7
1556 :     sub export_assignments {
1557 :     my($fig,$cgi,$html,$who) = @_;
1558 :     my($genome,$x);
1559 :    
1560 :     my @genomes = map { $_ =~ /\((\d+\.\d+)\)/; $1 } $cgi->param('korgs');
1561 :    
1562 :     if (@genomes == 0)
1563 :     {
1564 : parrello 1.75 @genomes = $fig->genomes;
1565 : overbeek 1.7 }
1566 :    
1567 : overbeek 1.10 my @assignments = $fig->assignments_made(\@genomes,$who,$cgi->param('after_date'));
1568 : overbeek 1.7 if (@assignments == 0)
1569 :     {
1570 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no assignments where made by $who"));
1571 : overbeek 1.7 }
1572 :     else
1573 :     {
1574 : parrello 1.75 my $col_hdrs = ["FIG id", "External ID", "Genus/Species","Assignment"];
1575 :     my $tab = [];
1576 :     my($x,$peg,$func);
1577 :     foreach $x (@assignments)
1578 :     {
1579 :     ( $peg, $func ) = @$x;
1580 :     push( @$tab,[ HTML::set_prot_links( $cgi, $peg ),
1581 :     HTML::set_prot_links( $cgi, ext_id( $fig, $peg ) ),
1582 :     $fig->genus_species($fig->genome_of($peg)),
1583 :     $func
1584 :     ] );
1585 :     }
1586 :    
1587 :     if ($cgi->param('save_assignments'))
1588 :     {
1589 :     my $user = $cgi->param('save_user');
1590 :     if ($user)
1591 :     {
1592 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1593 :     my $file = &FIG::epoch_to_readable(time) . ":$who:exported_from_local_SEED";
1594 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1595 :     {
1596 :     print TMP join("",map { join("\t",@$_) . "\n" } map { [$_->[0],$_->[3]] } @$tab);
1597 :     close(TMP);
1598 :     }
1599 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1600 :     }
1601 :     else
1602 :     {
1603 :     push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1604 :     }
1605 :     }
1606 :    
1607 :     if ($cgi->param('tabs'))
1608 :     {
1609 :     print $cgi->header;
1610 :     print "<pre>\n";
1611 :     print join("",map { join("\t",@$_) . "\n" } @$tab);
1612 :     print "</pre>\n";
1613 :     exit;
1614 :     }
1615 :     else
1616 :     {
1617 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Assignments Made by $who"));
1618 :     }
1619 : overbeek 1.7 }
1620 :     }
1621 :    
1622 :     sub ext_id {
1623 :     my($fig,$peg) = @_;
1624 :    
1625 :     my @mapped = grep { $_ !~ /^fig/ } map { $_->[0] } $fig->mapped_prot_ids($peg);
1626 :     if (@mapped == 0)
1627 :     {
1628 : parrello 1.75 return $peg;
1629 : overbeek 1.7 }
1630 :    
1631 :     my @tmp = ();
1632 :     if ((@tmp = grep { $_ =~ /^sp/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1633 :     if ((@tmp = grep { $_ =~ /^pir/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1634 :     if ((@tmp = grep { $_ =~ /^gi/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1635 :     if ((@tmp = grep { $_ =~ /^tr/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1636 :     if ((@tmp = grep { $_ =~ /^tn/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1637 :     if ((@tmp = grep { $_ =~ /^kegg/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1638 :    
1639 :     return $peg;
1640 :     }
1641 :    
1642 : overbeek 1.28 sub translate_assignments {
1643 :     my($fig,$cgi,$html,$from_func,$to_func) = @_;
1644 :    
1645 : overbeek 1.56 my @funcs = grep { $_ =~ /^\S.*\S$/ } split(/[\012\015]+/,$from_func);
1646 :    
1647 : overbeek 1.28 my $user = $cgi->param('save_user');
1648 :     if ($user)
1649 :     {
1650 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1651 :     my $file = &FIG::epoch_to_readable(time) . ":$user:translation";
1652 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1653 :     {
1654 : overbeek 1.55 my($peg,$func);
1655 : overbeek 1.28
1656 : overbeek 1.56 foreach $from_func (@funcs)
1657 : overbeek 1.28 {
1658 : overbeek 1.57 my $from_funcQ = quotemeta $from_func;
1659 :    
1660 : overbeek 1.56 foreach $peg ($fig->seqs_with_role($from_func))
1661 : overbeek 1.28 {
1662 : overbeek 1.56 if ($peg =~ /^fig\|/)
1663 : overbeek 1.28 {
1664 : overbeek 1.56 $func = $fig->function_of($peg);
1665 :     if ($func eq $from_func)
1666 :     {
1667 :     print TMP "$peg\t$to_func\n";
1668 :     }
1669 : overbeek 1.73 else
1670 : overbeek 1.57 {
1671 : overbeek 1.73 my @pieces = grep { $_ } split(/(\s+[\/@]\s+)|(\s*;\s+)/,$func);
1672 :     if (@pieces > 1)
1673 :     {
1674 :     my $func1 = join("",map { $_ =~ s/^$from_funcQ$/$to_func/; $_ } @pieces);
1675 :     if ($func ne $func1)
1676 :     {
1677 :     print TMP "$peg\t$func1\n";
1678 :     }
1679 :     }
1680 : overbeek 1.57 }
1681 : overbeek 1.28 }
1682 :     }
1683 :     }
1684 :     close(TMP);
1685 :     }
1686 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1687 :     }
1688 :     else
1689 :     {
1690 : parrello 1.75 push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1691 : overbeek 1.28 }
1692 :     }
1693 : efrank 1.92
1694 :     sub find_pegs_by_cv1 {
1695 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1696 :    
1697 :     # Remember kind of search that got us hear so we can call back
1698 :     # with same kind
1699 :     my $search = "Search";
1700 :     if ($cgi->param('Search genome selected below')) {
1701 :     $search=uri_escape('Search genome selected below');
1702 :     } elsif ( $cgi->param('Search Selected Organisms') ) {
1703 :     $search = uri_escape('Search Selected Organisms');
1704 :     } elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1705 :     $search = uri_escape('Find Genes in Org that Might Play the Role');
1706 :     }
1707 :    
1708 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1709 :    
1710 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1711 :     my $find_table_rows;
1712 :     my $counter = 0;
1713 :     for my $r (@$search_results)
1714 :     {
1715 :     my @temp = split("\t",$r);
1716 :     my $row = [];
1717 :     my $id= $temp[1];
1718 :     my $term = $temp[2];
1719 :     my $id_and_term = $id."; ".$term;
1720 :     my $pattern=uri_escape("$id; $term");
1721 :    
1722 :     my $link = "index.cgi?pattern=$pattern&Search=1&user=$user";
1723 :     my $cb = "<a href=$link>Find PEGs</a>";
1724 :    
1725 :     #feh my $cb = $cgi->submit(-name=>'$search', -value=>'Find PEGs');
1726 :     #my $cb_value = $cv."split_here".$id."; ".$term;
1727 :     #my $cb ="<input type=checkbox name=find_checked_$counter value='$cb_value'>" ;
1728 :     push(@$row,$cb);
1729 :     push(@$row,$cv);
1730 :     push(@$row,$id_and_term);
1731 :     push(@$find_table_rows,$row);
1732 :     $counter = $counter + 1;
1733 :     }
1734 :    
1735 :     my $find_terms_button="";
1736 :     if ($counter > 0) {
1737 :     $find_terms_button= $cgi->submit(-name=>'$search', -value=>'$search');
1738 :     }
1739 :    
1740 :     # build the page
1741 :     push @$html,
1742 :     $cgi->start_form(),
1743 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1744 :     $cgi->br,
1745 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1746 :     $cgi->hr,
1747 :     "<h4>Terms Matching Your Criteria </h4>\n",
1748 :     $cgi->br,
1749 :     &HTML::make_table($find_col_hdrs,$find_table_rows),
1750 :     $cgi->br,
1751 :     $find_terms_button,
1752 :     $cgi->end_form;
1753 :    
1754 :     return $html;
1755 :     }
1756 :    
1757 :     sub find_pegs_by_cv {
1758 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1759 :    
1760 :     # Remember kind of search that got us hear so we can call back
1761 :     # with same kind (not working so force to simple Search)
1762 :    
1763 :     my $search = "Search";
1764 :    
1765 :     #if ($cgi->param('Search genome selected below')) {
1766 :     # $search='Search genome selected below';
1767 :     #} elsif ( $cgi->param('Search Selected Organisms') ) {
1768 :     # $search = 'Search Selected Organisms';
1769 :     #} elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1770 :     # $search = 'Find Genes in Org that Might Play the Role';
1771 :     #}
1772 :    
1773 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1774 :    
1775 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1776 :     my @patterns=();
1777 :     for my $r (@$search_results)
1778 :     {
1779 :     my @temp = split("\t",$r);
1780 :     my $id= $temp[1];
1781 :     my $term = $temp[2];
1782 :     my $pattern="$id; $term";
1783 :    
1784 :     push(@patterns,$pattern);
1785 :     }
1786 :    
1787 :     my @pattern_radio;
1788 :     if ($#patterns + 1) {
1789 :     @pattern_radio = $cgi->radio_group( -name => 'pattern',
1790 :     -values => [ @patterns ]
1791 :     );
1792 :     } else {
1793 :     @pattern_radio = ("Nothing found");
1794 :     }
1795 :    
1796 :     my $find_terms_button= $cgi->submit(-name=>"Search", -value=>"Search");
1797 :    
1798 :     # build the page
1799 :     push @$html,
1800 :     $cgi->start_form(),
1801 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1802 :     $cgi->br,
1803 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1804 :     $cgi->hr,
1805 :     "<h4>$cv Terms Matching Your Criteria </h4>\n",
1806 :     $cgi->br,
1807 :     $find_terms_button,
1808 :     $cgi->br,
1809 :     $cgi->br,
1810 :     join( "<br>", @pattern_radio),
1811 :     # &HTML::make_table($find_col_hdrs,$find_table_rows),
1812 :     $cgi->br,
1813 :     $find_terms_button,
1814 :     $cgi->end_form;
1815 :    
1816 :     return $html;
1817 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3