]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_genome_seq
fixed minor bugs
[biopieces.git] / bp_bin / get_genome_seq
1 #!/usr/bin/env perl -w
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
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 $lookup_hash{ $record->{ "CHR" } } )
113         {
114             ( $index_beg, $index_len ) = @{ $lookup_hash{ $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 $lookup_hash{ $record->{ "S_ID" } } )
120         {
121             ( $index_beg, $index_len ) = @{ $lookup_hash{ $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 "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
127         {
128             ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
129         
130             $beg = $record->{ "S_BEG" } + $index_beg;
131             $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
132         }
133
134         $beg -= $options->{ "flank" };
135         $len += 2 * $options->{ "flank" };
136
137         if ( $beg <= $index_beg )
138         {
139             $len -= $index_beg - $beg;
140             $beg = $index_beg;
141         }
142
143         $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
144
145         next if $beg > $index_beg + $index_len;
146
147         $record->{ "CHR_BEG" } = $beg - $index_beg;
148         $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
149
150         $record->{ "SEQ" } = Maasha::Filesys::file_read( $fh, $beg, $len );
151
152         if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
153         {
154             Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
155             $record->{ "SEQ" } = reverse $record->{ "SEQ" };
156         }
157
158         if ( $options->{ "mask" } )
159         {
160             if ( $record->{ "BLOCK_COUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
161             {
162                 $record->{ "SEQ" } = lc $record->{ "SEQ" };
163             
164                 @begs = split ",", $record->{ "Q_BEGS" };
165                 @lens = split ",", $record->{ "BLOCK_LENS" };
166
167                 for ( $i = 0; $i < @begs; $i++ ) {
168                     substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
169                 }
170             }
171         }
172         elsif ( $options->{ "splice" } )
173         {
174             if ( $record->{ "BLOCK_COUNT" } > 1 ) # splice block sequences
175             {
176                 $seq  = "";
177                 @begs = split ",", $record->{ "Q_BEGS" };
178                 @lens = split ",", $record->{ "BLOCK_LENS" };
179
180                 for ( $i = 0; $i < @begs; $i++ ) {
181                     $seq .= substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
182                 }
183
184                 $record->{ "SEQ" } = $seq;
185             }
186         }
187
188         $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
189     }
190
191     Maasha::Biopieces::put_record( $record, $out );
192 }
193
194 Maasha::Biopieces::close_stream( $in );
195 Maasha::Biopieces::close_stream( $out );
196
197
198 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
199
200
201 BEGIN
202 {
203     Maasha::Biopieces::status_set();
204 }
205
206
207 END
208 {
209     Maasha::Biopieces::status_log();
210 }
211
212
213 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
214
215
216 __END__