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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41 my ( $options, $in, $out, $record, $genome_file, $index_file, $fh, $genome,
42 $beg, $end, $len, $index_beg, $index_len, @begs, @lens, $index, $seq, $i );
44 $options = Maasha::Biopieces::parse_options(
46 { long => 'genome', short => 'g', type => 'genome', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
47 { long => 'chr', short => 'c', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'beg', short => 'b', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
49 { long => 'end', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
50 { long => 'len', short => 'l', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
51 { long => 'flank', short => 'f', type => 'uint', mandatory => 'no', default => 0, allowed => undef, disallowed => undef },
52 { long => 'mask', short => 'm', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
53 { long => 'splice', short => 's', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
57 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
60 if ( $options->{ "genome" } )
62 $genome = $options->{ "genome" };
64 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
65 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
67 $fh = Maasha::Filesys::file_read_open( $genome_file );
68 $index = Maasha::Fasta::index_retrieve( $index_file );
70 if ( defined $options->{ "chr" } and exists $index->{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
72 ( $index_beg, $index_len ) = @{ $index->{ $options->{ "chr" } } };
74 $beg = $index_beg + $options->{ "beg" } - 1;
76 if ( $options->{ "len" } ) {
77 $len = $options->{ "len" };
78 } elsif ( $options->{ "end" } ) {
79 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
82 $beg -= $options->{ "flank" };
83 $len += 2 * $options->{ "flank" };
85 if ( $beg <= $index_beg )
87 $len -= $index_beg - $beg;
91 if ( $beg + $len > $index_beg + $index_len ) {
92 $len = $index_beg + $index_len - $beg;
95 next if $beg > $index_beg + $index_len;
97 $record->{ "CHR" } = $options->{ "chr" };
98 $record->{ "CHR_BEG" } = $beg - $index_beg;
99 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
101 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
102 $record->{ "SEQ_LEN" } = $len;
104 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 $index->{ $record->{ "CHR" } } )
114 ( $index_beg, $index_len ) = @{ $index->{ $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 $index->{ $record->{ "S_ID" } } )
121 ( $index_beg, $index_len ) = @{ $index->{ $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 "VMATCH" and exists $index->{ $record->{ "S_ID" } } )
128 ( $index_beg, $index_len ) = @{ $index->{ $record->{ "S_ID" } } };
130 $beg = $record->{ "S_BEG" } + $index_beg;
131 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
133 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $index->{ $record->{ "S_ID" } } )
135 ( $index_beg, $index_len ) = @{ $index->{ $record->{ "S_ID" } } };
137 $beg = $record->{ "S_BEG" } + $index_beg;
138 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
141 $beg -= $options->{ "flank" };
142 $len += 2 * $options->{ "flank" };
144 if ( $beg <= $index_beg )
146 $len -= $index_beg - $beg;
150 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
152 next if $beg > $index_beg + $index_len;
154 $record->{ "CHR_BEG" } = $beg - $index_beg;
155 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
157 $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
159 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
161 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
162 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
165 if ( $options->{ "mask" } )
167 if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
169 $record->{ "SEQ" } = lc $record->{ "SEQ" };
171 @begs = split ",", $record->{ "Q_BEGS" };
172 @lens = split ",", $record->{ "BLOCK_LENS" };
174 for ( $i = 0; $i < @begs; $i++ ) {
175 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
179 elsif ( $options->{ "splice" } )
181 if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
184 @begs = split ",", $record->{ "Q_BEGS" };
185 @lens = split ",", $record->{ "BLOCK_LENS" };
187 for ( $i = 0; $i < @begs; $i++ ) {
188 $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
191 $record->{ "SEQ" } = $seq;
195 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
198 Maasha::Biopieces::put_record( $record, $out );
201 Maasha::Biopieces::close_stream( $in );
202 Maasha::Biopieces::close_stream( $out );
205 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
210 Maasha::Biopieces::status_set();
216 Maasha::Biopieces::status_log();
220 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<