]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_genome_align
fixed seq qual length check
[biopieces.git] / bp_bin / get_genome_align
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 alignments from a multiple genome alignment.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Common;
33 use Maasha::UCSC;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $record, $tmp_dir, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
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 => 'strand', short => 's', type => 'string', mandatory => 'no',  default => '+',   allowed => '+,-', disallowed => undef },
49     ]   
50 );
51
52 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
54
55 $tmp_dir = Maasha::Biopieces::get_tmpdir();
56
57 $align_num = 1;
58
59 $maf_track = maf_track( $options->{ "genome" } );
60
61 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
62 {
63     $beg = $options->{ "beg" } - 1;
64     
65     if ( $options->{ "end" } ) {
66         $end = $options->{ "end" };
67     } elsif ( $options->{ "len" } ) {
68         $end = $beg + $options->{ "len" };
69     }
70
71     $align = Maasha::UCSC::maf_extract( $tmp_dir, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
72
73     foreach $entry ( @{ $align } )
74     {
75         $entry->{ "CHR" }     = $record->{ "CHR" };
76         $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
77         $entry->{ "CHR_END" } = $record->{ "CHR_END" };
78         $entry->{ "STRAND" }  = $record->{ "STRAND" } || '+';
79         $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
80         $entry->{ "SCORE" }   = $record->{ "SCORE" };
81
82         Maasha::Biopieces::put_record( $entry, $out );
83     }
84 }
85
86 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
87 {
88     if ( $record->{ "REC_TYPE" } eq "BED" )
89     {
90         $align = Maasha::UCSC::maf_extract( $tmp_dir, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
91     }
92     elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
93     {
94         $align = Maasha::UCSC::maf_extract( $tmp_dir, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
95     }
96     elsif ( $record->{ "REC_TYPE" } eq "PSL" )
97     {
98         $align = Maasha::UCSC::maf_extract( $tmp_dir, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
99     }
100     elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
101     {
102         $align = Maasha::UCSC::maf_extract( $tmp_dir, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
103     }
104
105     foreach $entry ( @{ $align } )
106     {
107         $entry->{ "CHR" }     = $record->{ "CHR" };
108         $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
109         $entry->{ "CHR_END" } = $record->{ "CHR_END" };
110         $entry->{ "STRAND" }  = $record->{ "STRAND" };
111         $entry->{ "Q_ID" }    = $record->{ "Q_ID" };
112         $entry->{ "SCORE" }   = $record->{ "SCORE" };
113
114         Maasha::Biopieces::put_record( $entry, $out );
115     }
116
117     $align_num++;
118 }
119
120 Maasha::Biopieces::close_stream( $in );
121 Maasha::Biopieces::close_stream( $out );
122
123
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125
126
127 sub maf_track
128 {
129     # Martin A. Hansen, April 2008.
130
131     # Given a genome returns the corresponding mafTrack database table name.
132
133     my ( $genome,   # genome to lookup.
134        ) = @_;
135
136     # Returns a string.
137
138     my ( %hash );
139
140     # The below has should be in a config file - fix later.
141
142     %hash = (
143         danRer4 => 'multiz7way',
144         dm2     => 'multiz15way',
145         dm3     => 'multiz15way',
146         fr2     => 'multiz7way',
147         galGal3 => 'multiz7way',
148         gasAcu1 => 'multiz7way',
149         hg18    => 'multiz17way',
150         mm8     => 'multiz17way',
151         mm9     => 'multiz17way',
152         oryLat1 => 'multiz7way',
153         panTro2 => 'multiz17way',
154         tetNig1 => 'multiz7way',
155     );
156
157     Maasha::Common::error( qw(multiz track not found) ) if not exists $hash{ $genome };
158
159     return $hash{ $genome };
160 }
161
162
163 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
164
165
166 BEGIN
167 {
168     Maasha::Biopieces::status_set();
169 }
170
171
172 END
173 {
174     Maasha::Biopieces::status_log();
175 }
176
177
178 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
179
180
181 __END__