Do not use this module directly. Use it via the Bio::Variation::IO class.
sub write
{ my ($self,@h) = @_;
if( ! defined $h[0] ) {
$self->throw("Attempting to write with no information!");
}
my $str;
my $output = IO::String->new($str);
my $w = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT => 4 );
foreach my $h (@h) {
$h->alphabet || $self->throw("Moltype of the reference sequence is not set!");
my $hasAA = 0;
foreach my $mut ($h->each_Variant) {
$hasAA = 1 if $mut->isa('Bio::Variation::AAChange');
}
if ($hasAA) {
$w->startTag("seqDiff",
"id" => $h->id,
"moltype" => $h->alphabet,
"offset" => $h->offset,
"sysname" => $h->sysname,
"trivname" => $h->trivname
);
} else {
$w->startTag("seqDiff",
"id" => $h->id,
"moltype" => $h->alphabet,
"offset" => $h->offset,
"sysname" => $h->sysname
);
}
my @allvariants = $h->each_Variant;
my %variants = ();
foreach my $mut ($h->each_Variant) {
push @{$variants{$mut->mut_number} }, $mut;
}
foreach my $var (sort keys %variants) {
foreach my $mut (@{$variants{$var}}) {
if( $mut->isa('Bio::Variation::DNAMutation') ) {
$mut->isMutation(0) if not $mut->isMutation;
my @alleles = $mut->each_Allele;
my $count = 0;
foreach my $allele (@alleles) {
$count++;
my ($variation_number, $change_number) = split /\./, $mut->mut_number;
if ($change_number and $change_number != $count){
$mut->mut_number("$change_number.$count");
}
$mut->allele_mut($allele);
$w->startTag("DNA",
"number" => $mut->mut_number,
"start" => $mut->start,
"end" => $mut->end,
"length" => $mut->length,
"isMutation" => $mut->isMutation
);
if ($mut->label) {
foreach my $label (split ', ', $mut->label) {
$w->startTag("label");
$w->characters($label);
$w->endTag;
}
}
if ($mut->proof) {
$w->startTag("proof");
$w->characters($mut->proof );
$w->endTag;
}
if ($mut->upStreamSeq) {
$w->startTag("upFlank");
$w->characters($mut->upStreamSeq );
$w->endTag;
}
$w->startTag("allele_ori");
$w->characters($mut->allele_ori->seq) if $mut->allele_ori->seq ;
$w->endTag;
$w->startTag("allele_mut");
$w->characters($mut->allele_mut->seq) if $mut->allele_mut->seq;
$w->endTag;
if ($mut->dnStreamSeq) {
$w->startTag("dnFlank");
$w->characters($mut->dnStreamSeq );
$w->endTag;
}
if ($mut->restriction_changes) {
$w->startTag("restriction_changes");
$w->characters($mut->restriction_changes);
$w->endTag;
}
if ($mut->region) {
if($mut->region_value and $mut->region_dist) {
$w->startTag("region",
"value" => $mut->region_value,
"dist" => $mut->region_dist
);
}
elsif($mut->region_value) {
$w->startTag("region",
"value" => $mut->region_value
);
}
elsif($mut->region_dist) {
$w->startTag("region",
"dist" => $mut->region_dist
);
} else {
$w->startTag("region");
}
$w->characters($mut->region );
$w->endTag;
}
$w->endTag; }
}
elsif( $mut->isa('Bio::Variation::RNAChange') ) {
$mut->isMutation(0) if not $mut->isMutation;
my @alleles = $mut->each_Allele;
my $count = 0;
foreach my $allele (@alleles) {
$count++;
my ($variation_number, $change_number) = split /\./, $mut->mut_number;
if ($change_number and $change_number != $count){
$mut->mut_number("$change_number.$count");
}
$mut->allele_mut($allele);
$w->startTag("RNA",
"number" => $mut->mut_number,
"start" => $mut->start,
"end" => $mut->end,
"length" => $mut->length,
"isMutation" => $mut->isMutation
);
if ($mut->label) {
foreach my $label (split ', ', $mut->label) {
$w->startTag("label");
$w->characters($label );
$w->endTag;
}
}
if ($mut->proof) {
$w->startTag("proof");
$w->characters($mut->proof );
$w->endTag;
}
if ($mut->upStreamSeq) {
$w->startTag("upFlank");
$w->characters($mut->upStreamSeq );
$w->endTag;
}
if ($mut->allele_ori) {
$w->startTag("allele_ori");
$w->characters($mut->allele_ori->seq) if $mut->allele_ori->seq ;
$w->endTag;
}
if ($mut->allele_mut) {
$w->startTag("allele_mut");
$w->characters($mut->allele_mut->seq) if $mut->allele_mut->seq ;
$w->endTag;
}
if ($mut->dnStreamSeq) {
$w->startTag("dnFlank");
$w->characters($mut->dnStreamSeq );
$w->endTag;
}
if ($mut->region eq 'coding') {
if (! $mut->codon_mut) {
$w->startTag("codon",
"codon_ori" => $mut->codon_ori,
"codon_pos" => $mut->codon_pos
);
} else {
$w->startTag("codon",
"codon_ori" => $mut->codon_ori,
"codon_mut" => $mut->codon_mut,
"codon_pos" => $mut->codon_pos
);
}
$w->endTag;
}
if ($mut->codon_table != 1) {
$w->startTag("codon_table");
$w->characters($mut->codon_table);
$w->endTag;
}
if ($mut->restriction_changes) {
$w->startTag("restriction_changes");
$w->characters($mut->restriction_changes);
$w->endTag;
}
if ($mut->region) {
if($mut->region_value and $mut->region_dist) {
$w->startTag("region",
"value" => $mut->region_value,
"dist" => $mut->region_dist
);
}
elsif($mut->region_value) {
$w->startTag("region",
"value" => $mut->region_value
);
}
elsif($mut->region_dist) {
$w->startTag("region",
"dist" => $mut->region_dist
);
} else {
$w->startTag("region");
}
$w->characters($mut->region );
$w->endTag;
}
$w->endTag; }
}
elsif( $mut->isa('Bio::Variation::AAChange') ) {
$mut->isMutation(0) if not $mut->isMutation;
my @alleles = $mut->each_Allele;
my $count = 0;
foreach my $allele (@alleles) {
$count++;
my ($variation_number, $change_number) = split /\./, $mut->mut_number;
if ($change_number and $change_number != $count){
$mut->mut_number("$change_number.$count");
}
$mut->allele_mut($allele);
$w->startTag("AA",
"number" => $mut->mut_number,
"start" => $mut->start,
"end" => $mut->end,
"length" => $mut->length,
"isMutation" => $mut->isMutation
);
if ($mut->label) {
foreach my $label (split ', ', $mut->label) {
$w->startTag("label");
$w->characters($label );
$w->endTag;
}
}
if ($mut->proof) {
$w->startTag("proof");
$w->characters($mut->proof );
$w->endTag;
}
if ($mut->allele_ori) {
$w->startTag("allele_ori");
$w->characters($mut->allele_ori->seq) if $mut->allele_ori->seq;
$w->endTag;
}
if ($mut->allele_mut) {
$w->startTag("allele_mut");
$w->characters($mut->allele_mut->seq) if $mut->allele_mut->seq;
$w->endTag;
}
if ($mut->region) {
if($mut->region_value and $mut->region_dist) {
$w->startTag("region",
"value" => $mut->region_value,
"dist" => $mut->region_dist
);
}
elsif($mut->region_value) {
$w->startTag("region",
"value" => $mut->region_value
);
}
elsif($mut->region_dist) {
$w->startTag("region",
"dist" => $mut->region_dist
);
} else {
$w->startTag("region");
}
$w->characters($mut->region );
$w->endTag;
}
$w->endTag; }
}
}
}
}
$w->endTag;
$w->end;
$self->_print($str);
$output = undef;
return 1;
}
1; } |