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 subsequences from an indexed sequence file.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, $index, $fh, $record, $index_beg, $index_len, $beg, $end, $len, $seq );
43 $options = Maasha::Biopieces::parse_options(
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 },
53 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
54 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
56 if ( $options->{ 'beg' } ) {
57 $options->{ 'beg' }--;
59 $options->{ 'beg' } = 0;
62 $index = Maasha::Fasta::index_retrieve( $options->{ 'index' } . ".index" );
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' }) );
68 $fh = Maasha::Filesys::file_read_open( $options->{ 'index' } . ".seq" );
71 if ( $options->{ 'seq_name' } and exists $index->{ $options->{ 'seq_name' } } )
73 ( $index_beg, $index_len ) = @{ $index->{ $options->{ 'seq_name' } } };
75 $beg = $options->{ 'beg' };
76 $end = $options->{ 'end' };
77 $len = $options->{ 'len' };
79 $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len );
83 $record->{ 'SEQ_NAME' } = $options->{ 'seq_name' };
84 $record->{ 'SEQ' } = $seq;
85 $record->{ 'SEQ_LEN' } = length $record->{ 'SEQ' };
87 Maasha::Biopieces::put_record( $record, $out );
92 while ( $record = Maasha::Biopieces::get_record( $in ) )
94 if ( exists $index->{ $record->{ 'SEQ_NAME' } } )
96 ( $index_beg, $index_len ) = @{ $index->{ $record->{ 'SEQ_NAME' } } };
98 $beg = $record->{ 'BEG' } || $options->{ 'beg' };
99 $end = $record->{ 'END' } || $options->{ 'end' };
100 $len = $record->{ 'LEN' } || $options->{ 'len' };
102 $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len );
106 $record->{ 'SEQ' } = $seq;
107 $record->{ 'SEQ_LEN' } = length $record->{ 'SEQ' };
111 Maasha::Biopieces::put_record( $record, $out );
116 Maasha::Biopieces::close_stream( $in );
117 Maasha::Biopieces::close_stream( $out );
120 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125 # Martin A. Hansen, September 2009.
127 # Adjust the index coordianates according to specified options,
128 # and retrieves the sequence thus specified. The sequence is
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
141 my ( $beg, $len, $seq );
155 $len = $opt_end - $opt_beg;
159 $len = $index_len - $opt_beg;
162 if ( $len > $index_len - $opt_beg ) {
163 $len = $index_len - $opt_beg;
166 return if $beg > $index_beg + $index_len - 1 or $len <= 0;
168 $seq = Maasha::Filesys::file_read( $fh, $beg, $len );
174 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
179 Maasha::Biopieces::status_set();
185 Maasha::Biopieces::status_log();
189 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<