Bio::AlignIO
nexus
Summary
Bio::AlignIO::nexus - NEXUS format sequence input/output stream
Package variables
No package variables defined.
Inherit
Synopsis
Do not use this module directly. Use it via the
Bio::AlignIO class.
use Bio::AlignIO;
my $in = new Bio::AlignIO(-format => 'nexus',
-file => 'aln.nexus');
while( my $aln = $in->next_aln ) {
# do something with the alignment
}
Description
This object can transform
Bio::Align::AlignI objects to and from NEXUS
data blocks. See method documentation for supported NEXUS features.
Methods
Methods description
Title : next_aln Usage : $aln = $stream->next_aln() Function: Returns the next alignment in the stream.
Supports the following NEXUS format features:
- The file has to start with '#NEXUS'
- Reads in the name of the alignment from a comment
(anything after 'TITLE: ') .
- Sequence names can be given in a taxa block, too.
- If matchchar notation is used, converts
them back to sequence characters.
- Does character conversions specified in the
NEXUS equate command.
- Sequence names of type 'Homo sapiens' and
Homo_sapiens are treated identically.
Returns : Bio::Align::AlignI object Args : |
Title : write_aln Usage : $stream->write_aln(@aln) Function: Writes the $aln object into the stream in interleaved NEXUS format. Everything is written into a data block. SimpleAlign methods match_char, missing_char and gap_char must be set if you want to see them in the output. Returns : 1 for success and 0 for error Args : Bio::Align::AlignI object |
Title : flag Usage : $obj->flag($name,$value) Function: Get/Set a flag value Returns : value of flag (a scalar) Args : on set, new value (a scalar or undef, optional) |
Methods code
BEGIN { %valid_type = map {$_, 1} qw( dna rna protein standard );
} |
sub _initialize
{ my ($self, @args) = @_;
$self->SUPER::_initialize(@args);
my ($show_symbols, $endblock) =
$self->_rearrange([qw(SHOW_SYMBOLS SHOW_ENDBLOCK)], @args);
my @names = qw(symbols endblock);
for my $v ( $show_symbols, $endblock ) {
$v = 1 unless defined $v; my $n = shift @names;
$self->flag($n, $v);
}} |
sub next_aln
{ my $self = shift;
my $entry;
my ($aln_name, $seqcount, $residuecount, %hash, $alphabet,
$match, $gap, $missing, $equate, $interleave,
$name,$str,@names,$seqname,$start,$end,$count,$seq);
my $aln = Bio::SimpleAlign->new(-source => 'nexus');
$entry = $self->_readline;
$entry = $self->_readline while defined $entry && $entry =~ /^\s+$/;
return unless $entry;
$self->throw("Not a valid interleaved NEXUS file! [#NEXUS] not starting the file\n$entry")
unless $entry =~ /^#NEXUS/i;
while (defined($entry = $self->_readline)) {
local ($_) = $entry;
/\[TITLE. *([^\]]+)]\s+/i and $aln_name = $1;
last if /^begin +data/i || /^begin +taxa/i;
}
$aln_name =~ s/\s/_/g and $aln->id($aln_name) if $aln_name;
my $incomment;
while ($entry = $self->_readline) {
local ($_) = $entry;
next if s/\[[^\]]+\]//g; if( s/\[[^\]]+$// ) {
$incomment = 1;
next if /^\s*$/;
} elsif($incomment) {
if( s/^[^\]]*\]// ) {
$incomment = 0;
} else {
next;
}
} elsif( /taxlabels/i ) {
} else {
/ntax\s*=\s*(\d+)/i and $seqcount = $1;
/nchar\s*=\s*(\d+)/i and $residuecount = $1;
/matchchar\s*=\s*(.)/i and $match = $1;
/gap\s*=\s*(.)/i and $gap = $1;
/missing\s*=\s*(.)/i and $missing = $1;
/equate\s*=\s*\"([^\"]+)/i and $equate = $1; /datatype\s*=\s*(\w+)/i and $alphabet = lc $1;
/interleave/i and $interleave = 1 ;
last if /matrix/io;
}
}
$self->throw("Not a valid NEXUS sequence file. Datatype not specified.")
unless $alphabet;
$self->throw("Not a valid NEXUS sequence file. Datatype should not be [$alphabet]")
unless $valid_type{$alphabet};
$self->throw("\"$gap\" is not a valid gap character. For compatability, gap char can not be one of: ()[]{}/\,;:=*'`\"<>^")
if $gap && $gap =~ /[\(\)\[\]\{\}\/\\\,\;\:\=\*\'\`\<\>\^]/;
$self->throw("\"$missing\" is not a valid missing character. For compatability, missing char can not be one of: ()[]{}/\,;:=*'`\"<>^")
if $missing && $missing =~ /[\(\)\[\]\{\}\/\\\,\;\:\=\*\'\`\<\>\^]/;
$aln->gap_char($gap);
$aln->missing_char($missing);
while ($entry = $self->_readline) {
unless ($entry =~ /^\s+$/) {
$self->_pushback($entry);
last;
}
}
if (@names == 0) { while ($entry = $self->_readline) {
local ($_) = $entry;
if( s/\[[^\]]+\]//g ) { next if /^\s*$/;
}
if ($interleave && defined$count && ($count <= $seqcount)) {
/^\s+$/ and last;
} else {
/^\s+$/ and next;
}
/^\s*;/ and last; if ( /^\s*([\"\'](.+?)[\"\']|(\S+))\s+(.*)\s*$/ ) {
$name = ($2 || $3);
if ($4) {
$str = $4;
} else {
$str='';
while (local ($_) = $self->_readline) {
my $str_tmp = $_;
$str_tmp =~ s/[\s;]//g;
$str .= $str_tmp;
last if length$str == $residuecount;
}
}
$name =~ s/ /_/g;
push @names, $name;
$str =~ s/[\s;]//g;
$count = @names;
$hash{$count} = $str;
}
$self->throw("Not a valid interleaved NEXUS file! seqcount [$count] > predeclared [$seqcount] in the first section") if $count > $seqcount;
/;/ and last; }
}
$count = 0;
if ( $interleave ) { while( $entry = $self->_readline) {
local ($_) = $entry;
if( s/\[[^\]]+\]//g ) { next if /^\s*$/; }
/^\s*;/ and last; $count = 0, next if $entry =~ /^\s*$/;
if (/^\s*('([^']*?)'|([^']\S*))\s+(.*)$/) { $str = $4;
$str =~ s/[\s;]//g;
$count++;
$hash{$count} .= $str;
};
$self->throw("Not a valid interleaved NEXUS file!
seqcount [$count] > predeclared [$seqcount] ") if $count > $seqcount;
/;/ and last; }
}
return 0 if @names < 1;
$count = 0;
foreach $name ( @names ) {
$count++;
if( $name =~ /(\S+)\/(\d+)-(\d+)/ ) {
$seqname = $1;
$start = $2;
$end = $3;
} else {
$seqname=$name;
$start = 1;
$str = $hash{$count};
$str =~ s/[^A-Za-z]//g;
$end = length($str);
}
$self->throw("Length of sequence [$seqname] is not [$residuecount]! ")
unless CORE::length($hash{$count}) == $residuecount;
$seq = new Bio::LocatableSeq('-seq'=>$hash{$count},
'-id'=>$seqname,
'-start'=>$start,
'-end'=>$end,
'alphabet'=>$alphabet
);
$aln->add_seq($seq);
}
$aln->unmatch($match) if $match;
if ($equate) {
$aln->map_chars($1, $2) while $equate =~ /(\S)=(\S)/g;
}
while (defined $entry &&
$entry !~ /endblock/i) {
$entry = $self->_readline;
}
return $aln;} |
sub _read_taxlabels
{ my ($self) = @_;
my ($name, @names);
while (my $entry = $self->_readline) {
last if $entry =~ m/^\s*(END)?;/i;
if( $entry =~ m/\s*(\S+)\s+/ ) {
($name) = ($1);
$name =~ s/\[[^\[]+\]//g;
$name =~ s/\W/_/g;
push @names, $name;
}
}
return @names;} |
sub write_aln
{ my ($self,@aln) = @_;
my $count = 0;
my $wrapped = 0;
my $maxname;
my ($length,$date,$name,$seq,$miss,$pad,%hash,@arr,$tempcount,$index );
my ($match, $missing, $gap,$symbols) = ('', '', '','');
foreach my $aln (@aln) {
if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
$self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
next;
}
$self->throw("All sequences in the alignment must be the same length")
unless $aln->is_flush($self->verbose);
$length = $aln->length();
$self->_print (sprintf("#NEXUS\n[TITLE: %s]\n\nbegin data;\ndimensions ntax=%s nchar=%s;\n",
$aln->id, $aln->no_sequences, $length));
$match = "match=". $aln->match_char if $aln->match_char;
$missing = "missing=". $aln->missing_char if $aln->missing_char;
$gap = "gap=". $aln->gap_char if $aln->gap_char;
$symbols = 'symbols="'.join('',$aln->symbol_chars). '"'
if( $self->flag('symbols') && $aln->symbol_chars);
$self->_print
(sprintf("format interleave datatype=%s %s %s %s %s;\n\nmatrix\n",
$aln->get_seq_by_pos(1)->alphabet, $match,
$missing, $gap, $symbols));
my $indent = $aln->maxdisplayname_length+2;
$aln->set_displayname_flat();
foreach $seq ( $aln->each_seq() ) {
my $nmid = $aln->displayname($seq->get_nse());
if( $nmid =~ /[^\w\d]/ ) {
$name = sprintf("%-${indent}s", "\'" . $nmid . "\'");
} else {
$name = sprintf("%-${indent}s", $nmid);
}
$hash{$name} = $seq->seq;
push(@arr,$name);
}
while( $count < $length ) {
foreach $name ( @arr ) {
my $dispname = $name;
$self->_print (sprintf("%${indent}s ",$dispname));
$tempcount = $count;
$index = 0;
while( ($tempcount + 10 < $length) && ($index < 5) ) {
$self->_print (sprintf("%s ",substr($hash{$name},$tempcount,10)));
$tempcount += 10;
$index++;
}
if( $index < 5) {
$self->_print (sprintf("%s ",substr($hash{$name},$tempcount)));
$tempcount += 10;
}
$self->_print ("\n");
}
$self->_print ("\n\n");
$count = $tempcount;
$wrapped = 1;
}
if( $self->flag('endblock') ) {
$self->_print (";\n\nendblock;\n");
} else {
$self->_print (";\n\nend;\n");
}
}
$self->flush if $self->_flush_on_write && defined $self->_fh;
return 1;} |
sub flag
{ my ($self,$name,$val) = @_;
return $self->{'flag'}->{$name} = $val if defined $val;
return $self->{'flag'}->{$name};} |
General documentation
Will Fisher has written an excellent standalone NEXUS format parser in
Perl, readnexus. A number of tricks were adapted from it.
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
http://bugzilla.open-bio.org/
| AUTHORS - Heikki Lehvaslaiho | Top |
Email: heikki-at-bioperl-dot-org
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _
Title : new
Usage : $alignio = new Bio::AlignIO(-format => 'nexus',
-file => 'filename');
Function: returns a new Bio::AlignIO object to handle clustalw files
Returns : Bio::AlignIO::clustalw object
Args : -verbose => verbosity setting (-1,0,1,2)
-file => name of file to read in or with ">" - writeout
-fh => alternative to -file param - provide a filehandle
to read from/write to
-format => type of Alignment Format to process or produce
Customization of nexus flavor output
-show_symbols => print the symbols="ATGC" in the data definition
(MrBayes does not like this)
boolean [default is 1]
-show_endblock => print an 'endblock;' at the end of the data
(MyBayes does not like this)
boolean [default is 1]