--- /dev/null
+#!/usr/bin/env perl
+
+# Copyright (C) 2007-2009 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Cluster sequences in the stream.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+use warnings;
+use strict;
+use Data::Dumper;
+use Maasha::Common;
+use Maasha::Biopieces;
+use Maasha::Fasta;
+use Maasha::Seq;
+use Maasha::Filesys;
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+my ( $options, $in, $out, $tmp_dir, $tmp_fh1, $tmp_fh2, $fh, $record, $entry, $type, @args, $arg_str, $clusters );
+
+$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 },
+ ]
+);
+
+$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" );
+
+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 );
+ }
+
+ Maasha::Biopieces::put_record( $record, $tmp_fh2 );
+}
+
+close $tmp_fh1;
+close $tmp_fh2;
+
+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' };
+
+$arg_str = join " ", @args;
+
+if ( $type =~ /protein/i ) {
+ Maasha::Common::run( "cdhit", $arg_str );
+} else {
+ Maasha::Common::run( "cdhit-est", $arg_str );
+}
+
+$clusters = parse_clusters( "$tmp_dir/cluster.out.clstr" );
+
+$tmp_fh2 = Maasha::Filesys::file_read_open( "$tmp_dir/cluster.stream" );
+
+while ( $record = Maasha::Biopieces::get_record( $tmp_fh2 ) )
+{
+ if ( exists $clusters->{ $record->{ 'SEQ_NAME' } } ) {
+ $record->{ 'CLUSTER' } = $clusters->{ $record->{ 'SEQ_NAME' } };
+ }
+
+ Maasha::Biopieces::put_record( $record, $out );
+}
+
+Maasha::Biopieces::close_stream( $in );
+Maasha::Biopieces::close_stream( $out );
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+sub parse_clusters
+{
+ # 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.
+
+ my ( $file, # cluster file
+ ) = @_;
+
+ # 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/;
+
+ foreach $line ( @lines )
+ {
+ if ( $line =~ />(.*)/ )
+ {
+ $seq_name = $1;
+
+ $clusters{ $seq_name } = $cluster;
+ }
+ }
+ }
+
+ close $fh;
+
+ return wantarray ? %clusters : \%clusters;
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+BEGIN
+{
+ Maasha::Biopieces::status_set();
+}
+
+
+END
+{
+ Maasha::Biopieces::status_log();
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+__END__