| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SeqIO::asciitree | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Chris Mungall <cjm@fruitfly.org> | ||
| 7 | # | ||
| 8 | # Copyright Chris Mungall | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::SeqIO::asciitree - asciitree sequence input/output stream | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # It is probably best not to use this object directly, but | ||
| 21 | # rather go through the SeqIO handler system. Go: | ||
| 22 | |||
| 23 | $instream = FAST::Bio::SeqIO->new(-file => $filename, | ||
| 24 | -format => 'chadoxml'); | ||
| 25 | $outstream = FAST::Bio::SeqIO->new(-file => $filename, | ||
| 26 | -format => 'asciitree'); | ||
| 27 | |||
| 28 | while ( my $seq = $instream->next_seq() ) { | ||
| 29 | $outstream->write_seq(); | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is a WRITE-ONLY SeqIO module. It writes a FAST::Bio::SeqI object | ||
| 36 | containing nested SeqFeature objects in such a way that the SeqFeature | ||
| 37 | containment hierarchy is visible as a tree structure | ||
| 38 | |||
| 39 | |||
| 40 | =head1 FEEDBACK | ||
| 41 | |||
| 42 | =head2 Mailing Lists | ||
| 43 | |||
| 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. | ||
| 47 | |||
| 48 | bioperl-l@bioperl.org - General discussion | ||
| 49 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 50 | |||
| 51 | =head2 Support | ||
| 52 | |||
| 53 | Please direct usage questions or support issues to the mailing list: | ||
| 54 | |||
| 55 | I<bioperl-l@bioperl.org> | ||
| 56 | |||
| 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. | ||
| 61 | |||
| 62 | =head2 Reporting Bugs | ||
| 63 | |||
| 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: | ||
| 66 | |||
| 67 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 68 | |||
| 69 | =head1 AUTHOR - Chris Mungall | ||
| 70 | |||
| 71 | Email cjm@fruitfly.org | ||
| 72 | |||
| 73 | =head1 APPENDIX | ||
| 74 | |||
| 75 | The rest of the documentation details each of the object | ||
| 76 | methods. Internal methods are usually preceded with a _ | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | # Let the code begin... | ||
| 81 | |||
| 82 | package FAST::Bio::SeqIO::asciitree; | ||
| 83 | use strict; | ||
| 84 | |||
| 85 | |||
| 86 | use base qw(FAST::Bio::SeqIO); | ||
| 87 | |||
| 88 | sub _initialize { | ||
| 89 | my($self,@args) = @_; | ||
| 90 | |||
| 91 | $self->SUPER::_initialize(@args); | ||
| 92 | # hash for functions for decoding keys. | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 show_detail | ||
| 96 | |||
| 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) | ||
| 103 | |||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub show_detail{ | ||
| 108 | my $self = shift; | ||
| 109 | |||
| 110 | return $self->{'show_detail'} = shift if @_; | ||
| 111 | return $self->{'show_detail'}; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 next_seq | ||
| 116 | |||
| 117 | Title : next_seq | ||
| 118 | Usage : $seq = $stream->next_seq() | ||
| 119 | Function: returns the next sequence in the stream | ||
| 120 | Returns : FAST::Bio::Seq object | ||
| 121 | Args : | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub next_seq { | ||
| 126 | my ($self,@args) = @_; | ||
| 127 | $self->throw("This is a WRITE-ONLY adapter"); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | =head2 write_seq | ||
| 132 | |||
| 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 FAST::Bio::SeqI objects | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub write_seq { | ||
| 142 | my ($self,@seqs) = @_; | ||
| 143 | |||
| 144 | foreach my $seq ( @seqs ) { | ||
| 145 | $self->throw("Attempting to write with no seq!") unless defined $seq; | ||
| 146 | |||
| 147 | if( ! ref $seq || ! $seq->isa('FAST::Bio::SeqI') ) { | ||
| 148 | $self->warn(" $seq is not a SeqI compliant module. Attempting to dump, but may fail!"); | ||
| 149 | } | ||
| 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); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 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'); | ||
| 165 | } | ||
| 166 | if ($sf->has_tag('product')) { | ||
| 167 | ($label) = $sf->get_tag_values('product'); | ||
| 168 | } | ||
| 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); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | my $detail = join("\n", @lines); | ||
| 191 | |||
| 192 | my @sub_sfs = $sf->get_SeqFeatures; | ||
| 193 | my $locstr = ''; | ||
| 194 | if (!@sub_sfs) { | ||
| 195 | $locstr = $self->_locstr($sf); | ||
| 196 | } | ||
| 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"); | ||
| 204 | } | ||
| 205 | $self->write_indented_sf($indent+1, @sub_sfs); | ||
| 206 | } | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 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; | ||
| 217 | |||
| 218 | my $splitlocstr = ''; | ||
| 219 | if ($sf->isa("FAST::Bio::SeqFeatureI")) { | ||
| 220 | my @locs = ($sf->location); | ||
| 221 | if ($sf->location->isa("FAST::Bio::Location::SplitLocationI")) { | ||
| 222 | @locs = $sf->location->each_Location; | ||
| 223 | $splitlocstr = "; SPLIT: ".join(" ", | ||
| 224 | map {$self->_locstr($_)} @locs); | ||
| 225 | |||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return | ||
| 230 | sprintf("%d..%d[%s] $splitlocstr", $sf->start, $sf->end, $ss); | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; |