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