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 # Assemble tag contigs from overlapping BED type records in the stream.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
33 use Maasha::UCSC::BED;
36 chrom => 0, # BED field names
51 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
54 my ( $options, $in, $out, $tmp_dir, $fh_in, $fh_out, $cols, $record, $bed_entry, %fh_hash, %file_hash, $file, $strand, $id, $tag_file, $bed_file );
56 $options = Maasha::Biopieces::parse_options(
58 { long => 'check', short => 'C', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
62 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
63 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
65 `cd "$ENV{ BP_C }/Maasha/src" && make`; # FIXME OMG this is crufty!
67 $tmp_dir = Maasha::Biopieces::get_tmpdir();
69 $cols = 6; # we only need the first 6 BED columns
71 while ( $record = Maasha::Biopieces::get_record( $in ) )
73 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
75 if ( not exists $fh_hash{ $bed_entry->[ chrom ] . $bed_entry->[ strand ] } )
77 $file = "$tmp_dir/$bed_entry->[ chrom ]$bed_entry->[ strand ]";
79 $file_hash{ $bed_entry->[ chrom ] . $bed_entry->[ strand ] } = $file;
80 $fh_hash{ $bed_entry->[ chrom ] . $bed_entry->[ strand ] } = Maasha::Filesys::file_write_open( $file );
83 $fh_out = $fh_hash{ $bed_entry->[ chrom ] . $bed_entry->[ strand ] };
85 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
91 foreach $file ( sort keys %file_hash )
93 $bed_file = $file_hash{ $file };
94 $tag_file = "$bed_file.tc";
96 $strand = substr $file, -1, 1;
98 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
100 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
102 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
104 $bed_entry->[ name ] = sprintf( "TC%08d", $id );
105 $bed_entry->[ strand ] = $strand;
107 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
108 Maasha::Biopieces::put_record( $record, $out );
120 Maasha::Biopieces::close_stream( $in );
121 Maasha::Biopieces::close_stream( $out );
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
129 Maasha::Biopieces::status_set();
135 Maasha::Biopieces::status_log();
139 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<