]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_genome_seq
fixed seq qual length check
[biopieces.git] / bp_bin / get_genome_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 a genome sequence.
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, $record, $genome_file, $index_file, $fh, $genome,
42      $beg, $end, $len, $index_beg, $index_len, @begs, @lens, $index, $seq, $i );
43
44 $options = Maasha::Biopieces::parse_options(
45     [
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 },
54     ]   
55 );
56
57 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59
60 if ( $options->{ "genome" } ) 
61 {
62     $genome      = $options->{ "genome" };
63
64     $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
65     $index_file  = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
66
67     $fh          = Maasha::Filesys::file_read_open( $genome_file );
68     $index       = Maasha::Fasta::index_retrieve( $index_file );
69
70     if ( defined $options->{ "chr" } and exists $index->{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
71     {
72         ( $index_beg, $index_len ) = @{ $index->{ $options->{ "chr" } } };
73
74         $beg = $index_beg + $options->{ "beg" } - 1;
75
76         if ( $options->{ "len" } ) {
77             $len = $options->{ "len" };
78         } elsif ( $options->{ "end" } ) {
79             $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
80         }   
81         
82         $beg -= $options->{ "flank" };
83         $len += 2 * $options->{ "flank" };
84
85         if ( $beg <= $index_beg )
86         {
87             $len -= $index_beg - $beg;
88             $beg = $index_beg;
89         }
90
91         if ( $beg + $len > $index_beg + $index_len ) {
92             $len = $index_beg + $index_len - $beg;
93         }
94
95         next if $beg > $index_beg + $index_len;
96
97         $record->{ "CHR" }     = $options->{ "chr" };
98         $record->{ "CHR_BEG" } = $beg - $index_beg;
99         $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
100         
101         $record->{ "SEQ" }     = Maasha::Filesys::file_read( $fh, $beg, $len );
102         $record->{ "SEQ_LEN" } = $len;
103
104         Maasha::Biopieces::put_record( $record, $out );
105     }   
106 }
107
108 while ( $record = Maasha::Biopieces::get_record( $in ) )
109 {
110     if ( $options->{ "genome" } and not $record->{ "SEQ" } )
111     {
112         if ( $record->{ "REC_TYPE" } eq "BED" and exists $index->{ $record->{ "CHR" } } )
113         {
114             ( $index_beg, $index_len ) = @{ $index->{ $record->{ "CHR" } } };
115         
116             $beg = $record->{ "CHR_BEG" } + $index_beg;
117             $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
118         }
119         elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $index->{ $record->{ "S_ID" } } )
120         {
121             ( $index_beg, $index_len ) = @{ $index->{ $record->{ "S_ID" } } };
122         
123             $beg = $record->{ "S_BEG" } + $index_beg;
124             $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
125         }
126         elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and exists $index->{ $record->{ "S_ID" } } )
127         {
128             ( $index_beg, $index_len ) = @{ $index->{ $record->{ "S_ID" } } };
129         
130             $beg = $record->{ "S_BEG" } + $index_beg;
131             $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
132         }
133         elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $index->{ $record->{ "S_ID" } } )
134         {
135             ( $index_beg, $index_len ) = @{ $index->{ $record->{ "S_ID" } } };
136         
137             $beg = $record->{ "S_BEG" } + $index_beg;
138             $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
139         }
140
141         $beg -= $options->{ "flank" };
142         $len += 2 * $options->{ "flank" };
143
144         if ( $beg <= $index_beg )
145         {
146             $len -= $index_beg - $beg;
147             $beg = $index_beg;
148         }
149
150         $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
151
152         next if $beg > $index_beg + $index_len;
153
154         $record->{ "CHR_BEG" } = $beg - $index_beg;
155         $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
156
157         $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
158
159         if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
160         {
161             Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
162             $record->{ "SEQ" } = reverse $record->{ "SEQ" };
163         }
164
165         if ( $options->{ "mask" } )
166         {
167             if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
168             {
169                 $record->{ "SEQ" } = lc $record->{ "SEQ" };
170             
171                 @begs = split ",", $record->{ "Q_BEGS" };
172                 @lens = split ",", $record->{ "BLOCK_LENS" };
173
174                 for ( $i = 0; $i < @begs; $i++ ) {
175                     substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
176                 }
177             }
178         }
179         elsif ( $options->{ "splice" } )
180         {
181             if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
182             {
183                 $seq  = "";
184                 @begs = split ",", $record->{ "Q_BEGS" };
185                 @lens = split ",", $record->{ "BLOCK_LENS" };
186
187                 for ( $i = 0; $i < @begs; $i++ ) {
188                     $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
189                 }
190
191                 $record->{ "SEQ" } = $seq;
192             }
193         }
194
195         $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
196     }
197
198     Maasha::Biopieces::put_record( $record, $out );
199 }
200
201 Maasha::Biopieces::close_stream( $in );
202 Maasha::Biopieces::close_stream( $out );
203
204
205 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
206
207
208 BEGIN
209 {
210     Maasha::Biopieces::status_set();
211 }
212
213
214 END
215 {
216     Maasha::Biopieces::status_log();
217 }
218
219
220 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221
222
223 __END__