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 a genome sequence.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
37 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 my ( $options, $in, $out, $record, $genome_file, $index_file, $fh, $genome,
41 $beg, $end, $len, $index_beg, $index_len, @begs, @lens, $index, $seq, $i, %lookup_hash );
43 $options = Maasha::Biopieces::parse_options(
45 { long => 'genome', short => 'g', type => 'genome', 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 { long => 'len', short => 'l', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
50 { long => 'flank', short => 'f', type => 'uint', mandatory => 'no', default => 0, allowed => undef, disallowed => undef },
51 { long => 'mask', short => 'm', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
52 { long => 'splice', short => 's', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
56 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
57 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59 if ( $options->{ "genome" } )
61 $genome = $options->{ "genome" };
63 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
64 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
66 $fh = Maasha::Filesys::file_read_open( $genome_file );
67 $index = Maasha::Fasta::index_retrieve( $index_file );
69 shift @{ $index }; # Get rid of the file size info
71 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
73 if ( defined $options->{ "chr" } and exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
75 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
77 $beg = $index_beg + $options->{ "beg" } - 1;
79 if ( $options->{ "len" } ) {
80 $len = $options->{ "len" };
81 } elsif ( $options->{ "end" } ) {
82 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
85 $beg -= $options->{ "flank" };
86 $len += 2 * $options->{ "flank" };
88 if ( $beg <= $index_beg )
90 $len -= $index_beg - $beg;
94 if ( $beg + $len > $index_beg + $index_len ) {
95 $len = $index_beg + $index_len - $beg;
98 next if $beg > $index_beg + $index_len;
100 $record->{ "CHR" } = $options->{ "chr" };
101 $record->{ "CHR_BEG" } = $beg - $index_beg;
102 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
104 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
105 $record->{ "SEQ_LEN" } = $len;
107 Maasha::Biopieces::put_record( $record, $out );
111 while ( $record = Maasha::Biopieces::get_record( $in ) )
113 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
115 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
117 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
119 $beg = $record->{ "CHR_BEG" } + $index_beg;
120 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
122 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
124 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
126 $beg = $record->{ "S_BEG" } + $index_beg;
127 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
129 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and exists $lookup_hash{ $record->{ "S_ID" } } )
131 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
133 $beg = $record->{ "S_BEG" } + $index_beg;
134 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
136 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
138 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
140 $beg = $record->{ "S_BEG" } + $index_beg;
141 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
144 $beg -= $options->{ "flank" };
145 $len += 2 * $options->{ "flank" };
147 if ( $beg <= $index_beg )
149 $len -= $index_beg - $beg;
153 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
155 next if $beg > $index_beg + $index_len;
157 $record->{ "CHR_BEG" } = $beg - $index_beg;
158 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
160 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
162 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
164 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
165 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
168 if ( $options->{ "mask" } )
170 if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
172 $record->{ "SEQ" } = lc $record->{ "SEQ" };
174 @begs = split ",", $record->{ "Q_BEGS" };
175 @lens = split ",", $record->{ "BLOCK_LENS" };
177 for ( $i = 0; $i < @begs; $i++ ) {
178 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
182 elsif ( $options->{ "splice" } )
184 if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
187 @begs = split ",", $record->{ "Q_BEGS" };
188 @lens = split ",", $record->{ "BLOCK_LENS" };
190 for ( $i = 0; $i < @begs; $i++ ) {
191 $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
194 $record->{ "SEQ" } = $seq;
198 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
201 Maasha::Biopieces::put_record( $record, $out );
204 Maasha::Biopieces::close_stream( $in );
205 Maasha::Biopieces::close_stream( $out );
208 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
213 Maasha::Biopieces::status_set();
219 Maasha::Biopieces::status_log();
223 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<