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