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

Annotation of /FigWebServices/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.122 - (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 : olson 1.122 $ENV{"SCRIPT_NAME"} =~ s/$this_script/frame.cgi/;
809 : paczian 1.102 @prot_out = TICK("./frame.cgi");
810 :     } else {
811 :     $ENV{'REQUEST_METHOD'} = "GET";
812 :     $ENV{"QUERY_STRING"} = "prot=$peg\&user=$user";
813 :     $ENV{"REQUEST_URI"} =~ s/$this_script/protein.cgi/;
814 : olson 1.122 $ENV{"SCRIPT_NAME"} =~ s/$this_script/protein.cgi/;
815 : paczian 1.102 @prot_out = TICK("./protein.cgi");
816 :     }
817 : parrello 1.75 print @prot_out;
818 :     exit;
819 : efrank 1.1 }
820 : overbeek 1.71 $pattern =~ s/([a-zA-Z0-9])\|([a-zA-Z0-9])/$1\\\|$2/ig;
821 : efrank 1.92
822 :     my $search_kind = $cgi->param("search_kind");
823 :     if ( $search_kind && ! ($search_kind eq "DIRECT") ) {
824 :     #otherwise $search_kind is name of controlled vocab
825 :     find_pegs_by_cv($fig, $cgi, $html, $user, $pattern, $search_kind);
826 :     return;
827 :     }
828 :    
829 : overbeek 1.17 push( @$html, $cgi->br );
830 : olson 1.70 my( $peg_index_data, $role_index_data ) = $fig->search_index($pattern, $cgi->param("substring_match") eq "on");
831 : overbeek 1.17 my $maxpeg = defined( $cgi->param("maxpeg") ) ? $cgi->param("maxpeg") : 100;
832 :     my $maxrole = defined( $cgi->param("maxrole") ) ? $cgi->param("maxrole") : 100;
833 : mkubal 1.109 my $output_file = "$FIG_Config::temp/search_results.txt";
834 : parrello 1.115 Trace("Producing search output file $output_file") if T(3);
835 : mkubal 1.109 open(OUT,">$output_file");
836 : overbeek 1.17
837 : redwards 1.53 # RAE added lines to allow searching within a single organism
838 : golsen 1.59 # if ($cgi->param('korgs'))
839 :     # {
840 :     # $cgi->param('korgs') =~ /\((\d+\.*\d*)\)/;
841 :     # $org=$1; # this should be undef if korgs is not defined
842 :    
843 :     # push (@$html, $cgi->br, "Matches found in ",$cgi->param('korgs'), $cgi->p);
844 :     # my @clean_data; my @clean_index;
845 :     # while (@$peg_index_data)
846 :     # {
847 :     # my ($data, $index)=(shift @$peg_index_data, shift @$role_index_data);
848 :     # next unless (${$data}[0] =~ /^fig\|$org\.peg/);
849 :     # push @clean_data, $data;
850 :     # push @clean_index, $index;
851 :     # }
852 :    
853 :     # @$peg_index_data=@clean_data;
854 :     # @$role_index_data=@clean_index;
855 :     # }
856 :     ## End of added lines
857 : redwards 1.53
858 : mkubal 1.99 # RAE version with separate submit buttoxns and more than one org in korg
859 : redwards 1.63 # this is used by organisms.cgi for group specific searches
860 :     if ( $cgi->param('korgs') && $cgi->param('Search Selected Organisms')
861 :     )
862 :     {
863 :     my @temp;
864 :     foreach my $org ($cgi->param('korgs'))
865 :     {
866 :     push @temp, grep { $_->[0] =~ /^fig\|$org/ } @$peg_index_data;
867 :     }
868 :     @$peg_index_data = @temp;
869 :     }
870 :    
871 : golsen 1.59 # GJO version with separate submit buttons
872 : redwards 1.53
873 : golsen 1.59 if ( $cgi->param('korgs') && $cgi->param('korgs') =~ /\((\d+\.*\d*)\)/
874 :     && $cgi->param('Search genome selected below')
875 :     )
876 :     {
877 : parrello 1.75 my $org = $1;
878 :     push @$html, $cgi->br, "Matches found in ",$cgi->param('korgs'), $cgi->p;
879 : mkubal 1.79 @$peg_index_data = grep { $_->[0] =~ /^fig\|$org\.*/ } @$peg_index_data;
880 : redwards 1.53 }
881 : parrello 1.115 Trace("Initial push.") if T(3);
882 : golsen 1.59 if ( ( $maxpeg > 0 ) && @$peg_index_data )
883 : overbeek 1.17 {
884 : parrello 1.75 # RAE: Added javascript buttons see below. Only two things are needed.
885 :     # The form must have a name parameter, and the one line of code for the
886 :     # buttons. Everything else is automatic
887 :    
888 :     push( @$html, $cgi->start_form( -method => 'post',
889 :     -target => "window$$",
890 :     -action => 'fid_checked.cgi',
891 :     -name => 'found_pegs'
892 :     ),
893 :     $cgi->hidden(-name => 'user', -value => $user),
894 :     "For Selected (checked) sequences: ",
895 :     $cgi->submit('get sequences'),
896 :     $cgi->submit('view annotations'),
897 :     $cgi->submit('assign/annotate'),
898 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
899 :     $cgi->br, $cgi->br
900 :     );
901 : efrank 1.1
902 : redwards 1.63 # RAE Add the check all/uncheck all boxes.
903 :     push (@$html, $cgi->br, &HTML::java_buttons("found_pegs", "checked"), $cgi->br);
904 : parrello 1.75
905 :     my $n = @$peg_index_data;
906 :     if ($n > $maxpeg)
907 :     {
908 :     $msg = "Showing first $maxpeg out of $n protein genes";
909 :     $#{$peg_index_data} = $maxpeg-1;
910 :     }
911 :     else
912 :     {
913 : mkubal 1.79 $msg = "Showing $n FEATURES";
914 : parrello 1.75 }
915 :    
916 : parrello 1.115 my $col_hdrs = ["Sel","FEATURE","Organism","Aliases","Functions","Who","Attributes"];
917 : overbeek 1.106 my $tab = [ map { format_peg_entry( $fig, $cgi, $_ ) } sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
918 : mkubal 1.109
919 :     my $tab2 = [ sort {$a->[1] cmp $b->[1]} @$peg_index_data ];
920 : parrello 1.115 Trace("Final html push.") if T(3);
921 : mkubal 1.109 push( @$html,$cgi->br,
922 :     "<a href=$FIG_Config::temp_url/search_results.txt>Download_Search_Results</a>",
923 :     &HTML::make_table($col_hdrs,$tab,$msg),
924 : parrello 1.75 $cgi->br,
925 :     "For SELECTed (checked) sequences: ",
926 :     $cgi->submit('get sequences'),
927 :     $cgi->submit('view annotations'),
928 :     $cgi->submit('assign/annotate'),
929 :     $cgi->param('SPROUT') ? () : $cgi->submit('view similarities'),
930 :     $cgi->br,
931 :     $cgi->end_form
932 : mkubal 1.109 );
933 :    
934 :     foreach my $t (@$tab2){
935 :     my $string = join("\t",@$t);
936 :     print OUT "$string\n";
937 :     }
938 :    
939 : efrank 1.1 }
940 : golsen 1.59 elsif ( $maxpeg > 0 )
941 :     {
942 : parrello 1.75 push @$html, $cgi->h3('No matching protein genes');
943 : golsen 1.59 }
944 : overbeek 1.17
945 : golsen 1.59 if ( ( $maxrole > 0 ) && @$role_index_data )
946 : efrank 1.1 {
947 : parrello 1.75 my $n = @$role_index_data;
948 :     if ($n > $maxrole)
949 :     {
950 :     $msg = "Showing first $maxrole out of $n Roles";
951 :     $#{$role_index_data} = $maxrole - 1;
952 :     }
953 :     else
954 :     {
955 :     $msg = "Showing $n Roles";
956 :     }
957 :    
958 :     if ( $maxpeg > 0 ) { push( @$html, $cgi->hr ) }
959 :     my $col_hdrs = ["Role"];
960 :     my $tab = [ map { &format_role_entry($fig,$cgi,$_) } @$role_index_data ];
961 :     push( @$html, &HTML::make_table($col_hdrs,$tab,$msg) );
962 : efrank 1.1 }
963 : golsen 1.59 elsif ( $maxrole > 0 )
964 :     {
965 : parrello 1.75 push @$html, $cgi->h3('No matching roles');
966 : golsen 1.59 }
967 : parrello 1.115 Trace("Show-indexed-objects method complete.") if T(3);
968 : efrank 1.1 }
969 :    
970 : golsen 1.59
971 : efrank 1.1 sub format_peg_entry {
972 : golsen 1.67 my( $fig, $cgi, $entry ) = @_;
973 : efrank 1.1
974 : parrello 1.115 my($peg,$gs,$aliases,$function,$who,$attribute) = @$entry;
975 : overbeek 1.17
976 : golsen 1.21 $gs =~ s/\s+\d+$//; # Org name comes with taxon_id appended (why?) -- GJO
977 : efrank 1.1
978 : golsen 1.67 my $box = "<input type=checkbox name=checked value=\"$peg\">";
979 : overbeek 1.17 return [ $box, &HTML::fid_link($cgi,$peg), $gs, $aliases, $function, $who ];
980 : efrank 1.1 }
981 :    
982 :     sub format_role_entry {
983 :     my($fig,$cgi,$entry) = @_;
984 :    
985 :     return [&HTML::role_link($cgi,$entry)];
986 :     }
987 :    
988 :     sub run_prot_scan_for_matches {
989 :     my($fig,$cgi,$html,$org,$pat) = @_;
990 :     my($string,$peg,$beg,$end,$user,$col_hdrs,$tab,$i);
991 :    
992 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
993 :     open(PAT,">$tmp_pat")
994 : parrello 1.75 || die "could not open $tmp_pat";
995 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
996 :     print PAT "$pat\n";
997 :     close(PAT);
998 :     my @out = `$FIG_Config::ext_bin/scan_for_matches -p $tmp_pat < $FIG_Config::organisms/$org/Features/peg/fasta`;
999 :     if (@out < 1)
1000 :     {
1001 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
1002 : efrank 1.1 }
1003 :     else
1004 :     {
1005 : parrello 1.75 if (@out > 2000)
1006 :     {
1007 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
1008 :     $#out = 1999;
1009 :     }
1010 :    
1011 :     push(@$html,$cgi->pre);
1012 :     $user = $cgi->param('user');
1013 :     $col_hdrs = ["peg","begin","end","string","function of peg"];
1014 :     for ($i=0; ($i < @out); $i += 2)
1015 :     {
1016 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
1017 :     {
1018 :     $peg = $1;
1019 :     $beg = $2;
1020 :     $end = $3;
1021 :     $string = $out[$i+1];
1022 :     chomp $string;
1023 :     push( @$tab, [ &HTML::fid_link($cgi,$peg,1),
1024 :     $beg,
1025 :     $end,
1026 :     $string,
1027 :     scalar $fig->function_of( $peg, $user )
1028 :     ]
1029 :     );
1030 :     }
1031 :     }
1032 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
1033 :     push(@$html,$cgi->end_pre);
1034 : efrank 1.1 }
1035 :     unlink($tmp_pat);
1036 :     }
1037 :    
1038 : overbeek 1.17 #==============================================================================
1039 :     # Scan for matches
1040 :     #==============================================================================
1041 :    
1042 : efrank 1.1 sub run_dna_scan_for_matches {
1043 :     my($fig,$cgi,$html,$org,$pat) = @_;
1044 :     my($string,$contig,$beg,$end,$col_hdrs,$tab,$i);
1045 :    
1046 :     my $tmp_pat = "$FIG_Config::temp/tmp$$.pat";
1047 :     open(PAT,">$tmp_pat")
1048 : parrello 1.75 || die "could not open $tmp_pat";
1049 : efrank 1.1 $pat =~ s/[\s\012\015]+/ /g;
1050 :     print PAT "$pat\n";
1051 :     close(PAT);
1052 :     my @out = `cat $FIG_Config::organisms/$org/contigs | $FIG_Config::ext_bin/scan_for_matches -c $tmp_pat`;
1053 :     if (@out < 1)
1054 :     {
1055 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no hits"));
1056 : efrank 1.1 }
1057 :     else
1058 :     {
1059 : parrello 1.75 if (@out > 2000)
1060 :     {
1061 :     push(@$html,$cgi->h1("truncating to the first 1000 hits"));
1062 :     $#out = 1999;
1063 :     }
1064 :    
1065 :     push(@$html,$cgi->pre);
1066 :     $col_hdrs = ["contig","begin","end","string"];
1067 :     for ($i=0; ($i < @out); $i += 2)
1068 :     {
1069 :     if ($out[$i] =~ /^>([^:]+):\[(\d+),(\d+)\]/)
1070 :     {
1071 :     $contig = $1;
1072 :     $beg = $2;
1073 :     $end = $3;
1074 :     $string = $out[$i+1];
1075 :     chomp $string;
1076 :     push(@$tab,[$contig,$beg,$end,$string]);
1077 :     }
1078 :     }
1079 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Matches"));
1080 :     push(@$html,$cgi->end_pre);
1081 : efrank 1.1 }
1082 :     unlink($tmp_pat);
1083 :     }
1084 :    
1085 : overbeek 1.17 #==============================================================================
1086 :     # BLAST search
1087 :     #==============================================================================
1088 :    
1089 : efrank 1.1 sub run_blast {
1090 : golsen 1.45 my( $fig, $cgi, $html, $org, $tool, $seq ) = @_;
1091 :     my( $query, @out );
1092 : efrank 1.1
1093 : golsen 1.45 my $tmp_seq = "$FIG_Config::temp/run_blast_tmp$$.seq";
1094 : efrank 1.1
1095 : overbeek 1.17 #--------------------------------------------------------------------------
1096 : golsen 1.97 # Does the request require a defined genome? We never check that the
1097 :     # database build works, so the least we can do is some up-front tests.
1098 :     # -- GJO
1099 :     #--------------------------------------------------------------------------
1100 :    
1101 :     if ( $tool !~ /complete genomes/ )
1102 :     {
1103 :     if ( ! $org || ! -d "$FIG_Config::organisms/$org" )
1104 :     {
1105 :     push @$html, $cgi->h2("Sorry, $tool requires selecting a genome." );
1106 :     return;
1107 :     }
1108 :    
1109 :     if ( ( $tool =~ /blastn/ ) || ( $tool =~ /tblastx/ ) )
1110 :     {
1111 :     if ( ! -f "$FIG_Config::organisms/$org/contigs" )
1112 :     {
1113 :     push @$html, $cgi->h2("Sorry, cannot find DNA data for genome $org." );
1114 :     return;
1115 :     }
1116 :     }
1117 :     else
1118 :     {
1119 :     if ( ! -f "$FIG_Config::organisms/$org/Features/peg/fasta" )
1120 :     {
1121 :     push @$html, $cgi->h2("Sorry, cannot find protein data for genome $org." );
1122 :     return;
1123 :     }
1124 :     }
1125 :     }
1126 :    
1127 :     #--------------------------------------------------------------------------
1128 : overbeek 1.17 # Is the request for an id? Get the sequence
1129 :     #--------------------------------------------------------------------------
1130 : golsen 1.97
1131 : golsen 1.91 if ( ( $query ) = $seq =~ /^\s*([a-zA-Z]{2,4}\|\S+)/ )
1132 : efrank 1.1 {
1133 : parrello 1.75 # Replaced $id with $query so that output inherits label -- GJO
1134 : golsen 1.91 # Found ugly fairure to build correct query sequence for
1135 :     # 'blastp against complete genomes'. Can't figure out
1136 :     # why it ever worked with and id -- GJO
1137 :    
1138 : parrello 1.75 $seq = "";
1139 : golsen 1.91 if ( ($tool eq "blastp") || ($tool eq "tblastn")
1140 :     || ($tool eq 'blastp against complete genomes')
1141 :     )
1142 : parrello 1.75 {
1143 :     $seq = $fig->get_translation($query);
1144 : golsen 1.97 my $func = $fig->function_of( $query, $user );
1145 :     $query .= " $func" if $func;
1146 : parrello 1.75 }
1147 :     elsif ($query =~ /^fig/)
1148 :     {
1149 :     my @locs;
1150 :     if ((@locs = $fig->feature_location($query)) && (@locs > 0))
1151 :     {
1152 :     $seq = $fig->dna_seq($fig->genome_of($query),@locs);
1153 :     }
1154 :     }
1155 :     if (! $seq)
1156 :     {
1157 :     push(@$html,$cgi->h1("Sorry, could not get sequence for $query"));
1158 :     return;
1159 :     }
1160 : efrank 1.1 }
1161 : golsen 1.45
1162 : overbeek 1.17 #--------------------------------------------------------------------------
1163 :     # Is it a fasta format? Get the query name
1164 :     #--------------------------------------------------------------------------
1165 : golsen 1.45
1166 :     elsif ( $seq =~ s/^>\s*(\S+[^\n\012\015]*)// ) # more flexible match -- GJO
1167 : efrank 1.1 {
1168 : parrello 1.75 $query = $1;
1169 : efrank 1.1 }
1170 : golsen 1.45
1171 : overbeek 1.17 #--------------------------------------------------------------------------
1172 :     # Take it as plain text
1173 :     #--------------------------------------------------------------------------
1174 : golsen 1.45
1175 : efrank 1.1 else
1176 :     {
1177 : parrello 1.75 $query = "query";
1178 : efrank 1.1 }
1179 : golsen 1.45
1180 :     #
1181 :     # The rest is taken as the sequence
1182 :     #
1183 :    
1184 : golsen 1.23 $seq =~ s/\s+//g;
1185 : golsen 1.45 open( SEQ, ">$tmp_seq" ) || die "run_blast could not open $tmp_seq";
1186 : efrank 1.1 print SEQ ">$query\n$seq\n";
1187 : golsen 1.45 close( SEQ );
1188 : efrank 1.1
1189 :     if (! $ENV{"BLASTMAT"}) { $ENV{"BLASTMAT"} = "$FIG_Config::blastmat" }
1190 : golsen 1.88 my $blast_opt = $cgi->param( 'blast_options' ) || '';
1191 : efrank 1.1
1192 : golsen 1.45 if ( $tool eq "blastp" )
1193 : efrank 1.1 {
1194 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1195 :     &verify_db( $db, "p" );
1196 : golsen 1.88 @out = map { &HTML::set_prot_links($cgi,$_) } execute_blastall( 'blastp', $tmp_seq, $db, $blast_opt );
1197 : efrank 1.1 }
1198 : golsen 1.45
1199 :     elsif ( $tool eq "blastx" )
1200 : efrank 1.1 {
1201 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/Features/peg/fasta";
1202 :     &verify_db( $db, "p" );
1203 : golsen 1.88 @out = map { &HTML::set_prot_links($cgi,$_) } execute_blastall( 'blastx', $tmp_seq, $db, $blast_opt );
1204 : efrank 1.1 }
1205 : golsen 1.45
1206 :     elsif ( $tool eq "blastn" )
1207 : efrank 1.1 {
1208 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1209 :     &verify_db( $db, "n" ); ### fix to get all contigs
1210 : golsen 1.88 @out = execute_blastall( 'blastn', $tmp_seq, $db, "-r 1 -q -1 " . $blast_opt );
1211 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1212 : efrank 1.1 }
1213 : golsen 1.45
1214 :     elsif ( $tool eq "tblastn" )
1215 : efrank 1.1 {
1216 : parrello 1.75 my $db = "$FIG_Config::organisms/$org/contigs";
1217 :     &verify_db( $db, "n" ); ### fix to get all contigs
1218 : golsen 1.88 @out = execute_blastall( 'tblastn', $tmp_seq, $db, $blast_opt );
1219 : parrello 1.75 push @$html, blast_graphics( $fig, $cgi, $org, \@out, $tool );
1220 : efrank 1.1 }
1221 : golsen 1.45
1222 :     elsif ( $tool eq 'blastp against complete genomes' ) ### this tool gets nonstandard treatment: RAO
1223 : overbeek 1.30 {
1224 : golsen 1.103 &blast_complete( $fig, $cgi, $html, $tmp_seq, $query, $seq );
1225 : golsen 1.91 unlink( $tmp_seq );
1226 :     return;
1227 : overbeek 1.30 }
1228 : golsen 1.45
1229 : overbeek 1.17 if (@out < 1) # This is really a bigger problem than no hits (GJO)
1230 : efrank 1.1 {
1231 : golsen 1.88 push @$html, $cgi->h1( "Sorry, no blast output" );
1232 : efrank 1.1 }
1233 :     else
1234 :     {
1235 : golsen 1.88 push @$html, $cgi->pre, @out, $cgi->end_pre;
1236 : efrank 1.1 }
1237 : golsen 1.45 unlink( $tmp_seq );
1238 : efrank 1.1 }
1239 :    
1240 : golsen 1.45
1241 : golsen 1.88 # `$blastall -p $prog -i $tmp_seq -d $db $blast_opt`
1242 :     # execute_blastall( $prog, $input_file, $db, $options )
1243 :    
1244 :     sub execute_blastall
1245 :     {
1246 :     my( $prog, $input, $db, $options ) = @_;
1247 :    
1248 :     my $blastall = "$FIG_Config::ext_bin/blastall";
1249 :     my @args = ( '-p', $prog, '-i', $input, '-d', $db, split(/\s+/, $options) );
1250 :    
1251 :     my $bfh;
1252 :     my $pid = open( $bfh, "-|" );
1253 :     if ( $pid == 0 )
1254 :     {
1255 :     exec( $blastall, @args );
1256 :     die join( " ", $blastall, @args, "failed: $!" );
1257 :     }
1258 :    
1259 : golsen 1.91 <$bfh>
1260 : golsen 1.88 }
1261 :    
1262 :    
1263 : golsen 1.91 # Changed to:
1264 :     # Include low complexity filter in blast search.
1265 :     # Remove all but first match to a given database sequence.
1266 :     # Sort by bit-score, not E-value (which becomes equal for all strong matches).
1267 :     # Limit to 1000 matches.
1268 :     # -- GJO
1269 :    
1270 :     sub blast_complete
1271 :     {
1272 : golsen 1.103 my( $fig, $cgi, $html, $seqfile, $query, $seq ) = @_;
1273 : golsen 1.88 my( $genome, @sims );
1274 :    
1275 : overbeek 1.30 @sims = ();
1276 : golsen 1.91 foreach $genome ( $fig->genomes("complete") )
1277 : overbeek 1.30 {
1278 : parrello 1.75 my $db = "$FIG_Config::organisms/$genome/Features/peg/fasta";
1279 :     next if (! -s $db);
1280 : overbeek 1.30
1281 : parrello 1.75 &verify_db($db,"p");
1282 :     my $sim;
1283 : golsen 1.91 my %seen = ();
1284 :     push @sims, map { chomp;
1285 :     $sim = [ split /\t/ ];
1286 :     $sim->[10] =~ s/^e-/1.0e-/;
1287 :     $seen{ $sim->[1] }++ ? () : $sim
1288 :     }
1289 :     execute_blastall( 'blastp', $seqfile, $db, '-m 8 -F T -e 1e-5' );
1290 : overbeek 1.30 }
1291 : golsen 1.91
1292 :     @sims = sort { $b->[11] <=> $a->[11] } @sims;
1293 :     if ( @sims > 1000 ) { @sims = @sims[0 .. 999] }
1294 : golsen 1.103 &format_sims( $fig, $cgi, $html, \@sims, $query, $seq );
1295 : overbeek 1.30 }
1296 :    
1297 : golsen 1.65
1298 :     #------------------------------------------------------------------------------
1299 : golsen 1.97 # Graphically display search results on contigs
1300 : golsen 1.65 #
1301 :     # use FIGjs qw( toolTipScript );
1302 :     # use GenoGraphics qw( render );
1303 :     #------------------------------------------------------------------------------
1304 :     #
1305 : golsen 1.97 # Fields produced by next_blast_hsp:
1306 :     #
1307 :     # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1308 :     # 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
1309 : golsen 1.65 #------------------------------------------------------------------------------
1310 :    
1311 :     sub blast_graphics {
1312 :     my ( $fig_or_sprout, $cgi, $genome, $out, $tool ) = @_;
1313 :    
1314 :     my $e_min = 0.1;
1315 :     my $gg = [];
1316 : golsen 1.97 my @html = ();
1317 : golsen 1.65
1318 : golsen 1.97 # Changed to use standalone parsing function, not shell script -- GJO
1319 : golsen 1.65
1320 : golsen 1.97 my $outcopy = [ @$out ];
1321 :     while ( $_ = &gjoparseblast::next_blast_hsp( $outcopy ) )
1322 : golsen 1.65 {
1323 : golsen 1.97 my ( $qid, $qlen, $contig, $slen ) = @$_[0, 2, 3, 5 ];
1324 :     my ( $e_val, $n_mat, $n_id, $q1, $q2, $s1, $s2 ) = @$_[ 7, 10, 11, 15, 16, 18, 19 ];
1325 :     next if $e_val > $e_min;
1326 :     my ( $genes, $min, $max ) = hsp_context( $fig_or_sprout, $cgi, $genome,
1327 :     $e_val, 100 * $n_id / $n_mat,
1328 :     $qid, $q1, $q2, $qlen,
1329 :     $contig, $s1, $s2, $slen
1330 :     );
1331 :     if ($min && $max)
1332 : golsen 1.65 {
1333 : golsen 1.97 push @$gg, [ substr( $contig, 0, 18 ), $min, $max, $genes ];
1334 : golsen 1.65 }
1335 : golsen 1.97 }
1336 : golsen 1.65
1337 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1338 :     # $genes = [ $gene, $gene, ... ];
1339 :     # $map = [ $label, $min_coord, $max_coord, $genes ];
1340 :     # $gg = [ $map, $map, ... ];
1341 :     # render( $gg, $width, $obj_half_heigth, $save, $img_index_number )
1342 :    
1343 :     if ( @$gg )
1344 :     {
1345 :     # print STDERR Dumper( $gg );
1346 :     my $gs = $fig_or_sprout->genus_species( $genome );
1347 :     my $space = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1348 :     my $legend = "<TABLE>\n"
1349 :     . " <TR>\n"
1350 :     . " <TD>Q = Query sequence$space</TD>\n"
1351 :     . " <TD Bgcolor='#FF0000'>$space</TD><TD>Frame 1 translation$space</TD>\n"
1352 :     . " <TD Bgcolor='#00FF00'>$space</TD><TD>Frame 2 translation$space</TD>\n"
1353 :     . " <TD Bgcolor='#0000FF'>$space</TD><TD>Frame 3 translation$space</TD>\n"
1354 :     . " <TD Bgcolor='#808080'>$space</TD><TD>Untranslated feature</TD>\n"
1355 :     . " </TR>\n"
1356 :     . "</TABLE><P />";
1357 :    
1358 :     push @html, "\n", FIGjs::toolTipScript(), "\n",
1359 :     $cgi->h2( "Results of $tool search of contigs from $gs\n"),
1360 :     $legend,
1361 :     @{ GenoGraphics::render( $gg, 600, 4, 0, 1 ) },
1362 :     $cgi->hr, "\n";
1363 : golsen 1.65 }
1364 :    
1365 :     return @html;
1366 :     }
1367 :    
1368 :    
1369 :     sub hsp_context {
1370 :     my( $fig_or_sprout, $cgi, $genome, $e_val, $pct_id,
1371 :     $qid, $q1, $q2, $qlen,
1372 : parrello 1.75 $contig, $s1, $s2, $slen ) = @_;
1373 : golsen 1.65 my $half_sz = 5000;
1374 :    
1375 :     my( $from, $to, $features, $fid, $beg, $end );
1376 :     my( $link, $lbl, $isprot, $function, $uniprot, $info, $prot_query );
1377 :    
1378 :     my $user = $cgi->param( 'user' ) || "";
1379 :     my $sprout = $cgi->param( 'SPROUT' ) ? '&SPROUT=1' : '';
1380 :    
1381 :     my @genes = ();
1382 :    
1383 :     # Based on the match position of the query, select the context region:
1384 :    
1385 :     ( $from, $to ) = ( $s1 <= $s2 ) ? ( $s1 - $half_sz, $s2 + $half_sz )
1386 :     : ( $s2 - $half_sz, $s1 + $half_sz );
1387 :     $from = 1 if ( $from < 1 );
1388 :     $to = $slen if ( $to > $slen );
1389 :    
1390 :     # Get the genes in the region, and adjust the ends to include whole genes:
1391 :    
1392 :     ( $features, $from, $to ) = genes_in_region( $fig_or_sprout, $cgi, $genome, $contig, $from, $to );
1393 :    
1394 : golsen 1.103 # Fix the end points if features have moved them to exclude query:
1395 :    
1396 :     if ( $s1 < $s2 ) { $from = $s1 if $s1 < $from; $to = $s2 if $s2 > $to }
1397 :     else { $from = $s2 if $s2 < $from; $to = $s1 if $s1 > $to }
1398 : golsen 1.65
1399 :     # Add the other features:
1400 :    
1401 :     foreach $fid ( @$features )
1402 :     {
1403 : parrello 1.75 my $contig1;
1404 :     ( $contig1, $beg, $end ) = boundaries_of( $fig_or_sprout, feature_locationS( $fig_or_sprout, $fid ) );
1405 :     next if $contig1 ne $contig;
1406 :    
1407 :     $link = "";
1408 :     if ( ( $lbl ) = $fid =~ /peg\.(\d+)$/ ) {
1409 : overbeek 1.82 ( $link = $cgi->url(-relative => 1) ) =~ s/index\.cgi/protein.cgi/;
1410 : parrello 1.75 $link .= "?prot=$fid&user=$user$sprout";
1411 :     $isprot = 1;
1412 :     } elsif ( ( $lbl ) = $fid =~ /\.([a-z]+)\.\d+$/ ) {
1413 :     $lbl = uc $lbl;
1414 :     $isprot = 0;
1415 :     } else {
1416 :     $lbl = "";
1417 :     $isprot = 0;
1418 :     }
1419 :    
1420 :     $function = function_ofS( $fig_or_sprout, $fid );
1421 :    
1422 :     $uniprot = join ", ", grep { /^uni\|/ } feature_aliasesL( $fig_or_sprout, $fid);
1423 :    
1424 :     $info = join( '<br />', "<b>Feature:</b> $fid",
1425 :     "<b>Contig:</b> $contig",
1426 :     "<b>Begin:</b> $beg",
1427 :     "<b>End:</b> $end",
1428 :     $function ? "<b>Function:</b> $function" : '',
1429 :     $uniprot ? "<b>Uniprot ID:</b> $uniprot" : ''
1430 :     );
1431 :    
1432 : golsen 1.97 # $gene = [ $beg, $end, $shape, $color, $text, $url, $pop-up, $alt_action, $pop-up_title ];
1433 :    
1434 : parrello 1.75 push @genes, [ feature_graphic( $beg, $end, $isprot ),
1435 :     $lbl, $link, $info,
1436 :     $isprot ? () : ( undef, "Feature information" )
1437 :     ];
1438 : golsen 1.65 }
1439 :    
1440 :     # Draw the query. The subject coordinates are always DNA. If the query
1441 :     # is protein, it is about 3 times shorter than the matching contig DNA.
1442 :     # Splitting the difference, if 1.7 times the query length is still less
1443 :     # than the subject length, we will call it a protein query (and reading
1444 :     # frame in the contig coordinates has meaning). If it is nucleotides,
1445 :     # there is no defined frame.
1446 :    
1447 :     $info = join( '<br />', $qid ne 'query ' ? "<b>Query:</b> $qid" : (),
1448 :     "<b>Length:</b> $qlen",
1449 :     "<b>E-value:</b> $e_val",
1450 :     "<b>% identity:</b> " . sprintf( "%.1f", $pct_id ),
1451 :     "<b>Region of similarity:</b> $q1 &#150; $q2"
1452 :     );
1453 :     $prot_query = ( 1.7 * abs( $q2 - $q1 ) < abs( $s2 - $s1 ) ) ? 1 : 0;
1454 :    
1455 : golsen 1.104 if ( $user && $prot_query )
1456 : golsen 1.97 {
1457 :     $link = $cgi->url(-relative => 1);
1458 :     $link =~ s/index\.cgi/propose_new_peg.cgi/;
1459 :     $link .= "?user=$user&genome=$genome&covering=${contig}_${s1}_${s2}";
1460 :     }
1461 :     else
1462 :     {
1463 :     $link = undef;
1464 :     }
1465 :    
1466 : golsen 1.65 push @genes, [ feature_graphic( $s1, $s2, $prot_query ),
1467 : golsen 1.97 'Q', $link, $info, undef, 'Query and match information'
1468 : golsen 1.65 ];
1469 :    
1470 :     return \@genes, $from, $to;
1471 :     }
1472 :    
1473 :    
1474 :     sub feature_graphic {
1475 :     my ( $beg, $end, $isprot ) = @_;
1476 :     my ( $min, $max, $symb, $color );
1477 :    
1478 :     ( $min, $max, $symb ) = ( $beg <= $end ) ? ( $beg, $end, "rightArrow" )
1479 :     : ( $end, $beg, "leftArrow" );
1480 :    
1481 :     # Color proteins by translation frame
1482 :    
1483 :     $color = $isprot ? qw( blue red green )[ $beg % 3 ] : 'grey';
1484 :    
1485 :     ( $min, $max, $symb, $color );
1486 :     }
1487 :    
1488 :    
1489 :     sub genes_in_region {
1490 :     my( $fig_or_sprout, $cgi, $genome, $contig, $min, $max ) = @_;
1491 :    
1492 :     if ( $cgi->param( 'SPROUT' ) )
1493 :     {
1494 : parrello 1.75 my( $x, $feature_id );
1495 :     my( $feat, $min, $max ) = $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1496 :     my @tmp = sort { ($a->[1] cmp $b->[1]) or
1497 :     (($a->[2]+$a->[3]) <=> ($b->[2]+$b->[3]))
1498 :     }
1499 :     map { $feature_id = $_;
1500 :     $x = feature_locationS( $fig_or_sprout, $feature_id );
1501 :     $x ? [ $feature_id, boundaries_of( $fig_or_sprout, $x )] : ()
1502 :     }
1503 :     @$feat;
1504 :     return ( [map { $_->[0] } @tmp ], $min, $max );
1505 : golsen 1.65 }
1506 :     else
1507 :     {
1508 : parrello 1.75 return $fig_or_sprout->genes_in_region( $genome, $contig, $min, $max );
1509 : golsen 1.65 }
1510 :     }
1511 :    
1512 :    
1513 :     sub feature_locationS {
1514 :     my ( $fig_or_sprout, $peg ) = @_;
1515 :     scalar $fig_or_sprout->feature_location( $peg );
1516 :     }
1517 :    
1518 :    
1519 :     sub boundaries_of {
1520 :     my( $fig_or_sprout, $loc ) = @_;
1521 :     $fig_or_sprout->boundaries_of( $loc );
1522 :     }
1523 :    
1524 :    
1525 :     sub function_ofS {
1526 :     my( $fig_or_sprout, $peg, $user ) = @_;
1527 :     scalar $fig_or_sprout->function_of( $peg, $user );
1528 :     }
1529 :    
1530 :    
1531 :     sub feature_aliasesL {
1532 :     my( $fig_or_sprout, $fid ) = @_;
1533 :     my @tmp = $fig_or_sprout->feature_aliases( $fid );
1534 :     @tmp
1535 :     }
1536 :    
1537 :    
1538 : overbeek 1.30 sub format_sims {
1539 : golsen 1.103 my( $fig, $cgi, $html, $sims, $query, $seq ) = @_;
1540 :     my( $col_hdrs, $table, @ids, $ids, $sim, %seen, $n, $fid );
1541 : overbeek 1.30
1542 :     $col_hdrs = [ "Select up to here",
1543 : parrello 1.75 "Similar sequence",
1544 :     "E-val",
1545 :     "Function",
1546 :     "Organism",
1547 :     "Aliases"
1548 :     ];
1549 : overbeek 1.30
1550 :     $table = [];
1551 :     @ids = ();
1552 : golsen 1.103 $n = 0; # Count reported sequences
1553 :     foreach $sim ( @$sims )
1554 : overbeek 1.30 {
1555 : golsen 1.103 $fid = $sim->[1];
1556 :     next if $seen{ $fid }++; # One hit per sequence
1557 :     next if $fig->is_deleted_fid( $fid ); # Hide deleted sequences
1558 :     my $alii = scalar $fig->feature_aliases( $fid );
1559 :     $alii =~ s/,/, /g;
1560 :     push( @$table, [ $cgi->checkbox( -name => 'list_to',
1561 :     -value => $fid,
1562 :     -override => 1,
1563 :     -checked => 0,
1564 :     -label => ""
1565 :     ),
1566 :     &HTML::fid_link( $cgi, $fid ),
1567 :     [ $sim->[10], "TD NoWrap" ],
1568 :     scalar $fig->function_of( $fid ),
1569 :     $fig->genus_species( $fig->genome_of( $fid ) ),
1570 :     $alii
1571 :     ]
1572 :     );
1573 :     push( @ids, $fid );
1574 :     last if ++$n >= 1000; # Stop after 1000
1575 : overbeek 1.30 }
1576 : golsen 1.103
1577 : overbeek 1.30 $ids = join(",",@ids);
1578 :     my $target = "window$$";
1579 : golsen 1.103 push( @$html, $cgi->start_form( -method => 'post',
1580 : parrello 1.75 -target => $target,
1581 : olson 1.89 -action => $this_script
1582 : parrello 1.75 ),
1583 : golsen 1.103 $cgi->hidden(-name => 'ids', -value => $ids),
1584 :     $cgi->hidden(-name => 'qid', -value => $query),
1585 :     $cgi->hidden(-name => 'qseq', -value => $seq),
1586 :     $cgi->submit('Extract Matched Sequences'),
1587 :     # $cgi->submit('Align Matched Sequences'),
1588 :     &HTML::make_table($col_hdrs,$table,"Best Hits"),
1589 :     $cgi->submit('Extract Matched Sequences'),
1590 :     # $cgi->submit('Align Matched Sequences'),
1591 :     $cgi->end_form
1592 :     );
1593 : overbeek 1.30 }
1594 : overbeek 1.17
1595 : golsen 1.103
1596 : efrank 1.1 sub verify_db {
1597 :     my($db,$type) = @_;
1598 :    
1599 : overbeek 1.17 if ($type =~ /^p/i)
1600 : efrank 1.1 {
1601 : parrello 1.75 if ((! -s "$db.psq") || (-M "$db.psq" > -M $db))
1602 :     {
1603 :     system "$FIG_Config::ext_bin/formatdb -p T -i $db";
1604 :     }
1605 : efrank 1.1 }
1606 :     else
1607 :     {
1608 : parrello 1.75 if ((! -s "$db.nsq") || (-M "$db.nsq" > -M $db))
1609 :     {
1610 :     system "$FIG_Config::ext_bin/formatdb -p F -i $db";
1611 :     }
1612 : efrank 1.1 }
1613 : parrello 1.75 }
1614 : overbeek 1.7
1615 :     sub export_assignments {
1616 :     my($fig,$cgi,$html,$who) = @_;
1617 :     my($genome,$x);
1618 :    
1619 :     my @genomes = map { $_ =~ /\((\d+\.\d+)\)/; $1 } $cgi->param('korgs');
1620 :    
1621 :     if (@genomes == 0)
1622 :     {
1623 : parrello 1.75 @genomes = $fig->genomes;
1624 : overbeek 1.7 }
1625 :    
1626 : overbeek 1.10 my @assignments = $fig->assignments_made(\@genomes,$who,$cgi->param('after_date'));
1627 : overbeek 1.7 if (@assignments == 0)
1628 :     {
1629 : parrello 1.75 push(@$html,$cgi->h1("Sorry, no assignments where made by $who"));
1630 : overbeek 1.7 }
1631 :     else
1632 :     {
1633 : parrello 1.75 my $col_hdrs = ["FIG id", "External ID", "Genus/Species","Assignment"];
1634 :     my $tab = [];
1635 :     my($x,$peg,$func);
1636 :     foreach $x (@assignments)
1637 :     {
1638 :     ( $peg, $func ) = @$x;
1639 :     push( @$tab,[ HTML::set_prot_links( $cgi, $peg ),
1640 :     HTML::set_prot_links( $cgi, ext_id( $fig, $peg ) ),
1641 :     $fig->genus_species($fig->genome_of($peg)),
1642 :     $func
1643 :     ] );
1644 :     }
1645 :    
1646 :     if ($cgi->param('save_assignments'))
1647 :     {
1648 :     my $user = $cgi->param('save_user');
1649 :     if ($user)
1650 :     {
1651 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1652 :     my $file = &FIG::epoch_to_readable(time) . ":$who:exported_from_local_SEED";
1653 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1654 :     {
1655 :     print TMP join("",map { join("\t",@$_) . "\n" } map { [$_->[0],$_->[3]] } @$tab);
1656 :     close(TMP);
1657 :     }
1658 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1659 :     }
1660 :     else
1661 :     {
1662 :     push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1663 :     }
1664 :     }
1665 :    
1666 :     if ($cgi->param('tabs'))
1667 :     {
1668 :     print $cgi->header;
1669 :     print "<pre>\n";
1670 :     print join("",map { join("\t",@$_) . "\n" } @$tab);
1671 :     print "</pre>\n";
1672 :     exit;
1673 :     }
1674 :     else
1675 :     {
1676 :     push(@$html,&HTML::make_table($col_hdrs,$tab,"Assignments Made by $who"));
1677 :     }
1678 : overbeek 1.7 }
1679 :     }
1680 :    
1681 :     sub ext_id {
1682 :     my($fig,$peg) = @_;
1683 :    
1684 :     my @mapped = grep { $_ !~ /^fig/ } map { $_->[0] } $fig->mapped_prot_ids($peg);
1685 :     if (@mapped == 0)
1686 :     {
1687 : parrello 1.75 return $peg;
1688 : overbeek 1.7 }
1689 :    
1690 :     my @tmp = ();
1691 :     if ((@tmp = grep { $_ =~ /^sp/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1692 :     if ((@tmp = grep { $_ =~ /^pir/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1693 :     if ((@tmp = grep { $_ =~ /^gi/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1694 :     if ((@tmp = grep { $_ =~ /^tr/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1695 :     if ((@tmp = grep { $_ =~ /^tn/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1696 :     if ((@tmp = grep { $_ =~ /^kegg/ } @mapped) && (@tmp > 0)) { return $tmp[0] }
1697 :    
1698 :     return $peg;
1699 :     }
1700 :    
1701 : overbeek 1.28 sub translate_assignments {
1702 :     my($fig,$cgi,$html,$from_func,$to_func) = @_;
1703 :    
1704 : overbeek 1.56 my @funcs = grep { $_ =~ /^\S.*\S$/ } split(/[\012\015]+/,$from_func);
1705 :    
1706 : overbeek 1.28 my $user = $cgi->param('save_user');
1707 :     if ($user)
1708 :     {
1709 :     &FIG::verify_dir("$FIG_Config::data/Assignments/$user");
1710 :     my $file = &FIG::epoch_to_readable(time) . ":$user:translation";
1711 :     if (open(TMP,">$FIG_Config::data/Assignments/$user/$file"))
1712 :     {
1713 : overbeek 1.55 my($peg,$func);
1714 : overbeek 1.28
1715 : overbeek 1.56 foreach $from_func (@funcs)
1716 : overbeek 1.28 {
1717 : overbeek 1.57 my $from_funcQ = quotemeta $from_func;
1718 :    
1719 : overbeek 1.56 foreach $peg ($fig->seqs_with_role($from_func))
1720 : overbeek 1.28 {
1721 : overbeek 1.56 if ($peg =~ /^fig\|/)
1722 : overbeek 1.28 {
1723 : overbeek 1.56 $func = $fig->function_of($peg);
1724 :     if ($func eq $from_func)
1725 :     {
1726 :     print TMP "$peg\t$to_func\n";
1727 :     }
1728 : overbeek 1.73 else
1729 : overbeek 1.57 {
1730 : overbeek 1.73 my @pieces = grep { $_ } split(/(\s+[\/@]\s+)|(\s*;\s+)/,$func);
1731 :     if (@pieces > 1)
1732 :     {
1733 :     my $func1 = join("",map { $_ =~ s/^$from_funcQ$/$to_func/; $_ } @pieces);
1734 :     if ($func ne $func1)
1735 :     {
1736 :     print TMP "$peg\t$func1\n";
1737 :     }
1738 :     }
1739 : overbeek 1.57 }
1740 : overbeek 1.28 }
1741 :     }
1742 :     }
1743 :     close(TMP);
1744 :     }
1745 :     push(@$html,$cgi->h1("Saved Assignment Set $file"));
1746 :     }
1747 :     else
1748 :     {
1749 : parrello 1.75 push(@$html,$cgi->h1("You need to specify a user to save an assignment set"));
1750 : overbeek 1.28 }
1751 :     }
1752 : efrank 1.92
1753 :     sub find_pegs_by_cv1 {
1754 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1755 :    
1756 :     # Remember kind of search that got us hear so we can call back
1757 :     # with same kind
1758 :     my $search = "Search";
1759 :     if ($cgi->param('Search genome selected below')) {
1760 :     $search=uri_escape('Search genome selected below');
1761 :     } elsif ( $cgi->param('Search Selected Organisms') ) {
1762 :     $search = uri_escape('Search Selected Organisms');
1763 :     } elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1764 :     $search = uri_escape('Find Genes in Org that Might Play the Role');
1765 :     }
1766 :    
1767 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1768 :    
1769 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1770 :     my $find_table_rows;
1771 :     my $counter = 0;
1772 :     for my $r (@$search_results)
1773 :     {
1774 :     my @temp = split("\t",$r);
1775 :     my $row = [];
1776 :     my $id= $temp[1];
1777 :     my $term = $temp[2];
1778 :     my $id_and_term = $id."; ".$term;
1779 :     my $pattern=uri_escape("$id; $term");
1780 :    
1781 :     my $link = "index.cgi?pattern=$pattern&Search=1&user=$user";
1782 :     my $cb = "<a href=$link>Find PEGs</a>";
1783 :    
1784 :     #feh my $cb = $cgi->submit(-name=>'$search', -value=>'Find PEGs');
1785 :     #my $cb_value = $cv."split_here".$id."; ".$term;
1786 :     #my $cb ="<input type=checkbox name=find_checked_$counter value='$cb_value'>" ;
1787 :     push(@$row,$cb);
1788 :     push(@$row,$cv);
1789 :     push(@$row,$id_and_term);
1790 :     push(@$find_table_rows,$row);
1791 :     $counter = $counter + 1;
1792 :     }
1793 :    
1794 :     my $find_terms_button="";
1795 :     if ($counter > 0) {
1796 :     $find_terms_button= $cgi->submit(-name=>'$search', -value=>'$search');
1797 :     }
1798 :    
1799 :     # build the page
1800 :     push @$html,
1801 :     $cgi->start_form(),
1802 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1803 :     $cgi->br,
1804 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1805 :     $cgi->hr,
1806 :     "<h4>Terms Matching Your Criteria </h4>\n",
1807 :     $cgi->br,
1808 :     &HTML::make_table($find_col_hdrs,$find_table_rows),
1809 :     $cgi->br,
1810 :     $find_terms_button,
1811 :     $cgi->end_form;
1812 :    
1813 :     return $html;
1814 :     }
1815 :    
1816 :     sub find_pegs_by_cv {
1817 :     my ($fig, $cgi, $html, $user, $pattern, $cv) = @_;
1818 :    
1819 :     # Remember kind of search that got us hear so we can call back
1820 :     # with same kind (not working so force to simple Search)
1821 :    
1822 :     my $search = "Search";
1823 :    
1824 :     #if ($cgi->param('Search genome selected below')) {
1825 :     # $search='Search genome selected below';
1826 :     #} elsif ( $cgi->param('Search Selected Organisms') ) {
1827 :     # $search = 'Search Selected Organisms';
1828 :     #} elsif ( $cgi->param('Find Genes in Org that Might Play the Role') ) {
1829 :     # $search = 'Find Genes in Org that Might Play the Role';
1830 :     #}
1831 :    
1832 :     my $search_results = $fig->search_cv_file($cv, $pattern);
1833 :    
1834 :     my $find_col_hdrs = ["Find","Vocab. Name","ID; Term"];
1835 :     my @patterns=();
1836 :     for my $r (@$search_results)
1837 :     {
1838 :     my @temp = split("\t",$r);
1839 :     my $id= $temp[1];
1840 :     my $term = $temp[2];
1841 :     my $pattern="$id; $term";
1842 :    
1843 :     push(@patterns,$pattern);
1844 :     }
1845 :    
1846 :     my @pattern_radio;
1847 :     if ($#patterns + 1) {
1848 :     @pattern_radio = $cgi->radio_group( -name => 'pattern',
1849 :     -values => [ @patterns ]
1850 :     );
1851 :     } else {
1852 :     @pattern_radio = ("Nothing found");
1853 :     }
1854 :    
1855 :     my $find_terms_button= $cgi->submit(-name=>"Search", -value=>"Search");
1856 :    
1857 :     # build the page
1858 :     push @$html,
1859 :     $cgi->start_form(),
1860 :     $cgi->hidden(-name=>'user', -value=>'$user'),
1861 :     $cgi->br,
1862 :     "<h2>Search for PEGs annotated with Contrlled Vocabulary Terms</h2>",
1863 :     $cgi->hr,
1864 :     "<h4>$cv Terms Matching Your Criteria </h4>\n",
1865 :     $cgi->br,
1866 :     $find_terms_button,
1867 :     $cgi->br,
1868 :     $cgi->br,
1869 :     join( "<br>", @pattern_radio),
1870 :     # &HTML::make_table($find_col_hdrs,$find_table_rows),
1871 :     $cgi->br,
1872 :     $find_terms_button,
1873 :     $cgi->end_form;
1874 :    
1875 :     return $html;
1876 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3