FAST/Bio/AlignIO/msf.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 152 SCAR 50.5
CC
12
Cov
0%
write_aln
56.0
next_aln
42.0
BEGIN
2.0
CRAP 156.0
1 #
2 # BioPerl module for FAST::Bio::AlignIO::msf
3 # based on the FAST::Bio::SeqIO::msf module
4 # by Ewan Birney <birney@ebi.ac.uk>
5 # and Lincoln Stein <lstein@cshl.org>
6 #
7 # and the SimpleAlign.pm module of Ewan Birney
8 #
9 # Copyright Peter Schattner
10 #
11 # You may distribute this module under the same terms as perl itself
12 # _history
13 # September 5, 2000
14 # POD documentation - main docs before the code
15  
16 =head1 NAME
17  
18 FAST::Bio::AlignIO::msf - msf sequence input/output stream
19  
20 =head1 SYNOPSIS
21  
22 Do not use this module directly. Use it via the L<FAST::Bio::AlignIO> class.
23  
24 =head1 DESCRIPTION
25  
26 This object can transform L<FAST::Bio::Align::AlignI> objects to and from msf
27 flat file databases.
28  
29 =head1 FEEDBACK
30  
31 =head2 Support
32  
33 Please direct usage questions or support issues to the mailing list:
34  
35 I<bioperl-l@bioperl.org>
36  
37 rather than to the module maintainer directly. Many experienced and
38 reponsive experts will be able look at the problem and quickly
39 address it. Please include a thorough description of the problem
40 with code and data examples if at all possible.
41  
42 =head2 Reporting Bugs
43  
44 Report bugs to the Bioperl bug tracking system to help us keep track
45 the bugs and their resolution. Bug reports can be submitted via the
46 web:
47  
48 https://redmine.open-bio.org/projects/bioperl/
49  
50 =head1 AUTHORS - Peter Schattner
51  
52 Email: schattner@alum.mit.edu
53  
54  
55 =head1 APPENDIX
56  
57 The rest of the documentation details each of the object
58 methods. Internal methods are usually preceded with a _
59  
60 =cut
61  
62 # Let the code begin...
63  
64 package FAST::Bio::AlignIO::msf;
65 use vars qw(%valid_type);
66 use strict;
67  
68 use FAST::Bio::SeqIO::gcg; # for GCG_checksum()
69 use FAST::Bio::SimpleAlign;
70  
71 use base qw(FAST::Bio::AlignIO);
72  
73 BEGIN {
74 %valid_type = qw( dna N rna N protein P );
75 }
76  
77 =head2 next_aln
78  
79 Title : next_aln
80 Usage : $aln = $stream->next_aln()
81 Function: returns the next alignment in the stream. Tries to read *all* MSF
82 It reads all non whitespace characters in the alignment
83 area. For MSFs with weird gaps (eg ~~~) map them by using
84 $aln->map_chars('~','-')
85 Returns : FAST::Bio::Align::AlignI object
86 Args : NONE
87  
88 =cut
89  
90 sub next_aln {
91 my $self = shift;
92 my $entry;
93 my (%hash,$name,$str,@names,$seqname,$start,$end,$count,$seq);
94  
95 my $aln = FAST::Bio::SimpleAlign->new(-source => 'gcg' );
96  
97 while( $entry = $self->_readline) {
98 $entry =~ m{//} && last; # move to alignment section
99 $entry =~ /Name:\s+(\S+)/ && do { $name = $1;
100 $hash{$name} = ""; # blank line
101 push(@names,$name); # we need it ordered!
102 };
103 # otherwise - skip
104 }
105  
106 # alignment section
107  
108 while( $entry = $self->_readline) {
109 next if ( $entry =~ /^\s+(\d+)/ ) ;
110 $entry =~ /^\s*(\S+)\s+(.*)$/ && do {
111 $name = $1;
112 $str = $2;
113 if( ! exists $hash{$name} ) {
114 $self->throw("$name exists as an alignment line but not in the header. Not confident of what is going on!");
115 }
116 $str =~ s/\s//g;
117 $str =~ s/~/-/g;
118 $hash{$name} .= $str;
119 };
120 }
121  
122 # return 0 if scalar @names < 1;
123 if (scalar(@names) < 1) {
124 undef $aln;
125 return $aln;
126 }
127  
128 # now got this as a name - sequence hash. Let's make some sequences!
129  
130 for $name ( @names ) {
131 if( $name =~ m{(\S+)/(\d+)-(\d+)} ) {
132 $seqname = $1;
133 $start = $2;
134 $end = $3;
135 } else {
136 $seqname = $name;
137 $start = 1;
138 $str = $hash{$name};
139 $str =~ s/[^0-9A-Za-z$FAST::Bio::LocatableSeq::OTHER_SYMBOLS]//g;
140  
141 $end = length($str);
142 }
143  
144 $seq = FAST::Bio::LocatableSeq->new('-seq' => $hash{$name},
145 '-display_id' => $seqname,
146 '-start' => $start,
147 '-end' => $end,
148 '-alphabet' => $self->alphabet,
149 );
150 $aln->add_seq($seq);
151  
152 # If $end <= 0, we have either reached the end of
153 # file in <> or we have encountered some other error
154  
155 }
156 return $aln if $aln->num_sequences;
157 return;
158 }
159  
160  
161 =head2 write_aln
162  
163 Title : write_aln
164 Usage : $stream->write_aln(@aln)
165 Function: writes the $aln object into the stream in MSF format
166 Sequence type of the alignment is determined by the first sequence.
167 Returns : 1 for success and 0 for error
168 Args : FAST::Bio::Align::AlignI object
169  
170  
171 =cut
172  
173 sub write_aln {
174 my ($self,@aln) = @_;
175 my $msftag;
176 my $type;
177 my $count = 0;
178 my $maxname;
179 my ($length,$date,$name,$seq,$miss,$pad,%hash,@arr,$tempcount,$index);
180 foreach my $aln (@aln) {
181 if( ! $aln || ! $aln->isa('FAST::Bio::Align::AlignI') ) {
182 $self->warn("Must provide a FAST::Bio::Align::AlignI object when calling write_aln");
183 next;
184 }
185 $date = localtime(time);
186 $msftag = "MSF";
187 $type = $valid_type{$aln->get_seq_by_pos(1)->alphabet};
188 $maxname = $aln->maxdisplayname_length();
189 $length = $aln->length();
190 $name = $aln->id();
191 if( !defined $name ) {
192 $name = "Align";
193 }
194  
195 $self->_print (sprintf("\n%s MSF: %d Type: %s %s Check: 00 ..\n\n",
196 $name, $aln->num_sequences, $type, $date));
197  
198 my $seqCountFormat = "%".($maxname > 20 ? $maxname + 2: 22)."s%-27d%27d\n";
199 my $seqNameFormat = "%-".($maxname > 20 ? $maxname : 20)."s ";
200
201 foreach $seq ( $aln->each_seq() ) {
202 $name = $aln->displayname($seq->get_nse());
203 $miss = $maxname - length ($name);
204 $miss += 2;
205 $pad = " " x $miss;
206  
207 $self->_print (sprintf(" Name: %s%sLen: %d Check: %d Weight: 1.00\n",$name,$pad,length $seq->seq(), FAST::Bio::SeqIO::gcg->GCG_checksum($seq)));
208  
209 $hash{$name} = $seq->seq();
210 push(@arr,$name);
211 }
212 # ok - heavy handed, but there you go.
213 #
214 $self->_print ("\n//\n\n\n");
215  
216 while( $count < $length ) {
217 # there is another block to go!
218 $self->_print (sprintf($seqCountFormat,' ',$count+1,$count+50));
219 foreach $name ( @arr ) {
220 $self->_print (sprintf($seqNameFormat,$name));
221  
222 $tempcount = $count;
223 $index = 0;
224 while( ($tempcount + 10 < $length) && ($index < 5) ) {
225  
226 $self->_print (sprintf("%s ",substr($hash{$name},
227 $tempcount,10)));
228  
229 $tempcount += 10;
230 $index++;
231 } #
232 # ok, could be the very last guy ;)
233 #
234 if( $index < 5) {
235 # space to print!
236 #
237 $self->_print (sprintf("%s ",substr($hash{$name},$tempcount)));
238 $tempcount += 10;
239 }
240 $self->_print ("\n");
241 }
242 $self->_print ("\n\n");
243 $count = $tempcount;
244 }
245 }
246 $self->flush if $self->_flush_on_write && defined $self->_fh;
247 return 1;
248 }
249  
250 1;