| 1 | package Bio::ToolBox::db_helper::seqfasta; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use English qw(-no_match_vars); | ||
| 7 | use Module::Load; # for dynamic loading during runtime | ||
| 8 | use List::Util qw(min max sum); | ||
| 9 | use Statistics::Lite qw(median); | ||
| 10 | use Bio::ToolBox::db_helper::constants; | ||
| 11 | use Bio::ToolBox::db_helper::config; | ||
| 12 | use Bio::DB::Fasta; | ||
| 13 | use Bio::DB::SeqFeature::Store; | ||
| 14 | require Exporter; | ||
| 15 | |||
| 16 | our $VERSION = '2.00'; | ||
| 17 | |||
| 18 | # Exported names | ||
| 19 | our @ISA = qw(Exporter); | ||
| 20 | |||
| 21 | ## no critic | ||
| 22 | ## this is never intended to be used directly by end users | ||
| 23 | ## and exporting everything is required | ||
| 24 | our @EXPORT = qw( | ||
| 25 | open_fasta_db | ||
| 26 | open_store_db | ||
| 27 | collect_store_scores | ||
| 28 | ); | ||
| 29 | ## use critic | ||
| 30 | |||
| 31 | my $WIGGLE_OK = 0; | ||
| 32 | my $CONFIG_OK = 0; | ||
| 33 | |||
| 34 | sub open_fasta_db { | ||
| 35 | my $database = shift; | ||
| 36 | my $db; | ||
| 37 | |||
| 38 | eval { | ||
| 39 | # to prevent annoying error messages, we silence warnings | ||
| 40 | # we are often trying to open without absolute confirmation this will work | ||
| 41 | # hence working inside an eval | ||
| 42 | local $SIG{__WARN__} = sub { }; | ||
| 43 | $db = Bio::DB::Fasta->new($database); | ||
| 44 | }; | ||
| 45 | unless ($db) { | ||
| 46 | carp | ||
| 47 | "ERROR: unable to open fasta file as database! Make sure the file's directory\n" | ||
| 48 | . "writable, and if a *.index file exists, try deleting it so it can be rebuilt\n"; | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | return $db; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub open_store_db { | ||
| 55 | my $database = shift; | ||
| 56 | my $db; | ||
| 57 | |||
| 58 | # first determine type of database we're dealing with | ||
| 59 | # by checking for an extension | ||
| 60 | |||
| 61 | # GFF3 file to be loaded into memory | ||
| 62 | if ( $database =~ m/\.gff3? (?:\.gz)? $/xi ) { | ||
| 63 | |||
| 64 | # gff3 file can be gzipped | ||
| 65 | # this might take a while, so print a statement | ||
| 66 | print " Loading file into memory database...\n"; | ||
| 67 | eval { | ||
| 68 | $db = Bio::DB::SeqFeature::Store->new( | ||
| 69 | -adaptor => 'memory', | ||
| 70 | -gff => $database, | ||
| 71 | ); | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | elsif ( $database =~ m/\. (?: db | sqlite )$/xi ) { | ||
| 76 | eval { | ||
| 77 | $db = Bio::DB::SeqFeature::Store->new( | ||
| 78 | -adaptor => 'DBI::SQLite', | ||
| 79 | -dsn => $database, | ||
| 80 | ); | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | |||
| 84 | else { | ||
| 85 | # open the connection using parameters from the configuration file | ||
| 86 | # we'll try to use database specific parameters first, else use | ||
| 87 | # the db_default parameters | ||
| 88 | my $adaptor = $BTB_CONFIG->param( $database . '.adaptor' ) | ||
| 89 | || $BTB_CONFIG->param('default_db.adaptor'); | ||
| 90 | my $user = $BTB_CONFIG->param( $database . '.user' ) | ||
| 91 | || $BTB_CONFIG->param('default_db.user'); | ||
| 92 | my $pass = | ||
| 93 | $BTB_CONFIG->param( $database . '.pass' ) | ||
| 94 | || $BTB_CONFIG->param('default_db.pass') | ||
| 95 | || undef; | ||
| 96 | |||
| 97 | # check for empty passwords | ||
| 98 | # config::simple passes an empty array when nothing was defined | ||
| 99 | if ( ref($pass) eq 'ARRAY' and scalar @{$pass} == 0 ) { $pass = undef } | ||
| 100 | |||
| 101 | # set up the dsn | ||
| 102 | # it can be specifically defined | ||
| 103 | my $dsn = $BTB_CONFIG->param( $database . '.dsn' ) || undef; | ||
| 104 | unless ( defined $dsn ) { | ||
| 105 | |||
| 106 | # or dsn can be generated with the dsn_prefix | ||
| 107 | $dsn = $BTB_CONFIG->param( $database . '.dsn_prefix' ) | ||
| 108 | || $BTB_CONFIG->param('default_db.dsn_prefix'); | ||
| 109 | $dsn .= $database; | ||
| 110 | } | ||
| 111 | |||
| 112 | # establish the database connection | ||
| 113 | eval { | ||
| 114 | # to prevent annoying error messages from B:DB:SF:S | ||
| 115 | local $SIG{__WARN__} = sub { }; | ||
| 116 | |||
| 117 | # attempt a connection | ||
| 118 | $db = Bio::DB::SeqFeature::Store->new( | ||
| 119 | -adaptor => $adaptor, | ||
| 120 | -dsn => $dsn, | ||
| 121 | -user => $user, | ||
| 122 | -pass => $pass, | ||
| 123 | ); | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | |||
| 127 | # return opened database object or nothing if unsuccessful | ||
| 128 | return unless $db; | ||
| 129 | return $db; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub collect_store_scores { | ||
| 133 | |||
| 134 | # pass the required information | ||
| 135 | # passed parameters as array ref | ||
| 136 | # chromosome, start, stop, strand, strandedness, method, db, dataset | ||
| 137 | my $param = shift; | ||
| 138 | |||
| 139 | # database feature types | ||
| 140 | my @types = splice( @{$param}, DATA ); | ||
| 141 | |||
| 142 | # set up iterator from database | ||
| 143 | my $iterator = $param->[DB]->get_seq_stream( | ||
| 144 | -seq_id => $param->[CHR], | ||
| 145 | -start => $param->[STRT], | ||
| 146 | -end => $param->[STOP], | ||
| 147 | -primary_tag => \@types, | ||
| 148 | ); | ||
| 149 | return unless $iterator; # we should always get an iterator back, even if the | ||
| 150 | # iterator returns nothing useful for us | ||
| 151 | |||
| 152 | # collect the first feature | ||
| 153 | my $feature = $iterator->next_seq; | ||
| 154 | unless ($feature) { | ||
| 155 | |||
| 156 | # that's odd, we should get a feature | ||
| 157 | # try rebuilding the iterator with an alternate chromosome name | ||
| 158 | my $chromo = $param->[CHR] =~ /^chr(.+)$/i ? $1 : 'chr' . $param->[CHR]; | ||
| 159 | $iterator = $param->[DB]->get_seq_stream( | ||
| 160 | -seq_id => $chromo, | ||
| 161 | -start => $param->[STRT], | ||
| 162 | -end => $param->[STOP], | ||
| 163 | -primary_tag => \@types, | ||
| 164 | ); | ||
| 165 | $feature = $iterator->next_seq or return; | ||
| 166 | } | ||
| 167 | |||
| 168 | # places to stick the scores | ||
| 169 | my @scores; | ||
| 170 | my %pos2data; | ||
| 171 | |||
| 172 | ## First check for potential Wig Data | ||
| 173 | # the legacy wiggle adaptor uses a GFF seqfeature db to store file paths | ||
| 174 | # to the individual chromosome wib files | ||
| 175 | # if this is the case, we must pass this on to the wiggle adaptor | ||
| 176 | if ( $feature->has_tag('wigfile') ) { | ||
| 177 | |||
| 178 | # data is in wig format, or at least the first datapoint is | ||
| 179 | |||
| 180 | # determine the type of wigfile | ||
| 181 | my ($wigfile) = $feature->get_tag_values('wigfile'); | ||
| 182 | |||
| 183 | ## Bio::Graphics wib file | ||
| 184 | if ( $wigfile =~ /\.wib$/ ) { | ||
| 185 | |||
| 186 | # data is in old-style binary wiggle format | ||
| 187 | # based on the Bio::Graphics::Wiggle adaptor | ||
| 188 | |||
| 189 | # get the full list of features to pass off to the | ||
| 190 | # helper subroutine | ||
| 191 | push @{$param}, $feature; | ||
| 192 | while ( my $f = $iterator->next_seq ) { | ||
| 193 | push @{$param}, $f; | ||
| 194 | } | ||
| 195 | |||
| 196 | # check that we have wiggle support | ||
| 197 | unless ($WIGGLE_OK) { | ||
| 198 | eval { | ||
| 199 | my $class = 'Bio::ToolBox::db_helper::wiggle'; | ||
| 200 | autoload $class; | ||
| 201 | $WIGGLE_OK = 1; | ||
| 202 | }; | ||
| 203 | } | ||
| 204 | if ($WIGGLE_OK) { | ||
| 205 | |||
| 206 | # get the dataset scores using Bio::ToolBox::db_helper::wiggle | ||
| 207 | if ( $param->[RETT] == 2 ) { | ||
| 208 | return collect_wig_position_scores($param); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | return collect_wig_scores($param); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | croak "FATAL: Wiggle support is not enabled! $EVAL_ERROR"; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | ## BigWig file | ||
| 220 | elsif ( $wigfile =~ /\.bw$/ ) { | ||
| 221 | |||
| 222 | # data is in bigwig format | ||
| 223 | # we are abandoning this support | ||
| 224 | croak 'FATAL: Supporting bigWig files via seqfeature attribute is no longer ' | ||
| 225 | . "supported.\n Please use bigWig files directly\n"; | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | croak "FATAL: Unrecognized wigfile attribute '$wigfile'!" | ||
| 229 | . " Unable to continue!\n"; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | ## Database Data | ||
| 234 | # Working with data stored directly in the database | ||
| 235 | # this is more straight forward in collection | ||
| 236 | |||
| 237 | # Walk through the datapoints | ||
| 238 | while ($feature) { | ||
| 239 | |||
| 240 | # Check which data to take based on strand | ||
| 241 | if ( | ||
| 242 | $param->[STND] eq 'all' # all data is requested | ||
| 243 | or $feature->strand == 0 # unstranded data | ||
| 244 | or ( | ||
| 245 | # sense data | ||
| 246 | $param->[STR] == $feature->strand and $param->[STND] eq 'sense' | ||
| 247 | ) | ||
| 248 | or ( | ||
| 249 | # antisense data | ||
| 250 | $param->[STR] != $feature->strand and $param->[STND] eq 'antisense' | ||
| 251 | ) | ||
| 252 | ) | ||
| 253 | { | ||
| 254 | # we have acceptable data to collect | ||
| 255 | |||
| 256 | # data is in the database | ||
| 257 | # much easier to collect | ||
| 258 | |||
| 259 | # store data in either indexed hash or score array | ||
| 260 | if ( $param->[RETT] == 2 ) { | ||
| 261 | |||
| 262 | # determine position to record | ||
| 263 | my $position; | ||
| 264 | if ( $feature->start == $feature->end ) { | ||
| 265 | |||
| 266 | # just one position recorded | ||
| 267 | $position = $feature->start; | ||
| 268 | } | ||
| 269 | else { | ||
| 270 | # calculate the midpoint | ||
| 271 | $position = int( ( $feature->start + $feature->end ) / 2 ); | ||
| 272 | } | ||
| 273 | |||
| 274 | # store the appropriate value | ||
| 275 | if ( $param->[METH] eq 'count' ) { | ||
| 276 | $pos2data{$position} += 1; | ||
| 277 | } | ||
| 278 | elsif ( $param->[METH] eq 'pcount' ) { | ||
| 279 | $pos2data{$position} += 1 | ||
| 280 | if ( $feature->start >= $param->[STRT] | ||
| 281 | and $feature->end <= $param->[STOP] ); | ||
| 282 | } | ||
| 283 | elsif ( $param->[METH] eq 'ncount' ) { | ||
| 284 | push @{ $pos2data{$position} }, | ||
| 285 | $feature->display_name | ||
| 286 | || $feature->primary_id | ||
| 287 | || sprintf( "%s:%s-%s:%s", | ||
| 288 | $feature->seq_id, $feature->start, | ||
| 289 | $feature->end, $feature->strand ); | ||
| 290 | } | ||
| 291 | else { | ||
| 292 | push @{ $pos2data{$position} }, $feature->score; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | else { | ||
| 297 | # just store the score in the array | ||
| 298 | # store the appropriate value | ||
| 299 | if ( $param->[METH] eq 'count' ) { | ||
| 300 | push @scores, 1; | ||
| 301 | } | ||
| 302 | elsif ( $param->[METH] eq 'pcount' ) { | ||
| 303 | push @scores, 1 | ||
| 304 | if ( $feature->start >= $param->[STRT] | ||
| 305 | and $feature->end <= $param->[STOP] ); | ||
| 306 | } | ||
| 307 | elsif ( $param->[METH] eq 'ncount' ) { | ||
| 308 | push @scores, | ||
| 309 | $feature->display_name | ||
| 310 | || $feature->primary_id | ||
| 311 | || sprintf( "%s:%s-%s:%s", | ||
| 312 | $feature->seq_id, $feature->start, | ||
| 313 | $feature->end, $feature->strand ); | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | push @scores, $feature->score; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | # prepare for next | ||
| 322 | $feature = $iterator->next_seq || undef; | ||
| 323 | } | ||
| 324 | |||
| 325 | # post-process the collected position->score values | ||
| 326 | # combine multiple values recorded at the same position | ||
| 327 | if ( $param->[RETT] == 2 ) { | ||
| 328 | if ( $param->[METH] eq 'ncount' ) { | ||
| 329 | foreach my $position ( keys %pos2data ) { | ||
| 330 | my %name2count; | ||
| 331 | foreach ( @{ $pos2data{$position} } ) { $name2count{$_} += 1 } | ||
| 332 | $pos2data{$position} = scalar( keys %name2count ); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | elsif ( $param->[METH] eq 'count' or $param->[METH] eq 'pcount' ) { | ||
| 336 | |||
| 337 | # do nothing, these aren't arrays | ||
| 338 | } | ||
| 339 | elsif ( $param->[METH] eq 'mean' ) { | ||
| 340 | foreach my $position ( keys %pos2data ) { | ||
| 341 | $pos2data{$position} = sum( @{ $pos2data{$position} } ) / | ||
| 342 | scalar( @{ $pos2data{$position} } ); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | elsif ( $param->[METH] eq 'median' ) { | ||
| 346 | foreach my $position ( keys %pos2data ) { | ||
| 347 | $pos2data{$position} = median( @{ $pos2data{$position} } ); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | elsif ( $param->[METH] eq 'min' ) { | ||
| 351 | foreach my $position ( keys %pos2data ) { | ||
| 352 | $pos2data{$position} = min( @{ $pos2data{$position} } ); | ||
| 353 | } | ||
| 354 | } | ||
| 355 | elsif ( $param->[METH] eq 'max' ) { | ||
| 356 | foreach my $position ( keys %pos2data ) { | ||
| 357 | $pos2data{$position} = max( @{ $pos2data{$position} } ); | ||
| 358 | } | ||
| 359 | } | ||
| 360 | elsif ( $param->[METH] eq 'sum' ) { | ||
| 361 | foreach my $position ( keys %pos2data ) { | ||
| 362 | $pos2data{$position} = sum( @{ $pos2data{$position} } ); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | else { | ||
| 366 | # just take the mean for everything else | ||
| 367 | foreach my $position ( keys %pos2data ) { | ||
| 368 | $pos2data{$position} = sum( @{ $pos2data{$position} } ) / | ||
| 369 | scalar( @{ $pos2data{$position} } ); | ||
| 370 | } | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | # return the appropriate data | ||
| 375 | if ( $param->[RETT] == 2 ) { | ||
| 376 | return wantarray ? %pos2data : \%pos2data; | ||
| 377 | } | ||
| 378 | else { | ||
| 379 | return wantarray ? @scores : \@scores; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | 1; | ||
| 384 | |||
| 385 | __END__ |