t/AlignIO/AlignIO.t: fix number of tests in plan (fixup c523e6bed866)
[bioperl-live.git] / Bio / SeqIO / asciitree.pm
blobf3de06ac5d156f713d67f7b41ce0f55506b23fdd
2 # BioPerl module for Bio::SeqIO::asciitree
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Chris Mungall <cjm@fruitfly.org>
8 # Copyright Chris Mungall
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::SeqIO::asciitree - asciitree sequence input/output stream
18 =head1 SYNOPSIS
20 # It is probably best not to use this object directly, but
21 # rather go through the SeqIO handler system. Go:
23 $instream = Bio::SeqIO->new(-file => $filename,
24 -format => 'chadoxml');
25 $outstream = Bio::SeqIO->new(-file => $filename,
26 -format => 'asciitree');
28 while ( my $seq = $instream->next_seq() ) {
29 $outstream->write_seq();
33 =head1 DESCRIPTION
35 This is a WRITE-ONLY SeqIO module. It writes a Bio::SeqI object
36 containing nested SeqFeature objects in such a way that the SeqFeature
37 containment hierarchy is visible as a tree structure
40 =head1 FEEDBACK
42 =head2 Mailing Lists
44 User feedback is an integral part of the evolution of this and other
45 Bioperl modules. Send your comments and suggestions preferably to one
46 of the Bioperl mailing lists. Your participation is much appreciated.
48 bioperl-l@bioperl.org - General discussion
49 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51 =head2 Support
53 Please direct usage questions or support issues to the mailing list:
55 I<bioperl-l@bioperl.org>
57 rather than to the module maintainer directly. Many experienced and
58 reponsive experts will be able look at the problem and quickly
59 address it. Please include a thorough description of the problem
60 with code and data examples if at all possible.
62 =head2 Reporting Bugs
64 Report bugs to the Bioperl bug tracking system to help us keep track
65 the bugs and their resolution. Bug reports can be submitted via the web:
67 https://github.com/bioperl/bioperl-live/issues
69 =head1 AUTHOR - Chris Mungall
71 Email cjm@fruitfly.org
73 =head1 APPENDIX
75 The rest of the documentation details each of the object
76 methods. Internal methods are usually preceded with a _
78 =cut
80 # Let the code begin...
82 package Bio::SeqIO::asciitree;
83 use strict;
86 use base qw(Bio::SeqIO);
88 sub _initialize {
89 my($self,@args) = @_;
91 $self->SUPER::_initialize(@args);
92 # hash for functions for decoding keys.
95 =head2 show_detail
97 Title : show_detail
98 Usage : $obj->show_detail($newval)
99 Function:
100 Example :
101 Returns : value of show_detail (a scalar)
102 Args : on set, new value (a scalar or undef, optional)
105 =cut
107 sub show_detail{
108 my $self = shift;
110 return $self->{'show_detail'} = shift if @_;
111 return $self->{'show_detail'};
115 =head2 next_seq
117 Title : next_seq
118 Usage : $seq = $stream->next_seq()
119 Function: returns the next sequence in the stream
120 Returns : Bio::Seq object
121 Args :
123 =cut
125 sub next_seq {
126 my ($self,@args) = @_;
127 $self->throw("This is a WRITE-ONLY adapter");
131 =head2 write_seq
133 Title : write_seq
134 Usage : $stream->write_seq($seq)
135 Function: writes the $seq object (must be seq) to the stream
136 Returns : 1 for success and 0 for error
137 Args : array of 1 to n Bio::SeqI objects
139 =cut
141 sub write_seq {
142 my ($self,@seqs) = @_;
144 foreach my $seq ( @seqs ) {
145 $self->throw("Attempting to write with no seq!") unless defined $seq;
147 if( ! ref $seq || ! $seq->isa('Bio::SeqI') ) {
148 $self->warn(" $seq is not a SeqI compliant module. Attempting to dump, but may fail!");
150 $self->_print("Seq: ".$seq->accession_number);
151 $self->_print("\n");
152 my @top_sfs = $seq->get_SeqFeatures;
153 $self->write_indented_sf(1, @top_sfs);
157 sub write_indented_sf {
158 my $self = shift;
159 my $indent = shift;
160 my @sfs = @_;
161 foreach my $sf (@sfs) {
162 my $label = '';
163 if ($sf->has_tag('standard_name')) {
164 ($label) = $sf->get_tag_values('standard_name');
166 if ($sf->has_tag('product')) {
167 ($label) = $sf->get_tag_values('product');
169 my $COLS = 60;
170 my $tab = ' ' x 10;
171 my @lines = ();
172 if ($self->show_detail) {
173 my @tags = $sf->all_tags;
174 foreach my $tag (@tags) {
175 my @vals = $sf->get_tag_values($tag);
176 foreach my $val (@vals) {
177 $val = "\"$val\"";
178 push(@lines,
179 "$tab/$tag=");
180 while (my $cut =
181 substr($val, 0, $COLS - length($lines[-1]), '')) {
182 $lines[-1] .= "$cut";
183 if ($val) {
184 push(@lines, $tab);
190 my $detail = join("\n", @lines);
192 my @sub_sfs = $sf->get_SeqFeatures;
193 my $locstr = '';
194 if (!@sub_sfs) {
195 $locstr = $self->_locstr($sf);
197 my $col1 = sprintf("%s%s $label",
198 ' ' x $indent, $sf->primary_tag);
199 my $line = sprintf("%-50s %s\n",
200 substr($col1, 0, 50), $locstr);
201 $self->_print($line);
202 if ($detail) {
203 $self->_print($detail."\n");
205 $self->write_indented_sf($indent+1, @sub_sfs);
207 return;
210 sub _locstr {
211 my $self = shift;
212 my $sf = shift;
213 my $strand = $sf->strand || 0;
214 my $ss = '.';
215 $ss = '+' if $strand > 0;
216 $ss = '-' if $strand < 0;
218 my $splitlocstr = '';
219 if ($sf->isa("Bio::SeqFeatureI")) {
220 my @locs = ($sf->location);
221 if ($sf->location->isa("Bio::Location::SplitLocationI")) {
222 @locs = $sf->location->each_Location;
223 $splitlocstr = "; SPLIT: ".join(" ",
224 map {$self->_locstr($_)} @locs);
229 return
230 sprintf("%d..%d[%s] $splitlocstr", $sf->start, $sf->end, $ss);