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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30 use Maasha::Biopieces;
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $genome_file, $index_file, $fh, $genome,
39 $beg, $end, $len, $index_beg, $index_len, @begs, @lens, $index, $seq, $i, %lookup_hash );
41 $options = Maasha::Biopieces::parse_options(
43 { long => 'genome', short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
44 { long => 'chr', short => 'c', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45 { long => 'beg', short => 'b', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
46 { long => 'end', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
47 { long => 'len', short => 'l', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
48 { long => 'flank', short => 'f', type => 'uint', mandatory => 'no', default => 0, allowed => undef, disallowed => undef },
49 { long => 'mask', short => 'm', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 { long => 'splice', short => 's', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
54 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
55 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
57 if ( $options->{ "genome" } )
59 $genome = $options->{ "genome" };
61 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
62 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
64 $fh = Maasha::Filesys::file_read_open( $genome_file );
65 $index = Maasha::Fasta::index_retrieve( $index_file );
67 shift @{ $index }; # Get rid of the file size info
69 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
71 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
73 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
75 $beg = $index_beg + $options->{ "beg" } - 1;
77 if ( $options->{ "len" } ) {
78 $len = $options->{ "len" };
79 } elsif ( $options->{ "end" } ) {
80 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
83 $beg -= $options->{ "flank" };
84 $len += 2 * $options->{ "flank" };
86 if ( $beg <= $index_beg )
88 $len -= $index_beg - $beg;
92 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
94 next if $beg > $index_beg + $index_len;
96 $record->{ "CHR" } = $options->{ "chr" };
97 $record->{ "CHR_BEG" } = $beg - $index_beg;
98 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
100 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
101 $record->{ "SEQ_LEN" } = $len;
103 Maasha::Biopieces::put_record( $record, $out );
108 while ( $record = Maasha::Biopieces::get_record( $in ) )
110 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
112 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
114 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
116 $beg = $record->{ "CHR_BEG" } + $index_beg;
117 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
119 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
121 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
123 $beg = $record->{ "S_BEG" } + $index_beg;
124 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
126 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
128 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
130 $beg = $record->{ "S_BEG" } + $index_beg;
131 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
134 $beg -= $options->{ "flank" };
135 $len += 2 * $options->{ "flank" };
137 if ( $beg <= $index_beg )
139 $len -= $index_beg - $beg;
143 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
145 next if $beg > $index_beg + $index_len;
147 $record->{ "CHR_BEG" } = $beg - $index_beg;
148 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
150 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
152 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
154 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
155 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
158 if ( $options->{ "mask" } )
160 if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
162 $record->{ "SEQ" } = lc $record->{ "SEQ" };
164 @begs = split ",", $record->{ "Q_BEGS" };
165 @lens = split ",", $record->{ "BLOCK_LENS" };
167 for ( $i = 0; $i < @begs; $i++ ) {
168 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
172 elsif ( $options->{ "splice" } )
174 if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
177 @begs = split ",", $record->{ "Q_BEGS" };
178 @lens = split ",", $record->{ "BLOCK_LENS" };
180 for ( $i = 0; $i < @begs; $i++ ) {
181 $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
184 $record->{ "SEQ" } = $seq;
188 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
191 Maasha::Biopieces::put_record( $record, $out );
195 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
200 $run_time_beg = Maasha::Biopieces::run_time();
202 Maasha::Biopieces::log_biopiece();
208 Maasha::Biopieces::close_stream( $in );
209 Maasha::Biopieces::close_stream( $out );
211 $run_time_end = Maasha::Biopieces::run_time();
213 Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
217 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<