#!/usr/bin/perl
package ClustalWlocal;

##############################################################
##### 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->{'_format'} = $args{'format'} || 'fasta';
    $self->{'_ktuple'} = $args{'ktuple'} || 2;
    $self->{'_matrix'} = $args{'matrix'} || 'BLOSUM';
    $self->{'_case'} = $args{'case'} || 'lower';  ## lower OR upper

    $self->_runClustalW;
   
    return $self;
}

#############################################################
sub _runClustalW {
#############################################################
    my ($self) = @_;
    
    my $configPath = ConfigPathdictyBase->new;
  
    my $infile = $configPath->tmpDir."clustalwIn.$$.tmp";
    my $outfile = $configPath->tmpDir."clustalwOut.$$.tmp";
    $self->{'_dndfile'} = $configPath->tmpDir."clustalwIn.$$.dnd";

    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 = $configPath->DictyosteliumStem."cgi-bin/clustalw/clustalw";

    my $cmd = "$clustalw align -infile=$infile -output=".$self->{'_format'}." -ktuple=".$self->{'_ktuple'}." -matrix=".$self->{'_matrix'}." -case=".$self->{'_case'}." -outfile=$outfile";  
 
#    system "rsh Dictyostelium '$cmd' >$outfile.tmp" || 
#	print "ClustalWlocal: problem running command: $!\n";
 
    open(IN, "rsh Dictyostelium '$cmd' |") || die "ClustalWlocal: problem running command: $!\n";


    sleep 10;

    my $msg;

    while (-z $self->{'_dndfile'} || -z $outfile) {

	if (!$msg) {

	    print "It may take a while to run the clustalw program, please wait ...<p>";
	    $msg++;

	}
	else {
	    print "Wait ......<br>";

	    sleep 5;
	}

    }
    
    while(<IN>) {

	# print "Wait ....<br>";

    }

    close(IN);

    $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








