[Bio] / FigKernelPackages / raelib.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/raelib.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.30 - (view) (download) (as text)

1 : olson 1.28 #
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 : redwards 1.1 # -*- perl -*-
19 :    
20 :     =pod
21 :    
22 : parrello 1.13 =head1 RAE Library
23 : redwards 1.1
24 :     Some routines and things that Rob uses. Please feel free to use at will and incorporate into
25 :     your own code or move them into FIG.pm or elsewhere.
26 :    
27 :     =cut
28 :    
29 :     package raelib;
30 :     use strict;
31 : redwards 1.17 use Bio::SeqIO;
32 :     use Bio::Seq;
33 :     use Bio::SeqFeature::Generic;
34 : redwards 1.1 use FIG;
35 :     my $fig=new FIG;
36 :    
37 : redwards 1.5 =head2 new
38 :    
39 :     Just instantiate the object and return $self
40 :    
41 :     =cut
42 :    
43 :     sub new {
44 : redwards 1.27 my ($class)=@_;
45 :     my $self={};
46 :     return bless $self, $class;
47 : redwards 1.5 }
48 :    
49 :    
50 :    
51 :    
52 : redwards 1.4 =head2 features_on_contig
53 :    
54 :     Returns a reference to an array containing all the features on a contig in a genome.
55 :    
56 :     use:
57 :    
58 :     my $arrayref=$rae->features_on_contig($genome, $contig);
59 :    
60 :     or
61 :    
62 :     foreach my $peg (@{$rae->features_on_contig($genome, $contig)}) {
63 :     ... blah blah ...
64 :     }
65 :    
66 :     returns undef if contig is not a part of genome or there is nothing to return, otherwise returns a list of pegs
67 :    
68 :     v. experimental and guaranteed not to work!
69 :    
70 :     =cut
71 :    
72 :     sub features_on_contig {
73 :     my ($self, $genome, $contig)=@_;
74 :     # were this in FIG.pm you'd use this line:
75 :     #my $rdbH = $self->db_handle;
76 :    
77 :     my $rdbH = $fig->db_handle;
78 :     my $relational_db_response=$rdbH->SQL('SELECT id FROM features WHERE (genome = \'' . $genome . '\' AND location ~* \'' . $contig . '\')');
79 :     # this is complicated. A reference to an array of references to arrays, and we only want the first element.
80 :     # simplify.
81 :     my @results;
82 :     foreach my $res (@$relational_db_response) {push @results, $res->[0]}
83 :     return \@results;
84 :     }
85 :    
86 :    
87 :    
88 :    
89 :    
90 :    
91 :    
92 : redwards 1.7 =head2 pirsfcorrespondence
93 : redwards 1.1
94 : redwards 1.18 Generate the pirsf->fig id correspondence. This is only done once and the correspondence file is written. This is so that we can easily go back and forth.
95 : redwards 1.1
96 : redwards 1.18 The correspondence has PIR ID \t FIG ID\n, and is probably based on ftp://ftp.pir.georgetown.edu/pir_databases/pirsf/data/pirsfinfo.dat
97 : redwards 1.1
98 : redwards 1.18 This method takes three arguments:
99 : redwards 1.9 from : pirsfinfo.dat file
100 :     to : file to write information to
101 :     verbose : report on progress
102 :    
103 : redwards 1.18 Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
104 :    
105 :     Returns the number of lines in the pirsinfo file that were read.
106 : redwards 1.9
107 : redwards 1.1 =cut
108 :    
109 : redwards 1.7 sub pirsfcorrespondence {
110 : redwards 1.9 my ($self, $from, $to, $verbose)=@_;
111 : redwards 1.10 unless (-e $from) {
112 :     print STDERR "File $from does not exist as called in $0\n";
113 :     return 0;
114 :     }
115 : redwards 1.18 if ($from =~ /\.gz$/) {
116 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
117 :     }
118 :     else {
119 :     open (IN, $from) || die "Can't open $from";
120 :     }
121 : redwards 1.8 open (OUT, ">$to") || die "Can't write to $to";
122 : redwards 1.9 my $linecount;
123 : redwards 1.1 while (<IN>) {
124 : redwards 1.9 $linecount++;
125 : redwards 1.14 if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
126 : redwards 1.1 if (/^>/) {print OUT; next}
127 :     chomp;
128 : redwards 1.14 foreach my $peg ($self->swiss_pir_ids($_)) {
129 : redwards 1.1 print OUT $_, "\t", $peg, "\n";
130 :     }
131 : redwards 1.14 }
132 :     close IN;
133 :     close OUT;
134 :     return $linecount;
135 :     }
136 :    
137 :     =head2 uniprotcorrespondence
138 :    
139 :     Generate a correspondence table between uniprot knowledge base IDs and FIG ID's.
140 :    
141 :     The uniprot KB file is in the form: UniProtKB_Primary_Accession | UniProtKB_ID | Section | Protein Name
142 :    
143 :     This method takes three arguments:
144 :     from : uniprotKB file
145 :     to : file to write information to
146 :     verbose : report on progress
147 :    
148 : redwards 1.18 Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
149 :    
150 :     Returns the number of lines in the uniprotkb file that were read.
151 : redwards 1.14
152 :     =cut
153 :    
154 :     sub uniprotcorrespondence {
155 :     my ($self, $from, $to, $verbose)=@_;
156 :     unless (-e $from) {
157 :     print STDERR "File $from does not exist as called in $0\n";
158 :     return 0;
159 :     }
160 : redwards 1.18 if ($from =~ /\.gz$/) {
161 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
162 :     }
163 :     else {
164 :     open (IN, $from) || die "Can't open $from";
165 :     }
166 : redwards 1.14 open (OUT, ">$to") || die "Can't write to $to";
167 :     my $linecount;
168 :     while (<IN>) {
169 :     chomp;
170 :     $linecount++;
171 :     if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
172 :     my @line=split /\s+\|\s+/;
173 : redwards 1.16 my $added;
174 : redwards 1.14 foreach my $peg ($self->swiss_pir_ids($line[0])) {
175 :     print OUT "$_ | $peg\n";
176 : redwards 1.16 $added=1;
177 : redwards 1.12 }
178 : redwards 1.16 unless ($added) {print OUT "$_\n"}
179 : redwards 1.1 }
180 :     close IN;
181 :     close OUT;
182 : redwards 1.9 return $linecount;
183 : redwards 1.1 }
184 :    
185 : redwards 1.18 =head2 prositecorrespondence
186 :    
187 :     Generate a correspondence table between prosite and seed using sp id's and seed ids.
188 :    
189 :     The SwissProt prosite file is from ftp://ca.expasy.org/databases/prosite/release_with_updates/prosite.dat and is in horrible swiss prot format, so we'll parse out those things that we need and put them in the file
190 :    
191 :     The output file will have the following columns:
192 :    
193 :     prosite family accession number, prosite family name, family type, swiss-prot protein id, fig protein id.
194 :    
195 :     The family type is one of rule, pattern, or matrix. Right now (Prosite Release 19.2 of 24-May-2005) there are 4 rules, 1322 patterns, and 521 matrices.
196 :    
197 :     This method takes three arguments:
198 :     from : prosite file
199 :     to : file to write information to
200 :     verbose : report on progress
201 :    
202 :     Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
203 :    
204 :     Returns the number of lines in the prosite file that were read.
205 :    
206 :     =cut
207 :    
208 :     sub prositecorrespondence {
209 :     my ($self, $from, $to, $verbose)=@_;
210 :     unless (-e $from) {
211 :     print STDERR "File $from does not exist as called in $0\n";
212 :     return 0;
213 :     }
214 :     if ($from =~ /\.gz$/) {
215 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
216 :     }
217 :     else {
218 :     open (IN, $from) || die "Can't open $from";
219 :     }
220 :     open (OUT, ">$to") || die "Can't write to $to";
221 :     my $linecount;
222 :     my ($famac, $famname, $famtype)=('','','');
223 :     while (<IN>) {
224 :     chomp;
225 :     $linecount++;
226 :     if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
227 :     if (m#//#) {($famac, $famname, $famtype)=('','',''); next}
228 :     elsif (m/^ID\s*(.*?);\s*(\S+)/) {($famname, $famtype)=($1, $2); next}
229 : redwards 1.19 elsif (m/^AC\s*(\S+)/) {$famac=$1; $famac =~ s/\;\s*$//; next}
230 : redwards 1.18 next unless (m/^DR/); # ignore all the other crap in the prosite file for now. Note we might, at some point, want to grab all that, but that is for another time.
231 :     #
232 :     # this is the format of the DR lines:
233 :     # DR P11460, FATB_VIBAN , T; P40409, FEUA_BACSU , T; P37580, FHUD_BACSU , T;
234 :     s/^DR\s*//;
235 :     foreach my $piece (split /\s*\;\s*/, $_) {
236 :     my ($acc, $nam, $unk)=split /\s*\,\s*/, $piece;
237 :     foreach my $fig ($self->swiss_pir_ids($acc)) {
238 : redwards 1.20 print OUT join "\t", ($famac, $famname, $famtype, $acc, $fig), "\n";
239 : redwards 1.18 }
240 :     }
241 :     }
242 :     }
243 : redwards 1.14
244 :     =head2 swiss_pir_ids()
245 :    
246 : redwards 1.18 SwissProt/PIR have lots of ID's that we want to get, usually in this order - uni --> tr --> sp. This routine will map swissprot/pir ids to fig id's, and return an array of FIG id's that match the ID.
247 : redwards 1.14
248 :     =cut
249 :    
250 :     sub swiss_pir_ids {
251 :     my ($self, $id)=@_;
252 :     return () unless ($id);
253 : redwards 1.18 $id =~ s/^\s+//; $id =~ s/\s+$//; # trim off the whitespace
254 :    
255 : redwards 1.15 my @return=($fig->by_alias("uni|$id"));
256 : redwards 1.14 return @return if ($return[0]);
257 :    
258 : redwards 1.15 @return=($fig->by_alias("tr|$id"));
259 : redwards 1.14 return @return if ($return[0]);
260 :    
261 : redwards 1.15 @return=($fig->by_alias("sp|$id"));
262 : redwards 1.14 return @return if ($return[0]);
263 :    
264 :     return ();
265 :     }
266 : redwards 1.1
267 :     =head2 ss_by_id
268 :    
269 :     Generate a list of subsystems that a peg occurs in. This is a ; separated list.
270 :     This is a wrapper that removes roles and ignores essential things
271 :    
272 :     =cut
273 :    
274 :     sub ss_by_id {
275 :     my ($self, $peg)=@_;
276 :     my $ssout;
277 :     foreach my $ss (sort $fig->subsystems_for_peg($peg))
278 :     {
279 :     next if ($$ss[0] =~ /essential/i); # Ignore the Essential B-subtilis subsystems
280 :     $ssout.=$$ss[0]."; ";
281 :     }
282 :     $ssout =~ s/; $//;
283 :     return $ssout;
284 :     }
285 :    
286 : redwards 1.3 =head2 ss_by_homol
287 :    
288 :     Generate a list of subsystems that homologs of a peg occur in. This is a ; separated list.
289 :     This is also a wrapper around sims and ss, but makes everything unified
290 :    
291 :     =cut
292 :    
293 :     sub ss_by_homol {
294 :     my ($self, $peg)=@_;
295 :     return unless ($peg);
296 :     my ($maxN, $maxP)=(50, 1e-20);
297 :    
298 :     # find the sims
299 :     my @sims=$fig->sims($peg, $maxN, $maxP, 'fig');
300 :    
301 :     # we are only going to keep the best hit for each peg
302 :     # in a subsystem
303 :     my $best_ss_score; my $best_ss_id;
304 :     foreach my $sim (@sims)
305 :     {
306 :     my $simpeg=$$sim[1];
307 :     my $simscore=$$sim[10];
308 :     my @subsys=$fig->subsystems_for_peg($simpeg);
309 :     foreach my $ss (@subsys)
310 :     {
311 :     if (! defined $best_ss_score->{$$ss[0]}) {$best_ss_score->{$$ss[0]}=$simscore; $best_ss_id->{$$ss[0]}=$simpeg}
312 :     elsif ($best_ss_score->{$$ss[0]} > $simscore)
313 :     {
314 :     $best_ss_score->{$$ss[0]}=$simscore;
315 :     $best_ss_id->{$$ss[0]}=$simpeg;
316 :     }
317 :     }
318 :     }
319 :    
320 :     my $ssoutput=join "", (map {"$_ (".$best_ss_id->{$_}."), "} keys %$best_ss_id);
321 :    
322 :     $ssoutput =~ s/, $//;
323 :     return $ssoutput;
324 :     }
325 :    
326 :     =head2 tagvalue
327 :    
328 :     This will just check for tag value pairs and return either an array of values or a single ; separated list (if called as a scalar)
329 :    
330 :     e.g. $values=raelib->tagvalue($peg, "PIRSF"); print join "\n", @$values;
331 :    
332 :     Returns an empty array if no tag/value appropriate.
333 :    
334 :     Just because I use this a lot I don't want to waste rewriting it.
335 :    
336 :     =cut
337 :    
338 :     sub tagvalue {
339 :     my ($self, $peg, $tag)=@_;
340 :     my @return;
341 :     my @attr=$fig->feature_attributes($peg);
342 :     foreach my $attr (@attr) {
343 : redwards 1.11 my ($gotpeg, $gottag, $val, $link)=@$attr;
344 : redwards 1.3 push @return, $val if ($gottag eq $tag);
345 :     }
346 :     return wantarray ? @return : join "; ", @return;
347 :     }
348 : redwards 1.1
349 : redwards 1.5 =head2 locations_on_contig
350 :    
351 :     Return the locations of a sequence on a contig.
352 :    
353 :     This will look for exact matches to a sequence on a contig, and return a reference to an array that has all the locations.
354 :    
355 :     my $locations=$raelib->locations_on_contig($genome, $contig, 'GATC', undef);
356 :     foreach my $bp (@$locations) { ... do something ... }
357 :    
358 :     first argument : genome number
359 :     second argument : contig name
360 :     third argument : sequence to look for
361 :     fourth argument : beginning position to start looking from (can be undef)
362 :     fifth argument : end position to stop looking from (can be undef)
363 :     sixth argument : check reverse complement (0 or undef will check forward, 1 or true will check rc)
364 :    
365 :     Note, the position is calculated before the sequence is rc'd
366 :    
367 :     =cut
368 :    
369 :     sub locations_on_contig {
370 :     my ($self, $genome, $contig, $sequence, $from, $to, $check_reverse)=@_;
371 :     my $return=[];
372 :    
373 :     # get the dna sequence of the contig, and make sure it is uppercase
374 :     my $contig_ln=$fig->contig_ln($genome, $contig);
375 :     return $return unless ($contig_ln);
376 :     unless ($from) {$from=1}
377 :     unless ($to) {$to=$contig_ln}
378 :     if ($from > $to) {($from, $to)=($to, $from)}
379 :     my $dna_seq=$fig->dna_seq($genome, $contig."_".$from."_".$to);
380 :     $dna_seq=uc($dna_seq);
381 :    
382 :     # if we want to check the rc, we actually rc the query
383 :     $sequence=$fig->reverse_comp($sequence) if ($check_reverse);
384 :     $sequence=uc($sequence);
385 :    
386 :     # now find all the matches
387 :     my $posn=index($dna_seq, $sequence, 0);
388 :     while ($posn > -1) {
389 :     push @$return, $posn;
390 :     $posn=index($dna_seq, $sequence, $posn+1);
391 :     }
392 :     return $return;
393 :     }
394 :    
395 :    
396 :     =head2 scrolling_org_list
397 :    
398 :     This is the list from index.cgi, that I call often. It has one minor modification: the value returned is solely the organisms id and does not contain genus_species information. I abstracted this here: 1, so I could call it often, and 2, so I could edit it once.
399 :    
400 : overbeek 1.24 use like this push @$html, $raelib->scrolling_org_list($cgi, $multiple, $default);
401 : redwards 1.5
402 :     multiple selections will only be set if $multiple is true
403 :    
404 : overbeek 1.24 default will set a default to override (maybe) korgs
405 :    
406 : redwards 1.5 =cut
407 :    
408 :     sub scrolling_org_list {
409 : overbeek 1.24 my ($self, $cgi, $multiple, $default)=@_;
410 : redwards 1.5 unless ($multiple) {$multiple=0}
411 :    
412 :     my @display = ( 'All', 'Archaea', 'Bacteria', 'Eucarya', 'Viruses', 'Environmental samples' );
413 :    
414 :     #
415 :     # Canonical names must match the keywords used in the DBMS. They are
416 :     # defined in compute_genome_counts.pl
417 :     #
418 :     my %canonical = (
419 :     'All' => undef,
420 :     'Archaea' => 'Archaea',
421 :     'Bacteria' => 'Bacteria',
422 :     'Eucarya' => 'Eukaryota',
423 :     'Viruses' => 'Virus',
424 :     'Environmental samples' => 'Environmental Sample'
425 :     );
426 :    
427 :     my $req_dom = $cgi->param( 'domain' ) || 'All';
428 :     my @domains = $cgi->radio_group( -name => 'domain',
429 :     -default => $req_dom,
430 :     -override => 1,
431 :     -values => [ @display ]
432 :     );
433 :    
434 :     my $n_domain = 0;
435 :     my %dom_num = map { ( $_, $n_domain++ ) } @display;
436 :     my $req_dom_num = $dom_num{ $req_dom } || 0;
437 :    
438 :     #
439 :     # Viruses and Environmental samples must have completeness = All (that is
440 :     # how they are in the database). Otherwise, default is Only "complete".
441 :     #
442 :     my $req_comp = ( $req_dom_num > $dom_num{ 'Eucarya' } ) ? 'All'
443 :     : $cgi->param( 'complete' ) || 'Only "complete"';
444 :     my @complete = $cgi->radio_group( -name => 'complete',
445 :     -default => $req_comp,
446 :     -override => 1,
447 :     -values => [ 'All', 'Only "complete"' ]
448 :     );
449 :     #
450 :     # Use $fig->genomes( complete, restricted, domain ) to get org list:
451 :     #
452 :     my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
453 :    
454 :     my $orgs; my $label;
455 :     @$orgs = $fig->genomes( $complete, undef, $canonical{ $req_dom } );
456 :    
457 :     foreach (@$orgs) {
458 :     my $gs = $fig->genus_species($_);
459 : overbeek 1.29 my $gc=$fig->number_of_contigs($_);
460 : redwards 1.5 $label->{$_} = "$gs ($_) [$gc contigs]";
461 :     }
462 :    
463 :     @$orgs = sort {$label->{$a} cmp $label->{$b}} @$orgs;
464 :    
465 :     my $n_genomes = @$orgs;
466 :    
467 :     return ( "<TABLE>\n",
468 :     " <TR>\n",
469 :     " <TD>",
470 : redwards 1.6 $cgi->scrolling_list( -name => 'korgs',
471 :     -values => $orgs,
472 :     -labels => $label,
473 :     -size => 10,
474 :     -multiple => $multiple,
475 : overbeek 1.24 -default => $default,
476 : redwards 1.5 ), $cgi->br,
477 :     "$n_genomes genomes shown ",
478 :     $cgi->submit( 'Update List' ), $cgi->reset, $cgi->br,
479 :     " </TD>",
480 :     " <TD>",
481 :     join( "<br>", "<b>Domain(s) to show:</b>", @domains), "<br>\n",
482 :     join( "<br>", "<b>Completeness?</b>", @complete), "\n",
483 :     "</TD>",
484 :     " </TR>\n",
485 :     "</TABLE>\n",
486 :     );
487 :     }
488 :    
489 : redwards 1.21
490 :     =head2 scrolling_subsys_list
491 :    
492 :     Create a scrolling list of all subsystems. Just like scrolling_org_list, this will make the list and allow you to select multiples.
493 :    
494 :     use like this
495 :    
496 :     push @$html, $raelib->scrolling_subsys_list($cgi, $multiple);
497 :    
498 :     =cut
499 :    
500 :     sub scrolling_subsys_list {
501 :     my ($self, $cgi, $multiple)=@_;
502 :     $multiple=0 unless (defined $multiple);
503 : redwards 1.22 my @ss=sort {uc($a) cmp uc($b)} $fig->all_subsystems();
504 : redwards 1.21 my $label;
505 :     # generate labels for the list
506 :     foreach my $s (@ss) {my $k=$s; $k =~ s/\_/ /g; $k =~ s/ / /g; $k =~ s/\s+$//; $label->{$s}=$k}
507 :     return $cgi->scrolling_list(
508 :     -name => 'subsystems',
509 :     -values => \@ss,
510 :     -labels => $label,
511 :     -size => 10,
512 :     -multiple=> $multiple,
513 :     );
514 :     }
515 :    
516 :     =head2 subsys_names_for_display
517 :    
518 :     Return a list of subsystem names for display. This will take a list as an argument and return a nice clean list for display.
519 :    
520 :     $raelib->subsys_names_for_display(@ss);
521 :     or
522 :     $raelib->subsys_names_for_display($fig->all_subsystems());
523 :    
524 :     =cut
525 :    
526 :     sub subsys_names_for_display {
527 :     my ($self, @ss)=@_;
528 :     foreach (@ss) {s/\_/ /g; 1 while (s/ / /g); s/\s+$//}
529 :     return @ss;
530 :     }
531 :    
532 : redwards 1.17 =head2 GenBank
533 : redwards 1.5
534 : redwards 1.17 This object will take a genome number and return a Bio::Seq::RichSeq object that has the whole genome
535 :     in GenBank format. This should be a nice way of getting some data out, but will probably be quite slow
536 :     at building the object.
537 : redwards 1.1
538 : redwards 1.17 Note that you need to call this with the genome name and the contig. This will then go through that contig.
539 : redwards 1.1
540 : redwards 1.17 Something like this should work
541 :    
542 :     foreach my $contig ($fig->all_contigs($genome)) {
543 :     my $seqobj=FIGRob->GenBank($genome, $contig);
544 :     # process the contig
545 :     }
546 :    
547 :     =cut
548 :    
549 :     sub GenBank {
550 :     my ($self, $genome, $contig)=@_;
551 :     my $gs=$fig->genus_species($genome);
552 :     return unless ($gs);
553 :     unless ($contig) {
554 :     print STDERR "You didn't provide a contig for $gs. I think that was a mistake. Sorry\n";
555 :     return;
556 :     }
557 :     my $len=$fig->contig_ln($genome, $contig);
558 :     unless ($len) {
559 :     print STDERR "$contig from $gs doesn't appear to have a length. Is it right?\n";
560 :     return;
561 :     }
562 :    
563 :    
564 :     # first find all the pegs ...
565 :     my $features; # all the features in the genome
566 :     my $allpegs; # all the pegs
567 :     my $translation; # all the protein sequences
568 :     foreach my $peg ($fig->pegs_of($genome)) {
569 :     my @location=$fig->feature_location($peg);
570 :     my $func=$fig->function_of($peg);
571 :     foreach my $loc (@location) {
572 :     $loc =~ /^(.*)\_(\d+)\_(\d+)$/;
573 :     my ($cg, $start, $stop)=($1, $2, $3);
574 :     next unless ($cg eq $contig);
575 :     # save this information for later
576 :     $features->{'peg'}->{$loc}=$func;
577 :     $allpegs->{'peg'}->{$loc}=$peg;
578 :     $translation->{'peg'}->{$loc}=$fig->get_translation($peg);
579 :     }
580 :     }
581 :     # ... and all the RNAs
582 :     foreach my $peg ($fig->rnas_of($genome)) {
583 :     my @location=$fig->feature_location($peg);
584 :     my $func=$fig->function_of($peg);
585 :     foreach my $loc (@location) {
586 :     $loc =~ /^(.*)\_(\d+)\_(\d+)$/;
587 :     my ($cg, $start, $stop)=($1, $2, $3);
588 :     next unless ($cg eq $contig);
589 :     # save this information for later
590 :     $features->{'rna'}->{$loc}=$func;
591 :     $allpegs->{'rna'}->{$loc}=$peg;
592 :     }
593 :     }
594 :    
595 :    
596 :     # now get all the contigs out
597 :     my $seq=$fig->dna_seq($genome, $contig."_1_".$len);
598 :     my $description = "Contig $contig from " . $fig->genus_species($genome);
599 :     my $sobj=Bio::Seq->new(
600 :     -seq => $seq,
601 :     -id => $contig,
602 :     -desc => $description,
603 :     -accession_number => $genome
604 :     );
605 :     foreach my $prot (keys %{$features->{'peg'}}) {
606 :     $prot =~ /^(.*)\_(\d+)\_(\d+)$/;
607 :     my ($cg, $start, $stop)=($1, $2, $3);
608 :     my $strand=1;
609 :     if ($stop < $start) {
610 :     ($stop, $start)=($start, $stop);
611 :     $strand=-1;
612 :     }
613 :    
614 :     my $feat=Bio::SeqFeature::Generic->new(
615 :     -start => $start,
616 :     -end => $stop,
617 :     -strand => $strand,
618 :     -primary => 'CDS',
619 :     -display_name => $allpegs->{'peg'}->{$prot},
620 :     -source_tag => 'the SEED',
621 :     -tag =>
622 :     {
623 :     db_xref => $allpegs->{'peg'}->{$prot},
624 :     note => 'Generated by the Fellowship for the Interpretation of Genomes',
625 :     function => $features->{'peg'}->{$prot},
626 :     translation => $translation->{'peg'}->{$prot}
627 :     }
628 :     );
629 :    
630 :     $sobj->add_SeqFeature($feat);
631 :     }
632 :    
633 :     foreach my $prot (keys %{$features->{'rna'}}) {
634 :     $prot =~ /^(.*)\_(\d+)\_(\d+)$/;
635 :     my ($cg, $start, $stop)=($1, $2, $3);
636 :     my $strand=1;
637 :     if ($stop < $start) {
638 :     ($stop, $start)=($start, $stop);
639 :     $strand=-1;
640 :     }
641 :    
642 :     my $feat=Bio::SeqFeature::Generic->new(
643 :     -start => $start,
644 :     -end => $stop,
645 :     -strand => $strand,
646 :     -primary => 'RNA',
647 :     -source_tag => 'the SEED',
648 :     -display_name => $allpegs->{'rna'}->{$prot},
649 :     -tag =>
650 :     {
651 :     db_xref => $allpegs->{'rna'}->{$prot},
652 :     note => 'Generated by the Fellowship for the Interpretation of Genomes',
653 :     function => $features->{'rna'}->{$prot},
654 :     }
655 :     );
656 :    
657 :     $sobj->add_SeqFeature($feat);
658 :     }
659 :     return $sobj;
660 :     }
661 :    
662 :     =head2 best_hit
663 :    
664 :     Returns the FIG id of the single best hit to a peg
665 :    
666 :     eg
667 :    
668 :     my $bh=$fr->best_hit($peg);
669 :     print 'function is ', scalar $fig->function_of($bh);
670 :    
671 :     =cut
672 :    
673 :     sub best_hit {
674 :     my ($self, $peg)=@_;
675 :     return unless ($peg);
676 :    
677 :     my ($maxN, $maxP)=(1, 1e-5);
678 :     my @sims=$fig->sims($peg, $maxN, $maxP, 'fig');
679 :     return ${$sims[0]}[1];
680 :     }
681 : redwards 1.1
682 : redwards 1.23
683 :     =head1 read_fasta
684 :    
685 :     Read a fasta format file and return a reference to a hash with the data. The key is the ID and the value is the sequence. If you supply the optional keep comments then the comments (anything after the first white space are returned as a sepaarte hash).
686 :    
687 :     Usage:
688 :     my $fasta=$raelib->read_fasta($file);
689 :     my ($fasta, $comments)=$raelib->read_fasta($file, 1);
690 :    
691 :     =cut
692 :    
693 :     sub read_fasta {
694 :     my ($self, $file, $keepcomments)=@_;
695 :     open (IN, $file) || die "Can't open $file";
696 :     my %f; my $t; my $s; my %c;
697 : overbeek 1.26 while (<IN>) {
698 : redwards 1.23 chomp;
699 :     if (/^>/) {
700 :     if ($s) {
701 :     $f{$t}=$s;
702 :     undef $s;
703 :     }
704 :     s/^>(\S+)\s*//;
705 :     $t=$1;
706 :     $c{$t}=$_ if ($_);
707 :     }
708 :     else {$s .= $_}
709 :     }
710 :     $f{$t}=$s;
711 :     if ($keepcomments) {return (\%f, \%c)}
712 :     else {return \%f}
713 :     }
714 :    
715 :     =head1 rc
716 :    
717 :     Reverse complement. It's too easy.
718 :    
719 :     =cut
720 :    
721 :     sub rc {
722 :     my ($self, $seq)=@_;
723 :     $seq=~tr/GATCgatc/CTAGctag/;
724 :     $seq = reverse $seq;
725 :     return $seq;
726 :     }
727 :    
728 : redwards 1.27
729 :     =head2 cookies
730 :    
731 :     Handle cookies. This method will get and set the value of the FIG cookie. Cookies are name/value pairs that are stored on the users computer. We then retrieve them using this method. The cookies are passed in as a reference to a hash, and the method returns a tuple of the cookie that can be passed to the browser and a reference to a hash with the data.
732 :    
733 :     If you do not pass any arguments the whole cookie will be returned.
734 :    
735 :     Use as follows:
736 :    
737 :     ($cookie, $data) = raelib->cookie($cgi, \%data);
738 :    
739 :     You do not need to pass in any data, in that case you will just get the cookie back
740 :    
741 :     Underneath, I create a single cookie called FIG which stores all the information. The names and value pairs are stored using = to join name to value and ; to concatenate. This way we can create a single cookie with all the data. I am using the FIG::clean_attribute_key method to remove unwanted characters from the name/value pairs, so don't use them.
742 :    
743 :     Note that for the moment I have put this routine here since it needs to maintain the state of the cookie (i.e. it needs to know what $self is). It should really be in HTML.pm but that is not, as far as I can tell, maintaining states?
744 :    
745 :     =cut
746 :    
747 :     sub cookie {
748 :     my ($self, $cgi, $input)=@_;
749 :     return unless ($cgi);
750 :     $self->{'cookie'}=$cgi->cookie(-name=>"FIG") unless ($self->{'cookie'});
751 :    
752 :     # first, create a hash from the existing cookie data
753 :     my $cookie;
754 :     map {
755 :     my ($kname, $kvalue)=split /\=/, $_;
756 :     $cookie->{$kname}=$kvalue;
757 :     } split /\;/, $self->{'cookie'};
758 :    
759 :     if ($input)
760 :     {
761 :     # add the values that were passed in
762 :     map {$cookie->{FIG->clean_attribute_key($_)}=$input->{$_}} keys %$input;
763 :     # put everything back together and set the cookie
764 :     my $newcookie=join ";", map {$_ . "=" . $cookie->{$_}} keys %$cookie;
765 :     $self->{'cookie'}=$cgi->cookie(-name=>"FIG", -value=>$newcookie, -expires=>'+1y');
766 :     }
767 :    
768 :     return ($self->{'cookie'}, $cookie);
769 :     }
770 :    
771 :    
772 :    
773 : overbeek 1.30 =head1 commify
774 :    
775 :     Put commas in numbers. I think this comes straight from the perl cookbook and is very useful for nice displays
776 :    
777 :     =cut
778 :    
779 :     sub commify {
780 :     my($self,$n) = @_;
781 :     my(@n) = ();
782 :     my($i);
783 :    
784 :     for ($i = (length($n) - 3); ($i > 0); $i -= 3)
785 :     {
786 :     unshift(@n,",",substr($n,$i,3));
787 :     }
788 :     unshift(@n,substr($n,0,$i+3));
789 :     return join("",@n);
790 :     }
791 :    
792 :    
793 :    
794 :    
795 : redwards 1.27
796 : redwards 1.1 1;
797 : redwards 1.17

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3