blib/lib/Bio/Graphics/FeatureDir.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 219 CC 21 SCAR 41.3
CC
21
Cov
0%
add_fh
182.0
_init_featuredb
20.0
_init_conf
12.0
CRAP 62.1
1 package Bio::Graphics::FeatureDir;
2  
3 =head1 NAME
4  
5 Bio::Graphics::FeatureDir -- A directory of feature files and conf files
6  
7 =head1 SYNOPSIS
8  
9 my $fd = Bio::Graphics::FeatureDir->new('/path/to/dir');
10 $fd->add_file('tracks.conf');
11 $fd->add_file('foo.gff3');
12 $fd->add_file('foo.wig');
13 $fd->add_fh(\*STDIN);
14
15 my $option = $fd->setting('EST' => 'bgcolor');
16 my @features = $fd->get_features_by_name('M101');
17  
18 =head1 DESCRIPTION
19  
20 This class implements most of the methods of
21 Bio::Graphics::FeatureFile, but stores the data files and features in
22 a directory indexed by the Bio::DB::SeqFeature::Store::berkeleydb
23 adaptor. Therefore it is fast.
24  
25 =head2 Methods
26  
27 =over 4
28  
29 =cut
30  
31 use strict;
32 use warnings;
33 use base 'Bio::Graphics::FeatureFile';
34  
35 use Bio::DB::SeqFeature::Store;
36 use File::Path;
37 use File::Spec;
38 use File::Basename 'basename';
39 use File::Temp 'tempdir','mktemp';
40 use Carp 'croak';
41  
42 =item $fd = Bio::Graphics::FeatureDir->new('/path/to/dir');
43  
44 =item $fd = Bio::Graphics::FeatureDir->new(-dir => '/path/to/dir');
45  
46 Create a new FeatureDir, based in the indicated directory. In addition
47 to the -dir directory argument, it takes any of the options that can
48 be passed to Bio::Graphics::FeatureFile except for the -file and -text
49 arguments;
50  
51 =cut
52  
53 sub new {
54 my $class = shift;
55 my %args;
56 if (@_ == 1) {
57 %args = (-dir=>shift);
58 } else {
59 %args = @_;
60 }
61 $args{-dir} ||= tempdir(CLEANUP=>1);
62 delete $args{-file};
63 delete $args{-text};
64  
65 my $self = $class->SUPER::new(%args);
66 $self->{dir} = $args{-dir};
67 $self->{verbose} = $args{-verbose};
68 $self->_init_featuredb;
69 $self->_init_conf;
70 return $self;
71 }
72  
73 =item $db->_init_featuredb
74  
75 Internal method. Initializes the underlying feature database.
76  
77 =cut
78  
79 sub _init_featuredb {
80 my $self = shift;
81 my $dir = $self->dir;
82 my $needs_init = $self->_maybe_create_dir($dir);
83 my $index = File::Spec->catfile($dir,"indexes");
84 $needs_init++ unless -e $index;
85  
86 my @args = (-adaptor => 'berkeleydb',
87 -dir => $dir,
88 -write => 1,
89 );
90 push @args,(-create => 1) if $needs_init;
91 push @args,(-verbose => 1) if $self->{verbose};
92  
93 $self->{db} = Bio::DB::SeqFeature::Store->new(@args)
94 or die "Couldn't initialize database";
95 return $self->{db};
96 }
97  
98 =item $db->_init_conf
99  
100 Internal method -- initialize the configuration file(s)
101  
102 =cut
103  
104 sub _init_conf {
105 my $self = shift;
106 my $dir = $self->dir;
107 my $needs_init = $self->_maybe_create_dir($dir);
108 my $master_conf = File::Spec->catfile($dir,'indexes','master.conf');
109 $needs_init++ unless -e $master_conf;
110 if ($needs_init) {
111 open my $fh,'>',$master_conf or die "$master_conf: $!";
112 my $pack = __PACKAGE__;
113 print $fh <<END;
114 [GENERAL]
115 description = Master configuration file created by $pack.
116  
117 #include "../*.conf";
118 END
119 close $fh;
120 }
121 $self->{conf} = Bio::Graphics::FeatureFile->new(-file=>$master_conf);
122 }
123  
124 =item $created = $db->_maybe_create_dir($dir)
125  
126 Create $dir and its parents if it doesn't exist. Return true if the directory
127 was created. Throws an exception on filesystem errors.
128  
129 =cut
130  
131 sub _maybe_create_dir {
132 my $self = shift;
133 my $dir = shift || $self->{dir};
134 unless (-e $dir && -d $dir) {
135 mkpath($dir) or die "Couldn't create directory $dir: $!";
136 return 1;
137 }
138 return;
139 }
140  
141  
142 =item $dir = $db->dir
143  
144 Returns the base directory.
145  
146 =cut
147  
148 sub dir {shift->{dir}}
149  
150 =item $conf = $db->conf
151  
152 Returns the underlying Bio::Graphics::FeatureFile object
153  
154 =cut
155  
156 sub conf {shift->{conf}}
157  
158 =back
159  
160 =item $db = $db->db
161  
162 Returns the underlying Bio::DB::SeqFeature::Store object
163  
164 =cut
165  
166 sub db {
167 my $self = shift;
168 return $self->{db} ||= Bio::DB::SeqFeature::Store->new(
169 -adaptor => 'berkeleydb',
170 -dir => $self->dir,
171 -write => 1);
172 }
173  
174 =item $db->add_file($file)
175  
176 Add the file to the directory. Can add files of type .fa, .gff, .gff3,
177 .conf and .ff.
178  
179 =cut
180  
181 sub add_file {
182 my $self = shift;
183 my $file = shift;
184 my $basename = basename $file;
185 open my $fh,$file or croak "Couldn't open $file: $!";
186 $self->add_fh($fh,$basename);
187 close $fh;
188 }
189  
190 =item $db->add_fh(\*FILEHANDLE [,'name'])
191  
192 Add the contents of the indicated filehandle to repository. Name is
193 optional; if provided it will be used as the base for all files
194 created.
195  
196 =cut
197  
198 sub add_fh {
199 my $self = shift;
200 my ($fh,$name) = @_;
201 $name =~ s/\.\w+$//; # get rid of extensions
202 $name ||= mktemp('XXXXXXXX');
203  
204 # status == unknown
205 # config
206 # gff3
207 # gff2
208 # ff
209 # wiggle
210 # fasta
211 my ($status,$new_status);
212 my $dir = $self->dir;
213 my %splitter;
214
215 while (<$fh>) {
216 # figure out transitions
217 $new_status = /^\#\#gff-version\s+3/i ? 'gff3'
218 :/^\#\#gff/i ? 'gff2'
219 : /^track/i ? 'wig'
220 : /^\[(.+)\]/i ? 'conf'
221 : /^>\w+/i ? 'fa'
222 : /^reference/i ? 'ff'
223 : undef;
224  
225 unless ($status || $new_status) { # guess what it is
226 my @tokens = split /\s+/;
227 $new_status = 'gff3' if @tokens >= 9 && $tokens[8] =~ /=/;
228 $new_status = 'ff' if $tokens[2] =~ /\d+(\.\.|-)\d+/;
229 }
230  
231
232  
233 if ($new_status) {
234 # this will create a new conf file for each section
235 if ($new_status eq 'conf') {
236 $splitter{conf} = Bio::Graphics::FileSplitter->new(
237 File::Spec->catfile($dir,"${name}.$1.${new_status}"));
238 }
239 else {
240 $splitter{$new_status} ||= Bio::Graphics::FileSplitter->new(
241 File::Spec->catfile($dir,"${name}.${new_status}"));
242 }
243  
244 $status = $new_status;
245 }
246  
247 next unless $splitter{$status};
248 $splitter{$status}->write($_);
249 }
250 undef %splitter;
251 $self->db->auto_reindex($dir);
252 $self->_init_conf;
253 }
254  
255 package Bio::Graphics::FileSplitter;
256  
257 sub new {
258 my $class = shift;
259 my $path = shift;
260 open my $fh,'>',$path or die "Could not open $path for writing: $!";
261 return bless {fh=>$fh},ref $class || $class;
262 }
263 sub write {
264 my $self = shift;
265 $self->{fh}->print($_) foreach @_;
266 }
267 sub DESTROY {
268 my $fh = shift->{fh};
269 close $fh if $fh;
270 }
271  
272 =cut
273  
274 =head1 SEE ALSO
275  
276 L<Bio::Graphics::Feature>,
277 L<Bio::Graphics::FeatureFile>
278  
279 =head1 AUTHOR
280  
281 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
282  
283 Copyright (c) 2009 Ontario Institute for Cancer Research
284  
285 This package and its accompanying libraries is free software; you can
286 redistribute it and/or modify it under the terms of the GPL (either
287 version 1, or at your option, any later version) or the Artistic
288 License 2.0. Refer to LICENSE for the full license text. In addition,
289 please see DISCLAIMER.txt for disclaimers of warranty.
290  
291 =cut
292  
293  
294 1;