]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/upload_to_ucsc
fixed seq qual length check
[biopieces.git] / bp_bin / upload_to_ucsc
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 # Upload data to local UCSC Genome Browser Database.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Common;
32 use Maasha::Biopieces;
33 use Maasha::Filesys;
34 use Maasha::UCSC;
35 use Maasha::UCSC::Wiggle;
36 use Maasha::UCSC::PSL;
37 use Maasha::UCSC::BED;
38
39
40 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
41
42
43 my ( $options, $in, $out, $record, $tmp_dir, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
44
45 $options = Maasha::Biopieces::parse_options(
46     [
47         { long => 'database',    short => 'd', type => 'string', mandatory => 'yes', default => undef,             allowed => undef,                         disallowed => undef },
48         { long => 'table',       short => 't', type => 'string', mandatory => 'yes', default => undef,             allowed => undef,                         disallowed => undef },
49         { long => 'no_stream',   short => 'x', type => 'flag',   mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
50         { long => 'short_label', short => 's', type => 'string', mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
51         { long => 'long_label',  short => 'l', type => 'string', mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
52         { long => 'group',       short => 'g', type => 'string', mandatory => 'no',  default => $ENV{ 'LOGNAME' }, allowed => undef,                         disallowed => undef },
53         { long => 'priority',    short => 'p', type => 'float',  mandatory => 'no',  default => 1,                 allowed => undef,                         disallowed => undef },
54         { long => 'use_score',   short => 'u', type => 'flag',   mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
55         { long => 'visibility',  short => 'V', type => 'string', mandatory => 'no',  default => 'pack',            allowed => 'hide,dense,squish,pack,full', disallowed => undef },
56         { long => 'color',       short => 'c', type => 'string', mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
57         { long => 'check',       short => 'C', type => 'flag',   mandatory => 'no',  default => undef,             allowed => undef,                         disallowed => undef },
58     ]   
59 );
60
61 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
62 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
63
64 $tmp_dir = Maasha::Biopieces::get_tmpdir();
65
66 $options->{ "short_label" } ||= $options->{ 'table' };
67 $options->{ "long_label" }  ||= $options->{ 'table' };
68 $options->{ "color" }       ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
69 $options->{ "chunk_size" }  ||= 10_000_000_000;    # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
70
71 $file   = "$tmp_dir/ucsc_upload.tmp";
72 $append = 0;
73 $first  = 1;
74 $i      = 0;
75
76 $fh_out = Maasha::Filesys::file_write_open( $file );
77
78 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
79 {
80     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
81
82     if ( $record->{ "REC_TYPE" } eq "fixed_step" )
83     {
84         $format = "WIGGLE";
85
86         if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
87             Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
88         }
89     }
90     elsif ( $record->{ "REC_TYPE" } eq "PSL" )
91     {
92         $format = "PSL";
93
94         Maasha::UCSC::PSL::psl_put_header( $fh_out ) if $first;
95         Maasha::UCSC::PSL::psl_put_entry( $record, $fh_out );
96         
97         $first = 0;
98     }
99     elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
100     {
101         # chrom chromStart  chromEnd    name    score   strand  size    secStr  conf 
102
103         $format  = "BED_SS";
104
105         print $fh_out join ( "\t",
106             $record->{ "CHR" },
107             $record->{ "CHR_BEG" },
108             $record->{ "CHR_END" } + 1,
109             $record->{ "Q_ID" },
110             $record->{ "SCORE" },
111             $record->{ "STRAND" },
112             $record->{ "SIZE" },
113             $record->{ "SEC_STRUCT" },
114             $record->{ "CONF" },
115         ), "\n";
116     }
117     elsif ( $record->{ "REC_TYPE" } eq "BED" )
118     {
119         $format  = "BED";
120         $columns = $record->{ "BED_COLS" };
121
122         if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
123             Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
124         }
125     }
126     elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
127     {
128         $format  = "BED";
129         $columns = 6;
130
131         if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
132             Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
133         }
134     }
135     elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
136     {
137         $format  = "BED";
138         $columns = 6;
139
140         $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
141
142         if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
143             Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
144         }
145     }
146     elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
147     {
148         $format  = "BED";
149         $columns = 6;
150
151         if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
152             Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
153         }
154     }
155
156     if ( $i == $options->{ "chunk_size" } )
157     {
158         close $fh_out;
159
160         if ( $format eq "BED" ) {
161             Maasha::UCSC::BED::bed_upload_to_ucsc( $tmp_dir, $file, $options, $append );
162         } elsif ( $format eq "PSL" ) {
163             Maasha::UCSC::BED::psl_upload_to_ucsc( $file, $options, $append ); 
164         }
165
166         unlink $file;
167
168         $first = 1;
169
170         $append = 1;
171
172         $fh_out = Maasha::Filesys::file_write_open( $file );
173     }
174
175     $i++;
176 }
177
178 close $fh_out;
179
180 if ( $format eq "BED" )
181 {
182     $type = "bed $columns";
183
184     Maasha::UCSC::BED::bed_upload_to_ucsc( $tmp_dir, $file, $options, $append );
185 }
186 elsif ( $format eq "BED_SS" )
187 {
188     $type = "type bed 6 +";
189
190     Maasha::UCSC::BED::bed_upload_to_ucsc( $tmp_dir, $file, $options, $append );
191 }
192 elsif ( $format eq "PSL" )
193 {
194     $type = "psl";
195
196     Maasha::UCSC::PSL::psl_upload_to_ucsc( $file, $options, $append ); 
197 }
198 elsif ( $format eq "WIGGLE" )
199 {
200     $options->{ "visibility" } = "full";
201
202     $wig_file = "$options->{ 'table' }.wig";
203     $wib_file = "$options->{ 'table' }.wib";
204
205     $wib_dir  = "$ENV{ 'HOME' }/ucsc/wib";
206
207     Maasha::Filesys::dir_create_if_not_exists( $wib_dir );
208
209     if ( $options->{ 'verbose' } ) {
210         `cd $tmp_dir && wigEncode $file $wig_file $wib_file`;
211     } else {
212         `cd $tmp_dir && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
213     }
214
215     Maasha::Common::run( "mv", "$tmp_dir/$wib_file $wib_dir" );
216
217     unlink $file;
218
219     $file = $wig_file;
220
221     $type = "wig 0";
222
223     Maasha::UCSC::Wiggle::wiggle_upload_to_ucsc( $tmp_dir, $wib_dir, $file, $options );
224 }
225
226 unlink $file;
227
228 Maasha::UCSC::ucsc_update_config( $options, $type );
229
230 Maasha::Biopieces::close_stream( $in );
231 Maasha::Biopieces::close_stream( $out );
232
233
234 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
235
236
237 BEGIN
238 {
239     Maasha::Biopieces::status_set();
240 }
241
242
243 END
244 {
245     Maasha::Biopieces::status_log();
246 }
247
248
249 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
250
251
252 __END__