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