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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.121 - (view) (download)

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3