#!/usr/bin/perl
package ClustalwForWine;

##############################################################
##### Author : Shuai Weng	
##### Date   : July 2002
##### Description : 

use strict;

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use ConfigPathdictyBase;


#############################################################
sub new {     
#############################################################     
  
    my ($self, %args) = @_;

    $self = {};
    bless $self;

    $self->{'_seqIdArrayRef'} = $args{'seqIdArrayRef'};
    $self->{'_seqHashRef'} = $args{'seqHashRef'};
    $self->{'_infile'} = $args{'infile'};
    $self->{'_format'} = $args{'format'} || 'fasta';
    $self->{'_ktuple'} = $args{'ktuple'} || 2;
    $self->{'_matrix'} = $args{'matrix'} || 'BLOSUM';
    $self->{'_case'} = $args{'case'} || 'lower';  ## lower OR upper
    $self->{'_id'} = $args{'id'} || $$;
 
    $self->_runClustalW;
   
    return $self;
}

#############################################################
sub _runClustalW {
#############################################################
    my ($self) = @_;
    
    my $configPath = ConfigPathdictyBase->new;
    my $dataDir = $configPath->wineStem."html/homolog/tmp/";
   
    my $id = $self->{'_id'};

    my $infile;
    my $outfile = $dataDir."clustalwOut.".$self->{'_id'}.".tmp";
    $self->{'_dndfile'} = $dataDir."clustalwIn.".$self->{'_id'}.".dnd";

    if ($self->{'_infile'}) {
	$infile = $self->{'_infile'};
    }
    else {
	$infile = $dataDir."clustalwIn.".$self->{'_id'}.".tmp";

	open(OUT, ">$infile") ||
	    die "Can't open '$infile' for writing:$!";
	foreach my $seqId (@{$self->{'_seqIdArrayRef'}}) {
	    my $seq = $self->{'_seqHashRef'}->{$seqId};
	    print OUT ">$seqId\n";
	    print OUT "$seq\n";

	}
	close(OUT);
    }

    my $clustalw = "/tools/bio/bin/clustalw";

    my $cmd = "$clustalw -infile=$infile -output=".$self->{'_format'}." -ktuple=".$self->{'_ktuple'}." -matrix=".$self->{'_matrix'}." -case=".$self->{'_case'}." -outfile=$outfile";  
    
    system ("$cmd >$outfile.tmp");
      
    $self->{'_outfile'} = $outfile;

}

#############################################################
sub alignmentFile {
#############################################################
    my ($self) = @_;

    return $self->{'_outfile'};
    
}

####################################################################
sub dndFile {
####################################################################
    my ($self) = @_;

    return $self->{'_dndfile'};

}

####################################################################
sub _err_report {
####################################################################
    my ($self, $args) = @_;

    my ($file, $line, $method) = (caller(1))[1..3];
    print "The argument '$args' must be passed to '$method'.", br;
    print "Please add this argument to line $line in $file.",br;
    exit;

}


########################################################################
1;
########################################################################

=pod

=head1 Name

ClustalWlocal.pm    

=head1 Description

This perl object (ClustalWlocal.pm) contains method for creating sequence alignment.

=head1 Instantiating a New ClustalWlocal Object

To instantiate a new ClustalWlocal object, you may use following syntax:

my $obj = ClustalWlocal->new(seqIdArrayRef=>$arrayRef,
			     seqHashRef=>$hashRef);


$arrayRef is the reference for the array containing the sequence identifiers in the order you want to display in the alignment.

$hashRef is the reference for the hash, in which the key is the sequence identifier and the value is the protein/DNA sequence.

=head1 Accessor Methods


=head2 alignmentFile

Usage:

my $file = $obj->alignmentFile;

This method returns the alignment file to the caller.

=head1 Author

shuai@genome.stanford.edu

=cut








