]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_seq
fixed seq qual length check
[biopieces.git] / bp_bin / get_seq
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 subsequences from an indexed sequence 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::Fasta;
35 use Maasha::Seq;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $options, $in, $out, $index, $fh, $record, $index_beg, $index_len, $beg, $end, $len, $seq );
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 => 'seq_name', short => 's', 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         { long => 'len',      short => 'l', type => 'uint',   mandatory => 'no',  default => undef, allowed => undef, disallowed => 0 },
50     ]   
51 );
52
53 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
54 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
55
56 if ( $options->{ 'beg' } ) {
57     $options->{ 'beg' }--;
58 } else {
59     $options->{ 'beg' } = 0;
60 }
61
62 $index = Maasha::Fasta::index_retrieve( $options->{ 'index' } . ".index" );
63
64 if ( $index->{ 'FILE_SIZE' } != Maasha::Filesys::file_size( $options->{ 'index' } . ".seq" ) ) {
65     Maasha::Common::error( qq(Filesize mismatch: $options->{ 'index' }.seq != $index->{ 'FILE_SIZE' }) );
66 }
67
68 $fh = Maasha::Filesys::file_read_open( $options->{ 'index' } . ".seq" );
69
70
71 if ( $options->{ 'seq_name' } and exists $index->{ $options->{ 'seq_name' } } )
72 {
73     ( $index_beg, $index_len ) = @{ $index->{ $options->{ 'seq_name' } } };
74
75     $beg = $options->{ 'beg' };
76     $end = $options->{ 'end' };
77     $len = $options->{ 'len' };
78
79     $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len );
80
81     if ( $seq )
82     {
83         $record->{ 'SEQ_NAME' } = $options->{ 'seq_name' };
84         $record->{ 'SEQ' }      = $seq;
85         $record->{ 'SEQ_LEN' }  = length $record->{ 'SEQ' };
86
87         Maasha::Biopieces::put_record( $record, $out );
88     }
89 }
90
91
92 while ( $record = Maasha::Biopieces::get_record( $in ) )
93 {
94     if ( exists $index->{ $record->{ 'SEQ_NAME' } } )
95     {
96         ( $index_beg, $index_len ) = @{ $index->{ $record->{ 'SEQ_NAME' } } };
97
98         $beg = $record->{ 'BEG' } || $options->{ 'beg' };
99         $end = $record->{ 'END' } || $options->{ 'end' };
100         $len = $record->{ 'LEN' } || $options->{ 'len' };
101
102         $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len );
103
104         if ( $seq )
105         {
106             $record->{ 'SEQ' }      = $seq;
107             $record->{ 'SEQ_LEN' }  = length $record->{ 'SEQ' };
108         }
109     }
110
111     Maasha::Biopieces::put_record( $record, $out );
112 }
113
114 close $fh;
115
116 Maasha::Biopieces::close_stream( $in );
117 Maasha::Biopieces::close_stream( $out );
118
119
120 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121
122
123 sub seq_get
124 {
125     # Martin A. Hansen, September 2009.
126     
127     # Adjust the index coordianates according to specified options,
128     # and retrieves the sequence thus specified. The sequence is
129     # returned.
130
131     my ( $fh,          # filehandle to sequence file
132          $index_beg,   # begin position of sequence entry
133          $index_len,   # length of sequence entry
134          $opt_beg,     # optional begin position - OPTIONAL
135          $opt_end,     # optional end position   - OPTIONAL
136          $opt_len,     # optional length         - OPTIONAL
137        ) = @_;
138
139     # Returns a string.
140
141     my ( $beg, $len, $seq );
142
143     $beg = $index_beg;
144     
145     if ( $opt_beg ) {
146         $beg += $opt_beg;
147     }
148
149     if ( $opt_len )
150     {
151         $len = $opt_len;
152     }
153     elsif ( $opt_end )
154     {
155         $len = $opt_end - $opt_beg;
156     }
157     else
158     {
159         $len = $index_len - $opt_beg;
160     }
161
162     if ( $len > $index_len - $opt_beg ) {
163         $len = $index_len - $opt_beg;
164     }
165
166     return if $beg > $index_beg + $index_len - 1 or $len <= 0;
167
168     $seq = Maasha::Filesys::file_read( $fh, $beg, $len );
169
170     return $seq;
171 }
172
173
174 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
175
176
177 BEGIN
178 {
179     Maasha::Biopieces::status_set();
180 }
181
182
183 END
184 {
185     Maasha::Biopieces::status_log();
186 }
187
188
189 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190
191
192 __END__