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