3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Write Biopiece records to the KISS Genome browser.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Maasha::Biopieces;
44 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
47 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 );
49 $data_dir = Maasha::Biopieces::biopiecesrc( "KISS_DATA_DIR" );
50 $user = Maasha::Biopieces::biopiecesrc( "KISS_USER" );
52 $options = Maasha::Biopieces::parse_options(
54 { long => 'user', short => 'u', type => 'string', mandatory => 'no', default => $user, allowed => undef, disallowed => undef },
55 { long => 'clade', short => 'c', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
56 { long => 'genome', short => 'g', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
57 { long => 'assembly', short => 'a', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
58 { long => 'track_name', short => 't', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
59 { long => 'force', short => 'f', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
60 { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
64 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
65 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
67 if ( $options->{ 'track_name' } )
69 $options->{ 'track_name' } =~ tr/ /_/;
71 $path = join "/", $data_dir, "Users/", $options->{ 'user' }, $options->{ 'clade' }, $options->{ 'genome' }, $options->{ 'assembly' };
73 Maasha::Common::error( qq(Path not found: "$path") ) if not -d $path;
75 $tmp_dir = Maasha::Biopieces::get_tmpdir();
77 while ( $record = Maasha::Biopieces::get_record( $in ) )
79 if ( $entry = Maasha::KISS::biopiece2kiss( $record ) )
81 $entry->{ 'S_ID' } = ( split / /, $entry->{ 'S_ID' } )[ 0 ];
83 if ( not exists $fh_hash{ $entry->{ 'S_ID' } } ) {
84 $fh_hash{ $entry->{ 'S_ID' } } = Maasha::Filesys::file_write_open( "$tmp_dir/$entry->{ 'S_ID' }" );
87 $fh_out = $fh_hash{ $entry->{ 'S_ID' } };
89 Maasha::KISS::kiss_entry_put( $entry, $fh_out );
92 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
95 foreach $key ( keys %fh_hash )
97 close $fh_hash{ $key };
99 $dst_dir = Maasha::Filesys::dir_create_if_not_exists( "$path/$key" );
100 $dst_dir = Maasha::Filesys::dir_create_if_not_exists( "$dst_dir/Tracks" );
102 @dirs = Maasha::Filesys::ls_dirs( $dst_dir );
104 foreach $dir ( @dirs )
106 $dir = ( split "/", $dir )[ -1 ];
108 if ( $dir =~ /^(\d+)/ ) {
113 @nums = sort { $a <=> $b } @nums;
115 $num = $nums[ -1 ] || 0;
119 $dst_dir = "$dst_dir/$num" . "_$options->{ 'track_name' }";
121 Maasha::Filesys::dir_create( $dst_dir );
123 Maasha::Filesys::file_copy( "$tmp_dir/$key", "$dst_dir/track_data.kiss" );
125 Maasha::KISS::kiss_sort( "$dst_dir/track_data.kiss" );
126 Maasha::KISS::kiss_index( "$dst_dir/track_data.kiss" );
128 unlink "$tmp_dir/$key";
133 $path = join "/", $data_dir, "Users/", $options->{ 'user' }, $options->{ 'clade' };
135 Maasha::Common::error( qq(Path not found: "$path") ) if not -d $path;
137 $dst_dir = Maasha::Filesys::dir_create( "$path/$options->{ 'genome' }" );
138 $dst_dir = Maasha::Filesys::dir_create( "$dst_dir/$options->{ 'assembly' }" );
140 while ( $record = Maasha::Biopieces::get_record( $in ) )
142 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
144 $entry->[ SEQ_NAME ] = ( split " ", $entry->[ SEQ_NAME ] )[ 0 ];
146 if ( $options->{ 'force' } ) {
147 Maasha::Filesys::dir_remove( "$dst_dir/$entry->[ SEQ_NAME ]" );
150 $contig_dir = Maasha::Filesys::dir_create( "$dst_dir/$entry->[ SEQ_NAME ]" );
151 $contig_dir = Maasha::Filesys::dir_create( "$contig_dir/Sequence" );
153 $fh_out = Maasha::Filesys::file_write_open( "$contig_dir/sequence.txt" );
154 print $fh_out $entry->[ SEQ ];
160 Maasha::Biopieces::close_stream( $in );
161 Maasha::Biopieces::close_stream( $out );
164 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
169 Maasha::Biopieces::status_set();
175 Maasha::Biopieces::status_log();
179 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<