]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/get_genome_phastcons
fixed seq qual length check
[biopieces.git] / bp_bin / get_genome_phastcons
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 phastcons scores from a specified genome.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Filesys;
33 use Maasha::UCSC::Wiggle;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
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     ]   
50 );
51
52 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
54
55 $phastcons_file  = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/phastcons/$options->{ 'genome' }.pp";
56 $phastcons_index = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/phastcons/$options->{ 'genome' }.pp.index";
57
58 $index           = Maasha::UCSC::Wiggle::fixedstep_index_retrieve( $phastcons_index );
59 $fh_phastcons    = Maasha::Filesys::file_read_open( $phastcons_file );
60
61 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
62 {
63     $options->{ "beg" } -= 1;   # request is 1-based
64     $options->{ "end" } -= 1;   # request is 1-based
65
66     if ( $options->{ "len" } ) {
67         $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
68     }
69
70     $scores = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
71
72     $record->{ "CHR" }       = $options->{ "chr" };
73     $record->{ "CHR_BEG" }   = $options->{ "beg" } - $options->{ "flank" };
74     $record->{ "CHR_END" }   = $options->{ "end" } + $options->{ "flank" };
75     
76     $record->{ "PHASTCONS" }   = join ",", @{ $scores };
77     $record->{ "PHAST_COUNT" } = scalar @{ $scores };  # DEBUG
78
79     Maasha::Biopieces::put_record( $record, $out );
80 }   
81
82 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
83 {
84     if ( $record->{ "REC_TYPE" } eq "BED" )
85     {
86         $scores = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
87     }
88     elsif ( $record->{ "REC_TYPE" } eq "PSL" )
89     {
90         $scores = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
91     }
92     elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
93     {
94         $scores = Maasha::UCSC::Wiggle::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
95     }
96
97     $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
98 #    $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores };  # DEBUG
99
100     Maasha::Biopieces::put_record( $record, $out );
101 }
102
103 close $fh_phastcons if $fh_phastcons;                                                                                                                                          
104
105 Maasha::Biopieces::close_stream( $in );
106 Maasha::Biopieces::close_stream( $out );
107
108
109 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
110
111
112 BEGIN
113 {
114     Maasha::Biopieces::status_set();
115 }
116
117
118 END
119 {
120     Maasha::Biopieces::status_log();
121 }
122
123
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125
126
127 __END__