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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3