TFBS/transfac_pro.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 95.8% 46 / 48 CC 1 SCAR 0.2
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« TFBS.pm ↑ Summary
1 # $Id: transfac_pro.pm,v 1.15 2006/08/12 11:00:03 sendu Exp $
2 #
3 # BioPerl module for Bio::DB::TFBS::transfac_pro
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Sendu Bala <bix@sendu.me.uk>
8 #
9 # Copyright Sendu Bala
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 Bio::DB::TFBS::transfac_pro - An implementation of Bio::DB::TFBS
18 which uses local flat files for transfac pro
19  
20 =head1 SYNOPSIS
21  
22 use Bio::DB::Taxonomy;
23  
24 my $db = new Bio::DB::Taxonomy(-source => 'transfac_pro'
25 -dat_dir => $directory);
26  
27 # we're interested in the gene P5
28 my ($gene_id) = $db->get_gene_ids(-name => 'P5'); # G000001
29  
30 # we want all the transcription factors that bind to our gene
31 my @factor_ids = $db->get_factor_ids(-gene => $gene_id);
32  
33 # get info about those TFs
34 foreach my $factor_id (@factor_ids) {
35 my $factor = $db->get_factor($factor_id);
36 my $name = $factor->universal_name;
37 # etc. - see Bio::Map::TranscriptionFactor, eg. find out where it binds
38 }
39  
40 # get a matrix
41 my $matrix = $db->get_matrix('M00001');
42  
43 # get a binding site sequence
44 my $seq = $db->get_site('R00001');
45  
46 =head1 DESCRIPTION
47  
48 This is an implementation which uses local flat files and the DB_File
49 module RECNO data structures to manage a local copy of the Transfac Pro TFBS
50 database.
51  
52 Required database files require a license which can be obtained via
53 http://www.biobase-international.com/pages/index.php?id=170
54  
55 Within the linux installation tarball you will find a cgibin tar ball, and
56 inside that is a data directory containing the .dat files needed by this
57 module. Point to that data directory with -dat_dir
58  
59 =head1 FEEDBACK
60  
61 =head2 Mailing Lists
62  
63 User feedback is an integral part of the evolution of this and other
64 Bioperl modules. Send your comments and suggestions preferably to
65 the Bioperl mailing list. Your participation is much appreciated.
66  
67 bioperl-l@bioperl.org - General discussion
68 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
69  
70 =head2 Support
71  
72 Please direct usage questions or support issues to the mailing list:
73  
74 I<bioperl-l@bioperl.org>
75  
76 rather than to the module maintainer directly. Many experienced and
77 reponsive experts will be able look at the problem and quickly
78 address it. Please include a thorough description of the problem
79 with code and data examples if at all possible.
80  
81 =head2 Reporting Bugs
82  
83 Report bugs to the Bioperl bug tracking system to help us keep track
84 of the bugs and their resolution. Bug reports can be submitted via
85 the web:
86  
87 https://github.com/bioperl/bioperl-live/issues
88  
89 =head1 AUTHOR - Sendu Bala
90  
91 Email bix@sendu.me.uk
92  
93 =head1 CONTRIBUTORS
94  
95 Based on Bio::DB::Taxonomy::flatfile by Jason Stajich
96  
97 =head1 APPENDIX
98  
99 The rest of the documentation details each of the object methods.
100 Internal methods are usually preceded with a _
101  
102 =cut
103  
104 # Let the code begin...
105  
106 package Bio::DB::TFBS::transfac_pro;
107 $Bio::DB::TFBS::transfac_pro::VERSION = '1.7.3';
108 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 ▶use Bio::Annotation::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use Bio::Annotation::SimpleValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use Bio::LocatableSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use Bio::SimpleAlign;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use Bio::Matrix::PSM::SiteMatrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use Bio::Map::GeneMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶use Bio::Map::TranscriptionFactor;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶use Bio::Map::Position;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 1 ▶use Bio::Map::Relative;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 0 ▶use DB_File;
Subroutine BEGIN: called CC=1 SCAR=2.6
120  
121 use constant SEPARATOR => ':!:';
122 use constant INTERNAL_SEPARATOR => '!:!';
123  
124 $DB_BTREE->{'flags'} = R_DUP; # allow duplicate values in DB_File BTREEs
125  
126 use base qw(Bio::DB::TFBS);
127  
128 =head2 new
129  
130 Title : new
131 Usage : my $obj = new Bio::DB::TFBS::transfac_pro();
132 Function: Builds a new Bio::DB::TFBS::transfac_pro object
133 Returns : an instance of Bio::DB::TTFBS::transfac_pro
134 Args : -dat_dir => name of directory where Transfac Pro .dat files
135 (required to initially build indexes)
136 -tax_db => Bio::DB::Taxonomy object, used when initially building
137 indexes, gives better results for species information
138 but not required.
139 -index_dir => name of directory where index files should be created
140 or already exist. (defaults to -dat_dir, required if
141 -dat_dir not supplied)
142 -force => 1 replace current indexes even if they exist
143  
144 =cut
145  
146 sub new {
147 my ($class, @args) = @_;
148  
149 my $self = $class->SUPER::new(@args);
150  
151 my ($dat_dir, $index_dir, $tax_db, $force) = $self->_rearrange([qw(DAT_DIR INDEX_DIR TAX_DB FORCE)], @args);
152 $self->throw("At least one of -dat_dir and -index_dir must be supplied") unless ($dat_dir || $index_dir);
153  
154 $self->index_directory($index_dir || $dat_dir);
155 $self->{_tax_db} = $tax_db if $tax_db;
156  
157 if ($dat_dir) {
158 $self->_build_index($dat_dir, $force);
159 }
160  
161 $self->_db_connect;
162 return $self;
163 }
164  
165 =head2 Bio::DB::TFBS Interface implementation
166  
167 =cut
168  
169 sub _get_ids {
170 my ($self, $dat, @args) = @_;
171 @args % 2 == 0 || $self->throw("Must provide key => value pairs");
172 my $hash = $self->{$dat} || $self->throw("Unknown .dat type '$dat'");
173  
174 if (@args) {
175 # get a subset corresponding to args
176 my @final;
177 my %args = @args;
178 my $multiple = 0;
179 while (my ($type, $value) = each %args) {
180 unless ($value) {
181 $self->warn("Arguement '$type' has no value, ignored");
182 next;
183 }
184 $type =~ s/-//;
185 $type = lc($type);
186 my $converter = $hash->{$type};
187 unless ($converter) {
188 $self->warn("Unknown search type '$type' for .dat type '$dat'");
189 next;
190 }
191  
192 my @ids = $converter->get_dup($value);
193 unless (@ids) {
194 @ids = $converter->get_dup(lc($value));
195 }
196  
197 if ($multiple) {
198 # we can have multiple types given at once, find the ids that
199 # satisfy all criteria
200 @final || return;
201 my %final = map { $_ => 1 } @final;
202 @final = grep { $final{$_} } @ids;
203 }
204 else {
205 @final = @ids;
206 $multiple++;
207 }
208 }
209  
210 return @final;
211 }
212 else {
213 # get them all
214 my $db_file_hash = $self->{$dat}->{id};
215  
216 my ($key, $prev_key, $value) = ('_!_', '!_!');
217 my @ids;
218 while (1) {
219 $db_file_hash->seq($key, $value, R_NEXT);
220 last if $prev_key eq $key;
221 push(@ids, $value); # confusing? when creating objects we store
222 # $value as accession and $key as id, but from
223 # this method we return $value as id given $id!
224 $prev_key = $key;
225 }
226  
227 return @ids;
228 }
229 }
230  
231 =head2 get_reference
232  
233 Title : get_reference
234 Usage : my $ref = $obj->get_reference($id);
235 Function: Get a literature reference.
236 Returns : Bio::Annotation::Reference
237 Args : string - a reference id ('RE...')
238  
239 =cut
240  
241 sub get_reference {
242 my ($self, $id) = @_;
243 $id || return;
244 my $data = $self->{reference}->{data}->{$id} || return;
245 my @data = split(SEPARATOR, $data);
246  
247 return Bio::Annotation::Reference->new(-pubmed => $data[0],
248 -authors => $data[1],
249 -title => $data[2],
250 -location => $data[3] );
251 }
252  
253 =head2 get_genemap
254  
255 Title : get_genemap
256 Usage : my $map = $obj->get_genemap($id);
257 Function: Get a GeneMap for a gene.
258 Returns : Bio::Map::GeneMap
259 Args : string - a gene id ('G...'), and optionally int (number of bp
260 upstream)
261  
262 =cut
263  
264 sub get_genemap {
265 my ($self, $id, $upstream) = @_;
266 $id || return;
267 return $self->{got_map}->{$id} if defined $self->{got_map}->{$id};
268 $upstream ||= 1000;
269 my $data = $self->{gene}->{data}->{$id} || return;
270 my @data = split(SEPARATOR, $data);
271  
272 # accession = id name description species_tax_id_or_raw_string
273 my $taxon = $self->{_tax_db} ? $self->{_tax_db}->get_taxon($data[3]) || $data[3] : $data[3];
274 my $map = Bio::Map::GeneMap->get(-uid => $id,
275 -gene => $data[1],
276 -species => $taxon,
277 -description => $data[2],
278 -upstream => $upstream);
279 $self->{got_map}->{$id} = $map; # prevents infinite recurse when we call get_factor below
280  
281 # spawn all the factors that belong on this gene map
282 # get_factor_ids(-gene => ...) only works for genes that encode factors;
283 # have to go via sites
284 foreach my $sid ($self->get_site_ids(-gene => $id)) {
285 foreach my $fid ($self->get_factor_ids(-site => $sid)) {
286 # it is quite deliberate that we deeply recurse to arrive at the
287 # correct answer, which involves pulling in most of the database
288 no warnings "recursion";
289 $self->get_factor($fid);
290 }
291 }
292  
293 return $map;
294 }
295  
296 =head2 get_seq
297  
298 Title : get_seq
299 Usage : my $seq = $obj->get_seq($id);
300 Function: Get the sequence of a site. The sequence will be annotated with the
301 the tags 'relative_start', 'relative_end', 'relative_type' and
302 'relative_to'.
303 Returns : Bio::Seq
304 Args : string - a site id ('R...')
305  
306 =cut
307  
308 sub get_seq {
309 my ($self, $id) = @_;
310 $id || return;
311 my $data = $self->{site}->{data}->{$id} || return;
312 my @data = split(SEPARATOR, $data);
313  
314 my $seq = Bio::Seq->new(-seq => $data[2],
315 -accession_number => $id,
316 -description => $data[6] ? 'Genomic sequence' : 'Consensus or artificial sequence',
317 -id => $data[0],
318 -strand => 1,
319 -alphabet => $data[7] || 'dna',
320 -species => $data[6]);
321  
322 my $annot = $seq->annotation;
323 my $sv = Bio::Annotation::SimpleValue->new(-tagname => 'relative_start', -value => $data[4] || 1);
324 $annot->add_Annotation($sv);
325 $sv = Bio::Annotation::SimpleValue->new(-tagname => 'relative_end', -value => $data[5] || ($data[4] || 1 + length($data[2]) - 1));
326 $annot->add_Annotation($sv);
327 $sv = Bio::Annotation::SimpleValue->new(-tagname => 'relative_type', -value => $data[3] || 'artificial');
328 $annot->add_Annotation($sv);
329 $sv = Bio::Annotation::SimpleValue->new(-tagname => 'relative_to', -value => $data[1]);
330 $annot->add_Annotation($sv);
331  
332 return $seq;
333 }
334  
335 =head2 get_fragment
336  
337 Title : get_fragment
338 Usage : my $seq = $obj->get_fragment($id);
339 Function: Get the sequence of a fragment.
340 Returns : Bio::Seq
341 Args : string - a site id ('FR...')
342  
343 =cut
344  
345 sub get_fragment {
346 my ($self, $id) = @_;
347 $id || return;
348 my $data = $self->{fragment}->{data}->{$id} || return;
349 my @data = split(SEPARATOR, $data);
350  
351 # accession = id gene_id1 gene_id2 species_tax_id_or_raw_string sequence source
352 return new Bio::Seq( -seq => $data[4],
353 -accession_number => $id,
354 -description => 'Between genes '.$data[1].' and '.$data[2],
355 -species => $data[3],
356 -id => $data[0],
357 -alphabet => 'dna' );
358 }
359  
360 =head2 get_matrix
361  
362 Title : get_matrix
363 Usage : my $matrix = $obj->get_matrix($id);
364 Function: Get a matrix that describes a binding site.
365 Returns : Bio::Matrix::PSM::SiteMatrix
366 Args : string - a matrix id ('M...'), optionally a sequence string from
367 which base frequencies will be calculated for the matrix model
368 (default 0.25 each)
369  
370 =cut
371  
372 sub get_matrix {
373 my ($self, $id, $seq) = @_;
374 $id || return;
375 $seq ||= 'atgc';
376 $seq = lc($seq);
377 my $data = $self->{matrix}->{data}->{$id} || return;
378 my @data = split(SEPARATOR, $data);
379 $data[4] || $self->throw("Matrix data missing for $id");
380  
381 my ($a, $c, $g, $t);
382 foreach my $position (split(INTERNAL_SEPARATOR, $data[4])) {
383 my ($a_count, $c_count, $g_count, $t_count) = split("\t", $position);
384 push(@{$a}, $a_count);
385 push(@{$c}, $c_count);
386 push(@{$g}, $g_count);
387 push(@{$t}, $t_count);
388 }
389  
390 # our psms include a simple background model so we can use
391 # sequence_match_weight() if desired
392 my $a_freq = ($seq =~ tr/a//) / length($seq);
393 my $c_freq = ($seq =~ tr/c//) / length($seq);
394 my $g_freq = ($seq =~ tr/g//) / length($seq);
395 my $t_freq = ($seq =~ tr/t//) / length($seq);
396  
397 my $psm = Bio::Matrix::PSM::SiteMatrix->new(-pA => $a,
398 -pC => $c,
399 -pG => $g,
400 -pT => $t,
401 -id => $data[0],
402 -accession_number => $id,
403 -sites => $data[3],
404 -width => scalar(@{$a}),
405 -correction => 1,
406 -model => { A => $a_freq, C => $c_freq, G => $g_freq, T => $t_freq } );
407  
408 #*** used to make a Bio::Matrix::PSM::Psm and add references, but it
409 # didn't seem worth it. You can get references from the database by:
410 #foreach my $ref_id ($db->get_reference_ids(-matrix => $id)) {
411 # my $ref = $db->get_reference($ref_id);
412 #}
413  
414 return $psm;
415 }
416  
417 =head2 get_aln
418  
419 Title : get_aln
420 Usage : my $aln = $obj->get_aln($id);
421 Function: Get the alignment that was used to generate a matrix. Each sequence
422 in the alignment will have an accession_number corresponding to the
423 Transfac site id, and id() based on that but unique within the
424 alignment.
425 Returns : Bio::SimpleAlign
426 Args : string - a matrix id ('M...'), optionally true to, when a matrix
427 lists no sequences, search for sequences via the matrix's factors,
428 picking the sites that best match the matrix
429  
430 =cut
431  
432 my %VALID_STRAND = map {$_ => 1} qw(-1 0 1);
433  
434 sub get_aln {
435 my ($self, $id, $via_factors) = @_;
436 $id || return;
437 my $data = $self->{matrix}->{data}->{$id} || $self->throw("matrix '$id' had no data in DB_File");
438 my @data = split(SEPARATOR, $data);
439  
440 if (! $data[5] && $via_factors) {
441 # This is a matrix with no site sequences given in matrix.dat.
442 # Find some matching site sequences via factors.
443  
444 # First, check its factors for sites
445 my %site_seqs;
446 my %factor_ids;
447 foreach my $factor_id ($self->get_factor_ids(-matrix => $id)) {
448 $factor_ids{$factor_id} = 1;
449 foreach my $site_id ($self->get_site_ids(-factor => $factor_id)) {
450 next if defined $site_seqs{$site_id};
451 my $seq = $self->get_seq($site_id);
452  
453 # skip sites that have no sequence, or have IUPAC symbols in
454 # their sequence (most probably the 'consensus' sequence itself
455 # that was used to make and exactly corresponds to the matrix)
456 my $seq_str = $seq->seq || next;
457 $seq_str =~ /[MRWSYKVHDB]/ and next;
458  
459 $site_seqs{$site_id} = $seq;
460 }
461 }
462 my @seqs = values %site_seqs;
463  
464 if (@seqs > 1) {
465 # pick the sub-seqs that match to the matrix with the best scores
466 my $matrix = $self->get_matrix($id);
467 my $desired_sequences = $matrix->sites;
468 return if @seqs < $desired_sequences;
469  
470 my $desired_length = $matrix->width;
471 my %best_seqs;
472 foreach my $seq (@seqs) {
473 my $for_str = $seq->seq;
474 next if length($for_str) < $desired_length;
475 my $rev_str = $seq->revcom->seq;
476  
477 my $best_score = 0;
478 my $best_subseq = '';
479 my $best_i = 0;
480 my $best_subseq_caps = 0;
481 my $best_revcom;
482 my $revcom = 0;
483 foreach my $seq_str ($for_str, $rev_str) {
484 for my $i (0..(length($seq_str) - $desired_length)) {
485 my $subseq = substr($seq_str, $i, $desired_length);
486 $subseq =~ s/[^ACGTacgt]//g; # can only score atcg
487 next unless length($subseq) == $desired_length; # short or 0-length seqs could get the highest scores!
488 my $score = $matrix->sequence_match_weight($subseq);
489  
490 # caps represent the author-chosen bit of a site
491 # sequence so we would prefer to choose a subseq that
492 # contains it
493 my $caps = $subseq =~ tr/ACGT//;
494  
495 #*** (don't know why numeric == fails for comparing
496 # scores, when the string eq works)
497 if ($score > $best_score || ("$score" eq "$best_score" && $caps > $best_subseq_caps)) {
498 $best_score = $score;
499 $best_subseq_caps = $caps;
500 $best_subseq = $subseq;
501 $best_i = $i;
502 $best_revcom = $revcom;
503 }
504 }
505 $revcom++;
506 }
507  
508 if ($best_score) {
509 $best_seqs{$seq->accession_number} = [$best_subseq, $seq->accession_number, ($best_i + 1), $revcom ? -1 : 1, $best_score];
510 }
511 }
512 my @sorted = sort { $best_seqs{$b}->[-1] <=> $best_seqs{$a}->[-1] } keys %best_seqs;
513 return if @sorted < $desired_sequences;
514 splice(@sorted, $desired_sequences);
515 my %wanted = map { $_ => 1 } @sorted;
516  
517 my @site_data;
518 foreach my $seq (@seqs) {
519 next unless exists $wanted{$seq->accession_number};
520 my @data = @{$best_seqs{$seq->accession_number}};
521 pop(@data);
522 push(@site_data, join('_', @data));
523 }
524  
525 $data[5] = join(INTERNAL_SEPARATOR, @site_data);
526 $self->{matrix}->{data}->{$id} = join(SEPARATOR, @data);
527 }
528 }
529 $data[5] || return;
530  
531 my @blocks = split(INTERNAL_SEPARATOR, $data[5]);
532  
533 # append gap chars to all sequences to make them the same length
534 # (applies to sequences found via factors, presumably, since we already
535 # do this for matrix alignments in transfac_pro.pm)
536 my $longest = 0;
537 foreach (@blocks) {
538 my ($seq) = split('_', $_);
539 my $length = length($seq);
540 if ($length > $longest) {
541 $longest = $length;
542 }
543 }
544 foreach my $i (0..$#blocks) {
545 my $block = $blocks[$i];
546 my ($seq, $seq_id) = split('_', $block);
547 my $length = length($seq);
548 if ($length < $longest) {
549 my $orig_seq = $seq;
550 $seq .= '-'x($longest - $length);
551 $block =~ s/^${orig_seq}_/${seq}_/;
552 $blocks[$i] = $block;
553 }
554 }
555  
556 # build the alignment
557 my $aln = Bio::SimpleAlign->new(-source => 'transfac_pro');
558 my %done_ids;
559 foreach (@blocks) {
560 my ($seq, $seq_acc, $start, $strand) = split('_', $_);
561  
562 $self->throw("Invalid strand $strand found in block $_")
563 unless exists $VALID_STRAND{$strand};
564 # we can get back multiple different subparts of the same site (sequence),
565 # so $seq_acc isn't unique across this loop. Can't use it as the seq id
566 # of the alignment (ids must be unique in SimpleAlign), so we
567 # uniquify the id and store the original id as the accession_number
568 my $seq_id;
569 $done_ids{$seq_acc}++;
570 if ($done_ids{$seq_acc} > 1) {
571 $seq_id = $seq_acc.'_'.$done_ids{$seq_acc};
572 }
573 else {
574 $seq_id = $seq_acc;
575 }
576  
577 my $gaps = $seq =~ tr/-//;
578 my $length = length($seq) - $gaps;
579 $self->throw("seq '$seq_id' for matrix '$id' had seq '$seq'") unless $length;
580 $aln->add_seq(Bio::LocatableSeq->new(-seq => $seq,
581 -id => $seq_id,
582 -accession_number => $seq_acc,
583 -start => $start,
584 -end => $start + $length - 1,
585 -strand => $strand));
586 }
587 $aln->id($id);
588 # could also store score? of?
589  
590 return $aln;
591 }
592  
593 =head2 get_factor
594  
595 Title : get_factor
596 Usage : my $factor = $obj->get_factor($id);
597 Function: Get the details of a transcription factor.
598 Returns : Bio::Map::TranscriptionFactor
599 Args : string - a factor id ('T...')
600  
601 =cut
602  
603 sub get_factor {
604 my ($self, $id) = @_;
605 $id || return;
606 return $self->{got_factor}->{$id} if defined $self->{got_factor}->{$id};
607 my $data = $self->{factor}->{data}->{$id} || return;
608 my @data = split(SEPARATOR, $data);
609  
610 # accession = id name species sequence
611 my $tf = Bio::Map::TranscriptionFactor->get(-id => $id,
612 -universal_name => $data[1]);
613 #*** not sure what to do with species and sequence, since we don't want to
614 # confuse the idea that a TF is a general thing that could bind to any
615 # species... then again, you might want to model species-specific variants
616 # of a TF with different binding abilities...
617 #*** idea of having inclusion and exclusion species so you can prevent/
618 # ignore a tf that binds to the wrong species (a species that doesn't even
619 # have the tf), and associating sequence with each species/tf combo so you
620 # can see how diverged the tf is and make assumptions about site difference
621 # allowance
622  
623 # place it on all its genemaps
624 foreach my $sid ($self->get_site_ids(-factor => $id)) {
625 my $s_data = $self->{site}->{data}->{$sid} || next;
626 my @s_data = split(SEPARATOR, $s_data);
627  
628 # accession = id gene_id sequence relative_to first_position last_position species_tax_id_or_raw_string
629 $s_data[1] || next; # site isn't relative to a gene, meaningless
630 $s_data[4] || next; # don't know where its supposed to be, can't model it
631 $s_data[5] ||= $s_data[4] + ($s_data[2] ? length($s_data[2]) - 1 : 0);
632  
633 # it is quite deliberate that we deeply recurse to arrive at the
634 # correct answer, which involves pulling in most of the database
635 no warnings "recursion";
636 my $gene_map = $self->get_genemap($s_data[1]) || next;
637 return $self->{got_factor}->{$id} if defined $self->{got_factor}->{$id};
638  
639 #*** not always relative to gene start...
640 # we need Bio::Map::Gene s to have some default tss and atg positions
641 # that we can be relative to
642 my $rel = Bio::Map::Relative->new(-element => $gene_map->gene, -description => $s_data[3]);
643 Bio::Map::Position->new(-map => $gene_map, -element => $tf, -start => $s_data[4], -end => $s_data[5], -relative => $rel);
644 }
645  
646 $self->{got_factor}->{$id} = $tf;
647 return $tf;
648 }
649  
650 # since get_factor() is uncertain, just have direct access methods to factor
651 # information
652 sub get_factor_name {
653 my ($self, $id) = @_;
654 my $details = $self->_get_factor_details($id) || return;
655 return $details->{name};
656 }
657 sub get_factor_species {
658 my ($self, $id) = @_;
659 my $details = $self->_get_factor_details($id) || return;
660 return $details->{species};
661 }
662 sub get_factor_sequence {
663 my ($self, $id) = @_;
664 my $details = $self->_get_factor_details($id) || return;
665 return $details->{sequence};
666 }
667 sub _get_factor_details {
668 my ($self, $id) = @_;
669 $id || return;
670  
671 return $self->{factor_details}->{$id} if defined $self->{factor_details}->{$id};
672  
673 my $data = $self->{factor}->{data}->{$id} || return;
674 my @data = split(SEPARATOR, $data);
675  
676 # accession = id name species sequence
677  
678 my %details = (name => $data[1], species => $data[2], sequence => $data[3]);
679 $self->{factor_details}->{$id} = \%details;
680  
681 return \%details;
682 }
683  
684 =head2 get_reference_ids
685  
686 Title : get_reference_ids
687 Usage : my @ids = $obj->get_reference_ids(-key => $value);
688 Function: Get all the reference ids that are associated with the supplied
689 args.
690 Returns : list of strings (ids)
691 Args : -key => value, where value is a string id, and key is one of:
692 -pubmed -site -gene -matrix -factor
693  
694 =cut
695  
696 sub get_reference_ids {
697 my $self = shift;
698 return $self->_get_ids('reference', @_);
699 }
700  
701 # -id -name -species -site -factor -reference
702 sub get_gene_ids {
703 my $self = shift;
704 return $self->_get_ids('gene', @_);
705 }
706  
707 =head2 get_site_ids
708  
709 Title : get_site_ids
710 Usage : my @ids = $obj->get_site_ids(-key => $value);
711 Function: Get all the site ids that are associated with the supplied
712 args.
713 Returns : list of strings (ids)
714 Args : -key => value, where value is a string id, and key is one of:
715 -id -species -gene -matrix -factor -reference
716  
717 =cut
718  
719 sub get_site_ids {
720 my $self = shift;
721 return $self->_get_ids('site', @_);
722 }
723  
724 =head2 get_matrix_ids
725  
726 Title : get_matrix_ids
727 Usage : my @ids = $obj->get_matrix_ids(-key => $value);
728 Function: Get all the matrix ids that are associated with the supplied
729 args.
730 Returns : list of strings (ids)
731 Args : -key => value, where value is a string id, and key is one of:
732 -id -name -site -factor -reference
733  
734 =cut
735  
736 sub get_matrix_ids {
737 my $self = shift;
738 return $self->_get_ids('matrix', @_);
739 }
740  
741 =head2 get_factor_ids
742  
743 Title : get_factor_ids
744 Usage : my @ids = $obj->get_factor_ids(-key => $value);
745 Function: Get all the factor ids that are associated with the supplied
746 args.
747 Returns : list of strings (ids)
748 Args : -key => value, where value is a string id, and key is one of:
749 -id -name -species -interactors -gene -matrix -site -reference
750 NB: -gene only gets factor ids for genes that encode factors
751  
752 =cut
753  
754 sub get_factor_ids {
755 my $self = shift;
756 return $self->_get_ids('factor', @_);
757 }
758  
759 =head2 get_fragment_ids
760  
761 Title : get_fragment_ids
762 Usage : my @ids = $obj->get_fragment_ids(-key => $value);
763 Function: Get all the fragment ids that are associated with the supplied
764 args.
765 Returns : list of strings (ids)
766 Args : -key => value, where value is a string id, and key is one of:
767 -id -species -gene -factor -reference
768  
769 =cut
770  
771 sub get_fragment_ids {
772 my $self = shift;
773 return $self->_get_ids('fragment', @_);
774 }
775  
776 =head2 Helper methods
777  
778 =cut
779  
780 # internal method which does the indexing
781 sub _build_index {
782 my ($self, $dat_dir, $force) = @_;
783  
784 # MLDBM would give us transparent complex data structures with DB_File,
785 # allowing just one index file, but its yet another requirement and we
786 # don't strictly need it
787  
788 my $index_dir = $self->index_directory;
789 my $gene_index = "$index_dir/gene.dat.index";
790 my $reference_index = "$index_dir/reference.dat.index";
791 my $matrix_index = "$index_dir/matrix.dat.index";
792 my $factor_index = "$index_dir/factor.dat.index";
793 my $fragment_index = "$index_dir/fragment.dat.index";
794 my $site_index = "$index_dir/site.dat.index";
795  
796 my $reference_dat = "$dat_dir/reference.dat";
797 if (! -e $reference_index || $force) {
798 open my $REF, '<', $reference_dat or $self->throw("Could not read reference file '$reference_dat': $!");
799  
800 my %references;
801 unlink $reference_index;
802 my $ref = tie(%references, 'DB_File', $reference_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
803 or $self->throw("CCould not open file '$reference_index': $!");
804  
805 my %pubmed;
806 my $reference_pubmed = $reference_index.'.pubmed';
807 unlink $reference_pubmed;
808 my $pub = tie(%pubmed, 'DB_File', $reference_pubmed, O_RDWR|O_CREAT, 0644, $DB_BTREE)
809 or $self->throw("Could not open file '$reference_pubmed': $!");
810  
811 my %gene;
812 my $reference_gene = $gene_index.'.reference';
813 unlink $reference_gene;
814 my $gene = tie(%gene, 'DB_File', $reference_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE)
815 or $self->throw("Could not open file '$reference_gene': $!");
816  
817 my %site;
818 my $reference_site = $site_index.'.reference';
819 unlink $reference_site;
820 my $site = tie(%site, 'DB_File', $reference_site, O_RDWR|O_CREAT, 0644, $DB_BTREE)
821 or $self->throw("Could not open file '$reference_site': $!");
822  
823 my %fragment;
824 my $reference_fragment = $fragment_index.'.reference';
825 unlink $reference_fragment;
826 my $fragment = tie(%fragment, 'DB_File', $reference_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE)
827 or $self->throw("Could not open file '$reference_fragment': $!");
828  
829 my %factor;
830 my $reference_factor = $factor_index.'.reference';
831 unlink $reference_factor;
832 my $factor = tie(%factor, 'DB_File', $reference_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE)
833 or $self->throw("Could not open file '$reference_factor': $!");
834  
835 my %matrix;
836 my $reference_matrix = $matrix_index.'.reference';
837 unlink $reference_matrix;
838 my $matrix = tie(%matrix, 'DB_File', $reference_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE)
839 or $self->throw("Could not open file '$reference_matrix': $!");
840  
841 # skip the first three header lines
842 <$REF>; <$REF>; <$REF>;
843  
844 my @data;
845 while (<$REF>) {
846 if (/^AC (\S+)/) {
847 $data[0] = $1;
848 }
849 elsif (/^RX PUBMED: (\d+)/) {
850 $data[1] = $1;
851 $pub->put("$1", $data[0]);
852 }
853 elsif (/^RA (.+)\n$/) {
854 $data[2] = $1;
855 }
856 elsif (/^RT (.+?)\.?\n$/) {
857 $data[3] = $1;
858 }
859 elsif (/^RL (.+?)\.?\n$/) {
860 $data[4] = $1;
861 }
862 elsif (/^GE TRANSFAC: (\w\d+)/) {
863 $gene->put($data[0], "$1");
864 }
865 elsif (/^BS TRANSFAC: (\w\d+)/) {
866 $site->put($data[0], "$1");
867 }
868 elsif (/^FA TRANSFAC: (\w\d+)/) {
869 $factor->put($data[0], "$1");
870 }
871 elsif (/^FR TRANSFAC: (FR\d+)/) {
872 $fragment->put($data[0], "$1");
873 }
874 elsif (/^MX TRANSFAC: (\w\d+)/) {
875 $matrix->put($data[0], "$1");
876 }
877 elsif (/^\/\//) {
878 # end of a record, store previous data and reset
879  
880 # accession = pubmed authors title location
881 $references{$data[0]} = join(SEPARATOR, ($data[1] || '',
882 $data[2] || '',
883 $data[3] || '',
884 $data[4] || ''));
885  
886 @data = ();
887 }
888 }
889 close $REF;
890  
891 $ref = $pub = $gene = $site = $fragment = $factor = $matrix = undef;
892 untie %references;
893 untie %pubmed;
894 untie %gene;
895 untie %site;
896 untie %fragment;
897 untie %factor;
898 untie %matrix;
899 }
900  
901 my $gene_dat = "$dat_dir/gene.dat";
902 if (! -e $gene_index || $force) {
903 open my $GEN, '<', $gene_dat or $self->throw("Could not read gene file '$gene_dat': $!");
904  
905 my %genes;
906 unlink $gene_index;
907 my $gene = tie(%genes, 'DB_File', $gene_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
908 or $self->throw("Could not open file '$gene_index': $!");
909  
910 my %id;
911 my $gene_id = $gene_index.'.id';
912 unlink $gene_id;
913 my $id = tie(%id, 'DB_File', $gene_id, O_RDWR|O_CREAT, 0644, $DB_BTREE)
914 or $self->throw("Could not open file '$gene_id': $!");
915  
916 my %name;
917 my $gene_name = $gene_index.'.name';
918 unlink $gene_name;
919 my $name = tie(%name, 'DB_File', $gene_name, O_RDWR|O_CREAT, 0644, $DB_BTREE)
920 or $self->throw("Could not open file '$gene_name': $!");
921  
922 my %species;
923 my $gene_species = $gene_index.'.species';
924 unlink $gene_species;
925 my $species = tie(%species, 'DB_File', $gene_species, O_RDWR|O_CREAT, 0644, $DB_BTREE)
926 or $self->throw("Could not open file '$gene_species': $!");
927  
928 my %site;
929 my $gene_site = $site_index.'.gene';
930 unlink $gene_site;
931 my $site = tie(%site, 'DB_File', $gene_site, O_RDWR|O_CREAT, 0644, $DB_BTREE)
932 or $self->throw("Could not open file '$gene_site': $!");
933  
934 my %factor;
935 my $gene_factor = $factor_index.'.gene';
936 unlink $gene_factor;
937 my $factor = tie(%factor, 'DB_File', $gene_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE)
938 or $self->throw("Could not open file '$gene_factor': $!");
939  
940 my %fragment;
941 my $gene_fragment = $fragment_index.'.gene';
942 unlink $gene_fragment;
943 my $fragment = tie(%fragment, 'DB_File', $gene_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE)
944 or $self->throw("Could not open file '$gene_fragment': $!");
945  
946 my %reference;
947 my $gene_reference = $reference_index.'.gene';
948 unlink $gene_reference;
949 my $reference = tie(%reference, 'DB_File', $gene_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE)
950 or $self->throw("Could not open file '$gene_reference': $!");
951  
952 # skip the first three header lines
953 <$GEN>; <$GEN>; <$GEN>;
954  
955 my @data;
956 while (<$GEN>) {
957 if (/^AC (\S+)/) {
958 $data[0] = $1;
959 }
960 elsif (/^ID (\S+)/) {
961 $data[1] = $1;
962 $id->put("$1", $data[0]);
963 }
964 elsif (/^SD (.+)$/) {
965 $data[2] = lc("$1");
966 $name->put(lc("$1"), $data[0]);
967 }
968 elsif (/^SY (.+)\.$/) {
969 foreach (split('; ', lc("$1"))) {
970 $name->put($_, $data[0]);
971 }
972 }
973 elsif (/^DE (.+)$/) {
974 $data[3] = $1;
975 }
976 elsif (/^OS (.+)$/) {
977 my $raw_species = $1;
978 my $taxid = $self->_species_to_taxid($raw_species);
979 $data[4] = $taxid || $raw_species;
980 $species->put($data[4], $data[0]);
981 }
982 elsif (/^RN .+?(RE\d+)/) {
983 $reference->put($data[0], "$1");
984 }
985 elsif (/^BS .+?(R\d+)/) {
986 $site->put($data[0], "$1");
987 }
988 elsif (/^FA (T\d+)/) {
989 $factor->put($data[0], "$1");
990 }
991 elsif (/^BR (FR\d+)/) {
992 $fragment->put($data[0], "$1");
993 }
994 elsif (/^\/\//) {
995 # end of a record, store previous data and reset
996  
997 # accession = id name description species_tax_id_or_raw_string
998 $genes{$data[0]} = join(SEPARATOR, ($data[1] || '',
999 $data[2] || '',
1000 $data[3] || '',
1001 $data[4] || ''));
1002  
1003 @data = ();
1004 }
1005 }
1006 close $GEN;
1007  
1008 $gene = $id = $name = $species = $site = $factor = $reference = undef;
1009 untie %genes;
1010 untie %id;
1011 untie %name;
1012 untie %species;
1013 untie %site;
1014 untie %factor;
1015 untie %reference;
1016 }
1017  
1018 my $site_dat = "$dat_dir/site.dat";
1019 if (! -e $site_index || $force) {
1020 open my $SIT, '<', $site_dat or $self->throw("Could not read site file '$site_dat': $!");
1021  
1022 my %sites;
1023 unlink $site_index;
1024 my $site = tie(%sites, 'DB_File', $site_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
1025 or $self->throw("Could not open file '$site_index': $!");
1026  
1027 my %id;
1028 my $site_id = $site_index.'.id';
1029 unlink $site_id;
1030 my $id = tie(%id, 'DB_File', $site_id, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1031 or $self->throw("Could not open file '$site_id': $!");
1032  
1033 my %species;
1034 my $site_species = $site_index.'.species';
1035 unlink $site_species;
1036 my $species = tie(%species, 'DB_File', $site_species, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1037 or $self->throw("Could not open file '$site_species': $!");
1038  
1039 my %qualities;
1040 my $site_qualities = $site_index.'.qual';
1041 unlink $site_qualities;
1042 my $quality = tie(%qualities, 'DB_File', $site_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH)
1043 or $self->throw("Could not open file '$site_qualities': $!");
1044  
1045 my %gene;
1046 my $site_gene = $gene_index.'.site';
1047 unlink $site_gene;
1048 my $gene = tie(%gene, 'DB_File', $site_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1049 or $self->throw("Could not open file '$site_gene': $!");
1050  
1051 my %matrix;
1052 my $site_matrix = $matrix_index.'.site';
1053 unlink $site_matrix;
1054 my $matrix = tie(%matrix, 'DB_File', $site_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1055 or $self->throw("Could not open file '$site_matrix': $!");
1056  
1057 my %factor;
1058 my $site_factor = $factor_index.'.site';
1059 unlink $site_factor;
1060 my $factor = tie(%factor, 'DB_File', $site_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1061 or $self->throw("Could not open file '$site_factor': $!");
1062  
1063 my %reference;
1064 my $site_reference = $reference_index.'.site';
1065 unlink $site_reference;
1066 my $reference = tie(%reference, 'DB_File', $site_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1067 or $self->throw("Could not open file '$site_reference': $!");
1068  
1069 # skip the first three header lines
1070 <$SIT>; <$SIT>; <$SIT>;
1071  
1072 my @data;
1073 while (<$SIT>) {
1074 if (/^AC (\S+)/) {
1075 $data[0] = $1;
1076 }
1077 elsif (/^ID (\S+)/) {
1078 $data[1] = $1;
1079 $id->put("$1", $data[0]);
1080 }
1081 elsif (/^TY (.+)$/) {
1082 $data[8] = $1;
1083 }
1084 elsif (/^DE .*Gene: (G\d+)/) {
1085 $data[2] = $1;
1086 $gene->put($data[0], "$1");
1087  
1088 # if it has no gene it is an artificial sequence, unless it
1089 # has a species (OS line), in which case it is unassigned
1090 # genomic; either way we won't be able to make a
1091 # Bio::Map::PositionI later on, so such sites won't be
1092 # on any MapI.
1093 }
1094 elsif (/^OS (.+)$/) {
1095 # Since not all sites in site.dat with a species have a gene,
1096 # (small handful are unassigned 'genomic') can't delegate to
1097 # gene.dat and must parse species here (effectively again)
1098 my $raw_species = $1;
1099 my $taxid = $self->_species_to_taxid($raw_species);
1100 $data[7] = $taxid || $raw_species;
1101 $species->put($data[7], $data[0]);
1102 }
1103 elsif (/^SQ (.+)\.$/) {
1104 $data[3] = $1;
1105 # there can actually be more than one SQ line, seemingly with
1106 # variations of the sequence (not a long sequence split over
1107 # two lines); not sure what to do with data; currently we end
1108 # up storing only the last variant.
1109 }
1110 elsif (/^S1 (.+)$/) {
1111 $data[4] = $1;
1112 # if S1 not present, means transcriptional start
1113 }
1114 elsif (/^SF (.+)$/) {
1115 $data[5] = $1;
1116 }
1117 elsif (/^ST (.+)$/) {
1118 $data[6] = $1;
1119 }
1120 elsif (/^RN .+?(RE\d+)/) {
1121 $reference->put($data[0], "$1");
1122 }
1123 elsif (/^MX (M\d+)/) {
1124 $matrix->put($data[0], "$1");
1125 }
1126 elsif (/^BF (T\d+); .+?; Quality: (\d)/) {
1127 $factor->put($data[0], "$1");
1128 $qualities{$data[0].SEPARATOR.$1} = $2;
1129 }
1130 elsif (/^\/\//) {
1131 # end of a record, store previous data and reset
1132  
1133 # accession = id gene_id sequence relative_to first_position last_position species_tax_id_or_raw_string type
1134 $sites{$data[0]} = join(SEPARATOR, ($data[1] || '',
1135 $data[2] || '',
1136 $data[3] || '',
1137 $data[4] || 'TSS',
1138 $data[5] || '',
1139 $data[6] || '',
1140 $data[7] || '',
1141 $data[8] || ''));
1142  
1143 @data = ();
1144 }
1145 }
1146 close $SIT;
1147  
1148 $site = $id = $species = $quality = $gene = $matrix = $factor = $reference = undef;
1149 untie %sites;
1150 untie %id;
1151 untie %species;
1152 untie %qualities;
1153 untie %gene;
1154 untie %matrix;
1155 untie %factor;
1156 untie %reference;
1157 }
1158  
1159 my $matrix_dat = "$dat_dir/matrix.dat";
1160 if (! -e $matrix_index || $force) {
1161 open my $MAT, '<', $matrix_dat or $self->throw("Could not read matrix file '$matrix_dat': $!");
1162  
1163 my %matrices;
1164 unlink $matrix_index;
1165 my $matrix = tie(%matrices, 'DB_File', $matrix_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
1166 or $self->throw("Could not open file '$matrix_index': $!");
1167  
1168 my %id;
1169 my $matrix_id = $matrix_index.'.id';
1170 unlink $matrix_id;
1171 my $id = tie(%id, 'DB_File', $matrix_id, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1172 or $self->throw("Could not open file '$matrix_id': $!");
1173  
1174 my %name;
1175 my $matrix_name = $matrix_index.'.name';
1176 unlink $matrix_name;
1177 my $name = tie(%name, 'DB_File', $matrix_name, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1178 or $self->throw("Could not open file '$matrix_name': $!");
1179  
1180 my %site;
1181 my $matrix_site = $site_index.'.matrix';
1182 unlink $matrix_site;
1183 my $site = tie(%site, 'DB_File', $matrix_site, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1184 or $self->throw("Could not open file '$matrix_site': $!");
1185  
1186 my %factor;
1187 my $matrix_factor = $factor_index.'.matrix';
1188 unlink $matrix_factor;
1189 my $factor = tie(%factor, 'DB_File', $matrix_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1190 or $self->throw("Could not open file '$matrix_factor': $!");
1191  
1192 my %reference;
1193 my $matrix_reference = $reference_index.'.matrix';
1194 unlink $matrix_reference;
1195 my $reference = tie(%reference, 'DB_File', $matrix_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1196 or $self->throw("Could not open file '$matrix_reference': $!");
1197  
1198 # skip the first three header lines
1199 <$MAT>; <$MAT>; <$MAT>;
1200  
1201 my @data;
1202 my @matrix_data;
1203 my @site_data;
1204 while (<$MAT>) {
1205 if (/^AC (\S+)/) {
1206 $data[0] = $1;
1207 }
1208 elsif (/^ID (\S+)/) {
1209 $data[1] = $1;
1210 $id->put("$1", $data[0]);
1211 }
1212 elsif (/^NA (.+)$/) {
1213 $data[2] = $1;
1214 $name->put("$1", $data[0]);
1215 }
1216 elsif (/^DE (.+)$/) {
1217 $data[3] = $1;
1218 }
1219 elsif (/^\d\d \s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
1220 # a, c, g, t counts/weights
1221 push(@matrix_data, join("\t", ($1, $2, $3, $4)));
1222  
1223 # Work out the number of sites as the largest number of
1224 # sites amongst all positions in the sequences. (The BA
1225 # line isn't reliable for telling us the correct number of
1226 # sites all the time)
1227 my $num = $1 + $2 + $3 + $4;
1228 $data[4] ||= 0;
1229 if ($num > $data[4]) {
1230 $data[4] = $num;
1231 }
1232 }
1233 elsif (/^BS ([\sa-zA-Z]+); (.+?); (-?\d+); \d+;.*; ([np])/) {
1234 # sequence id start strand
1235 push(@site_data, join('_', ($1, $2, $3, $4 eq 'p' ? 1 : -1)));
1236 $site->put($data[0], $2);
1237 }
1238 elsif (/^BF (T\d+)/) {
1239 $factor->put($data[0], "$1");
1240 }
1241 elsif (/^RN .+?(RE\d+)/) {
1242 $reference->put($data[0], "$1");
1243 }
1244 elsif (/^\/\//) {
1245 # end of a record, store previous data and reset
1246 my $matrix_data = join(INTERNAL_SEPARATOR, @matrix_data) || '';
1247  
1248 # sites of a matrix are pre-aligned but padded with spaces on
1249 # the left and no padding on the right; pad with -s both sides
1250 my $longest_seq = 0;
1251  
1252 # For all the work, does anything meaningful actually get passed
1253 # on here? Commenting out fixes the latest crashes on trunk.
1254 # 5-10-10 cjfields
1255  
1256 #foreach my $site_seq (map {my ($seq) = split("_", $_ ,2); $seq;} @site_data) {
1257 # $site_seq =~ s/ /-/g;
1258 # my $length = length($site_seq);
1259 # if ($length > $longest_seq) {
1260 # $longest_seq = $length;
1261 # }
1262 #}
1263 #foreach my $site (@site_data) {
1264 # my ($site_seq) = split("_", $site ,2);
1265 # my $length = length($site_seq);
1266 # if ($length < $longest_seq) {
1267 # $site_seq .= '-' x ($longest_seq - $length);
1268 # }
1269 #}
1270  
1271 my $site_data = join(INTERNAL_SEPARATOR, @site_data) || '';
1272  
1273 # accession = id name description num_of_sites matrix_data site_data
1274 $matrices{$data[0]} = join(SEPARATOR, ($data[1] || '',
1275 $data[2] || '',
1276 $data[3] || '',
1277 $data[4],
1278 $matrix_data,
1279 $site_data));
1280  
1281 @data = @matrix_data = @site_data = ();
1282 }
1283 }
1284 close $MAT;
1285  
1286 $matrix = $id = $name = $site = $factor = $reference = undef;
1287 untie %matrices;
1288 untie %id;
1289 untie %name;
1290 untie %site;
1291 untie %factor;
1292 untie %reference;
1293 }
1294  
1295 my $factor_dat = "$dat_dir/factor.dat";
1296 if (! -e $factor_index || $force) {
1297 open my $FAC, '<', $factor_dat or $self->throw("Could not read factor file '$factor_dat': $!");
1298  
1299 my %factors;
1300 unlink $factor_index;
1301 my $factor = tie(%factors, 'DB_File', $factor_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
1302 or $self->throw("Could not open file '$factor_index': $!");
1303  
1304 my %id;
1305 my $factor_id = $factor_index.'.id';
1306 unlink $factor_id;
1307 my $id = tie(%id, 'DB_File', $factor_id, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1308 or $self->throw("Could not open file '$factor_id': $!");
1309  
1310 my %name;
1311 my $factor_name = $factor_index.'.name';
1312 unlink $factor_name;
1313 my $name = tie(%name, 'DB_File', $factor_name, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1314 or $self->throw("Could not open file '$factor_name': $!");
1315  
1316 my %species;
1317 my $factor_species = $factor_index.'.species';
1318 unlink $factor_species;
1319 my $species = tie(%species, 'DB_File', $factor_species, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1320 or $self->throw("Could not open file '$factor_species': $!");
1321  
1322 my %interactors;
1323 my $factor_interactors = $factor_index.'.interactors';
1324 unlink $factor_interactors;
1325 my $interact = tie(%interactors, 'DB_File', $factor_interactors, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1326 or $self->throw("Could not open file '$factor_interactors': $!");
1327  
1328 my %gene;
1329 my $factor_gene = $gene_index.'.factor';
1330 unlink $factor_gene;
1331 my $gene = tie(%gene, 'DB_File', $factor_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1332 or $self->throw("Could not open file '$factor_gene': $!");
1333  
1334 my %matrix;
1335 my $factor_matrix = $matrix_index.'.factor';
1336 unlink $factor_matrix;
1337 my $matrix = tie(%matrix, 'DB_File', $factor_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1338 or $self->throw("Could not open file '$factor_matrix': $!");
1339  
1340 my %site;
1341 my $factor_site = $site_index.'.factor';
1342 unlink $factor_site;
1343 my $site = tie(%site, 'DB_File', $factor_site, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1344 or $self->throw("Could not open file '$factor_site': $!");
1345  
1346 my %fragment;
1347 my $factor_fragment = $fragment_index.'.factor';
1348 unlink $factor_fragment;
1349 my $fragment = tie(%fragment, 'DB_File', $factor_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1350 or $self->throw("Could not open file '$factor_fragment': $!");
1351  
1352 my %reference;
1353 my $factor_reference = $reference_index.'.factor';
1354 unlink $factor_reference;
1355 my $reference = tie(%reference, 'DB_File', $factor_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1356 or $self->throw("Could not open file '$factor_reference': $!");
1357  
1358 # skip the first three header lines
1359 <$FAC>; <$FAC>; <$FAC>;
1360  
1361 my @data;
1362 my $sequence = '';
1363 while (<$FAC>) {
1364 if (/^AC (\S+)/) {
1365 $data[0] = $1;
1366 }
1367 elsif (/^ID (\S+)/) {
1368 # IDs are always the same as AC? Is this needed?
1369 $data[1] = $1;
1370 $id->put("$1", $data[0]);
1371 }
1372 elsif (/^FA (.+)$/) {
1373 $data[2] = $1;
1374 $name->put("$1", $data[0]);
1375 }
1376 elsif (/^OS (.+)$/) {
1377 # This is the species the actual factor came from, which may
1378 # differ from the species of any sequences it is described as
1379 # binding to. Not all factors that have a species have a gene,
1380 # so can't delegate species to a gene lookup.
1381 my $raw_species = $1;
1382 my $taxid = $self->_species_to_taxid($raw_species);
1383 $data[3] = $taxid || $raw_species;
1384 $species->put($data[3], $data[0]);
1385 }
1386 elsif (/^GE (G\d+)/) {
1387 $gene->put($data[0], "$1");
1388 }
1389 elsif (/^SQ (.+)$/) {
1390 $sequence .= $1;
1391 }
1392 elsif (/^IN (T\d+)/) {
1393 $interact->put($data[0], "$1");
1394 }
1395 elsif (/^MX (M\d+)/) {
1396 $matrix->put($data[0], "$1");
1397 }
1398 elsif (/^BS (R\d+)/) {
1399 $site->put($data[0], "$1");
1400 }
1401 elsif (/^BR (FR\d+)/) {
1402 $fragment->put($data[0], "$1");
1403 }
1404 elsif (/^RN .+?(RE\d+)/) {
1405 $reference->put($data[0], "$1");
1406 }
1407 elsif (/^\/\//) {
1408 # end of a record, store previous data and reset
1409  
1410 # accession = id name species sequence
1411 $factors{$data[0]} = join(SEPARATOR, ($data[1] || '',
1412 $data[2] || '',
1413 $data[3] || '',
1414 $sequence));
1415  
1416 @data = ();
1417 $sequence = '';
1418 }
1419 }
1420 close $FAC;
1421  
1422 $factor = $id = $name = $species = $interact = $gene = $matrix = $site = $fragment = $reference = undef;
1423 untie %factors;
1424 untie %id;
1425 untie %name;
1426 untie %species;
1427 untie %interactors;
1428 untie %gene;
1429 untie %matrix;
1430 untie %site;
1431 untie %fragment;
1432 untie %reference;
1433 }
1434  
1435 my $fragment_dat = "$dat_dir/fragment.dat";
1436 if (! -e $fragment_index || $force) {
1437 if (open my $FRA, '<', $fragment_dat) {
1438 my %fragments;
1439 unlink $fragment_index;
1440 my $fragment = tie(%fragments, 'DB_File', $fragment_index, O_RDWR|O_CREAT, 0644, $DB_HASH)
1441 or $self->throw("Could not open file '$fragment_index': $!");
1442  
1443 my %id;
1444 my $fragment_id = $fragment_index.'.id';
1445 unlink $fragment_id;
1446 my $id = tie(%id, 'DB_File', $fragment_id, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1447 or $self->throw("Could not open file '$fragment_id': $!");
1448  
1449 my %qualities;
1450 my $fragment_qualities = $fragment_index.'.qual';
1451 unlink $fragment_qualities;
1452 my $quality = tie(%qualities, 'DB_File', $fragment_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH)
1453 or $self->throw("Could not open file '$fragment_qualities': $!");
1454  
1455 my %species;
1456 my $fragment_species = $fragment_index.'.species';
1457 unlink $fragment_species;
1458 my $species = tie(%species, 'DB_File', $fragment_species, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1459 or $self->throw("Could not open file '$fragment_species': $!");
1460  
1461 my %gene;
1462 my $fragment_gene = $gene_index.'.fragment';
1463 unlink $fragment_gene;
1464 my $gene = tie(%gene, 'DB_File', $fragment_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1465 or $self->throw("Could not open file '$fragment_gene': $!");
1466  
1467 my %factor;
1468 my $fragment_factor = $factor_index.'.fragment';
1469 unlink $fragment_factor;
1470 my $factor = tie(%factor, 'DB_File', $fragment_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1471 or $self->throw("Could not open file '$fragment_factor': $!");
1472  
1473 my %reference;
1474 my $fragment_reference = $reference_index.'.fragment';
1475 unlink $fragment_reference;
1476 my $reference = tie(%reference, 'DB_File', $fragment_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE)
1477 or $self->throw("Could not open file '$fragment_reference': $!");
1478  
1479 # skip the first three header lines
1480 <$FRA>; <$FRA>; <$FRA>;
1481  
1482 my @data;
1483 while (<$FRA>) {
1484 if (/^AC (\S+)/) {
1485 $data[0] = $1;
1486 }
1487 elsif (/^ID (\S+)/) {
1488 # IDs are always the same as AC? Is this needed?
1489 $data[1] = $1;
1490 $id->put("$1", $data[0]);
1491 }
1492 elsif (/^DE Gene: (G\d+)(?:.+Gene: (G\d+))?/) {
1493 my ($gene1, $gene2) = ($1, $2);
1494 $data[2] = $gene1;
1495 $data[3] = $gene2; # could be undef
1496 $gene->put($data[0], $gene1);
1497 $gene->put($data[0], $gene2) if $gene2;
1498 }
1499 elsif (/^OS (.+)$/) {
1500 # As per the site.dat parsing
1501 my $raw_species = $1;
1502 my $taxid = $self->_species_to_taxid($raw_species);
1503 $data[4] = $taxid || $raw_species;
1504 $species->put($data[4], $data[0]);
1505 }
1506 elsif (/^SQ [atcgn]*([ATCGN]+)[atcgn]*/) {
1507 $data[5] .= $1;
1508 # there can be (usually are) multiple SQ lines with a single
1509 # long seq split over them. The 'real' sequence is in caps
1510 }
1511 elsif (/^SC Build (\S+):$/) {
1512 $data[6] = $1;
1513 # maybe parse it out a little more? We have build,
1514 # chromosomal coords and strand, eg.
1515 # SC Build HSA_May2004: Chr.2 43976692..43978487 (FORWARD).
1516 }
1517 elsif (/^RN .+?(RE\d+)/) {
1518 $reference->put($data[0], "$1");
1519 }
1520 elsif (/^BF (T\d+); .+?; Quality: (\d)/) {
1521 $factor->put($data[0], "$1");
1522 $qualities{$data[0].SEPARATOR.$1} = $2;
1523 }
1524 elsif (/^\/\//) {
1525 # end of a record, store previous data and reset
1526  
1527 # accession = id gene_id1 gene_id2 species_tax_id_or_raw_string sequence source
1528 $fragments{$data[0]} = join(SEPARATOR, ($data[1] || '',
1529 $data[2] || '',
1530 $data[3] || '',
1531 $data[4] || '',
1532 $data[5] || '',
1533 $data[6] || ''));
1534  
1535 @data = ();
1536 }
1537 }
1538 close $FRA;
1539  
1540 $fragment = $id = $species = $quality = $gene = $factor = $reference = undef;
1541 untie %fragments;
1542 untie %id;
1543 untie %species;
1544 untie %qualities;
1545 untie %gene;
1546 untie %factor;
1547 untie %reference;
1548 }
1549 else {
1550 $self->warn("Could not read fragment file '$fragment_dat', assuming you have an old version of Transfac Pro with no fragment.dat file");
1551 }
1552 }
1553 }
1554  
1555 # connect the internal db handle
1556 sub _db_connect {
1557 my $self = shift;
1558 return if $self->{'_initialized'};
1559  
1560 my $index_dir = $self->index_directory;
1561 my $gene_index = "$index_dir/gene.dat.index";
1562 my $reference_index = "$index_dir/reference.dat.index";
1563 my $matrix_index = "$index_dir/matrix.dat.index";
1564 my $factor_index = "$index_dir/factor.dat.index";
1565 my $site_index = "$index_dir/site.dat.index";
1566 my $fragment_index = "$index_dir/fragment.dat.index";
1567  
1568 foreach ($gene_index, $reference_index, $matrix_index, $factor_index, $site_index, $fragment_index) {
1569 if (! -e $_) {
1570 #$self->warn("Index files have not been created");
1571 #return 0;
1572 }
1573 }
1574  
1575 # reference
1576 {
1577 $self->{reference}->{data} = {};
1578 tie (%{$self->{reference}->{data}}, 'DB_File', $reference_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$reference_index': $!");
1579  
1580 my $reference_pubmed = $reference_index.'.pubmed';
1581 $self->{reference}->{pubmed} = tie (%{$self->{reference}->{pubmed}}, 'DB_File', $reference_pubmed, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$reference_pubmed': $!");
1582  
1583 my $reference_gene = $gene_index.'.reference';
1584 $self->{gene}->{reference} = tie (%{$self->{gene}->{reference}}, 'DB_File', $reference_gene, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$reference_gene': $!");
1585  
1586 my $reference_site = $site_index.'.reference';
1587 $self->{site}->{reference} = tie (%{$self->{site}->{reference}}, 'DB_File', $reference_site, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$reference_site': $!");
1588  
1589 my $reference_fragment = $fragment_index.'.reference';
1590 $self->{fragment}->{reference} = tie (%{$self->{fragment}->{reference}}, 'DB_File', $reference_fragment, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$reference_fragment': $!");
1591  
1592 my $reference_factor = $factor_index.'.reference';
1593 $self->{factor}->{reference} = tie (%{$self->{factor}->{reference}}, 'DB_File', $reference_factor, undef, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_factor': $!");
1594  
1595 my $reference_matrix = $matrix_index.'.reference';
1596 $self->{matrix}->{reference} = tie (%{$self->{matrix}->{reference}}, 'DB_File', $reference_matrix, undef, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_matrix': $!");
1597 }
1598  
1599 # gene
1600 {
1601 $self->{gene}->{data} = {};
1602 tie (%{$self->{gene}->{data}}, 'DB_File', $gene_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$gene_index': $!");
1603  
1604 my $gene_id = $gene_index.'.id';
1605 $self->{gene}->{id} = tie(%{$self->{gene}->{id}}, 'DB_File', $gene_id, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_id': $!");
1606  
1607 my $gene_name = $gene_index.'.name';
1608 $self->{gene}->{name} = tie(%{$self->{gene}->{name}}, 'DB_File', $gene_name, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_name': $!");
1609  
1610 my $gene_species = $gene_index.'.species';
1611 $self->{gene}->{species} = tie(%{$self->{gene}->{species}}, 'DB_File', $gene_species, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_species': $!");
1612  
1613 my $gene_site = $site_index.'.gene';
1614 $self->{site}->{gene} = tie(%{$self->{site}->{gene}}, 'DB_File', $gene_site, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_site': $!");
1615  
1616 my $gene_fragment = $fragment_index.'.gene';
1617 $self->{fragment}->{gene} = tie(%{$self->{fragment}->{gene}}, 'DB_File', $gene_fragment, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_fragment': $!");
1618  
1619 my $gene_factor = $factor_index.'.gene';
1620 $self->{factor}->{gene} = tie(%{$self->{factor}->{gene}}, 'DB_File', $gene_factor, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_factor': $!");
1621  
1622 my $gene_reference = $reference_index.'.gene';
1623 $self->{reference}->{gene} = tie(%{$self->{reference}->{gene}}, 'DB_File', $gene_reference, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$gene_reference': $!");
1624 }
1625  
1626 # site
1627 {
1628 $self->{site}->{data} = {};
1629 tie (%{$self->{site}->{data}}, 'DB_File', $site_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$site_index': $!");
1630  
1631 my $site_id = $site_index.'.id';
1632 $self->{site}->{id} = tie(%{$self->{site}->{id}}, 'DB_File', $site_id, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$site_id': $!");
1633  
1634 my $site_species = $site_index.'.species';
1635 $self->{site}->{species} = tie(%{$self->{site}->{species}}, 'DB_File', $site_species, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file $site_species': $!");
1636  
1637 #*** quality not actually used by anything (yet)
1638 my $site_qualities = $site_index.'.qual';
1639 $self->{quality} = {};
1640 tie(%{$self->{quality}}, 'DB_File', $site_qualities, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$site_qualities': $!");
1641  
1642 my $site_gene = $gene_index.'.site';
1643 $self->{gene}->{site} = tie(%{$self->{gene}->{site}}, 'DB_File', $site_gene, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$site_gene': $!");
1644  
1645 my $site_matrix = $matrix_index.'.site';
1646 $self->{matrix}->{site} = tie(%{$self->{matrix}->{site}}, 'DB_File', $site_matrix, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$site_matrix': $!");
1647  
1648 my $site_factor = $factor_index.'.site';
1649 $self->{factor}->{site} = tie(%{$self->{factor}->{site}}, 'DB_File', $site_factor, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$site_factor': $!");
1650  
1651 my $site_reference = $reference_index.'.site';
1652 $self->{reference}->{site} = tie(%{$self->{reference}->{site}}, 'DB_File', $site_reference, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$site_reference': $!");
1653 }
1654  
1655 # fragment (may not be in older databases)
1656 if (-e $fragment_index) {
1657 $self->{fragment}->{data} = {};
1658 tie (%{$self->{fragment}->{data}}, 'DB_File', $fragment_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$fragment_index': $!");
1659  
1660 my $fragment_id = $fragment_index.'.id';
1661 $self->{fragment}->{id} = tie(%{$self->{fragment}->{id}}, 'DB_File', $fragment_id, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$fragment_id': $!");
1662  
1663 my $fragment_species = $fragment_index.'.species';
1664 $self->{fragment}->{species} = tie(%{$self->{fragment}->{species}}, 'DB_File', $fragment_species, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file $fragment_species': $!");
1665  
1666 #*** quality not actually used by anything (yet)
1667 my $fragment_qualities = $fragment_index.'.qual';
1668 $self->{fragment_quality} = {};
1669 tie(%{$self->{fragment_quality}}, 'DB_File', $fragment_qualities, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$fragment_qualities': $!");
1670  
1671 my $fragment_gene = $gene_index.'.fragment';
1672 $self->{gene}->{fragment} = tie(%{$self->{gene}->{fragment}}, 'DB_File', $fragment_gene, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$fragment_gene': $!");
1673  
1674 my $fragment_factor = $factor_index.'.fragment';
1675 $self->{factor}->{fragment} = tie(%{$self->{factor}->{fragment}}, 'DB_File', $fragment_factor, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$fragment_factor': $!");
1676  
1677 my $fragment_reference = $reference_index.'.fragment';
1678 $self->{reference}->{fragment} = tie(%{$self->{reference}->{fragment}}, 'DB_File', $fragment_reference, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$fragment_reference': $!");
1679 }
1680 else {
1681 die "no fragment_index at '$fragment_index'\n";
1682 }
1683  
1684 # matrix
1685 {
1686 $self->{matrix}->{data} = {};
1687 tie (%{$self->{matrix}->{data}}, 'DB_File', $matrix_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$matrix_index': $!");
1688  
1689 my $matrix_id = $matrix_index.'.id';
1690 $self->{matrix}->{id} = tie(%{$self->{matrix}->{id}}, 'DB_File', $matrix_id, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$matrix_id': $!");
1691  
1692 my $matrix_name = $matrix_index.'.name';
1693 $self->{matrix}->{name} = tie(%{$self->{matrix}->{name}}, 'DB_File', $matrix_name, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$matrix_name': $!");
1694  
1695 my $matrix_site = $site_index.'.matrix';
1696 $self->{site}->{matrix} = tie(%{$self->{site}->{matrix}}, 'DB_File', $matrix_site, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$matrix_site': $!");
1697  
1698 my $matrix_factor = $factor_index.'.matrix';
1699 $self->{factor}->{matrix} = tie(%{$self->{factor}->{matrix}}, 'DB_File', $matrix_factor, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$matrix_factor': $!");
1700  
1701 my $matrix_reference = $reference_index.'.matrix';
1702 $self->{reference}->{matrix} = tie(%{$self->{reference}->{matrix}}, 'DB_File', $matrix_reference, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$matrix_reference': $!");
1703 }
1704  
1705 # factor
1706 {
1707 $self->{factor}->{data} = {};
1708 tie (%{$self->{factor}->{data}}, 'DB_File', $factor_index, O_RDWR, undef, $DB_HASH) || $self->throw("Cannot open file '$factor_index': $!");
1709  
1710 my $factor_id = $factor_index.'.id';
1711 $self->{factor}->{id} = tie(%{$self->{factor}->{id}}, 'DB_File', $factor_id, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file 'factor_id': $!");
1712  
1713 my $factor_name = $factor_index.'.name';
1714 $self->{factor}->{name} = tie(%{$self->{factor}->{name}}, 'DB_File', $factor_name, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_name': $!");
1715  
1716 my $factor_species = $factor_index.'.species';
1717 $self->{factor}->{species} = tie(%{$self->{factor}->{species}}, 'DB_File', $factor_species, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_species': $!");
1718  
1719 my $factor_interactors = $factor_index.'.interactors';
1720 $self->{factor}->{interactors} = tie(%{$self->{factor}->{interactors}}, 'DB_File', $factor_interactors, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_interactors': $!");
1721  
1722 my $factor_gene = $gene_index.'.factor';
1723 $self->{gene}->{factor} = tie(%{$self->{gene}->{factor}}, 'DB_File', $factor_gene, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_gene': $!");
1724  
1725 my $factor_matrix = $matrix_index.'.factor';
1726 $self->{matrix}->{factor} = tie(%{$self->{matrix}->{factor}}, 'DB_File', $factor_matrix, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_matrix': $!");
1727  
1728 my $factor_site = $site_index.'.factor';
1729 $self->{site}->{factor} = tie(%{$self->{site}->{factor}}, 'DB_File', $factor_site, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_site': $!");
1730  
1731 my $factor_fragment = $fragment_index.'.factor';
1732 $self->{fragment}->{factor} = tie(%{$self->{fragment}->{factor}}, 'DB_File', $factor_fragment, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_fragment': $!");
1733  
1734 my $factor_reference = $reference_index.'.factor';
1735 $self->{reference}->{factor} = tie(%{$self->{reference}->{factor}}, 'DB_File', $factor_reference, O_RDWR, undef, $DB_BTREE) || $self->throw("Cannot open file '$factor_reference': $!");
1736 }
1737  
1738 $self->{'_initialized'} = 1;
1739 }
1740  
1741 =head2 index_directory
1742  
1743 Title : index_directory
1744 Function : Get/set the location that index files are stored. (this module
1745 will index the supplied database)
1746 Usage : $obj->index_directory($newval)
1747 Returns : value of index_directory (a scalar)
1748 Args : on set, new value (a scalar or undef, optional)
1749  
1750 =cut
1751  
1752 sub index_directory {
1753 my $self = shift;
1754 return $self->{'index_directory'} = shift if @_;
1755 return $self->{'index_directory'};
1756 }
1757  
1758 # resolve a transfac species string into an ncbi taxid
1759 sub _species_to_taxid {
1760 my ($self, $raw_species) = @_;
1761 $raw_species or return;
1762  
1763 my $species_string;
1764 my @split = split(', ', $raw_species);
1765 (@split > 1) ? ($species_string = $split[1]) : ($species_string = $split[0]);
1766  
1767 my $ncbi_taxid;
1768 if ($species_string =~ /^[A-Z]\S+ \S+$/) {
1769 SWITCH: for ($species_string) {
1770 # some species don't classify so custom handling
1771 /^Darnel ryegrass/ && do { $ncbi_taxid = 34176; last; };
1772 /^Coix lacryma/ && do { $ncbi_taxid = 4505; last; };
1773 /^Rattus spec/ && do { $ncbi_taxid = 10116; last; };
1774 /^Mus spec/ && do { $ncbi_taxid = 10090; last; };
1775 /^Equus spec/ && do { $ncbi_taxid = 9796; last; };
1776 /^Cavia sp/ && do { $ncbi_taxid = 10141; last; };
1777 /^Marsh marigold/ && do { $ncbi_taxid = 3449; last; };
1778 /^Phalaenopsis sp/ && do { $ncbi_taxid = 36900; last; };
1779 /^Anthirrhinum majus/ && do { $ncbi_taxid = 4151; last; };
1780 /^Equus spec/ && do { $ncbi_taxid = 9796; last; };
1781 /^Lycopodium spec/ && do { $ncbi_taxid = 13840; last; };
1782 /^Autographa californica/ && do { $ncbi_taxid = 307456; last; };
1783 /^E26 AEV/ && do { $ncbi_taxid = 31920; last; };
1784 /^Pseudocentrotus miliaris/ && do { $ncbi_taxid = 7677; last; }; # the genus is 7677 but this species isn't there
1785 /^SL3-3 (?:retro)?virus/ && do { $ncbi_taxid = 53454; last; }; # 53454 is unclassified MLV-related, SL3-3 a variant of that?
1786 /^Petunia sp/ && do { $ncbi_taxid = 4104; last; };
1787 }
1788 if (! $ncbi_taxid && defined $self->{_tax_db}) {
1789 ($ncbi_taxid) = $self->{_tax_db}->get_taxonids($species_string);
1790 }
1791 }
1792 else {
1793 # some species lines are poorly formatted so custom handling
1794 SWITCH: for ($raw_species) {
1795 # for speed, go by common first letters
1796 my $first_letter = substr($raw_species, 0, 1);
1797  
1798 $first_letter eq 'A' && do {
1799 /^Adiantum raddianum/ && do { $ncbi_taxid = 32168; last; };
1800 /^Avian sarcoma virus \(strain 17\)/ && do { $ncbi_taxid = 11877; last; };
1801 /^AMV/ && do { $ncbi_taxid = 11866; last; };
1802 /^AEV/ && do { $ncbi_taxid = 11861; last; };
1803 /^AS42|^Avian musculoaponeurotic/ && do { $ncbi_taxid = 11873; last; };
1804 /^Avian myelocytomatosis/ && do { $ncbi_taxid = 11869; last; };
1805 /^ASV 31/ && do { $ncbi_taxid = 35270; last; };
1806 /^A-MuLV/ && do { $ncbi_taxid = 188539; last; };
1807 /^Asparagus officinalis/ && do { $ncbi_taxid = 4686; last; };
1808 /^Agrobacterium tumefaciens/ && do { $ncbi_taxid = 358; last; };
1809 /^ALV/ && do { $ncbi_taxid = 11864; last; };
1810 /^AAV/ && do { $ncbi_taxid = 272636; last; };
1811 /^AKV MLV/ && do { $ncbi_taxid = 11791; last; };
1812 last;
1813 };
1814  
1815 $first_letter eq 'B' && do {
1816 /^BPV-1/ && do { $ncbi_taxid = 10559; last; };
1817 /^BKV/ && do { $ncbi_taxid = 10629; last; };
1818 /^Bolivian squirrel monkey/ && do { $ncbi_taxid = 39432; last; };
1819 last;
1820 };
1821  
1822 $first_letter eq 'C' && do {
1823 /^Cauliflower/ && do { $ncbi_taxid = 3715; last; };
1824 /^Chamek/ && do { $ncbi_taxid = 118643; last; };
1825 /^Candida albicans/ && do { $ncbi_taxid = 5476; last; };
1826 /^CaMV/ && do { $ncbi_taxid = 10641; last; };
1827 last;
1828 };
1829  
1830 $first_letter eq 'E' && do {
1831 /^Eucalyptus gunnii/ && do { $ncbi_taxid = 3933; last; };
1832 /^EBV, Epstein-Barr virus/ && do { $ncbi_taxid = 10376; last; };
1833 /^Eucalyptus globulus subsp. bicostata/ && do { $ncbi_taxid = 71272; last; };
1834 /^Eucalyptus globulus subsp. globulus/ && do { $ncbi_taxid = 71271; last; };
1835 last;
1836 };
1837  
1838 $first_letter eq 'F' && do {
1839 /^FBR MuLV/ && do { $ncbi_taxid = 11806; last; };
1840 /^FBJ MuLV/ && do { $ncbi_taxid = 11805; last; };
1841 /^FeLV|Feline leukemia/ && do { $ncbi_taxid = 11923; last; };
1842 /^Flaveria trinervia/ && do { $ncbi_taxid = 4227; last; };
1843 /^FSV/ && do { $ncbi_taxid = 11885; last; };
1844 /^F-MuLV/ && do { $ncbi_taxid = 11795; last; };
1845 last;
1846 };
1847  
1848 $first_letter eq 'H' && do {
1849 /^HSV-1/ && do { $ncbi_taxid = 10298; last; };
1850 /^HTLV-I/ && do { $ncbi_taxid = 11908; last; };
1851 /^HIV-1/ && do { $ncbi_taxid = 11676; last; };
1852 /^HPV-16/ && do { $ncbi_taxid = 333760; last; };
1853 /^HBV/ && do { $ncbi_taxid = 10407; last; };
1854 /^HBI/ && do { $ncbi_taxid = 11867; last; };
1855 /^HPV-8/ && do { $ncbi_taxid = 10579; last; };
1856 /^HPV-11/ && do { $ncbi_taxid = 10580; last; };
1857 /^HPV-18/ && do { $ncbi_taxid = 333761; last; };
1858 /^HCMV/ && do { $ncbi_taxid = 10359; last; };
1859 /^HSV/ && do { $ncbi_taxid = 126283; last; };
1860 /^HSV-2/ && do { $ncbi_taxid = 10310; last; };
1861 /^HCV/ && do { $ncbi_taxid = 11108; last; };
1862 /^HIV-2/ && do { $ncbi_taxid = 11709; last; };
1863 last;
1864 };
1865  
1866 $first_letter eq 'M' && do {
1867 /^MMTV/ && do { $ncbi_taxid = 11757; last; };
1868 /^Mo-MuLV/ && do { $ncbi_taxid = 11801; last; };
1869 /^MuLV/ && do { $ncbi_taxid = 11786; last; };
1870 /^MSV/ && do { $ncbi_taxid = 11802; last; };
1871 /^MC29/ && do { $ncbi_taxid = 11868; last; };
1872 /^MVM/ && do { $ncbi_taxid = 10794; last; };
1873 /^MH2E21/ && do { $ncbi_taxid = 11955; last; }; # 11955 is a species, presumably MH2E21 is the strain
1874 last;
1875 };
1876  
1877 $first_letter eq 'R' && do {
1878 /^Raphanus sativus/ && do { $ncbi_taxid = 3726; last; };
1879 /^REV-T/ && do { $ncbi_taxid = 11636; last; };
1880 /^RAV-0/ && do { $ncbi_taxid = 11867; last; }; # should be rous-associated virus 0 variant
1881 /^RSV/ && do { $ncbi_taxid = 11886; last; };
1882 /^RadLV/ && do { $ncbi_taxid = 31689; last; };
1883 /^RTBV/ && do { $ncbi_taxid = 10654; last; };
1884 last;
1885 };
1886  
1887 $first_letter eq 'S' && do {
1888 /^SV40/ && do { $ncbi_taxid = 10633; last; };
1889 /^Sesbania rostrata/ && do { $ncbi_taxid = 3895; last; };
1890 /^SIV/ && do { $ncbi_taxid = 11723; last; };
1891 /^Spinacia oleracea/ && do { $ncbi_taxid = 3562; last; };
1892 /^SCMV/ && do { $ncbi_taxid = 10364; last; }; # supposed to be AGM isolate
1893 last;
1894 };
1895  
1896 # and lower case
1897 $first_letter eq 'a' && do {
1898 /^adenovirus type 5/ && do { $ncbi_taxid = 28285; last; };
1899 /^adenovirus type 2/ && do { $ncbi_taxid = 10515; last; };
1900 /^adenovirus/ && do { $ncbi_taxid = 189831; last; }; # 189831 ('unclassified Adenoviridae') is the closest I can get, but this has no genus and is not a species
1901 last;
1902 };
1903  
1904 $first_letter eq 'b' && do {
1905 /^bell pepper/ && do { $ncbi_taxid = 4072; last; };
1906 /^baculovirus, Autographa californica/ && do { $ncbi_taxid = 46015; last; };
1907 /^broccoli/ && do { $ncbi_taxid = 36774; last; };
1908 /^barley/ && do { $ncbi_taxid = 112509; last; };
1909 last;
1910 };
1911  
1912 $first_letter eq 'c' && do {
1913 /^clawed frog/ && do { $ncbi_taxid = 8355; last; };
1914 /^chipmunk/ && do { $ncbi_taxid = 64680; last; };
1915 /^common tree shrew/ && do { $ncbi_taxid = 37347; last; };
1916 /^cat/ && do { $ncbi_taxid = 9685; last; };
1917 last;
1918 };
1919  
1920 # and misc
1921 /^NK24/ && do { $ncbi_taxid = 11955; last; };
1922 /^OK10/ && do { $ncbi_taxid = 11871; last; };
1923 /^Dendrobium grex/ && do { $ncbi_taxid = 84618; last; };
1924 /^KSHV/ && do { $ncbi_taxid = 37296; last; };
1925 /^Oncidium/ && do { $ncbi_taxid = 96474; last; };
1926 /^Japanese quail/ && do { $ncbi_taxid = 93934; last; };
1927 /^Nile tilapia/ && do { $ncbi_taxid = 8128; last; };
1928 /^GALV/ && do { $ncbi_taxid = 11840; last; };
1929 /^JCV/ && do { $ncbi_taxid = 10632; last; };
1930 /^LPV/ && do { $ncbi_taxid = 10574; last; };
1931 /^Py,/ && do { $ncbi_taxid = 36362; last; };
1932 /^DHBV/ && do { $ncbi_taxid = 12639; last; };
1933 /^VZV/ && do { $ncbi_taxid = 10335; last; };
1934 /^Vicia faba/ && do { $ncbi_taxid = 3906; last; };
1935  
1936 /^hamster/ && do { $ncbi_taxid = 10029; last; };
1937 /^sea urchin/ && do { $ncbi_taxid = 7668; last; };
1938 /^fruit fly/ && do { $ncbi_taxid = 7227; last; };
1939 /^halibut/ && do { $ncbi_taxid = 8267; last; };
1940 /^vaccinia virus/ && do { $ncbi_taxid = 10245; last; };
1941 /^taxonomic class Mammalia/ && do { $ncbi_taxid = 40674; last; }; # not a species
1942 /^taxonomic class Vertebrata/ && do { $ncbi_taxid = 7742; last; }; # not a species
1943 /^dog/ && do { $ncbi_taxid = 9615; last; };
1944 /^parsley/ && do { $ncbi_taxid = 4043; last; };
1945 /^mouse, Mus domesticus Torino/ && do { $ncbi_taxid = 10092; last; }; # 10092 is domesticus subspecies, but not the Torino strain
1946 /^lemur, Eulemur fulvus collaris/ && do { $ncbi_taxid = 47178; last; };
1947 /^red sea bream/ && do { $ncbi_taxid = 143350; last; };
1948 /^zebra finch/ && do { $ncbi_taxid = 59729; last; };
1949 /^mung bean/ && do { $ncbi_taxid = 3916; last; };
1950 /^soybean/ && do { $ncbi_taxid = 3847; last; };
1951 /^oat/ && do { $ncbi_taxid = 4498; last; };
1952 /^pseudorabies virus/ && do { $ncbi_taxid = 10345; last; };
1953 }
1954 }
1955  
1956 $self->warn("Didn't know what species '$raw_species' was, unable to classify") unless $ncbi_taxid;
1957 return $ncbi_taxid;
1958 }
1959  
1960 sub DESTROY {
1961 my $self = shift;
1962 # Destroy tied references to close filehandles
1963 # and allow proper temporary files deletion
1964 undef $self->{_tax_db}->{'_nodes'};
1965 undef $self->{_tax_db}->{'_id2name'};
1966 undef $self->{_tax_db}->{'_name2id'};
1967 undef $self->{_tax_db}->{'_parent2children'};
1968 undef $self->{_tax_db}->{'_parentbtree'};
1969 }
1970  
1971 1;
« TFBS.pm ↑ Summary