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