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

Annotation of /FigKernelPackages/raelib.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (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 : overbeek 1.31 For questions about this email RobE@theFIG.info
28 :    
29 : redwards 1.1 =cut
30 :    
31 :     package raelib;
32 :     use strict;
33 : redwards 1.17 use Bio::SeqIO;
34 :     use Bio::Seq;
35 : overbeek 1.34 use Bio::Tools::SeqStats;
36 : redwards 1.17 use Bio::SeqFeature::Generic;
37 : overbeek 1.31
38 :     # we don't know whether the Spreadsheet::WriteExcel methods are available on all systems, and even on the CI systems they are currently in my shared directory
39 :     # so we use an eval and set the boolean if we are cool.
40 :     my $useexcel;
41 :     BEGIN {
42 :     use lib '/home/seed/Rob/perl/lib/perl5/site_perl/5.8.5/';
43 :     eval "use Spreadsheet::WriteExcel";
44 :     unless ($@) {$useexcel=1}
45 :     }
46 :    
47 : overbeek 1.32
48 : redwards 1.1 use FIG;
49 :     my $fig=new FIG;
50 :    
51 : redwards 1.5 =head2 new
52 :    
53 :     Just instantiate the object and return $self
54 :    
55 :     =cut
56 :    
57 :     sub new {
58 : redwards 1.27 my ($class)=@_;
59 :     my $self={};
60 : overbeek 1.32 $self->{'useexcel'}=1 if ($useexcel);
61 : redwards 1.27 return bless $self, $class;
62 : redwards 1.5 }
63 :    
64 :    
65 :    
66 :    
67 : redwards 1.4 =head2 features_on_contig
68 :    
69 :     Returns a reference to an array containing all the features on a contig in a genome.
70 :    
71 :     use:
72 :    
73 :     my $arrayref=$rae->features_on_contig($genome, $contig);
74 :    
75 :     or
76 :    
77 :     foreach my $peg (@{$rae->features_on_contig($genome, $contig)}) {
78 :     ... blah blah ...
79 :     }
80 :    
81 :     returns undef if contig is not a part of genome or there is nothing to return, otherwise returns a list of pegs
82 :    
83 :     v. experimental and guaranteed not to work!
84 :    
85 :     =cut
86 :    
87 :     sub features_on_contig {
88 :     my ($self, $genome, $contig)=@_;
89 :     # were this in FIG.pm you'd use this line:
90 :     #my $rdbH = $self->db_handle;
91 :    
92 :     my $rdbH = $fig->db_handle;
93 :     my $relational_db_response=$rdbH->SQL('SELECT id FROM features WHERE (genome = \'' . $genome . '\' AND location ~* \'' . $contig . '\')');
94 :     # this is complicated. A reference to an array of references to arrays, and we only want the first element.
95 :     # simplify.
96 :     my @results;
97 :     foreach my $res (@$relational_db_response) {push @results, $res->[0]}
98 :     return \@results;
99 :     }
100 :    
101 :    
102 : overbeek 1.34 =head2 mol_wt
103 : redwards 1.4
104 : overbeek 1.34 Calculate the molecular weight of a protein.
105 : redwards 1.4
106 : overbeek 1.34 This just offlaods the calculation to BioPerl, which is probably dumb since we need to load the whole of bioperl in just for this, but I don't have time to rewrite their method now, and I am not going to copy and paste it since I didn't write it :)
107 : redwards 1.4
108 : overbeek 1.34 my ($lower, $upper)=$raelib->mol_wt($peg);
109 :    
110 :     $lower is the lower bound for the possible mw, upper is the upper bound.
111 :    
112 :     =cut
113 :    
114 :     sub mol_wt {
115 :     my ($self, $fid)=@_;
116 :     my $sobj=Bio::Seq->new(-seq => $fig->get_translation($fid), -id => $fid);
117 :     return Bio::Tools::SeqStats->get_mol_wt($sobj);
118 :     }
119 : redwards 1.4
120 :    
121 : redwards 1.7 =head2 pirsfcorrespondence
122 : redwards 1.1
123 : 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.
124 : redwards 1.1
125 : 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
126 : redwards 1.1
127 : redwards 1.18 This method takes three arguments:
128 : redwards 1.9 from : pirsfinfo.dat file
129 :     to : file to write information to
130 :     verbose : report on progress
131 :    
132 : redwards 1.18 Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
133 :    
134 :     Returns the number of lines in the pirsinfo file that were read.
135 : redwards 1.9
136 : redwards 1.1 =cut
137 :    
138 : redwards 1.7 sub pirsfcorrespondence {
139 : redwards 1.9 my ($self, $from, $to, $verbose)=@_;
140 : redwards 1.10 unless (-e $from) {
141 :     print STDERR "File $from does not exist as called in $0\n";
142 :     return 0;
143 :     }
144 : redwards 1.18 if ($from =~ /\.gz$/) {
145 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
146 :     }
147 :     else {
148 :     open (IN, $from) || die "Can't open $from";
149 :     }
150 : redwards 1.8 open (OUT, ">$to") || die "Can't write to $to";
151 : redwards 1.9 my $linecount;
152 : redwards 1.1 while (<IN>) {
153 : redwards 1.9 $linecount++;
154 : redwards 1.14 if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
155 : redwards 1.1 if (/^>/) {print OUT; next}
156 :     chomp;
157 : redwards 1.14 foreach my $peg ($self->swiss_pir_ids($_)) {
158 : redwards 1.1 print OUT $_, "\t", $peg, "\n";
159 :     }
160 : redwards 1.14 }
161 :     close IN;
162 :     close OUT;
163 :     return $linecount;
164 :     }
165 :    
166 :     =head2 uniprotcorrespondence
167 :    
168 :     Generate a correspondence table between uniprot knowledge base IDs and FIG ID's.
169 :    
170 :     The uniprot KB file is in the form: UniProtKB_Primary_Accession | UniProtKB_ID | Section | Protein Name
171 :    
172 :     This method takes three arguments:
173 :     from : uniprotKB file
174 :     to : file to write information to
175 :     verbose : report on progress
176 :    
177 : redwards 1.18 Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
178 :    
179 :     Returns the number of lines in the uniprotkb file that were read.
180 : redwards 1.14
181 :     =cut
182 :    
183 :     sub uniprotcorrespondence {
184 :     my ($self, $from, $to, $verbose)=@_;
185 :     unless (-e $from) {
186 :     print STDERR "File $from does not exist as called in $0\n";
187 :     return 0;
188 :     }
189 : redwards 1.18 if ($from =~ /\.gz$/) {
190 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
191 :     }
192 :     else {
193 :     open (IN, $from) || die "Can't open $from";
194 :     }
195 : redwards 1.14 open (OUT, ">$to") || die "Can't write to $to";
196 :     my $linecount;
197 :     while (<IN>) {
198 :     chomp;
199 :     $linecount++;
200 :     if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
201 :     my @line=split /\s+\|\s+/;
202 : redwards 1.16 my $added;
203 : redwards 1.14 foreach my $peg ($self->swiss_pir_ids($line[0])) {
204 :     print OUT "$_ | $peg\n";
205 : redwards 1.16 $added=1;
206 : redwards 1.12 }
207 : redwards 1.16 unless ($added) {print OUT "$_\n"}
208 : redwards 1.1 }
209 :     close IN;
210 :     close OUT;
211 : redwards 1.9 return $linecount;
212 : redwards 1.1 }
213 :    
214 : redwards 1.18 =head2 prositecorrespondence
215 :    
216 :     Generate a correspondence table between prosite and seed using sp id's and seed ids.
217 :    
218 :     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
219 :    
220 :     The output file will have the following columns:
221 :    
222 :     prosite family accession number, prosite family name, family type, swiss-prot protein id, fig protein id.
223 :    
224 :     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.
225 :    
226 :     This method takes three arguments:
227 :     from : prosite file
228 :     to : file to write information to
229 :     verbose : report on progress
230 :    
231 :     Note that if the from filename ends in .gz it assumed to be a gzipped file and will be opened accordingly.
232 :    
233 :     Returns the number of lines in the prosite file that were read.
234 :    
235 :     =cut
236 :    
237 :     sub prositecorrespondence {
238 :     my ($self, $from, $to, $verbose)=@_;
239 :     unless (-e $from) {
240 :     print STDERR "File $from does not exist as called in $0\n";
241 :     return 0;
242 :     }
243 :     if ($from =~ /\.gz$/) {
244 :     open(IN, "|gunzip -c $from") || die "Can't open $from using a gunzip pipe";
245 :     }
246 :     else {
247 :     open (IN, $from) || die "Can't open $from";
248 :     }
249 :     open (OUT, ">$to") || die "Can't write to $to";
250 :     my $linecount;
251 :     my ($famac, $famname, $famtype)=('','','');
252 :     while (<IN>) {
253 :     chomp;
254 :     $linecount++;
255 :     if ($verbose && !($linecount % 10000)) {print STDERR "Parsed $linecount lines\n"}
256 :     if (m#//#) {($famac, $famname, $famtype)=('','',''); next}
257 :     elsif (m/^ID\s*(.*?);\s*(\S+)/) {($famname, $famtype)=($1, $2); next}
258 : redwards 1.19 elsif (m/^AC\s*(\S+)/) {$famac=$1; $famac =~ s/\;\s*$//; next}
259 : 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.
260 :     #
261 :     # this is the format of the DR lines:
262 :     # DR P11460, FATB_VIBAN , T; P40409, FEUA_BACSU , T; P37580, FHUD_BACSU , T;
263 :     s/^DR\s*//;
264 :     foreach my $piece (split /\s*\;\s*/, $_) {
265 :     my ($acc, $nam, $unk)=split /\s*\,\s*/, $piece;
266 :     foreach my $fig ($self->swiss_pir_ids($acc)) {
267 : redwards 1.20 print OUT join "\t", ($famac, $famname, $famtype, $acc, $fig), "\n";
268 : redwards 1.18 }
269 :     }
270 :     }
271 :     }
272 : redwards 1.14
273 :     =head2 swiss_pir_ids()
274 :    
275 : 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.
276 : redwards 1.14
277 :     =cut
278 :    
279 :     sub swiss_pir_ids {
280 :     my ($self, $id)=@_;
281 :     return () unless ($id);
282 : redwards 1.18 $id =~ s/^\s+//; $id =~ s/\s+$//; # trim off the whitespace
283 :    
284 : redwards 1.15 my @return=($fig->by_alias("uni|$id"));
285 : redwards 1.14 return @return if ($return[0]);
286 :    
287 : redwards 1.15 @return=($fig->by_alias("tr|$id"));
288 : redwards 1.14 return @return if ($return[0]);
289 :    
290 : redwards 1.15 @return=($fig->by_alias("sp|$id"));
291 : redwards 1.14 return @return if ($return[0]);
292 :    
293 :     return ();
294 :     }
295 : redwards 1.1
296 :     =head2 ss_by_id
297 :    
298 :     Generate a list of subsystems that a peg occurs in. This is a ; separated list.
299 :     This is a wrapper that removes roles and ignores essential things
300 :    
301 :     =cut
302 :    
303 :     sub ss_by_id {
304 :     my ($self, $peg)=@_;
305 :     my $ssout;
306 :     foreach my $ss (sort $fig->subsystems_for_peg($peg))
307 :     {
308 :     next if ($$ss[0] =~ /essential/i); # Ignore the Essential B-subtilis subsystems
309 :     $ssout.=$$ss[0]."; ";
310 :     }
311 :     $ssout =~ s/; $//;
312 :     return $ssout;
313 :     }
314 :    
315 : redwards 1.3 =head2 ss_by_homol
316 :    
317 :     Generate a list of subsystems that homologs of a peg occur in. This is a ; separated list.
318 :     This is also a wrapper around sims and ss, but makes everything unified
319 :    
320 :     =cut
321 :    
322 :     sub ss_by_homol {
323 :     my ($self, $peg)=@_;
324 :     return unless ($peg);
325 :     my ($maxN, $maxP)=(50, 1e-20);
326 :    
327 :     # find the sims
328 :     my @sims=$fig->sims($peg, $maxN, $maxP, 'fig');
329 :    
330 :     # we are only going to keep the best hit for each peg
331 :     # in a subsystem
332 :     my $best_ss_score; my $best_ss_id;
333 :     foreach my $sim (@sims)
334 :     {
335 :     my $simpeg=$$sim[1];
336 :     my $simscore=$$sim[10];
337 :     my @subsys=$fig->subsystems_for_peg($simpeg);
338 :     foreach my $ss (@subsys)
339 :     {
340 :     if (! defined $best_ss_score->{$$ss[0]}) {$best_ss_score->{$$ss[0]}=$simscore; $best_ss_id->{$$ss[0]}=$simpeg}
341 :     elsif ($best_ss_score->{$$ss[0]} > $simscore)
342 :     {
343 :     $best_ss_score->{$$ss[0]}=$simscore;
344 :     $best_ss_id->{$$ss[0]}=$simpeg;
345 :     }
346 :     }
347 :     }
348 :    
349 :     my $ssoutput=join "", (map {"$_ (".$best_ss_id->{$_}."), "} keys %$best_ss_id);
350 :    
351 :     $ssoutput =~ s/, $//;
352 :     return $ssoutput;
353 :     }
354 :    
355 :     =head2 tagvalue
356 :    
357 :     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)
358 :    
359 :     e.g. $values=raelib->tagvalue($peg, "PIRSF"); print join "\n", @$values;
360 :    
361 :     Returns an empty array if no tag/value appropriate.
362 :    
363 :     Just because I use this a lot I don't want to waste rewriting it.
364 :    
365 :     =cut
366 :    
367 :     sub tagvalue {
368 :     my ($self, $peg, $tag)=@_;
369 :     my @return;
370 :     my @attr=$fig->feature_attributes($peg);
371 :     foreach my $attr (@attr) {
372 : redwards 1.11 my ($gotpeg, $gottag, $val, $link)=@$attr;
373 : redwards 1.3 push @return, $val if ($gottag eq $tag);
374 :     }
375 :     return wantarray ? @return : join "; ", @return;
376 :     }
377 : redwards 1.1
378 : redwards 1.5 =head2 locations_on_contig
379 :    
380 :     Return the locations of a sequence on a contig.
381 :    
382 :     This will look for exact matches to a sequence on a contig, and return a reference to an array that has all the locations.
383 :    
384 :     my $locations=$raelib->locations_on_contig($genome, $contig, 'GATC', undef);
385 :     foreach my $bp (@$locations) { ... do something ... }
386 :    
387 :     first argument : genome number
388 :     second argument : contig name
389 :     third argument : sequence to look for
390 :     fourth argument : beginning position to start looking from (can be undef)
391 :     fifth argument : end position to stop looking from (can be undef)
392 :     sixth argument : check reverse complement (0 or undef will check forward, 1 or true will check rc)
393 :    
394 :     Note, the position is calculated before the sequence is rc'd
395 :    
396 :     =cut
397 :    
398 :     sub locations_on_contig {
399 :     my ($self, $genome, $contig, $sequence, $from, $to, $check_reverse)=@_;
400 :     my $return=[];
401 :    
402 :     # get the dna sequence of the contig, and make sure it is uppercase
403 :     my $contig_ln=$fig->contig_ln($genome, $contig);
404 :     return $return unless ($contig_ln);
405 :     unless ($from) {$from=1}
406 :     unless ($to) {$to=$contig_ln}
407 :     if ($from > $to) {($from, $to)=($to, $from)}
408 :     my $dna_seq=$fig->dna_seq($genome, $contig."_".$from."_".$to);
409 :     $dna_seq=uc($dna_seq);
410 :    
411 :     # if we want to check the rc, we actually rc the query
412 :     $sequence=$fig->reverse_comp($sequence) if ($check_reverse);
413 :     $sequence=uc($sequence);
414 :    
415 :     # now find all the matches
416 :     my $posn=index($dna_seq, $sequence, 0);
417 :     while ($posn > -1) {
418 :     push @$return, $posn;
419 :     $posn=index($dna_seq, $sequence, $posn+1);
420 :     }
421 :     return $return;
422 :     }
423 :    
424 :    
425 :     =head2 scrolling_org_list
426 :    
427 :     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.
428 :    
429 : overbeek 1.34 use like this push @$html, $raelib->scrolling_org_list($cgi, $multiple, $default, $limit);
430 : redwards 1.5
431 :     multiple selections will only be set if $multiple is true
432 :    
433 : overbeek 1.24 default will set a default to override (maybe) korgs
434 :    
435 : overbeek 1.34 limit is a reference to an array of organism IDs that you want to limit the list to.
436 :    
437 : redwards 1.5 =cut
438 :    
439 :     sub scrolling_org_list {
440 : overbeek 1.34 my ($self, $cgi, $multiple, $default, $limit)=@_;
441 : redwards 1.5 unless ($multiple) {$multiple=0}
442 :    
443 :     my @display = ( 'All', 'Archaea', 'Bacteria', 'Eucarya', 'Viruses', 'Environmental samples' );
444 :    
445 :     #
446 :     # Canonical names must match the keywords used in the DBMS. They are
447 :     # defined in compute_genome_counts.pl
448 :     #
449 :     my %canonical = (
450 :     'All' => undef,
451 :     'Archaea' => 'Archaea',
452 :     'Bacteria' => 'Bacteria',
453 :     'Eucarya' => 'Eukaryota',
454 :     'Viruses' => 'Virus',
455 :     'Environmental samples' => 'Environmental Sample'
456 :     );
457 :    
458 :     my $req_dom = $cgi->param( 'domain' ) || 'All';
459 :     my @domains = $cgi->radio_group( -name => 'domain',
460 :     -default => $req_dom,
461 :     -override => 1,
462 :     -values => [ @display ]
463 :     );
464 :    
465 :     my $n_domain = 0;
466 :     my %dom_num = map { ( $_, $n_domain++ ) } @display;
467 :     my $req_dom_num = $dom_num{ $req_dom } || 0;
468 :    
469 :     #
470 :     # Viruses and Environmental samples must have completeness = All (that is
471 :     # how they are in the database). Otherwise, default is Only "complete".
472 :     #
473 :     my $req_comp = ( $req_dom_num > $dom_num{ 'Eucarya' } ) ? 'All'
474 :     : $cgi->param( 'complete' ) || 'Only "complete"';
475 :     my @complete = $cgi->radio_group( -name => 'complete',
476 :     -default => $req_comp,
477 :     -override => 1,
478 :     -values => [ 'All', 'Only "complete"' ]
479 :     );
480 :     #
481 :     # Use $fig->genomes( complete, restricted, domain ) to get org list:
482 :     #
483 :     my $complete = ( $req_comp =~ /^all$/i ) ? undef : "complete";
484 :    
485 :     my $orgs; my $label;
486 :     @$orgs = $fig->genomes( $complete, undef, $canonical{ $req_dom } );
487 : overbeek 1.34
488 :     # limit the list of organisms to a selected few if required
489 :     if ($limit)
490 :     {
491 :     my %lim=map {($_=>1)} @$limit;
492 :     my $norg;
493 :     foreach my $o (@$orgs) {push @$norg, $o if ($lim{$o})}
494 :     $orgs=$norg;
495 :     }
496 : redwards 1.5
497 :     foreach (@$orgs) {
498 :     my $gs = $fig->genus_species($_);
499 : overbeek 1.35 if ($fig->genome_domain($_) ne "Environmental Sample")
500 :     {
501 :     my $gc=$fig->number_of_contigs($_);
502 :     $label->{$_} = "$gs ($_) [$gc contigs]";
503 :     }
504 :     else
505 :     {
506 :     $label->{$_} = "$gs ($_) ";
507 :     }
508 : redwards 1.5 }
509 :    
510 :     @$orgs = sort {$label->{$a} cmp $label->{$b}} @$orgs;
511 :    
512 :     my $n_genomes = @$orgs;
513 :    
514 :     return ( "<TABLE>\n",
515 :     " <TR>\n",
516 :     " <TD>",
517 : redwards 1.6 $cgi->scrolling_list( -name => 'korgs',
518 :     -values => $orgs,
519 :     -labels => $label,
520 :     -size => 10,
521 :     -multiple => $multiple,
522 : overbeek 1.24 -default => $default,
523 : redwards 1.5 ), $cgi->br,
524 :     "$n_genomes genomes shown ",
525 :     $cgi->submit( 'Update List' ), $cgi->reset, $cgi->br,
526 :     " </TD>",
527 :     " <TD>",
528 :     join( "<br>", "<b>Domain(s) to show:</b>", @domains), "<br>\n",
529 :     join( "<br>", "<b>Completeness?</b>", @complete), "\n",
530 :     "</TD>",
531 :     " </TR>\n",
532 :     "</TABLE>\n",
533 :     );
534 :     }
535 :    
536 : redwards 1.21
537 :     =head2 scrolling_subsys_list
538 :    
539 :     Create a scrolling list of all subsystems. Just like scrolling_org_list, this will make the list and allow you to select multiples.
540 :    
541 :     use like this
542 :    
543 :     push @$html, $raelib->scrolling_subsys_list($cgi, $multiple);
544 :    
545 :     =cut
546 :    
547 :     sub scrolling_subsys_list {
548 :     my ($self, $cgi, $multiple)=@_;
549 :     $multiple=0 unless (defined $multiple);
550 : redwards 1.22 my @ss=sort {uc($a) cmp uc($b)} $fig->all_subsystems();
551 : redwards 1.21 my $label;
552 :     # generate labels for the list
553 :     foreach my $s (@ss) {my $k=$s; $k =~ s/\_/ /g; $k =~ s/ / /g; $k =~ s/\s+$//; $label->{$s}=$k}
554 :     return $cgi->scrolling_list(
555 :     -name => 'subsystems',
556 :     -values => \@ss,
557 :     -labels => $label,
558 :     -size => 10,
559 :     -multiple=> $multiple,
560 :     );
561 :     }
562 :    
563 :     =head2 subsys_names_for_display
564 :    
565 :     Return a list of subsystem names for display. This will take a list as an argument and return a nice clean list for display.
566 :    
567 :     $raelib->subsys_names_for_display(@ss);
568 :     or
569 :     $raelib->subsys_names_for_display($fig->all_subsystems());
570 :    
571 :     =cut
572 :    
573 :     sub subsys_names_for_display {
574 :     my ($self, @ss)=@_;
575 :     foreach (@ss) {s/\_/ /g; 1 while (s/ / /g); s/\s+$//}
576 :     return @ss;
577 :     }
578 :    
579 : redwards 1.17 =head2 GenBank
580 : redwards 1.5
581 : redwards 1.17 This object will take a genome number and return a Bio::Seq::RichSeq object that has the whole genome
582 :     in GenBank format. This should be a nice way of getting some data out, but will probably be quite slow
583 :     at building the object.
584 : redwards 1.1
585 : redwards 1.17 Note that you need to call this with the genome name and the contig. This will then go through that contig.
586 : redwards 1.1
587 : redwards 1.17 Something like this should work
588 :    
589 :     foreach my $contig ($fig->all_contigs($genome)) {
590 :     my $seqobj=FIGRob->GenBank($genome, $contig);
591 :     # process the contig
592 :     }
593 :    
594 :     =cut
595 :    
596 :     sub GenBank {
597 :     my ($self, $genome, $contig)=@_;
598 :     my $gs=$fig->genus_species($genome);
599 :     return unless ($gs);
600 :     unless ($contig) {
601 :     print STDERR "You didn't provide a contig for $gs. I think that was a mistake. Sorry\n";
602 :     return;
603 :     }
604 :     my $len=$fig->contig_ln($genome, $contig);
605 :     unless ($len) {
606 :     print STDERR "$contig from $gs doesn't appear to have a length. Is it right?\n";
607 :     return;
608 :     }
609 :    
610 :    
611 :     # first find all the pegs ...
612 :     my $features; # all the features in the genome
613 :     my $allpegs; # all the pegs
614 :     my $translation; # all the protein sequences
615 :     foreach my $peg ($fig->pegs_of($genome)) {
616 :     my @location=$fig->feature_location($peg);
617 :     my $func=$fig->function_of($peg);
618 :     foreach my $loc (@location) {
619 :     $loc =~ /^(.*)\_(\d+)\_(\d+)$/;
620 :     my ($cg, $start, $stop)=($1, $2, $3);
621 :     next unless ($cg eq $contig);
622 :     # save this information for later
623 :     $features->{'peg'}->{$loc}=$func;
624 :     $allpegs->{'peg'}->{$loc}=$peg;
625 :     $translation->{'peg'}->{$loc}=$fig->get_translation($peg);
626 :     }
627 :     }
628 :     # ... and all the RNAs
629 :     foreach my $peg ($fig->rnas_of($genome)) {
630 :     my @location=$fig->feature_location($peg);
631 :     my $func=$fig->function_of($peg);
632 :     foreach my $loc (@location) {
633 :     $loc =~ /^(.*)\_(\d+)\_(\d+)$/;
634 :     my ($cg, $start, $stop)=($1, $2, $3);
635 :     next unless ($cg eq $contig);
636 :     # save this information for later
637 :     $features->{'rna'}->{$loc}=$func;
638 :     $allpegs->{'rna'}->{$loc}=$peg;
639 :     }
640 :     }
641 :    
642 :    
643 :     # now get all the contigs out
644 :     my $seq=$fig->dna_seq($genome, $contig."_1_".$len);
645 :     my $description = "Contig $contig from " . $fig->genus_species($genome);
646 :     my $sobj=Bio::Seq->new(
647 :     -seq => $seq,
648 :     -id => $contig,
649 :     -desc => $description,
650 :     -accession_number => $genome
651 :     );
652 :     foreach my $prot (keys %{$features->{'peg'}}) {
653 :     $prot =~ /^(.*)\_(\d+)\_(\d+)$/;
654 :     my ($cg, $start, $stop)=($1, $2, $3);
655 :     my $strand=1;
656 :     if ($stop < $start) {
657 :     ($stop, $start)=($start, $stop);
658 :     $strand=-1;
659 :     }
660 :    
661 :     my $feat=Bio::SeqFeature::Generic->new(
662 :     -start => $start,
663 :     -end => $stop,
664 :     -strand => $strand,
665 :     -primary => 'CDS',
666 :     -display_name => $allpegs->{'peg'}->{$prot},
667 :     -source_tag => 'the SEED',
668 :     -tag =>
669 :     {
670 :     db_xref => $allpegs->{'peg'}->{$prot},
671 :     note => 'Generated by the Fellowship for the Interpretation of Genomes',
672 :     function => $features->{'peg'}->{$prot},
673 :     translation => $translation->{'peg'}->{$prot}
674 :     }
675 :     );
676 :    
677 :     $sobj->add_SeqFeature($feat);
678 :     }
679 :    
680 :     foreach my $prot (keys %{$features->{'rna'}}) {
681 :     $prot =~ /^(.*)\_(\d+)\_(\d+)$/;
682 :     my ($cg, $start, $stop)=($1, $2, $3);
683 :     my $strand=1;
684 :     if ($stop < $start) {
685 :     ($stop, $start)=($start, $stop);
686 :     $strand=-1;
687 :     }
688 :    
689 :     my $feat=Bio::SeqFeature::Generic->new(
690 :     -start => $start,
691 :     -end => $stop,
692 :     -strand => $strand,
693 :     -primary => 'RNA',
694 :     -source_tag => 'the SEED',
695 :     -display_name => $allpegs->{'rna'}->{$prot},
696 :     -tag =>
697 :     {
698 :     db_xref => $allpegs->{'rna'}->{$prot},
699 :     note => 'Generated by the Fellowship for the Interpretation of Genomes',
700 :     function => $features->{'rna'}->{$prot},
701 :     }
702 :     );
703 :    
704 :     $sobj->add_SeqFeature($feat);
705 :     }
706 :     return $sobj;
707 :     }
708 :    
709 :     =head2 best_hit
710 :    
711 :     Returns the FIG id of the single best hit to a peg
712 :    
713 :     eg
714 :    
715 :     my $bh=$fr->best_hit($peg);
716 :     print 'function is ', scalar $fig->function_of($bh);
717 :    
718 :     =cut
719 :    
720 :     sub best_hit {
721 :     my ($self, $peg)=@_;
722 :     return unless ($peg);
723 :    
724 :     my ($maxN, $maxP)=(1, 1e-5);
725 :     my @sims=$fig->sims($peg, $maxN, $maxP, 'fig');
726 :     return ${$sims[0]}[1];
727 :     }
728 : redwards 1.1
729 : redwards 1.23
730 :     =head1 read_fasta
731 :    
732 :     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).
733 :    
734 :     Usage:
735 :     my $fasta=$raelib->read_fasta($file);
736 :     my ($fasta, $comments)=$raelib->read_fasta($file, 1);
737 :    
738 :     =cut
739 :    
740 :     sub read_fasta {
741 :     my ($self, $file, $keepcomments)=@_;
742 :     open (IN, $file) || die "Can't open $file";
743 :     my %f; my $t; my $s; my %c;
744 : overbeek 1.26 while (<IN>) {
745 : redwards 1.23 chomp;
746 :     if (/^>/) {
747 :     if ($s) {
748 :     $f{$t}=$s;
749 :     undef $s;
750 :     }
751 :     s/^>(\S+)\s*//;
752 :     $t=$1;
753 :     $c{$t}=$_ if ($_);
754 :     }
755 :     else {$s .= $_}
756 :     }
757 :     $f{$t}=$s;
758 :     if ($keepcomments) {return (\%f, \%c)}
759 :     else {return \%f}
760 :     }
761 :    
762 :     =head1 rc
763 :    
764 :     Reverse complement. It's too easy.
765 :    
766 :     =cut
767 :    
768 :     sub rc {
769 :     my ($self, $seq)=@_;
770 :     $seq=~tr/GATCgatc/CTAGctag/;
771 :     $seq = reverse $seq;
772 :     return $seq;
773 :     }
774 :    
775 : redwards 1.27
776 :     =head2 cookies
777 :    
778 :     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.
779 :    
780 :     If you do not pass any arguments the whole cookie will be returned.
781 :    
782 :     Use as follows:
783 :    
784 :     ($cookie, $data) = raelib->cookie($cgi, \%data);
785 :    
786 :     You do not need to pass in any data, in that case you will just get the cookie back
787 :    
788 :     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.
789 :    
790 :     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?
791 :    
792 :     =cut
793 :    
794 :     sub cookie {
795 :     my ($self, $cgi, $input)=@_;
796 :     return unless ($cgi);
797 :     $self->{'cookie'}=$cgi->cookie(-name=>"FIG") unless ($self->{'cookie'});
798 :    
799 :     # first, create a hash from the existing cookie data
800 :     my $cookie;
801 :     map {
802 :     my ($kname, $kvalue)=split /\=/, $_;
803 :     $cookie->{$kname}=$kvalue;
804 :     } split /\;/, $self->{'cookie'};
805 :    
806 :     if ($input)
807 :     {
808 :     # add the values that were passed in
809 :     map {$cookie->{FIG->clean_attribute_key($_)}=$input->{$_}} keys %$input;
810 :     # put everything back together and set the cookie
811 :     my $newcookie=join ";", map {$_ . "=" . $cookie->{$_}} keys %$cookie;
812 :     $self->{'cookie'}=$cgi->cookie(-name=>"FIG", -value=>$newcookie, -expires=>'+1y');
813 :     }
814 :    
815 :     return ($self->{'cookie'}, $cookie);
816 :     }
817 :    
818 :    
819 : overbeek 1.34 =head1 is_number
820 :    
821 :     returns 1 if the argument is a number, and 0 if not. This is taken directly from the perl cookbook.
822 :    
823 :     =cut
824 :    
825 :     sub is_number {
826 :     my ($self, $no)=@_;
827 :     return 1 if ($no =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # Perl cookbook, page 44
828 :     return 0;
829 :     }
830 :    
831 :    
832 : redwards 1.27
833 : overbeek 1.30 =head1 commify
834 :    
835 :     Put commas in numbers. I think this comes straight from the perl cookbook and is very useful for nice displays
836 :    
837 :     =cut
838 :    
839 :     sub commify {
840 :     my($self,$n) = @_;
841 :     my(@n) = ();
842 :     my($i);
843 :    
844 :     for ($i = (length($n) - 3); ($i > 0); $i -= 3)
845 :     {
846 :     unshift(@n,",",substr($n,$i,3));
847 :     }
848 :     unshift(@n,substr($n,0,$i+3));
849 :     return join("",@n);
850 :     }
851 :    
852 :    
853 : overbeek 1.31 =head1 tab2excel
854 :    
855 :     This is experimental as of May, 2006.
856 :    
857 : overbeek 1.37 +++++ PLEASE NOTE WELL ++++
858 :     +
859 :     + Make the HTML table before you use this method.If you use the tab2excel feature you are fine,
860 :     + but if you use the methods separately you MUST make the HTML table first, becuase this will screw with your code :)
861 :     +
862 :     ++++++++++++++++++++++++++++
863 :    
864 :    
865 :    
866 : overbeek 1.31 There are a couple of perl modules that allow you to write to excel files, and so I am trying out the idea of taking our standard $tab table respresentation that is used in HTML.pm and making an excel file that people could download. It seems like that would be a great tool for them to have.
867 :    
868 : overbeek 1.32 At the moment the excel modules are in my shared space on the CI machines, and so won't work in every seed installation. Therefore the $self->{'useexcel'} boolean is set at compile time if we successfully load the module.
869 : overbeek 1.31
870 :     The issues are:
871 :     1. creating the excel file
872 :     2. reading through @$tab and presenting the data
873 :     3. Checking @$tab because each element can be a reference to an array with color or formatting information
874 :    
875 :     Formatting
876 :    
877 :     A separate set of formats must be created for each color and font combination since the formats are applied at the end of the processing of the file.
878 :    
879 :    
880 :     Usage:
881 :     Note the usage is ALMOST the same as make_table, but not quite. First, options is a reference to a hash rather than the hash itself
882 :     and second, the additional option "filename" that is the filename to be written;
883 :    
884 :     $url = $raelib->tab2excel($col_hdrs, $tab, $title, $options, "filename");
885 :    
886 :     The filename will be created in $FIG_Config::temp. The extension .xls will be added to the filename if it is not present.
887 :    
888 :     Returns:
889 :     A link to the file in the format
890 :     <p><a href="...">filename</a> [Download Excel file]</p>
891 :    
892 : overbeek 1.32 Note that there are four separate methods:
893 :     1. tab2excel is the method for a single call from HTML::make_table
894 :     this will make an excel file, fill it, and return the link;
895 :     2. make_excel_workbook is the method that instantiates a file
896 :     3. make_excel_worksheet is the method that actually populates the file
897 :     this loads all the data into the excel file, but if you know what you are doing you can call this many times,
898 :     each with a different spreadsheet
899 :     4. close_excel_file
900 :     this closes the file and writes it. It is what returns the link.
901 :    
902 :     tab2excel is a wrapper for all three so that the method in HTML::make_table is really easy.
903 :     See subsys.cgi for a more complex involvement of this!
904 :    
905 : overbeek 1.31
906 :     =cut
907 :    
908 :     sub tab2excel {
909 :     my($self, $col_hdrs, $tab, $title, $options, $filename)=@_;
910 : overbeek 1.32 return "<p>Couldn't load Spreadsheet::WriteExcel</p>\n" unless ($self->{'useexcel'});
911 :     $self->make_excel_workbook($filename, $options);
912 :     $self->make_excel_worksheet($col_hdrs, $tab, $title);
913 :     return $self->close_excel_file();
914 :     }
915 : overbeek 1.31
916 : overbeek 1.32 =head1 make_excel_workbook
917 :    
918 :     This is the method that actually makes individual workbook. You should call this once, with the name of the file that you want it to be known by. The options are to set borders and whatnot.
919 :    
920 :     =cut
921 :    
922 :     sub make_excel_workbook {
923 :     my($self, $filename, $options)=@_;
924 :     return "<p>Couldn't load Spreadsheet::WriteExcel</p>\n" unless ($self->{'useexcel'});
925 :    
926 : overbeek 1.31 $filename =~ s/^.*\///; # remove any path information. We are going to only write to FIG_Config::temp
927 :     unless ($filename =~ /\.xls$/) {$filename .=".xls"}
928 : overbeek 1.37
929 :     # do we already have this file -- if so, just return that info
930 :     return 1 if ($self->{'excel_short_filename'} eq $filename); # don't do anything, just return the fact that we have the book made!
931 :    
932 :    
933 : overbeek 1.32 $self->{'excel_short_filename'}=$filename;
934 :     $self->{'excel_filename'}=$FIG_Config::temp."/$filename";
935 : overbeek 1.31
936 : overbeek 1.37
937 : overbeek 1.31 # Each excel file consists of the file, and then of worksheets from within the file. These are the tabs at the bottom of the screen
938 :     # that can be added with "Insert->new worksheet" from the menus.
939 :     # Create a new workbook called simple.xls and add a worksheet
940 : overbeek 1.32
941 :     # instantiate the workbook
942 :     $self->{'excel_workbook'}=Spreadsheet::WriteExcel->new($self->{'excel_filename'});
943 :     $self->{'excel_workbook'}->set_tempdir($FIG_Config::temp); # you don't have to do this, but it may speed things up and reduce memory load.
944 :    
945 :     # define the default formats
946 :     my $border = defined $options->{border} ? $options->{border} : 0;
947 :     $self->{'excel_format'}->{default}=$self->{'excel_workbook'}->add_format(border=>$border, size=>10);
948 :     }
949 :    
950 :    
951 :     =head1 make_excel_worksheet()
952 :    
953 :     This is the method that makes the separate sheets in the file. You can add as many of these as you want.
954 :    
955 :     =cut
956 :    
957 :     sub make_excel_worksheet {
958 :     my($self, $col_hdrs, $tab, $title)=@_;
959 :     return "<p>Couldn't load Spreadsheet::WriteExcel</p>\n" unless ($self->{'useexcel'});
960 :     unless (defined $self->{'excel_workbook'})
961 :     {
962 :     print STDERR "The workbook was not defined. Couldn't fill it in\n";
963 :     return;
964 :     }
965 :    
966 : overbeek 1.37 if (length($title) > 31) {$title=substr($title, 0, 31)}
967 : overbeek 1.32 my $worksheet = $self->{'excel_workbook'}->add_worksheet($title);
968 : overbeek 1.31 # The general syntax for output to an excel file is write($row, $column, $value, $format). Note that row and
969 :     # column are zero indexed
970 :    
971 :     # write the column headers
972 :     # define a new format that is bold
973 : overbeek 1.32 $self->{'excel_format'}->{header} = $self->{'excel_workbook'}->add_format();
974 :     $self->{'excel_format'}->{header}->copy($self->{'excel_format'}->{default});
975 :     $self->{'excel_format'}->{header}->set_bold();
976 : overbeek 1.31
977 :     for my $i (0 .. $#$col_hdrs)
978 :     {
979 : overbeek 1.32 my $cell=$self->clean_excel_cell($col_hdrs->[$i]);
980 :     $worksheet->write(0, $i, $cell, $self->{'excel_format'}->{header});
981 : overbeek 1.31 }
982 :    
983 :     # now loop through the table and write them out. Remember to break on array refs
984 :     my $row_idx=1;
985 :     my $col_idx=0;
986 :     foreach my $row (@$tab)
987 :     {
988 :     foreach my $cell (@$row)
989 :     {
990 : overbeek 1.32 my $useformat=$self->{'excel_format'}->{default};
991 : overbeek 1.36
992 :     # there is an approach to setting color using \@bgcolor. Oh well.
993 :     if ( $cell =~ /^\@([^:]+)\:(.*)$/ )
994 :     {
995 :     $cell=[$2, $1];
996 :     }
997 :    
998 : overbeek 1.31 if (ref($cell) eq "ARRAY")
999 :     {
1000 : overbeek 1.32 ($cell, $useformat)=$self->parse_cell($cell);
1001 : overbeek 1.31 }
1002 :    
1003 :     $cell=$self->clean_excel_cell($cell);
1004 :     # this is a botch, but in some circumstances we need to split the cell out. e.g. if it is a URL
1005 :     # in this case we have a reference to an array, and we'll use a slight modification on the process
1006 :     if (ref($cell) eq "ARRAY") {$worksheet->write($row_idx, $col_idx, @$cell, $useformat)}
1007 :     else {$worksheet->write($row_idx, $col_idx, $cell, $useformat)}
1008 :    
1009 :     # increment to the next column
1010 :     $col_idx++;
1011 :     }
1012 :     # new line, and start of line
1013 :     $row_idx++;
1014 :     $col_idx=0;
1015 : overbeek 1.32 }
1016 :     }
1017 :    
1018 :    
1019 :    
1020 :    
1021 :     =head1 close_excel_file()
1022 :    
1023 :     We must explicitly close the file before creating the link so that the file is written. This is also what returns the link
1024 :    
1025 :     =cut
1026 :    
1027 :     sub close_excel_file{
1028 :     my ($self)=@_;
1029 : overbeek 1.31
1030 : overbeek 1.32 # close the workbook. this writes the files
1031 :     $self->{'excel_workbook'}->close();
1032 : overbeek 1.31
1033 :     # now generate the link to return
1034 : overbeek 1.32 my $size=(stat($self->{'excel_filename'}))[7];
1035 : overbeek 1.31 $size=int($size/1000);
1036 : overbeek 1.32 my $link="<p><a href=\"".$fig->temp_url."/".$self->{'excel_short_filename'}.'">'.
1037 :     $self->{'excel_short_filename'}."</a> [Download table in Excel format. $size kb]</p>\n";
1038 : overbeek 1.31 return $link;
1039 : overbeek 1.32
1040 : overbeek 1.31 }
1041 :    
1042 :    
1043 :    
1044 :    
1045 : overbeek 1.30
1046 :    
1047 : redwards 1.27
1048 : overbeek 1.31
1049 :    
1050 :     =head2 parse_cell()
1051 :    
1052 :     A method to take the cell from the table where there is some formatting information and figure out what we know. Return the data and the format.
1053 :    
1054 :     Requires the cell and the current $format.
1055 :    
1056 :     When applied to <td> the default formats that we'll deal with at the moment are
1057 :     align=
1058 :     background-color=
1059 :     color=
1060 :     bgcolor=
1061 :    
1062 :     Colors are funky in excel because it only has a limited palette. We rename colors as needed, and then save those so that we can use them again. We're only allowed 55 colors in excel (numbered 8..63). Because its a little stupid to mess with black and white and so on, I ignore those, and also start renumbering at color number 20, giving us 43 different colors.
1063 :    
1064 : overbeek 1.32 The reference to the hash excel_color has the custom excel colors stored in it for a few colors, and others are added to it.
1065 : overbeek 1.31
1066 :     =cut
1067 :    
1068 :     sub parse_cell {
1069 : overbeek 1.32 my ($self, $arr)=@_;
1070 :     return ($arr, $self->{'excel_format'}->{default}) unless (ref($arr) eq "ARRAY");
1071 : overbeek 1.31 my ($cell, $tag)=@$arr;
1072 :     $tag =~ s/\'/"/g; # this just makes it easier to parse the things like align='center' and align="center" that are both valid
1073 :    
1074 :     # we are going to define a series of formats that we can apply, this will have a key that is
1075 : overbeek 1.32 # th.center.bgcolor.fgcolor. Then if we already have that, we can use it, if not, we'll define it
1076 : overbeek 1.31
1077 : overbeek 1.32 my ($th, $center, $bgcolor, $fgcolor)=(undef, undef, undef, undef);
1078 : overbeek 1.31
1079 : overbeek 1.32 if ($tag =~ /^th/) {$th=1} # it is a header cell so we should make it bold
1080 : overbeek 1.31 if ($tag =~ /align\=\"(.*?)\"/i) {$center=$1}
1081 : overbeek 1.32
1082 :     # get rid of white tags because I don't care about them
1083 :     $tag =~ s/color\=.\#FFFFFF/ /ig;
1084 :    
1085 : overbeek 1.31 if ($tag =~ /background-color\=\"(.*?)\"/i || $tag =~ /bgcolor\=\"(.*?)\"/i)
1086 :     {
1087 :     my $color=$1;
1088 : overbeek 1.32 if (!defined $self->{'excel_color'}->{$color})
1089 : overbeek 1.31 {
1090 :     # find out the last custom color used and increment it
1091 :     my $max=19; # we are not going to use a color less than 20
1092 : overbeek 1.33 foreach my $k (keys %{$self->{'excel_color'}}) {($self->{'excel_color'}->{$k} > $max) ? ($max=$self->{'excel_color'}->{$k}) :1}
1093 : overbeek 1.31 $max++;
1094 : overbeek 1.32 $self->{'excel_color'}->{$color}=$self->{'excel_workbook'}->set_custom_color($max, $color);
1095 : overbeek 1.31 }
1096 : overbeek 1.32 $bgcolor=$self->{'excel_color'}->{$color};
1097 : overbeek 1.31 }
1098 : overbeek 1.32 elsif ($tag =~ /color\=\"(.*?)\"/i || $tag =~ /color\=\'(.*?)\'/i)
1099 : overbeek 1.31 {
1100 :     my $color=$1;
1101 : overbeek 1.32 if (!defined $self->{'excel_color'}->{$color})
1102 : overbeek 1.31 {
1103 :     # find out the last custom color used and increment it
1104 :     my $max=19; # we are not going to use a color less than 20
1105 : overbeek 1.33 foreach my $k (keys %{$self->{'excel_color'}}) {($self->{'excel_color'}->{$k} > $max) ? ($max=$self->{'excel_color'}->{$k}) :1}
1106 : overbeek 1.31 $max++;
1107 : overbeek 1.32 $self->{'excel_color'}->{$color}=$self->{'excel_workbook'}->set_custom_color($max, $color);
1108 : overbeek 1.31 }
1109 : overbeek 1.32 $fgcolor=$self->{'excel_color'}->{$color};
1110 : overbeek 1.31 }
1111 :    
1112 : overbeek 1.32 if (!defined $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor})
1113 : overbeek 1.31 {
1114 : overbeek 1.32 $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}=$self->{'excel_workbook'}->add_format();
1115 :     if ($th) {$self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}->copy($self->{'excel_format'}->{header})}
1116 :     else {$self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}->copy($self->{'excel_format'}->{default})}
1117 :     $center && $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}->set_align($center);
1118 :     $bgcolor && $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}->set_bg_color($bgcolor);
1119 :     $fgcolor && $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor}->set_color($fgcolor);
1120 : overbeek 1.31 }
1121 :    
1122 : overbeek 1.32 return ($cell, $self->{'excel_format'}->{$th.$center.$bgcolor.$fgcolor});
1123 : overbeek 1.31 }
1124 :    
1125 :    
1126 :     =head1 clean_excel_cell
1127 :    
1128 :     Process the cells to remove &nbsp; and also convert relative URLs to full URLs
1129 :    
1130 :     =cut
1131 :    
1132 :     sub clean_excel_cell {
1133 :     my ($self, $cell)=@_;
1134 :     if ($cell =~ /^\s*\&nbsp\;\s*$/) {$cell=undef} # ignore white space
1135 : overbeek 1.32
1136 :     # some cells have something like this:
1137 :     # <a onMouseover="javascript:if(!this.tooltip) this.tooltip=new Popup_Tooltip(this,'Role of BCAT','Branched-chain amino acid aminotransferase (EC 2.6.1.42)','','','','');this.tooltip.addHandler(); return false;" >BCAT</a>
1138 :     # we don't want those, but we do want the ones that have a real url hidden here.
1139 :     # so remove the mouseover part, and then see what is left
1140 :     if ($cell =~ s/onMouseover\=\".*?\"//)
1141 :     {
1142 :     if ($cell =~ s/\<a\s+>//i) {$cell =~ s/\<\/a>//i}
1143 :     }
1144 :    
1145 : overbeek 1.31 if ($cell =~ /\<a href=.(.*?).>(.*)<\/a>/)
1146 :     {
1147 :     # this is tricky because if the cell is a url then we need two separate things, the url and the link name
1148 :     my ($url, $link)=($1, $2);
1149 : overbeek 1.32 $url =~ s/^\.{1,2}\///; # remove notation of ./ and ../
1150 : overbeek 1.31 unless ($url =~ /^http/) {$url=$FIG_Config::cgi_url."/$url"}
1151 : overbeek 1.32 # this sucks as excel can only handle one link per cell, so we remove the other links. At the moment users will have to deal with that.
1152 :     $link =~ s/\<.*?\>//g;
1153 : overbeek 1.31 $cell=[$url, $link];
1154 :     }
1155 : overbeek 1.32 elsif ($cell =~ /<input type/)
1156 :     {
1157 :     if ($cell =~ /value='(.*?)'/) {$cell = $1}
1158 :     elsif ($cell =~ /value="(.*?)"/) {$cell = $1}
1159 :     }
1160 :     else
1161 :     {
1162 :     # this is all the html that I don't know what to do with, like <input type=...>
1163 :     $cell =~ s/\<.*?\>//g;
1164 :     }
1165 : overbeek 1.31 return $cell;
1166 :     }
1167 :    
1168 :    
1169 :    
1170 :    
1171 : redwards 1.1 1;
1172 : redwards 1.17

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3