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