Bio/SeqIO/msout.pm

filter: Statementstmt 88.1% 201 / 228 Branchbran 72.3% 55 / 76 Conditioncond 57.1% 12 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 96.6% 29 / 30 Podpod 86.3% 19 / 22 totaltotal 82.3% 321 / 390 CC 55 SCAR 19.2
CC
55
Cov
82%
get_next_seq
17.2
_load_run_info
11.4
_get_next_clean_hap
10.6
CRAP 6.8
« Bio/Ontology/Ontology.pm ↑ Summary Bio/DB/GFF/Util/Binning.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 Bio::SeqIO::msout - input stream for output by Hudson's ms
6  
7 =head1 SYNOPSIS
8  
9 Do not use this module directly. Use it via the Bio::SeqIO class.
10  
11 =head1 DESCRIPTION
12  
13 ms ( Hudson, R. R. (2002) Generating samples under a Wright-Fisher neutral
14 model. Bioinformatics 18:337-8 ) can be found at
15 http://home.uchicago.edu/~rhudson1/source/mksamples.html.
16  
17 Currently, this object can be used to read output from ms into seq objects.
18 However, because bioperl has no support for haplotypes created using an infinite
19 sites model (where '1' identifies a derived allele and '0' identifies an
20 ancestral allele), the sequences returned by msout are coded using A, T, C and
21 G. To decode the bases, use the sequence conversion table (a hash) returned by
22 get_base_conversion_table(). In the table, 4 and 5 are used when the ancestry is
23 unclear. This should not ever happen when creating files with ms, but it will be
24 used when creating msOUT files from a collection of seq objects ( To be added
25 later ). Alternatively, use get_next_hap() to get a string with 1's and 0's
26 instead of a seq object.
27  
28 =head2 Mapping to Finite Sites
29  
30 This object can now also be used to map haplotypes created using an infinite sites
31 model to sequences of arbitrary finite length. See set_n_sites() for more detail.
32 Thanks to Filipe G. Vieira <fgvieira@berkeley.edu> for the idea and code.
33  
34 =head1 FEEDBACK
35  
36 =head2 Mailing Lists
37  
38 User feedback is an integral part of the evolution of this and other
39 Bioperl modules. Send your comments and suggestions preferably to the
40 Bioperl mailing list. Your participation is much appreciated.
41  
42 bioperl-l@bioperl.org - General discussion
43 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
44  
45 =head2 Reporting Bugs
46  
47 Report bugs to the Bioperl bug tracking system to help us keep track
48 of the bugs and their resolution. Bug reports can be submitted via the
49 web:
50  
51 https://github.com/bioperl/bioperl-live/issues
52  
53 =head1 AUTHOR - Warren Kretzschmar
54  
55 This module was written by Warren Kretzschmar
56  
57 email: wkretzsch@gmail.com
58  
59 This module grew out of a parser written by Aida Andres.
60  
61 =head1 COPYRIGHT
62  
63 =head2 Public Domain Notice
64  
65 This software/database is ``United States Government Work'' under the
66 terms of the United States Copyright Act. It was written as part of
67 the authors' official duties for the United States Government and thus
68 cannot be copyrighted. This software/database is freely available to
69 the public for use without a copyright notice. Restrictions cannot
70 be placed on its present or future use.
71  
72 Although all reasonable efforts have been taken to ensure the accuracy
73 and reliability of the software and data, the National Human Genome
74 Research Institute (NHGRI) and the U.S. Government does not and cannot
75 warrant the performance or results that may be obtained by using this
76 software or data. NHGRI and the U.S. Government disclaims all
77 warranties as to performance, merchantability or fitness for any
78 particular purpose.
79  
80 =head1 METHODS
81  
82 =cut
83  
84 package Bio::SeqIO::msout;
85 2 $Bio::SeqIO::msout::VERSION = '1.7.8';
86 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 our $API_VERSION = qv('1.1.8');
88  
89 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use base qw(Bio::SeqIO); # This ISA Bio::SeqIO object
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 =head2 Methods for Internal Use
94  
95 =head3 _initialize
96  
97 Title : _initialize
98 Usage : $stream = Bio::SeqIO::msOUT->new($infile)
99 Function: extracts basic information about the file.
100 Returns : Bio::SeqIO object
101 Args : no_og, gunzip, gzip, n_sites
102 Details :
103 - include 'no_og' flag if the last population of an msout file contains
104 only one haplotype and you don't want the last haplotype to be
105 treated as the outgroup ( suggested when reading data created by ms ).
106 - including 'n_sites' (positive integer) causes all output haplotypes to be
107 mapped to a sequence of length 'n_sites'. See set_n_sites() for more details.
108  
109 =cut
110  
111 5 ▶sub _initialize {
Subroutine _initialize: called CC=4 SCAR=14.0
112 5 my ( $self, @args ) = @_;
113 5 $self->SUPER::_initialize(@args);
114  
115 5 ▶ unless ( defined $self->sequence_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->sequence_factory) 5 0 5
116 5 $self->sequence_factory( Bio::Seq::SeqFactory->new() );
117 }
118 5 my ($no_og) = $self->_rearrange( [qw(NO_OG)], @args );
119 5 my ($n_sites) = $self->_rearrange( [qw(N_SITES)], @args );
120  
121 5 my %initial_values = (
122 RUNS => undef,
123 N_SITES => undef,
124 SEGSITES => undef,
125 SEEDS => [],
126 MS_INFO_LINE => undef,
127 TOT_RUN_HAPS => undef,
128 POPS => [],
129 NEXT_RUN_NUM => undef, # What run is the next hap from? undef = EOF
130 LAST_READ_HAP_NUM => undef, # What did we just read from
131 LAST_HAPS_RUN_NUM => undef,
132 LAST_READ_POSITIONS => [],
133 LAST_READ_SEGSITES => undef,
134 BUFFER_HAP => undef,
135 NO_OUTGROUP => $no_og,
136 BASE_CONVERSION_TABLE_HASH_REF => {
137 'A' => 0,
138 'T' => 1,
139 'C' => 4,
140 'G' => 5,
141 },
142 );
143 5 foreach my $key ( keys %initial_values ) {
144 75 $self->{$key} = $initial_values{$key};
145 }
146 5 $self->set_n_sites($n_sites);
147  
148 # If the filehandle is defined open it and read a few lines
149 5 ▶ if ( ref( $self->{_filehandle} ) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_filehandle'} eq 'GLOB') { } 5 0 5
150 5 $self->_read_start();
151 5 return $self;
152 }
153  
154 # Otherwise throw a warning
155 else {
156 0 $self->throw(
157 "No filehandle defined. Please define a file handle through -file when calling msout with Bio::SeqIO"
158 );
159 }
160 }
161  
162 =head3 _read_start
163  
164 Title : _read_start
165 Usage : $stream->_read_start()
166 Function: reads from the filehandle $stream->{_filehandle} all information up to the first haplotype (sequence). Closes the filehandle if all lines have been read.
167 Returns : void
168 Args : none
169  
170 =cut
171  
172 5 ▶sub _read_start {
Subroutine _read_start: called CC=8 SCAR=20.9
173 5 my $self = shift;
174  
175 5 my $fh_IN = $self->{_filehandle};
176  
177 # get the first five lines and parse for important info
178 5 my ( $ms_info_line, $seeds ) = $self->_get_next_clean_hap( $fh_IN, 2, 1 );
179  
180 5 my @ms_info_line = split( /\s+/, $ms_info_line );
181  
182 5 my ( $tot_pops, @pop_haplos );
183  
184 # Parsing the ms header line
185 5 shift @ms_info_line;
186 5 my $tot_run_haps = shift @ms_info_line;
187 5 my $runs = shift @ms_info_line;
188 5 my $segsites;
189  
190 5 foreach my $word ( 0 .. $#ms_info_line ) {
191 21 ▶ if ( $ms_info_line[$word] eq '-I' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ms_info_line[$word] eq '-I') { } 3 18 21
elsif ($ms_info_line[$word] eq '-s') { } 3 15 18
192 3 $tot_pops = $ms_info_line[ $word + 1 ];
193 3 for my $pop_num ( 1 .. $tot_pops ) {
194 9 push @pop_haplos, $ms_info_line[ $word + 1 + $pop_num ];
195 }
196  
197 # if @pop_haplos contains a non-digit, then there is an error in the msinfo line.
198 3 ▶ if ( !defined $pop_haplos[-1] || $pop_haplos[-1] =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pop_haplos[-1] or $pop_haplos[-1] =~ /\D/) 0 3 3
Condition33%1/3
not defined $pop_haplos[-1] or $pop_haplos[-1] =~ /\D/
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $pop_haplos[-1] or $pop_haplos[-1] =~ /\D/
not defined $pop_haplos[-1] $pop_haplos[-1] =~ /\D/
Truth table33%1/3
not(A) or B
A= not defined $pop_haplos[-1]B= $pop_haplos[-1] =~ /\D/
ABresult
1X1
011
000
199 0 $self->throw(
200 "Incorrect number of populations in the ms info line (after the -I specifier)"
201 );
202 }
203 }
204 elsif ( $ms_info_line[$word] eq '-s' ) {
205 3 $segsites = $ms_info_line[ $word + 1 ];
206 }
207 15 else { next; }
208 }
209  
210 5 ▶ unless (@pop_haplos) { @pop_haplos = ($tot_run_haps) }
Branch: 1 branch
BranchTrueFalseTotal
unless (@pop_haplos) 2 3 5
211  
212 5 my @seeds = split( /\s+/, $seeds );
213  
214 # Save ms info data
215 5 $self->{RUNS} = $runs;
216 5 $self->{SEGSITES} = $segsites;
217 5 $self->{SEEDS} = \@seeds;
218 5 $self->{MS_INFO_LINE} = $ms_info_line;
219 5 $self->{TOT_RUN_HAPS} = $tot_run_haps;
220 5 $self->{POPS} = [@pop_haplos];
221  
222 5 return;
223 }
224  
225 =head2 Methods to Access Data
226  
227 =head3 get_segsites
228  
229 Title : get_segsites
230 Usage : $segsites = $stream->get_segsites()
231 Function: returns the number of segsites in the msOUT file (according to the msOUT header line's -s option), or the current run's segsites if -s was not specified in the command line (in this case the number of segsites varies from run to run).
232 Returns : scalar
233 Args : NONE
234  
235 =cut
236  
237 4 ▶sub get_segsites {
Subroutine get_segsites: called CC=2 SCAR=6.9
Pod documented
238 4 my $self = shift;
239 4 ▶ if ( defined $self->{SEGSITES} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'SEGSITES'}) { } 2 2 4
240 2 return $self->{SEGSITES};
241 }
242 else {
243 2 return $self->get_current_run_segsites;
244 }
245 }
246  
247 =head3 get_current_run_segsites
248  
249 Title : get_current_run_segsites
250 Usage : $segsites = $stream->get_current_run_segsites()
251 Function: returns the number of segsites in the run of the last read
252 haplotype (sequence).
253 Returns : scalar
254 Args : NONE
255  
256 =cut
257  
258 68 ▶sub get_current_run_segsites {
Subroutine get_current_run_segsites: called CC=1 SCAR=0.0
Pod documented
259 68 my $self = shift;
260 68 return $self->{LAST_READ_SEGSITES};
261 }
262  
263 =head3 get_n_sites
264  
265 Title : get_n_sites
266 Usage : $n_sites = $stream->get_n_sites()
267 Function: Gets the number of total sites (variable or not) to be output.
268 Returns : scalar if n_sites option is defined at call time of new()
269 Args : NONE
270 Note :
271 WARNING: Final sequence length might not be equal to n_sites if n_sites is
272 too close to number of segregating sites in the msout file.
273  
274 =cut
275  
276 65 ▶sub get_n_sites {
Subroutine get_n_sites: called CC=1 SCAR=0.0
Pod documented
277 65 my ($self) = @_;
278  
279 65 return $self->{N_SITES};
280 }
281  
282 =head3 set_n_sites
283  
284 Title : set_n_sites
285 Usage : $n_sites = $stream->set_n_sites($value)
286 Function: Sets the number of total sites (variable or not) to be output.
287 Returns : 1 on success; throws an error if $value is not a positive integer or undef
288 Args : positive integer
289 Note :
290 WARNING: Final sequence length might not be equal to n_sites if it is
291 too close to number of segregating sites.
292 - n_sites needs to be at least as large as the number of segsites of
293 the next haplotype returned
294 - n_sites may also be set to undef, in which case haplotypes are returned
295 under the infinite sites model assumptions.
296  
297 =cut
298  
299 5 ▶sub set_n_sites {
Subroutine set_n_sites: called CC=4 SCAR=19.7
Pod documented
300 5 my ( $self, $value ) = @_;
301  
302 # make sure $value is a positive integer if it is defined
303 5 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 5 5
304 0 ▶ $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+$/ and $value > 0 0 0 0
Condition0%0/3
$value =~ /^\d+$/ and $value > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /^\d+$/ and $value > 0
$value =~ /^\d+$/ $value > 0
Truth table0%0/3
A and B
A= $value =~ /^\d+$/B= $value > 0
ABresult
0X0
100
111
305 "first argument needs to be a positive integer. argument supplied: $value"
306 ) unless ( $value =~ m/^\d+$/ && $value > 0 );
307 }
308 5 $self->{N_SITES} = $value;
309  
310 5 return 1;
311 }
312  
313 =head3 get_runs
314  
315 Title : get_runs
316 Usage : $runs = $stream->get_runs()
317 Function: returns the number of runs in the msOUT file (according to the
318 msinfo line)
319 Returns : scalar
320 Args : NONE
321  
322 =cut
323  
324 4 ▶sub get_runs {
Subroutine get_runs: called CC=1 SCAR=0.0
Pod documented
325 4 my $self = shift;
326 4 return $self->{RUNS};
327 }
328  
329 =head3 get_Seeds
330  
331 Title : get_Seeds
332 Usage : @seeds = $stream->get_Seeds()
333 Function: returns an array of the seeds used in the creation of the msOUT file.
334 Returns : array
335 Args : NONE
336 Details : In older versions, ms used three seeds. Newer versions of ms seem to
337 use only one (longer) seed. This function will return all the seeds
338 found.
339  
340 =cut
341  
342 4 ▶sub get_Seeds {
Subroutine get_Seeds: called CC=1 SCAR=0.0
Pod documented
343 4 my $self = shift;
344 4 return @{ $self->{SEEDS} };
345 }
346  
347 =head3 get_Positions
348  
349 Title : get_Positions
350 Usage : @positions = $stream->get_Positions()
351 Function: returns an array of the names of each segsite of the run of the last
352 read hap.
353 Returns : array
354 Args : NONE
355 Details : The Positions may or may not vary from run to run depending on the
356 options used with ms.
357  
358 =cut
359  
360 4 ▶sub get_Positions {
Subroutine get_Positions: called CC=1 SCAR=0.0
Pod documented
361 4 my $self = shift;
362 4 return @{ $self->{LAST_READ_POSITIONS} };
363 }
364  
365 =head3 get_tot_run_haps
366  
367 Title : get_tot_run_haps
368 Usage : $number_of_haps_per_run = $stream->get_tot_run_haps()
369 Function: returns the number of haplotypes (sequences) in each run of the msOUT
370 file ( according to the msinfo line ).
371 Returns : scalar >= 0
372 Args : NONE
373 Details : This number should not vary from run to run.
374  
375 =cut
376  
377 4 ▶sub get_tot_run_haps {
Subroutine get_tot_run_haps: called CC=1 SCAR=0.0
Pod documented
378 4 my $self = shift;
379 4 return $self->{TOT_RUN_HAPS};
380 }
381  
382 =head3 get_ms_info_line
383  
384 Title : get_ms_info_line
385 Usage : $ms_info_line = $stream->get_ms_info_line()
386 Function: returns the header line of the msOUT file.
387 Returns : scalar
388 Args : NONE
389  
390 =cut
391  
392 4 ▶sub get_ms_info_line {
Subroutine get_ms_info_line: called CC=1 SCAR=0.0
Pod documented
393 4 my $self = shift;
394 4 return $self->{MS_INFO_LINE};
395 }
396  
397 =head3 tot_haps
398  
399 Title : tot_haps
400 Usage : $number_of_haplotypes_in_file = $stream->tot_haps()
401 Function: returns the number of haplotypes (sequences) in the msOUT file.
402 Information gathered from msOUT header line.
403 Returns : scalar
404 Args : NONE
405  
406 =cut
407  
408 10 ▶sub get_tot_haps {
Subroutine get_tot_haps: called CC=1 SCAR=0.0
Pod undocumented
409 10 my $self = shift;
410 10 return ( $self->{TOT_RUN_HAPS} * $self->{RUNS} );
411 }
412  
413 =head3 get_Pops
414  
415 Title : get_Pops
416 Usage : @pops = $stream->pops()
417 Function: returns an array of population sizes (order taken from the -I flag in
418 the msOUT header line). This array will include the last hap even if
419 it looks like an outgroup.
420 Returns : array of scalars > 0
421 Args : NONE
422  
423 =cut
424  
425 47 ▶sub get_Pops {
Subroutine get_Pops: called CC=1 SCAR=0.0
Pod documented
426 47 my $self = shift;
427 47 return @{ $self->{POPS} };
428 }
429  
430 =head3 get_next_run_num
431  
432 Title : get_next_run_num
433 Usage : $next_run_number = $stream->next_run_num()
434 Function: returns the number of the ms run that the next haplotype (sequence)
435 will be taken from (starting at 1). Returns undef if the complete
436 file has been read.
437 Returns : scalar > 0 or undef
438 Args : NONE
439  
440 =cut
441  
442 83 ▶sub get_next_run_num {
Subroutine get_next_run_num: called CC=1 SCAR=0.0
Pod documented
443 83 my $self = shift;
444 83 return $self->{NEXT_RUN_NUM};
445 }
446  
447 =head3 get_last_haps_run_num
448  
449 Title : get_last_haps_run_num
450 Usage : $last_haps_run_number = $stream->get_last_haps_run_num()
451 Function: returns the number of the ms run that the last haplotype (sequence)
452 was taken from (starting at 1). Returns undef if no hap has been
453 read yet.
454 Returns : scalar > 0 or undef
455 Args : NONE
456  
457 =cut
458  
459 62 ▶sub get_last_haps_run_num {
Subroutine get_last_haps_run_num: called CC=1 SCAR=0.0
Pod documented
460 62 my $self = shift;
461 62 return $self->{LAST_HAPS_RUN_NUM};
462 }
463  
464 =head3 get_last_read_hap_num
465  
466 Title : get_last_read_hap_num
467 Usage : $last_read_hap_num = $stream->get_last_read_hap_num()
468 Function: returns the number (starting with 1) of the last haplotype read from
469 the ms file
470 Returns : scalar >= 0
471 Args : NONE
472 Details : 0 means that no haplotype has been read yet. Is reset to 0 every run.
473  
474 =cut
475  
476 161 ▶sub get_last_read_hap_num {
Subroutine get_last_read_hap_num: called CC=1 SCAR=0.0
Pod documented
477 161 my $self = shift;
478 161 return $self->{LAST_READ_HAP_NUM};
479 }
480  
481 =head3 outgroup
482  
483 Title : outgroup
484 Usage : $outgroup = $stream->outgroup()
485 Function: returns '1' if the msOUT stream has an outgroup. Returns '0'
486 otherwise.
487 Returns : '1' or '0'
488 Args : NONE
489 Details : This method will return '1' only if the last population in the msOUT
490 file contains only one haplotype. If the last population is not an
491 outgroup then create the msOUT object using 'no_og' as input flag.
492 Also, return 0, if the run has only one population.
493  
494 =cut
495  
496 3 ▶sub outgroup {
Subroutine outgroup: called CC=4 SCAR=14.0
Pod documented
497 3 my $self = shift;
498 3 my @pops = $self->get_Pops;
499 3 ▶ if ( $pops[$#pops] == 1 && !defined $self->{NO_OUTGROUP} && @pops > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pops[$#pops] == 1 and not defined $self->{'NO_OUTGROUP'} and @pops > 1) { } 2 1 3
Condition66%4/6
$pops[$#pops] == 1 and not defined $self->{"NO_OUTGROUP"} and @pops > 1
!ll&&!rl&&r
102
$pops[$#pops] == 1 and not defined $self->{"NO_OUTGROUP"}
!ll&&!rl&&r
102
MC/DC33%1/3
$pops[$#pops] == 1 and not defined $self->{"NO_OUTGROUP"} and @pops > 1
$pops[$#pops] == 1 not defined $self->{"NO_OUTGROUP"} @pops > 1
Truth table50%2/4
A and not(B) and C
A= $pops[$#pops] == 1B= not defined $self->{"NO_OUTGROUP"}C= @pops > 1
ABCresult
0XX0
10X0
1100
1111
500 2 return 1;
501 }
502 1 else { return 0; }
503 }
504  
505 =head3 get_next_haps_pop_num
506  
507 Title : get_next_haps_pop_num
508 Usage : ($next_haps_pop_num, $num_haps_left_in_pop) = $stream->get_next_haps_pop_num()
509 Function: First return value is the population number (starting with 1) the
510 next hap will come from. The second return value is the number of haps
511 left to read in the population from which the next hap will come.
512 Returns : (scalar > 0, scalar > 0)
513 Args : NONE
514  
515 =cut
516  
517 20 ▶sub get_next_haps_pop_num {
Subroutine get_next_haps_pop_num: called CC=3 SCAR=11.0
Pod documented
518 20 my $self = shift;
519 20 my $last_read_hap = $self->get_last_read_hap_num;
520 20 my @pops = $self->get_Pops;
521  
522 20 foreach my $pop_num ( 0 .. $#pops ) {
523 28 ▶ if ( $last_read_hap < $pops[$pop_num] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_read_hap < $pops[$pop_num]) { } 18 10 28
524 18 return ( $pop_num + 1, $pops[$pop_num] - $last_read_hap );
525 }
526 10 else { $last_read_hap -= $pops[$pop_num] }
527 }
528  
529 # In this case we're at the beginning of the next run
530 2 return ( 1, $pops[0] );
531 }
532  
533 =head3 get_next_seq
534  
535 Title : get_next_seq
536 Usage : $seq = $stream->get_next_seq()
537 Function: reads and returns the next sequence (haplotype) in the stream
538 Returns : Bio::Seq object or void if end of file
539 Args : NONE
540 Note : This function is included only to conform to convention. The
541 returned Bio::Seq object holds a halpotype in coded form. Use the hash
542 returned by get_base_conversion_table() to convert 'A', 'T', 'C', 'G'
543 back into 1,2,4 and 5. Use get_next_hap() to retrieve the halptoype as
544 a string of 1,2,4 and 5s instead.
545  
546 =cut
547  
548 70 ▶sub get_next_seq {
Subroutine get_next_seq: called CC=8 SCAR=28.4
Pod documented
549 70 my $self = shift;
550 70 my $seqstring = $self->get_next_hap;
551  
552 69 ▶ return unless ($seqstring);
Branch: 1 branch
BranchTrueFalseTotal
unless $seqstring 7 62 69
553  
554 # Used to create unique ID;
555 62 my $run = $self->get_last_haps_run_num;
556  
557 # Converting numbers to letters so that the haplotypes can be stored as a
558 # seq object
559 62 my $rh_base_conversion_table = $self->get_base_conversion_table;
560 62 foreach my $base ( keys %{$rh_base_conversion_table} ) {
561 248 $seqstring =~ s/($rh_base_conversion_table->{$base})/$base/g;
562 }
563  
564 # Fill in non-variable positions
565 62 my $segsites = $self->get_current_run_segsites;
566 62 my $n_sites = $self->get_n_sites;
567 62 ▶ if ( defined($n_sites) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n_sites) 0 62 62
568  
569 # make sure that n_sites is at least as large
570 # as segsites for each run. Throw an exception otherwise.
571 0 ▶ $self->throw( "n_sites:\t$n_sites"
Branch: 1 branch
BranchTrueFalseTotal
unless $segsites <= $n_sites 0 0 0
572 . "\nsegsites:\t$segsites"
573 . "\nrun:\t$run"
574 . "\nn_sites needs to be at least the number of segsites of every run"
575 ) unless $segsites <= $n_sites;
576  
577 0 my $seq_len = 0;
578 0 my @seq;
579 0 my @pos = $self->get_Positions;
580 0 ▶ for ( my $i = 0 ; $i <= $#pos ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#pos) 0 0 0
581 0 $pos[$i] *= $n_sites;
582 0 my $rpt = $pos[$i] - 1 - $seq_len;
583 0 ▶ $rpt = $rpt >= 1 ? $rpt : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rpt >= 1 ? : 0 0 0
584 0 push( @seq, "A" x ( $rpt ) );
585 0 $seq_len += length( $seq[-1] );
586 0 push( @seq, substr( $seqstring, $i, 1 ) );
587 0 $seq_len += length( $seq[-1] );
588 }
589 0 push( @seq, "A" x ( $n_sites - $seq_len ) );
590 0 $seqstring = join( "", @seq );
591 }
592  
593 62 my $last_read_hap = $self->get_last_read_hap_num;
594 62 my $id = 'Hap_' . $last_read_hap . '_Run_' . $run;
595 62 ▶ my $description =
Branch: 1 branch
BranchTrueFalseTotal
defined $n_sites ? : 0 62 62
596 "Segsites $segsites;"
597 . " Positions "
598 . ( defined $n_sites ? $n_sites : $segsites ) . ";"
599 . " Haplotype $last_read_hap;"
600 . " Run $run;";
601 62 my $seq = $self->sequence_factory->create(
602 -seq => $seqstring,
603 -id => $id,
604 -desc => $description,
605 -alphabet => q(dna),
606 -direct => 1,
607 );
608  
609 62 return $seq
610  
611 }
612  
613 =head3 next_seq
614  
615 Title : next_seq
616 Usage : $seq = $stream->next_seq()
617 Function: Alias to get_next_seq()
618 Returns : Bio::Seq object or void if end of file
619 Args : NONE
620 Note : This function is only included for convention. It calls get_next_seq().
621 See get_next_seq() for details.
622  
623 =cut
624  
625 0 ▶sub next_seq {
Subroutine next_seq: not called CC=1 SCAR=6.9
Pod documented
626 0 my $self = shift;
627 0 return $self->get_next_seq();
628 }
629  
630 =head3 get_next_hap
631  
632 Title : get_next_hap
633 Usage : $hap = $stream->next_hap()
634 Function: reads and returns the next sequence (haplotype) in the stream.
635 Returns undef if all sequences in stream have been read.
636 Returns : Haplotype string (e.g. '110110000101101045454000101'
637 Args : NONE
638 Note : Use get_next_seq() if you want the halpotype returned as a
639 Bio::Seq object.
640  
641 =cut
642  
643 75 ▶sub get_next_hap {
Subroutine get_next_hap: called CC=4 SCAR=13.9
Pod documented
644 75 my $self = shift;
645  
646 # Let's figure out how many haps to read from the input file so that
647 # we get back to the beginning of the next run.
648  
649 75 my $end_run = 0;
650 75 ▶ if ( $self->{TOT_RUN_HAPS} == $self->{LAST_READ_HAP_NUM} + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"TOT_RUN_HAPS"} == $self->{"LAST_READ_HAP_NUM"} + 1) 11 64 75
651 11 $end_run = 1;
652 }
653  
654 # Setting last_haps_run_num
655 75 $self->{LAST_HAPS_RUN_NUM} = $self->get_next_run_num;
656  
657 75 my $last_read_hap = $self->get_last_read_hap_num;
658 my ($seqstring) =
659 75 $self->_get_next_clean_hap( $self->{_filehandle}, 1, $end_run );
660 75 ▶ if ( !defined $seqstring && $last_read_hap < $self->get_tot_haps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $seqstring and $last_read_hap < $self->get_tot_haps) 1 74 75
Condition100%3/3
not defined $seqstring and $last_read_hap < $self->get_tot_haps
!ll&&!rl&&r
6681
MC/DC100%2/2
not defined $seqstring and $last_read_hap < $self->get_tot_haps
not defined $seqstring $last_read_hap < $self->get_tot_haps
Truth table100%3/3
not(A) and B
A= not defined $seqstringB= $last_read_hap < $self->get_tot_haps
ABresult
0X0
100
111
661 1 $self->throw(
662 "msout file has only $last_read_hap hap(s), which is less than indicated in msinfo line ( "
663 . $self->get_tot_haps
664 . " )" );
665 }
666  
667 74 return $seqstring;
668 }
669  
670 =head3 get_next_pop
671  
672 Title : get_next_pop
673 Usage : @seqs = $stream->next_pop()
674 Function: reads and returns all the remaining sequences (haplotypes) in the
675 population of the next sequence. Returns an empty list if no more
676 haps remain to be read in the stream
677 Returns : array of Bio::Seq objects
678 Args : NONE
679  
680 =cut
681  
682 10 ▶sub get_next_pop {
Subroutine get_next_pop: called CC=4 SCAR=13.9
Pod documented
683 10 my $self = shift;
684  
685 # Let's figure out how many haps to read from the input file so that
686 # we get back to the beginning of the next run.
687  
688 10 my @pops = $self->get_Pops;
689 10 my @seqs; # holds Bio::Seq objects to return
690  
691 # Determine number of the pop that the next hap will be taken from
692 10 my ( $next_haps_pop_num, $haps_to_pull ) = $self->get_next_haps_pop_num;
693  
694 # If $haps_to_pull == 0, then we need to pull the whole population
695 10 ▶ if ( $haps_to_pull == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($haps_to_pull == 0) 0 10 10
696 0 $haps_to_pull = $pops[ $next_haps_pop_num - 1 ];
697 }
698  
699 10 for ( 1 .. $haps_to_pull ) {
700 28 my $seq = $self->get_next_seq;
701 28 ▶ next unless defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 3 25 28
702  
703 # Add Population number information to description
704 25 $seq->display_id(" Population number $next_haps_pop_num;");
705 25 push @seqs, $seq;
706 }
707  
708 10 return @seqs;
709 }
710  
711 =head3 next_run
712  
713 Title : next_run
714 Usage : @seqs = $stream->next_run()
715 Function: reads and returns all the remaining sequences (haplotypes) in the ms
716 run of the next sequence. Returns an empty list if all haps have been
717 read from the stream.
718 Returns : array of Bio::Seq objects
719 Args : NONE
720  
721 =cut
722  
723 10 ▶sub get_next_run {
Subroutine get_next_run: called CC=4 SCAR=13.9
Pod undocumented
724 10 my $self = shift;
725  
726 # Let's figure out how many haps to read from the input file so that
727 # we get back to the beginning of the next run.
728  
729 10 my ( $next_haps_pop_num, $haps_to_pull ) = $self->get_next_haps_pop_num;
730 10 my @seqs;
731  
732 10 my @pops = $self->get_Pops;
733  
734 10 foreach ( $next_haps_pop_num .. $#pops ) {
735 8 $haps_to_pull += $pops[$_];
736 }
737  
738 # Read those haps from the input file
739 # Next hap read will be the first hap of the first pop of the next run.
740  
741 10 for ( 1 .. $haps_to_pull ) {
742 35 my $seq = $self->get_next_seq;
743 34 ▶ next unless defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 3 31 34
744  
745 31 push @seqs, $seq;
746 }
747  
748 9 return @seqs;
749 }
750  
751 =head2 Methods to Retrieve Constants
752  
753 =head3 base_conversion_table
754  
755 Title : get_base_conversion_table
756 Usage : $table_hash_ref = $stream->get_base_conversion_table()
757 Function: returns a reference to a hash. The keys of the hash are the letters '
758 A','T','G','C'. The values associated with each key are the value that
759 each letter in the sequence of a seq object returned by a
760 Bio::SeqIO::msout stream should be translated to.
761 Returns : reference to a hash
762 Args : NONE
763 Synopsis:
764  
765 # retrieve the Bio::Seq object's sequence
766 my $haplotype = $seq->seq;
767  
768 # need to convert all letters to their corresponding numbers.
769 foreach my $base (keys %{$rh_base_conversion_table}){
770 $haplotype =~ s/($base)/$rh_base_conversion_table->{$base}/g;
771 }
772  
773 # $haplotype is now an ms style haplotype. (e.g. '100101101455')
774  
775 =cut
776  
777 66 ▶sub get_base_conversion_table {
Subroutine get_base_conversion_table: called CC=1 SCAR=0.0
Pod undocumented
778 66 my $self = shift;
779 66 return $self->{BASE_CONVERSION_TABLE_HASH_REF};
780 }
781  
782 ##############################################################################
783 ## subs for internal use only
784 ##############################################################################
785  
786 80 ▶sub _get_next_clean_hap {
Subroutine _get_next_clean_hap: called CC=10 SCAR=23.6
787  
788 #By Warren Kretzschmar
789  
790 # return the next non-empty line from file handle (chomped line)
791 # skipps to the next run if '//' is encountered
792 80 my ( $self, $fh, $times, $end_run ) = @_;
793 80 my @data;
794  
795 0 ▶ unless ( ref($fh) eq q(GLOB) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh eq "GLOB") 0 80 80
796  
797 80 ▶ unless ( defined $times && $times > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $times and $times > 0) 0 80 80
Condition33%1/3
defined $times and $times > 0
!ll&&!rl&&r
0080
MC/DC0%0/2
defined $times and $times > 0
defined $times $times > 0
Truth table33%1/3
A and B
A= defined $timesB= $times > 0
ABresult
0X0
100
111
798 0 $times = 1;
799 }
800  
801 80 ▶ if ( defined $self->{BUFFER_HAP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"BUFFER_HAP"}) 13 67 80
802 13 push @data, $self->{BUFFER_HAP};
803 13 $self->{BUFFER_HAP} = undef;
804 13 $self->{LAST_READ_HAP_NUM}++;
805 13 $times--;
806 }
807  
808 80 ▶ while ( 1 <= $times-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 <= $times--) 72 71 143
809  
810 # Find next clean line
811 72 my $data = <$fh>;
812 72 ▶ last if !defined($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 9 63 72
813 63 chomp $data;
814 63 ▶ while ( $data !~ /./ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ /./) 14 63 77
815 14 $data = <$fh>;
816 14 chomp $data;
817 }
818  
819 # If the next run is encountered here, then we have a programming
820 # or format error
821 0 ▶ if ( $data eq '//' ) { $self->throw("'//' found when not expected\n") }
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "//") 0 63 63
822  
823 63 $self->{LAST_READ_HAP_NUM}++;
824 63 push @data, $data;
825 }
826  
827 80 ▶ if ($end_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_run) 16 64 80
828 16 $self->_load_run_info($fh);
829 }
830  
831 80 return (@data);
832 }
833  
834 16 ▶sub _load_run_info {
Subroutine _load_run_info: called CC=11 SCAR=24.3
835  
836 16 my ( $self, $fh ) = @_;
837  
838 16 my $data = <$fh>;
839  
840 # getting rid of excess newlines
841 16 ▶ while ( defined($data) && $data !~ /./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and not $data =~ /./) 13 16 29
Condition100%3/3
defined $data and not $data =~ /./
!ll&&!rl&&r
31313
MC/DC100%2/2
defined $data and not $data =~ /./
defined $data not $data =~ /./
Truth table100%3/3
A and not(B)
A= defined $dataB= not $data =~ /./
ABresult
0X0
100
111
842 13 $data = <$fh>;
843 }
844  
845 # In this case we are at EOF
846 16 ▶ if ( !defined($data) ) { $self->{NEXT_RUN_NUM} = undef; return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 3 13 16
847  
848 13 ▶ while ( $data !~ /./ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ /./) 0 13 13
849 0 $data = <$fh>;
850 0 chomp $data;
851 }
852  
853 13 chomp $data;
854  
855 # If the next run is encountered, then skip to the next hap and save it in
856 # the buffer.
857 13 ▶ if ( $data eq '//' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq '//') { } 13 0 13
858 13 $self->{NEXT_RUN_NUM}++;
859 13 $self->{LAST_READ_HAP_NUM} = 0;
860 13 for ( 1 .. 3 ) {
861 39 $data = <$fh>;
862 39 ▶ while ( $data !~ /./ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ /./) 10 39 49
863 10 $data = <$fh>;
864 10 chomp $data;
865 }
866 39 chomp $data;
867  
868 39 ▶ if ( $_ eq '1' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '1') { } 13 26 39
elsif ($_ eq '2') { } 13 13 26
869 13 my @sites = split( /\s+/, $data );
870 13 $self->{LAST_READ_SEGSITES} = $sites[1];
871 }
872 elsif ( $_ eq '2' ) {
873 13 my @positions = split( /\s+/, $data );
874 13 shift @positions;
875 13 $self->{LAST_READ_POSITIONS} = \@positions;
876 }
877 else {
878 13 ▶ if ( !defined($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 13 13
879 0 $self->throw("run $self->{NEXT_RUN_NUM} has no haps./n");
880 }
881 13 $self->{BUFFER_HAP} = $data;
882 }
883 }
884 }
885 else {
886 0 $self->throw(
887 "'//' not encountered when expected. There are more haplos in one of the msOUT runs than advertised in the msinfo line."
888 );
889 }
890  
891 }
892 2 1;
« Bio/Ontology/Ontology.pm ↑ Summary Bio/DB/GFF/Util/Binning.pm »