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