Bio::Tools::Run::Alignment Lagan
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Toolbar
WebCvs
Summary
Bio::Tools::Run::Alignment::Lagan - Object for the local execution of the LAGAN suite of tools (including MLAGAN for multiple sequence alignments)
Package variables
No package variables defined.
Included modules
Bio::AlignIO
Bio::Matrix::IO
Bio::Root::IO
Bio::Seq
Bio::SeqIO
Bio::SimpleAlign
Cwd
File::Spec
Inherit
Bio::Tools::Run::WrapperBase
Synopsis
  use Bio::Tools::Run::Alignment::Lagan;
@params = ('chaos' => "The contents of this string will be passed as args to chaos", #Read you chaos README file for more info/This functionality #has not been tested and will be integrated in future versions. 'order' => "\"-gs -7 -gc -2 -mt 2 -ms -1\"", #Where gap start penalty of- 7, gap continue of -2, match of 2, #and mismatch of -1. 'recurse' => "\"(12,25),(7,25),(4,30)"\", #A list of (wordlength,score cutoff) pairs to be used in the #recursive anchoring 'tree' => "\"(sample1 (sample2 sample3))"\", #Used by mlagan / tree can also be passed when calling mlagan directly #SCORING PARAMETERS FOR MLAGAN: 'match' => 12, 'mismatch' => -8, 'gapstart' => -50, 'gapend' => -50, 'gapcont' => -2, );
Description
To run mlagan/lagan, you must have an environment variable that points to
the executable directory with files lagan.pl etc.
"LAGAN_DIR=/opt/lagan_executables/"
Simply having the executables in your path is not supported because the
executables themselves only work with the environment variable set.
All lagan and mlagan parameters listed in their Readmes can be set
except for the mfa flag which has been turned on by default to prevent
parsing of the alignment format.
TO USE LAGAN:
  my $lagan = Bio::Tools::Run::Alignment::Lagan->new(@params);
my $report_out = $lagan->lagan($seq1, $seq2);
A SimpleAlign object is returned.
TO USE MLAGAN:
  my $lagan = Bio::Tools::Run::Alignment::Lagan->new();
my $tree = "(($seqname1 $seqname2) $seqname3)";
my @sequence_objs; #an array of bioperl Seq objects
##If you use an unblessed seq array my $seq_ref = \@sequence_objs; bless $seq_ref, "ARRAY"; my $report_out = $lagan->mlagan($seq_ref, $tree); A SimpleAlign object is returned
Only basic mlagan/lagan functionality has been implemented due to the
iterative development of their project. Future maintenance upgrades
will include enhanced features and scoring.
Methods
new
No description
Code
laganDescriptionCode
mlaganDescriptionCode
nuc_matrixDescriptionCode
_setinputDescriptionCode
_generic_laganDescriptionCode
_setparamsDescriptionCode
_runlaganDescriptionCode
executableDescriptionCode
program_pathDescriptionCode
program_dirDescriptionCode
versionDescriptionCode
Methods description
lagancode    nextTop
  Runs the Lagan pairwise alignment algorithm
Inputs should be two PrimarySeq objects.
Returns an SimpleAlign object / preloaded with the tmp file of the Lagan multifasta output.
mlagancodeprevnextTop
  Runs the Mlagan multiple sequence alignment algorithm.
Inputs should be an Array of Primary Seq objects and a Phylogenetic Tree in
String format or as a Bio::Tree::TreeI compliant object.
Returns an SimpleAlign object / preloaded with the tmp file of the Mlagan
multifasta output.
nuc_matrixcodeprevnextTop
 Title   : nuc_matrix
Usage : my $matrix_obj = $obj->nuc_matrix();
-or-
$obj->nuc_matrix($matrix_obj);
-or-
$obj->nuc_matrix($matrix_file);
Function: Get/set the substitution matrix for use by mlagan. By default the
file $LAGAN_DIR/nucmatrix.txt is used by mlagan. By default this
method returns a corresponding Matrix.
Returns : Bio::Matrix::Mlagan object
Args : none to get, OR to set:
Bio::Matrix::MLagan object
OR
filename of an mlagan substitution matrix file
NB: due to a bug in mlagan 2.0, the -nucmatrixfile option does not work, so this Bioperl wrapper is unable to simply point mlagan to your desired matrix file (or to a temp file generated from your matrix object). Instead the $LAGAN_DIR/nucmatrix.txt file must actually be replaced. This wrapper will make a back-up copy of that file, write the new file in its place, then revert things back to the way they were after the alignment has been produced. For this reason, $LAGAN_DIR must be writable, as must $LAGAN_DIR/nucmatrix.txt.
_setinputcodeprevnextTop
 Title   : _setinput
Usage : Internal function, not to be called directly
Function: Create input file(s) for Lagan executables
Returns : name of files containing Lagan data input /
or array of files and phylo tree for Mlagan data input
_generic_lagancodeprevnextTop
 Title   : _generic_lagan
Usage : internal function not called directly
Returns : SimpleAlign object
_setparamscodeprevnextTop
 Title   : _setparams
Usage : Internal function, not to be called directly
Function: Create parameter inputs for (m)Lagan program
Returns : parameter string to be passed to Lagan
Args : Reference to calling object and name of (m)Lagan executable
_runlagancodeprevnextTop
 Title   :  _runlagan
Usage : Internal function, not to be called directly
Function: makes actual system call to (m)Lagan program
Example :
Returns : Report object in the SimpleAlign object
executablecodeprevnextTop
 Title   : executable
Usage : my $exe = $lagan->executable('mlagan');
Function: Finds the full path to the 'lagan' executable
Returns : string representing the full path to the exe
Args : [optional] name of executable to set path to
[optional] boolean flag whether or not warn when exe is not found
Thanks to Jason Stajich for providing the framework for this subroutine
program_pathcodeprevnextTop
 Title   : program_path
Usage : my $path = $lagan->program_path();
Function: Builds path for executable
Returns : string representing the full path to the exe
Thanks to Jason Stajich for providing the framework for this subroutine
program_dircodeprevnextTop
 Title   : program_dir
Usage : my $dir = $lagan->program_dir();
Function: Abstract get method for dir of program. To be implemented
by wrapper.
Returns : string representing program directory
Thanks to Jason Stajich for providing the framework for this subroutine
versioncodeprevnextTop
 Title   : version
Usage : my $version = $lagan->version;
Function: returns the program version
Returns : number
Args : none
Methods code
newdescriptionprevnextTop
sub new {
    my($class, @args) = @_;
    my $self = $class->SUPER::new(@args);
    
    $self->_set_from_args(\@args, -methods => [@LAGAN_PARAMS, @OTHER_PARAMS,
                                               @LAGAN_SWITCHES, @MLAGAN_PARAMS],
                                  -create => 1);
    
    my ($tfh, $tempfile) = $self->io->tempfile();
    my $outfile = $self->out || $self->outfile || $tempfile;
    $self->out($outfile);
    close($tfh);
    undef $tfh;
    return $self;
}
lagandescriptionprevnextTop
sub lagan {
    my ($self, $input1, $input2) = @_;
    $self->io->_io_cleanup();
    my $executable = 'lagan.pl';
		
    #my (undef, $tempfile) = $self->io->tempfile();
#$self->out($tempfile);
my ($infile1, $infile2) = $self->_setinput($executable, $input1, $input2); my $lagan_report = &_generic_lagan( $self, $executable, $infile1, $infile2 );
}
mlagandescriptionprevnextTop
sub mlagan {
    my ($self, $input1, $tree) = @_;
    $self->io->_io_cleanup();
    my $executable = 'mlagan';
    
    if ($tree && ref($tree) && $tree->isa('Bio::Tree::TreeI')) {
        # fiddle tree so mlagan will like it
my %orig_ids; foreach my $node ($tree->get_nodes) { my $seq_id = $node->name('supplied'); $seq_id = $seq_id ? shift @{$seq_id} : ($node->node_name ? $node->node_name : $node->id); $orig_ids{$seq_id} = $node->id; $node->id($seq_id); } # convert to string
my $tree_obj = $tree; $tree = $tree->simplify_to_leaves_string; # more fiddling
$tree =~ s/ /_/g; $tree =~ s/"//g; $tree =~ s/,/ /g; # unfiddle the tree object
foreach my $node ($tree_obj->get_nodes) { $node->id($orig_ids{$node->id}); } } my $infiles; ($infiles, $tree) = $self->_setinput($executable, $input1, $tree); my $lagan_report = &_generic_lagan ( $self, $executable, $infiles, $tree );
}
nuc_matrixdescriptionprevnextTop
sub nuc_matrix {
    my ($self, $thing, $gap_open, $gap_continue) = @_;
    
    if ($thing) {
        if (-e $thing) {
            my $min = Bio::Matrix::IO->new(-format => 'mlagan',
                                           -file   => $thing);
            $self->{_nuc_matrix} = $min->next_matrix;
        }
        elsif (ref($thing) && $thing->isa('Bio::Matrix::Mlagan')) {
            $self->{_nuc_matrix} = $thing;
        }
        else {
            $self->throw("Unknown kind of thing supplied, '$thing'");
        }
        
        $self->{_nuc_matrix_set} = 1;
    }
    
    unless (defined $self->{_nuc_matrix}) {
        # read the program default file
my $min = Bio::Matrix::IO->new(-format => 'mlagan', -file => File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt')); $self->{_nuc_matrix} = $min->next_matrix; } $self->{_nuc_matrix_set} = 1 if defined wantarray; return $self->{_nuc_matrix};
}
_setinputdescriptionprevnextTop
sub _setinput {
    my ($self, $executable, $input1, $input2) = @_;
    my ($fh, $infile1, $infile2, $temp1, $temp2, $seq1, $seq2);

    $self->io->_io_cleanup();
	
  SWITCH: {
        if (ref($input1) =~ /ARRAY/i) {

            ##INPUTS TO MLAGAN / WILL hAVE TO BE CHANGED IF LAGAN EVER
##SUPPORTS MULTI-INPUT
my @infilearr; foreach $seq1 (@$input1) { ($fh, $infile1) = $self->io->tempfile(); my $temp = Bio::SeqIO->new( -fh => $fh, -format => 'Fasta' ); unless ($seq1->isa("Bio::PrimarySeqI")) { return 0; } $temp->write_seq($seq1); close $fh; undef $fh; push @infilearr, $infile1; } $infile1 =\@ infilearr; last SWITCH; } elsif ($input1->isa("Bio::PrimarySeqI")) { ##INPUTS TO LAGAN
($fh, $infile1) = $self->io->tempfile(); #Want to make sure their are no white spaces in sequence.
#Happens if input1 is taken from an alignment.
my $sequence = $input1->seq(); $sequence =~ s/\W+//g; $input1->seq($sequence); $temp1 = Bio::SeqIO->new( -fh => $fh, -format => 'Fasta' ); $temp1->write_seq($input1); close $fh; undef $fh; last SWITCH; } } SWITCH2: { if (ref($input2)) { if ($input2->isa("Bio::PrimarySeqI")) { ($fh, $infile2) = $self->io->tempfile(); #Want to make sure their are no white spaces in
#sequence. Happens if input2 is taken from an
#alignment.
my $sequence = $input2->seq(); $sequence =~ s/\W+//g; $input2->seq($sequence); $temp2 = Bio::SeqIO->new( -fh => $fh, -format => 'Fasta' ); $temp2->write_seq($input2); close $fh; undef $fh; last SWITCH2; } } else { $infile2 = $input2; ##A tree as a scalar has been passed, pass it through
} } return ($infile1, $infile2);
}
_generic_lagandescriptionprevnextTop
sub _generic_lagan {
    my ($self, $executable, $input1, $input2) = @_;
    my $param_string = $self->_setparams($executable);
    my $lagan_report = &_runlagan($self, $executable, $param_string,
                                  $input1, $input2);
}
_setparamsdescriptionprevnextTop
sub _setparams {
    my ($self, $executable) = @_;
    
    my (@execparams, $nucmatrixfile);
    if ($executable eq 'lagan.pl') {
        @execparams = @LAGAN_PARAMS;
    }
    elsif ($executable eq 'mlagan') {
        @execparams = @MLAGAN_PARAMS;
        
        if ($self->{_nuc_matrix_set}) {
            # we create this file on every call because we have no way of
# knowing if user altered the matrix object
(my $handle, $nucmatrixfile) = $self->io->tempfile(); my $mout = Bio::Matrix::IO->new(-format => 'mlagan', -fh => $handle); $mout->write_matrix($self->nuc_matrix); $self->{_nucmatrixfile} = $nucmatrixfile; } } ##EXPAND OTHER LAGAN SUITE PROGRAMS HERE
my $param_string = $self->SUPER::_setparams(-params => [@execparams], -dash => 1); $param_string .= " -nucmatrixfile $nucmatrixfile" if $nucmatrixfile; return $param_string . " -mfa ";
}
_runlagandescriptionprevnextTop
sub _runlagan {
    my ($self, $executable, $param_string, $input1, $input2) = @_;
    my ($lagan_obj, $exe);
    if ( ! ($exe = $self->executable($executable))) {
        return;
    }
    
    my $version = $self->version;
    
    my $command_string;
    if ($executable eq 'lagan.pl') {
        $command_string = $exe . " " . $input1 . " " . $input2 . $param_string;
    }
    if ($executable eq 'mlagan') {
        $command_string = $exe;
        foreach my $tempfile (@$input1) {
            $command_string .= " " . $tempfile;
        }
        if (defined $input2) {
            $command_string .= " -tree " . "\"" . $input2 . "\"";
        }	
        $command_string .= " " . $param_string;
        
        my $matrix_file = $self->{_nucmatrixfile};
        if ($version <= 3 && $matrix_file) {
            # mlagan 2.0 bug-workaround
my $orig = File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt'); -e $orig || $self->throw("Strange, $orig doesn't seem to exist"); system("cp $orig $orig.bk") && $self->throw("Backup of $orig failed: $!"); system("cp $matrix_file $orig") && $self->throw("Copy of $matrix_file -> $orig failed: $!"); } } if (($self->silent || $self->quiet) && ($^O !~ /os2|dos|MSWin32|amigaos/)) { $command_string .= ' > /dev/null 2> /dev/null'; } # will do brute-force clean up of junk files generated by lagan
my $cwd = cwd(); opendir(my $cwd_dir, $cwd) || $self->throw("Could not open the current directory '$cwd'!"); my %ok_files; foreach my $thing (readdir($cwd_dir)) { if ($thing =~ /anch/) { $ok_files{$thing} = 1; } } closedir($cwd_dir); $self->debug("$command_string\n"); my $status = system(($version <= 3 ? '_POSIX2_VERSION=1 ' : '').$command_string); # temporary hack whilst lagan script 'rechaos.pl' uses obsolete sort syntax
if ($version <= 1 && $self->{_nucmatrixfile}) { my $orig = File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt'); system("mv $orig.bk $orig") && $self->warn("Restore of $orig from $orig.bk failed: $!"); } opendir($cwd_dir, $cwd) || $self->throw("Could not open the current directory '$cwd'!"); foreach my $thing (readdir($cwd_dir)) { if ($thing =~ /anch/) { unlink($thing) unless $ok_files{$thing}; } } closedir($cwd_dir); my $outfile = $self->out(); my $align = Bio::AlignIO->new( '-file' => $outfile, '-format' => 'fasta' ); my $aln = $align->next_aln(); return $aln;
}
executabledescriptionprevnextTop
sub executable {
    my ($self, $exename, $exe, $warn) = @_;
    $exename = 'lagan.pl' unless defined $exename;

    if ( defined $exe && -x $exe ) {
        $self->{'_pathtoexe'}->{$exename} = $exe;
    }
    unless ( defined $self->{'_pathtoexe'}->{$exename} ) {
        my $f = $self->program_path($exename);
        $exe = $self->{'_pathtoexe'}->{$exename} = $f if(-e $f && -x $f );
        
        unless( $exe )  {
            if ( ($exe = $self->io->exists_exe($exename)) && -x $exe ) {
                $self->{'_pathtoexe'}->{$exename} = $exe;
            } else {
                $self->warn("Cannot find executable for $exename") if $warn;
                $self->{'_pathtoexe'}->{$exename} = undef;
            }
        }
    }
    
    # even if its executable, we still need the environment variable to have
# been set
if (! $PROGRAM_DIR) { $self->warn("Environment variable LAGAN_DIR must be set, even if the lagan executables are in your path"); $self->{'_pathtoexe'}->{$exename} = undef; } return $self->{'_pathtoexe'}->{$exename};
}
program_pathdescriptionprevnextTop
sub program_path {
    my ($self,$program_name) = @_;
    my @path;
    push @path, $self->program_dir if $self->program_dir;
   	push @path, $program_name .($^O =~ /mswin/i ?'':'');
	# Option for Windows variants / None so far
return Bio::Root::IO->catfile(@path);
}
program_dirdescriptionprevnextTop
sub program_dir {
    $PROGRAM_DIR;
}
versiondescriptionprevnextTop
sub version {
    my $self = shift;
    my $exe = $self->executable('mlagan') || return;
    
    open(my $VER, "$exe -version 2>&1 |") || die "Could not open command '$exe -version'\n";
    my $version;
    while (my $line = <$VER>) {
        ($version) = $line =~ /(\d+\S+)/;
    }
    close($VER) || die "Could not complete command '$exe -version'\n";
    
    return $version;
}

1;
}
General documentation
FEEDBACKTop
Mailing ListsTop
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
  bioperl-l@bioperl.org                  - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
Support Top
 
Please direct usage questions or support issues to the mailing list:

bioperl-l@bioperl.org

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
Reporting BugsTop
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via the
web:
  http://bugzilla.open-bio.org/
AUTHOR - Stephen MontgomeryTop
Email smontgom@bcgsc.bc.ca
Genome Sciences Centre in beautiful Vancouver, British Columbia CANADA
CONTRIBUTORSTop
MLagan/Lagan is the hard work of Michael Brudno et al.
Sendu Bala bix@sendu.me.uk
APPENDIXTop
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _