| 1 | package Bio::ToolBox::db_helper::wiggle; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use Bio::ToolBox::db_helper::constants; | ||
| 7 | use Bio::Graphics::Wiggle; | ||
| 8 | require Exporter; | ||
| 9 | |||
| 10 | our $VERSION = '2.00'; | ||
| 11 | |||
| 12 | # Exported names | ||
| 13 | our @ISA = qw(Exporter); | ||
| 14 | |||
| 15 | ## no critic | ||
| 16 | ## this is never intended to be used directly by end users | ||
| 17 | ## and exporting everything is required | ||
| 18 | our @EXPORT = qw( | ||
| 19 | collect_wig_scores | ||
| 20 | collect_wig_position_scores | ||
| 21 | ); | ||
| 22 | ## use critic | ||
| 23 | |||
| 24 | # Hashes of opened file objects | ||
| 25 | my %OPENED_WIGFILES; # opened wigfile objects | ||
| 26 | # in empirical testing, this doesn't really seem to speed things up | ||
| 27 | # like I thought it would | ||
| 28 | # oh well, keep it anyway???? | ||
| 29 | # I think this is safe to keep opened wigfiles cached, even across forks, | ||
| 30 | # since they are being opened only during data collection, which should | ||
| 31 | # only occur within child processes, and there is no explicit db open | ||
| 32 | |||
| 33 | sub collect_wig_scores { | ||
| 34 | |||
| 35 | # we will actually call collect_wig_position_scores() | ||
| 36 | # but only return the values | ||
| 37 | my $wig_data = collect_wig_position_scores(shift); | ||
| 38 | return unless $wig_data; | ||
| 39 | |||
| 40 | # return the values | ||
| 41 | my @values = values %{$wig_data}; | ||
| 42 | return wantarray ? @values : \@values; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub collect_wig_position_scores { | ||
| 46 | |||
| 47 | # passed parameters as array ref | ||
| 48 | # chromosome, start, stop, strand, strandedness, method, db, dataset | ||
| 49 | my $param = shift; | ||
| 50 | |||
| 51 | # look at each wigfile | ||
| 52 | # usually there is only one, but for stranded data there may be | ||
| 53 | # two wigfiles (+ and -), so we'll check each wig file for strand info | ||
| 54 | my %pos2score; # position => score | ||
| 55 | for ( my $d = DATA; $d < scalar @{$param}; $d++ ) { | ||
| 56 | |||
| 57 | my $feature = $param->[$d]; | ||
| 58 | confess 'FATAL: dataset is not a seqfeature object!' | ||
| 59 | unless ref($feature) =~ /seqfeature/i; | ||
| 60 | |||
| 61 | # Check which data to take based on strand | ||
| 62 | if ( | ||
| 63 | $param->[STND] eq 'all' # all data is requested | ||
| 64 | or $feature->strand == 0 # unstranded data | ||
| 65 | or ( | ||
| 66 | # sense data | ||
| 67 | $param->[STR] == $feature->strand and $param->[STND] eq 'sense' | ||
| 68 | ) | ||
| 69 | or ( | ||
| 70 | # antisense data | ||
| 71 | $param->[STR] != $feature->strand and $param->[STND] eq 'antisense' | ||
| 72 | ) | ||
| 73 | ) | ||
| 74 | { | ||
| 75 | # we have acceptable data to collect | ||
| 76 | |||
| 77 | # collect from wigfile if present | ||
| 78 | if ( $feature->has_tag('wigfile') ) { | ||
| 79 | |||
| 80 | # get wigfile name | ||
| 81 | my @wigfiles = $feature->get_tag_values('wigfile'); | ||
| 82 | my $wigfile = shift @wigfiles; # there should only be one wigfile | ||
| 83 | confess 'FATAL: no wigfile passed!' unless $wigfile; | ||
| 84 | |||
| 85 | # check for opened wigfile | ||
| 86 | my $wig; | ||
| 87 | if ( exists $OPENED_WIGFILES{$wigfile} ) { | ||
| 88 | |||
| 89 | # this file is already opened, use it | ||
| 90 | $wig = $OPENED_WIGFILES{$wigfile}; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | # this file has not been opened yet, open it | ||
| 94 | unless ( -e $wigfile ) { | ||
| 95 | confess "FATAL: Binary wiggle file '$wigfile' does not exist!"; | ||
| 96 | } | ||
| 97 | $wig = Bio::Graphics::Wiggle->new( $wigfile, 0 ); | ||
| 98 | unless ($wig) { | ||
| 99 | confess "FATAL: unable to open data wigfile '$wigfile'"; | ||
| 100 | } | ||
| 101 | |||
| 102 | # store the opened object for later use | ||
| 103 | $OPENED_WIGFILES{$wigfile} = $wig; | ||
| 104 | } | ||
| 105 | |||
| 106 | # adjust as necessary to avoid wig errors | ||
| 107 | if ( $param->[STRT] < $wig->start ) { | ||
| 108 | |||
| 109 | # adjust the start position | ||
| 110 | $param->[STRT] = $wig->start; | ||
| 111 | } | ||
| 112 | elsif ( $param->[STRT] > $wig->end ) { | ||
| 113 | |||
| 114 | # nothing we can do here, no values | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | if ( $param->[STOP] > $wig->end ) { | ||
| 118 | |||
| 119 | # adjust the end position | ||
| 120 | $param->[STOP] = $wig->end; | ||
| 121 | } | ||
| 122 | elsif ( $param->[STOP] < $wig->start ) { | ||
| 123 | |||
| 124 | # nothing we can do here, no values | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | # collect the wig values | ||
| 129 | my $scores_ref = $wig->values( $param->[STRT] => $param->[STOP] ); | ||
| 130 | |||
| 131 | # re-associate position with the scores | ||
| 132 | my $step = | ||
| 133 | $wig->step || 1; # step should always be defined but just in case | ||
| 134 | my $pos = $param->[STRT]; | ||
| 135 | foreach my $s ( @{$scores_ref} ) { | ||
| 136 | |||
| 137 | #print Dumper($s); | ||
| 138 | if ( defined $s ) { | ||
| 139 | |||
| 140 | # the binary wig file (.wib) is usually set up with | ||
| 141 | # a step of 1 bp, even if the original wig file was not | ||
| 142 | # this can result in lots of undefined values at the | ||
| 143 | # positions where there was no original data | ||
| 144 | # hence the defined check here | ||
| 145 | # store a real value in the hash keyed under the position | ||
| 146 | if ( $param->[METH] eq 'count' ) { | ||
| 147 | $pos2score{$pos} = 1; | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $pos2score{$pos} = $s; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | # adjust position by the step size, | ||
| 155 | $pos += $step; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # return the wig data hash | ||
| 162 | return wantarray ? %pos2score : \%pos2score; | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ |