From 7fb67928e4eb16440945d0fcb06bdac9e5985ed6 Mon Sep 17 00:00:00 2001 From: Chris Fields Date: Sun, 22 Sep 2019 17:01:58 -0500 Subject: [PATCH] fix text wrapping with #321 --- lib/Bio/SeqIO/genbank.pm | 15 ++++++++------- t/SeqIO/genbank.t | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/Bio/SeqIO/genbank.pm b/lib/Bio/SeqIO/genbank.pm index 94663102e..dc56239eb 100644 --- a/lib/Bio/SeqIO/genbank.pm +++ b/lib/Bio/SeqIO/genbank.pm @@ -1303,18 +1303,19 @@ sub _print_GenBank_FTHelper { # If this breaks *NCBI* examples please file an issue with # example files - elsif ( not $FTQUAL_NO_QUOTE{$tag} - - ) { - my ($pat) = ( $value =~ /\s/ ? '\s|$' : '.|$' ); + elsif ( not $FTQUAL_NO_QUOTE{$tag} ) { + my ($pat) = ( $value =~ /\s/ ? '\s|$' : + '.|$' ); $self->_write_line_GenBank_regex ( " " x 21, " " x 21, "/$tag=\"$value\"", $pat, 80 ); } else { + my ($pat) = ( $value =~ /[,]/ ? '[,]|$' : + '.|$' ); $self->_write_line_GenBank_regex ( " " x 21, " " x 21, - "/$tag=$value", "\.\|\$", 80 ); + "/$tag=$value", $pat, 80 ); } } } @@ -1841,14 +1842,14 @@ sub _write_line_GenBank { sub _write_line_GenBank_regex { my ($self, $pre1, $pre2, $line, $regex, $length) = @_; - #print STDOUT "Going to print with $line!\n"; + # print STDOUT "Going to print with $line!\n"; $length or $self->throw("Miscalled write_line_GenBank without length. Programming error!"); my $subl = $length - (length $pre1) - 2; my @lines = (); - CHUNK: + CHUNK: while ($line) { foreach my $pat ($regex, '[,;\.\/-]\s|'.$regex, '[,;\.\/-]|'.$regex) { if ($line =~ m/^(.{0,$subl})($pat)(.*)/ ) { diff --git a/t/SeqIO/genbank.t b/t/SeqIO/genbank.t index 29a99cd6e..8f0af8cb7 100644 --- a/t/SeqIO/genbank.t +++ b/t/SeqIO/genbank.t @@ -4,7 +4,7 @@ use strict; BEGIN { use Bio::Root::Test; - test_begin(-tests => 304); + test_begin(-tests => 306); use_ok('Bio::SeqIO::genbank'); } @@ -718,3 +718,35 @@ like( $comment->as_text, qr/^Subtype :: H1N1/m, "Got correct Structured Comment"); like( $comment->as_text, qr/^##GISAID_EpiFlu\(TM\)Data-END##/m, "Got correct Structured Comment" ); + +# Issue #321 (github) + +{ + # Create blank sequence + my $seq=Bio::Seq->new(-seq => 'N' x 1200000, + -id => 'abacab'); + my $feature= Bio::SeqFeature::Generic->new(-primary=>'tRNA', + -start=>1123552, -end=>1123554); + my $text='(pos:complement(1123552..1123554),aa:Leu,seq:caa)'; + $feature->add_tag_value(anticodon => $text); + $seq->add_SeqFeature($feature); + + # Write genbank + my $string; + open my $str_fh, '>', \$string or skip("Could not write string, skipping", 2); + my $out = Bio::SeqIO->new(-format => 'genbank', + -fh => $str_fh, + -verbose => -1); + $out->write_seq($seq); + + like($string, qr/,$/m, 'unquoted labels wrap at comma'); + + # Read genbank + my $in = Bio::SeqIO->new(-format => 'genbank', + -string => $string, + -verbose => -1); + my $genbank = $in->next_seq; + my ($read_feature) = $genbank->get_SeqFeatures; + my ($read_text) = $read_feature->get_tag_values('anticodon'); + is($read_text, $text, 'Label is the same'); +} -- 2.11.4.GIT