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