use strict;
use warnings; 

use Bio::SeqIO;
use Bio::Tools::Run::Alignment::Clustalw;

#At last we try to call a local program Clustalw
# make sure you have it-- clustalx is not the same! that one is graphical interface.
# windows users: you most likely got a clustalw2.exe installed. move it to the working folder and change it to clustalw.exe

#this time there is no monster code--easier to modify



my @sequences; 
my $in=Bio::SeqIO->new(-file => "output.gb" ,
                           -format => 'genbank');
#genbank file can not be directly aligned, so we copy the code to make sequences in an array and use a reference to it; 

while (my $seq=$in->next_seq())
{
push @sequences, $seq; 
}


#build up a virtual machine to make alignments; you should be able to realize the differnt options and what is appropriate for your alignment
my @params = ('ktuple' => 2, 'matrix' => 'BLOSUM');
my $factory = Bio::Tools::Run::Alignment::Clustalw->new(@params);


my $seq_array_ref = \@sequences;
# where @seq_array is an array of Bio::Seq objects


#now align it! you can not really see the results as it's stored as a Bio::Align object--but we will store it soon
my $aln = $factory->align($seq_array_ref);

# and make a guidance tree! similarly it's a Bio::Tree object. 
my $tree = $factory->tree($seq_array_ref);

#or simply align the file if it's fasta
my $inputfilename="example.fasta";
if($inputfilename=~ m/fasta/)
{
my $aln = $factory->align($inputfilename);
}



use Bio::AlignIO; 
#whenever it's something ending with IO it's used for opening/writing stuff, 
my $out = Bio::AlignIO->new(-file   => ">out.aln.msf",-format => 'msf');
#we save the alignment in msf format. now you should have this file in your folder, but it's empty until we say: 
$out->write_aln($aln);



#similarly write the tree useing Bio::TreeIO; 
use Bio::TreeIO; 
my $outtree = new Bio::TreeIO(-file => '>mytree.nwk',-format => 'newick');
$outtree->write_tree($tree);





