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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3