]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/BGB_upload
fixed seq qual length check
[biopieces.git] / bp_bin / BGB_upload
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 # Write Biopiece records to the Biopieces Genome browser.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Data::Dumper;
32 use Maasha::Common;
33 use Maasha::KISS;
34 use Maasha::Biopieces;
35 use Maasha::Fasta;
36 use Maasha::BGB::Track;
37 use Maasha::BGB::Wiggle;
38 use Maasha::Filesys;
39
40 use constant {
41     SEQ_NAME => 0,
42     SEQ      => 1,
43
44     S_ID     => 0,
45 };
46
47
48 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49
50
51 my ( $data_dir, $user, $options, $path, $in, $out, $tmp_dir, %fh_hash, $fh_out, $record, $entry, $key, $dst_dir, @nums, $num, $contig_dir, $wig );
52
53 $data_dir = Maasha::Biopieces::biopiecesrc( "BGB_DATA_DIR" );
54 $user     = Maasha::Biopieces::biopiecesrc( "BGB_USER" );
55
56 $options = Maasha::Biopieces::parse_options(
57     [
58         { long => 'user',       short => 'u', type => 'string', mandatory => 'no',  default => $user,    allowed => undef,           disallowed => undef },
59         { long => 'clade',      short => 'c', type => 'string', mandatory => 'yes', default => undef,    allowed => undef,           disallowed => undef },
60         { long => 'genome',     short => 'g', type => 'string', mandatory => 'yes', default => undef,    allowed => undef,           disallowed => undef },
61         { long => 'assembly',   short => 'a', type => 'string', mandatory => 'yes', default => undef,    allowed => undef,           disallowed => undef },
62         { long => 'track_name', short => 't', type => 'string', mandatory => 'no',  default => undef,    allowed => undef,           disallowed => undef },
63         { long => 'track_type', short => 'T', type => 'string', mandatory => 'no',  default => 'linear', allowed => 'linear,wiggle', disallowed => undef },
64         { long => 'force',      short => 'f', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,           disallowed => undef },
65         { long => 'no_stream',  short => 'x', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,           disallowed => undef },
66     ]   
67 );
68
69 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
70 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
71
72 if ( $options->{ 'track_name' } )
73 {
74     $options->{ 'track_name' } =~ tr/ /_/;
75
76     $path = join "/", $data_dir, "Users", $options->{ 'user' }, $options->{ 'clade' }, $options->{ 'genome' }, $options->{ 'assembly' };
77
78     Maasha::Common::error( qq(Path not found: "$path") ) if not -d $path;
79
80     $tmp_dir = Maasha::Biopieces::get_tmpdir();
81
82     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
83     {
84         if ( $entry = Maasha::KISS::biopiece2kiss( $record ) )
85         {
86             $entry->[ S_ID ] = ( split / /, $entry->[ S_ID ] )[ 0 ];
87
88             if ( not exists $fh_hash{ $entry->[ S_ID ] } ) {
89                 $fh_hash{ $entry->[ S_ID ] } = Maasha::Filesys::file_write_open( "$tmp_dir/$entry->[ S_ID ]" );
90             }
91
92             $fh_out = $fh_hash{ $entry->[ S_ID ] };
93
94             Maasha::KISS::kiss_entry_put( $entry, $fh_out );
95         }
96
97         Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
98     }
99
100     $num = Maasha::BGB::Track::max_track( $options->{ 'user' }, $options->{ 'clade' }, $options->{ 'genome' }, $options->{ 'assembly' } );
101     $num = sprintf( "%04d", $num + 10 );
102
103     foreach $key ( keys %fh_hash )
104     {
105         close $fh_hash{ $key };
106
107         $dst_dir = Maasha::Filesys::dir_create_if_not_exists( "$path/$key" );
108         $dst_dir = Maasha::Filesys::dir_create_if_not_exists( "$dst_dir/Tracks" );
109
110         $dst_dir = "$dst_dir/$num" . "_$options->{ 'track_name' }";
111
112         Maasha::Filesys::dir_create( $dst_dir );
113
114         if ( $options->{ 'track_type' } eq 'linear' )
115         {
116             Maasha::Filesys::file_copy( "$tmp_dir/$key", "$dst_dir/track_data.kiss" );
117
118             Maasha::KISS::kiss_sort( "$dst_dir/track_data.kiss" );
119         }
120         elsif ( $options->{ 'track_type' } eq 'wiggle' )
121         {
122             $wig = Maasha::BGB::Wiggle::wiggle_encode( "$tmp_dir/$key" );
123             Maasha::BGB::Wiggle::wiggle_store( "$dst_dir/track_data.wig", $wig );
124         }
125
126         unlink "$tmp_dir/$key";
127     }
128 }
129 else
130 {
131     $path = join "/", $data_dir, "Users/", $options->{ 'user' }, $options->{ 'clade' };
132
133     Maasha::Common::error( qq(Path not found: "$path") ) if not -d $path;
134
135     $dst_dir = Maasha::Filesys::dir_create_if_not_exists( "$path/$options->{ 'genome' }" );
136     $dst_dir = Maasha::Filesys::dir_create( "$dst_dir/$options->{ 'assembly' }" );
137
138     while ( $record = Maasha::Biopieces::get_record( $in ) ) 
139     {
140         if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
141         {
142             $entry->[ SEQ_NAME ] = ( split " ", $entry->[ SEQ_NAME ] )[ 0 ];
143
144             if ( $options->{ 'force' } ) {
145                 Maasha::Filesys::dir_remove( "$dst_dir/$entry->[ SEQ_NAME ]" );
146             }
147
148             $contig_dir = Maasha::Filesys::dir_create( "$dst_dir/$entry->[ SEQ_NAME ]" );
149             $contig_dir = Maasha::Filesys::dir_create( "$contig_dir/Sequence" );
150             
151             $fh_out = Maasha::Filesys::file_write_open( "$contig_dir/sequence.txt" );
152             print $fh_out $entry->[ SEQ ];
153             close $fh_out;
154         }
155     }
156 }
157
158 Maasha::Biopieces::close_stream( $in );
159 Maasha::Biopieces::close_stream( $out );
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__