]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_fixedstep
fixed seq qual length check
[biopieces.git] / bp_bin / get_fixedstep
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Extract fixedstep scores from an indexed fixedstep file.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Biopieces;
33 use Maasha::Filesys;
34 use Maasha::Calc;
35 use Maasha::UCSC::Wiggle;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $in, $out, $index, $fh, $record, $new_record, $subindex, $entry, $scores );
42
43 $options = Maasha::Biopieces::parse_options(
44     [
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 },
49     ]   
50 );
51
52 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
54
55 $index = Maasha::UCSC::Wiggle::fixedstep_index_retrieve( $options->{ 'index' } . ".index" );
56
57 $fh = Maasha::Filesys::file_read_open( $options->{ 'index' } . ".wig" );
58
59 if ( $options->{ 'chr' } and exists $index->{ $options->{ 'chr' } } )
60 {
61     $subindex = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $options->{ 'chr' }, $options->{ 'beg' }, $options->{ 'end' } );
62
63     foreach $entry ( @{ $subindex } )
64     {
65         $scores = get_scores( $fh, $entry->{ 'INDEX_BEG' }, $entry->{ 'INDEX_LEN' } );
66
67         if ( $options->{ 'beg' } > $entry->{ 'CHR_BEG' } ) {
68             $scores = [ @{ $scores }[ ( $options->{ 'beg' } - $entry->{ 'CHR_BEG' } ) .. scalar @{ $scores } - 1 ] ];
69         }
70
71         if ( $options->{ 'end' } < $entry->{ 'CHR_END' } ) {
72             $scores = [ @{ $scores }[ 0 .. ( $options->{ 'end' } - $options->{ 'beg' } ) ] ];
73         }
74     
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 };
80
81         Maasha::Biopieces::put_record( $new_record, $out );
82     }
83 }
84
85
86 while ( $record = Maasha::Biopieces::get_record( $in ) )
87 {
88     if ( $record->{ 'CHR' } and $record->{ 'CHR_BEG' } and $record->{ 'CHR_END' } )
89     {
90         if ( exists $index->{ $record->{ 'CHR' } } )
91         {
92             $subindex = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $record->{ 'CHR' }, $record->{ 'CHR_BEG' }, $record->{ 'CHR_END' } );
93
94             foreach $entry ( @{ $subindex } )
95             {
96                 $scores = get_scores( $fh, $entry->{ 'INDEX_BEG' }, $entry->{ 'INDEX_LEN' } );
97
98                 if ( $record->{ 'CHR_BEG' } > $entry->{ 'CHR_BEG' } ) {
99                     $scores = [ @{ $scores }[ ( $record->{ 'CHR_BEG' } - $entry->{ 'CHR_BEG' } ) .. scalar @{ $scores } - 1 ] ];
100                 }
101
102                 if ( $record->{ 'CHR_END' } < $entry->{ 'CHR_END' } ) {
103                     $scores = [ @{ $scores }[ 0 .. ( $record->{ 'CHR_END' } - $record->{ 'CHR_BEG' } ) ] ];
104                 }
105             
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 };
111
112                 Maasha::Biopieces::put_record( $new_record, $out );
113             }
114         }
115     }
116 }
117
118 close $fh;
119
120 Maasha::Biopieces::close_stream( $in );
121 Maasha::Biopieces::close_stream( $out );
122
123
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125
126
127 sub get_scores
128 {
129     # Martin A. Hansen, June 2010.
130    
131     # Get scores from a fixedstep file based on index
132     # offset and length.
133
134     my ( $fh,       # filehandle to fixedstep file
135          $offset,   # file offset
136          $len,      # length
137        ) = @_;
138
139     # Returns a list.
140
141     my ( $block, @scores );
142
143     $block = Maasha::Filesys::file_read( $fh, $offset, $len );
144
145     @scores = split "\n", $block;
146
147     return wantarray ? @scores : \@scores;
148 }
149
150
151 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
152
153
154 BEGIN
155 {
156     Maasha::Biopieces::status_set();
157 }
158
159
160 END
161 {
162     Maasha::Biopieces::status_log();
163 }
164
165
166 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
167
168
169 __END__