]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/cluster_seq
fixed bug in grab
[biopieces.git] / bp_bin / cluster_seq
index 12301c20fd18ec80a5cd36deb28c6036eeb37fa5..53d8f2c72ca6cce554d674ec4ad5eb2a2a17d185 100755 (executable)
@@ -39,13 +39,13 @@ use Maasha::Filesys;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-my ( $options, $in, $out, $tmp_dir, $tmp_fh1, $tmp_fh2, $fh, $record, $entry, $type, @args, $arg_str, $clusters );
+my ( $options, $in, $out, $tmp_dir, $tmp_file, $tmp_fh, $record, $entry, @args, $arg_str, @fields, $line );
 
 $options = Maasha::Biopieces::parse_options(
     [
-        { long => 'identity',   short => 'i', type => 'float', mandatory => 'no', default => "0.9",   allowed => undef, disallowed => undef },
-        { long => 'word_size',  short => 'w', type => 'uint',  mandatory => 'no', default => 7,     allowed => undef, disallowed => 0 },
-        { long => 'fast_clust', short => 'f', type => 'flag',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
+        { long => 'identity', short => 'i', type => 'float', mandatory => 'no', default => "0.9", allowed => undef, disallowed => undef },
+        { long => 'library',  short => 'l', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
+        { long => 'no_sort',  short => 'n', type => 'flag',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
     ]   
 );
 
@@ -53,53 +53,63 @@ $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
 
 $tmp_dir  = Maasha::Biopieces::get_tmpdir();
-$tmp_fh1  = Maasha::Filesys::file_write_open( "$tmp_dir/cluster.fasta" );
-$tmp_fh2  = Maasha::Filesys::file_write_open( "$tmp_dir/cluster.stream" );
+$tmp_file = "$tmp_dir/uclust.fasta";
+$tmp_fh   = Maasha::Filesys::file_write_open( $tmp_file );
 
 while ( $record = Maasha::Biopieces::get_record( $in ) )
 {
-    if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
-    {
-        $type = Maasha::Seq::seq_guess_type( $record->{ 'SEQ' } ) if not $type;
-
-        Maasha::Fasta::put_entry( $entry, $tmp_fh1 );
+    if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
+        Maasha::Fasta::put_entry( $entry, $tmp_fh );
     }
 
-    Maasha::Biopieces::put_record( $record, $tmp_fh2 );
+    Maasha::Biopieces::put_record( $record, $out );
 }
 
-close $tmp_fh1;
-close $tmp_fh2;
+close $tmp_fh;
 
-push @args, "-d 120";
-push @args, "-i $tmp_dir/cluster.fasta";
-push @args, "-o $tmp_dir/cluster.out";
-push @args, "-n $options->{ 'word_size' }";
-push @args, "-c $options->{ 'identity' }";
-push @args, "-g 1" if not $options->{ 'fast_clust' };
-push @args, "> /dev/null 2>&1" if not $options->{ 'verbose' };
+uclust_sort( $tmp_file, $tmp_dir, $options->{ 'verbose' } ) if not $options->{ 'no_sort' };
 
-$arg_str = join " ", @args;
+push @args, "--input $tmp_file";
+push @args, "--id $options->{ 'identity' }";
+push @args, "--tmpdir $tmp_dir";
+push @args, "--uc $tmp_file.out";
+push @args, "--lib $options->{ 'library' }" if $options->{ 'library' };
+push @args, "--libonly"                     if $options->{ 'library' };
+push @args, "--quiet"                       if not $options->{ 'verbose' };
+push @args, "> /dev/null 2>&1"              if not $options->{ 'verbose' };
 
-if ( $type =~ /protein/i ) {
-    Maasha::Common::run( "cdhit", $arg_str );
-} else {
-    Maasha::Common::run( "cdhit-est", $arg_str );
-}
+$arg_str = join " ", @args;
 
-$clusters = parse_clusters( "$tmp_dir/cluster.out.clstr" );
+Maasha::Common::run( "uclust", $arg_str );
 
-$tmp_fh2 = Maasha::Filesys::file_read_open( "$tmp_dir/cluster.stream" );
+$tmp_fh = Maasha::Filesys::file_read_open( "$tmp_file.out" ); 
 
-while ( $record = Maasha::Biopieces::get_record( $tmp_fh2 ) )
+while ( $line = <$tmp_fh> )
 {
-    if ( exists $clusters->{ $record->{ 'SEQ_NAME' } } ) {
-        $record->{ 'CLUSTER' } = $clusters->{ $record->{ 'SEQ_NAME' } };
-    }
+    next if $line =~ /^#/;
+
+    chomp $line;
+
+    @fields = split "\t", $line;
+
+    $record = {
+        REC_TYPE => 'UCLUST',
+        TYPE     => $fields[ 0 ],
+        CLUSTER  => $fields[ 1 ],
+        SIZE     => $fields[ 2 ],
+        IDENT    => $fields[ 3 ],
+        STRAND   => $fields[ 4 ],
+        Q_BEG    => $fields[ 5 ],
+        S_BEG    => $fields[ 6 ],
+        CIGAR    => $fields[ 7 ],
+        SEQ_NAME => $fields[ 8 ],
+    };
 
     Maasha::Biopieces::put_record( $record, $out );
 }
 
+close $tmp_fh;
+
 Maasha::Biopieces::close_stream( $in );
 Maasha::Biopieces::close_stream( $out );
 
@@ -107,48 +117,33 @@ Maasha::Biopieces::close_stream( $out );
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-sub parse_clusters
+sub uclust_sort
 {
     # Martin A. Hansen, January 2010.
     
-    # Parses a CD-hit cluster file and returns a hash with
-    # sequence name as key and cluster number as value.
+    # Sorts a FASTA file according to sequence length
+    # - longest first -  using uclust.
 
-    my ( $file,   # cluster file
+    my ( $file,      # file to sort
+         $tmp_dir,   # temporary directory for sorting
+         $verbose,   # verbose flag  -  OPTIONAL
        ) = @_;
 
-    # Returns a hash.
-   
-    my ( $block, $fh, @lines, $line, %clusters, $seq_name, $cluster );
-
-    local $/ = "\n>";
-
-    $fh = Maasha::Filesys::file_read_open( $file );
-
-    while ( $block = <$fh> )
-    {
-        chomp $block;
-
-        @lines = split "\n", $block;
-
-        $cluster = shift @lines;
-
-        $cluster =~ s/>?Cluster (\d+)/$1/;
+    # Returns nothing.
+    
+    my ( @args, $arg_str );
 
-        foreach $line ( @lines )
-        {
-            if ( $line =~ />(.*)/ )
-            {
-                $seq_name = $1;
+    push @args, "--mergesort $file";
+    push @args, "--output $file.sort";
+    push @args, "--tmpdir $tmp_dir";
+    push @args, "--quiet"          if not $verbose;
+    push @args, "> /dev/null 2>&1" if not $verbose;
 
-                $clusters{ $seq_name } = $cluster;
-            }
-        }
-    }
+    $arg_str = join " ", @args;
 
-    close $fh;
+    Maasha::Common::run( "uclust", $arg_str );
 
-    return wantarray ? %clusters : \%clusters;
+    rename "$file.sort", $file;
 }