3 # Copyright (C) 2007-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Extract fixedstep scores from an indexed fixedstep file.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
35 use Maasha::UCSC::Wiggle;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, $index, $fh, $record, $new_record, $subindex, $entry, $scores );
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'index', short => 'i', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
46 { long => 'chr', short => 'c', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'beg', short => 'b', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
48 { long => 'end', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
52 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
55 $index = Maasha::UCSC::Wiggle::fixedstep_index_retrieve( $options->{ 'index' } . ".index" );
57 $fh = Maasha::Filesys::file_read_open( $options->{ 'index' } . ".wig" );
59 if ( $options->{ 'chr' } and exists $index->{ $options->{ 'chr' } } )
61 $subindex = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $options->{ 'chr' }, $options->{ 'beg' }, $options->{ 'end' } );
63 foreach $entry ( @{ $subindex } )
65 $scores = get_scores( $fh, $entry->{ 'INDEX_BEG' }, $entry->{ 'INDEX_LEN' } );
67 if ( $options->{ 'beg' } > $entry->{ 'CHR_BEG' } ) {
68 $scores = [ @{ $scores }[ ( $options->{ 'beg' } - $entry->{ 'CHR_BEG' } ) .. scalar @{ $scores } - 1 ] ];
71 if ( $options->{ 'end' } < $entry->{ 'CHR_END' } ) {
72 $scores = [ @{ $scores }[ 0 .. ( $options->{ 'end' } - $options->{ 'beg' } ) ] ];
75 $new_record->{ 'REC_TYPE' } = 'fixed_step';
76 $new_record->{ 'STEP' } = 1;
77 $new_record->{ 'CHR' } = $options->{ 'chr' };
78 $new_record->{ 'CHR_BEG' } = Maasha::Calc::max( $options->{ 'beg' }, $entry->{ 'CHR_BEG' } );
79 $new_record->{ 'VALS' } = join ";", @{ $scores };
81 Maasha::Biopieces::put_record( $new_record, $out );
86 while ( $record = Maasha::Biopieces::get_record( $in ) )
88 if ( $record->{ 'CHR' } and $record->{ 'CHR_BEG' } and $record->{ 'CHR_END' } )
90 if ( exists $index->{ $record->{ 'CHR' } } )
92 $subindex = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $record->{ 'CHR' }, $record->{ 'CHR_BEG' }, $record->{ 'CHR_END' } );
94 foreach $entry ( @{ $subindex } )
96 $scores = get_scores( $fh, $entry->{ 'INDEX_BEG' }, $entry->{ 'INDEX_LEN' } );
98 if ( $record->{ 'CHR_BEG' } > $entry->{ 'CHR_BEG' } ) {
99 $scores = [ @{ $scores }[ ( $record->{ 'CHR_BEG' } - $entry->{ 'CHR_BEG' } ) .. scalar @{ $scores } - 1 ] ];
102 if ( $record->{ 'CHR_END' } < $entry->{ 'CHR_END' } ) {
103 $scores = [ @{ $scores }[ 0 .. ( $record->{ 'CHR_END' } - $record->{ 'CHR_BEG' } ) ] ];
106 $new_record->{ 'REC_TYPE' } = 'fixed_step';
107 $new_record->{ 'STEP' } = 1;
108 $new_record->{ 'CHR' } = $record->{ 'CHR' };
109 $new_record->{ 'CHR_BEG' } = Maasha::Calc::max( $record->{ 'CHR_BEG' }, $entry->{ 'CHR_BEG' } );
110 $new_record->{ 'VALS' } = join ";", @{ $scores };
112 Maasha::Biopieces::put_record( $new_record, $out );
120 Maasha::Biopieces::close_stream( $in );
121 Maasha::Biopieces::close_stream( $out );
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
129 # Martin A. Hansen, June 2010.
131 # Get scores from a fixedstep file based on index
134 my ( $fh, # filehandle to fixedstep file
135 $offset, # file offset
141 my ( $block, @scores );
143 $block = Maasha::Filesys::file_read( $fh, $offset, $len );
145 @scores = split "\n", $block;
147 return wantarray ? @scores : \@scores;
151 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
156 Maasha::Biopieces::status_set();
162 Maasha::Biopieces::status_log();
166 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<