blib/lib/Bio/SNP/Inherit.pm

filter: Statementstmt 98.4% 386 / 392 Branchbran 87.1% 115 / 132 Conditioncond 89.2% 25 / 28 MC/DCmcdc 83.3% 15 / 18 Subroutinesub 100% 39 / 39 Podpod 100% 1 / 1 totaltotal 95.2% 581 / 610 CC 100 SCAR 23.3
CC
100
Cov
95%
_calculate_abh
26.8
_output_summary_and_abh_files
17.1
_parented_href
9.0
CRAP 10.2
↑ Summary
1 package Bio::SNP::Inherit;
2 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 $Bio::SNP::Inherit::VERSION = '0.002';
4 }
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #ABSTRACT: Module for determining the parental origin of specific SNPs based on genotype data.
7 11 ▶use Moose; #Obect system. Also turns on strict and warnings.
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use namespace::autoclean; #"Keep imports out of your namespace"
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use IO::File; #Provides file methods (in lieu of <> )
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp qw{ confess }; #Die with stack trace
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use List::MoreUtils qw{ all any none uniq zip }; #list-related functions
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Smart::Comments '###'; #Used for debugging
13  
14 #****************************************************************************#
15 # BUILD (START)#
16 # (This allows us to perform some tasks just after the object is created #
17 # but before it is returned to the user.) #
18 #----------------------------------------------------------------------------#
19 11 ▶sub BUILD{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
20 11 my $self = shift;
21  
22 #Get manifest data. Should die if there is an error.
23 11 $self->_process_manifest_file();
24  
25 #Process data file and output summary and abh results
26 9 $self->_output_summary_and_abh_files();
27  
28 8 return;
29 }
30 #----------------------------------------------------------------------------#
31 # BUILD (END)#
32 #****************************************************************************#
33  
34  
35 #****************************************************************************#
36 # CONSTANTS (START)#
37 #----------------------------------------------------------------------------#
38 11 my $EMPTY_STRING = q{};
39  
40 #Inheritance constants
41 11 my $MISSING = q{-};
42 11 my $MISSING_PARENT = $MISSING x 2;
43 11 my $NONPARENTAL = q{!};
44 11 my $NONPARENTAL_F1 = $NONPARENTAL x 2;
45 11 my $POLYM = q{%};
46 11 my $ALLELE_A = q{A};
47 11 my $ALLELE_B = q{B};
48 11 my $HETEROZYGOUS = q{H};
49 11 my $NONPOLYM = q{~};
50 11 my $NONPOLYM_CONFLICT = q{#};
51 #----------------------------------------------------------------------------#
52 # CONSTANTS (END)#
53 #****************************************************************************#
54  
55  
56 #****************************************************************************#
57 # MANIFEST FILE (START)#
58 #----------------------------------------------------------------------------#
59 11 has 'manifest_filename' => (
60 is=>'ro',
61 isa=>'Str',
62 required => 1,
63 );
64  
65 has '_sample_for' => (
66 traits => ['Hash'],
67 is => 'ro',
68 isa => 'HashRef',
69 12 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 11 handles =>{
71 _get__sample_for => 'get',
72 _sample_ids_from_manifest => 'keys',
73 },
74 );
75  
76 11 ▶sub _process_manifest_file {
Subroutine _process_manifest_file: called CC=8 SCAR=20.8
77 11 my $self = shift;
78 11 my %sample_for;
79  
80 #Open filehandle from manifest filename
81 11 ▶ my $fh = IO::File->new($self->manifest_filename, '<') or die $@;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->manifest_filename, "<") or die $@ 11 0 11
82  
83 #discard first line
84 11 my $first_line = <$fh>;
85  
86 #read in each id with its associated data
87 11 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 637 9 646
88  
89 #remove newline character
90 637 $line = _sans_newlines($line);
91  
92 #load each value from the line into appropriate variables
93 637 my ( $id, $name, $group, $parentA, $parentB, $replicate_of,
94 $F1_ancestor )
95 = split /\t/, $line;
96  
97 637 $id = _trim($id);
98  
99 #confess or next . . .
100 #If $id is an empty string, throw an error if anything but
101 # whitespace exists on the line
102 637 ▶ if ( $id eq $EMPTY_STRING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $EMPTY_STRING) 3 634 637
103 3 ▶ if ( $line =~ m{ \A \s+ \z}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \A \s+ \z/msx) { } 2 1 3
104 #Skip this empty line
105 2 next;
106 }
107 else {
108 1 confess "Empty id in manifest file: '$line'";
109 }
110 }
111  
112 #confess if hash for this given id already exists
113 confess 'duplicate sample id in manifest file' . "'$id'"
114 634 ▶ if exists $sample_for{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sample_for{$id} 1 633 634
115  
116 #remove leading/trailing whitespace
117 633 foreach ( $id, $name, $group, $parentA, $parentB, $replicate_of, $F1_ancestor ) {
118 4431 $_ = _trim($_);
119 }
120  
121 633 $sample_for{$id} = {};
122 633 my $sample = $sample_for{$id};
123  
124 #only store values that aren't empty
125 633 _add_if_not_empty( $sample, name => $name );
126 633 _add_if_not_empty( $sample, group => $group );
127 633 _add_if_not_empty( $sample, parentA => $parentA );
128 633 _add_if_not_empty( $sample, parentB => $parentB );
129 633 _add_if_not_empty( $sample, replicate_of => $replicate_of );
130 633 _add_if_not_empty( $sample, F1_ancestor => $F1_ancestor );
131  
132 #If this is a replicate of another sample, then store id in
133 # replicates array of the proper sample
134 633 ▶ if($replicate_of){
Branch: 1 branch
BranchTrueFalseTotal
if ($replicate_of) 50 583 633
135 50 push @{ $sample_for{$replicate_of}{replicates} }, $id;
136 }
137 }
138  
139 #Finished reading data, so close the filehandle
140 9 $fh->close();
141  
142 #Store reference to the hash containing all of the data
143 9 $self->{_sample_for} = \%sample_for;
144  
145 9 return;
146 }
147  
148 #Trim away leading and trailing whitespace
149 24271 ▶sub _trim{
Subroutine _trim: called CC=2 SCAR=6.9
150 24271 my $string = shift;
151  
152 #Don't try substitutions on undef
153 24271 ▶ return if ! defined( $string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 613 23658 24271
154  
155 #Trim all leading whitespace
156 23658 $string =~ s/\A \s+//xms;
157  
158 #Trim all trailing whitespace
159 23658 $string =~ s/\s+ \z//xms;
160  
161 #Return trimmed string
162 23658 return $string;
163 }
164  
165 #Adds key, value pairs to a hash if the value is not empty.
166 3798 ▶sub _add_if_not_empty{
Subroutine _add_if_not_empty: called CC=3 SCAR=11.0
167 3798 my $hashref = shift;
168 3798 my $key = shift;
169 3798 my $value = shift;
170  
171 #Only assign to hash for keys that are defined and not empty
172 3798 ▶ if(defined($value) && $value ne $EMPTY_STRING){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne $EMPTY_STRING) 1864 1934 3798
Condition100%3/3
defined $value and $value ne $EMPTY_STRING
!ll&&!rl&&r
61313211864
MC/DC100%2/2
defined $value and $value ne $EMPTY_STRING
defined $value $value ne $EMPTY_STRING
Truth table100%3/3
A and B
A= defined $valueB= $value ne $EMPTY_STRING
ABresult
0X0
100
111
173 1864 ${$hashref}{$key} = $value;
174 }
175 3798 return $hashref;
176 }
177 #----------------------------------------------------------------------------#
178 # MANIFEST FILE (END)#
179 #****************************************************************************#
180  
181  
182 #****************************************************************************#
183 # SUMMARIZE DATA (START)#
184 #----------------------------------------------------------------------------#
185  
186 #Must be specified in the constructor. This is indicated here by (1) making it
187 # required and by (2) not giving it a default nor a builder
188 11 has 'data_filename' => (
189 is => 'ro',
190 isa => 'Str',
191 required => 1,
192 );
193  
194 # leading underscore indicates a private attribute by convention
195 11 has '_data_fh' => (
196 is => 'ro',
197 isa => 'IO::File',
198 lazy => 1,
199 required => 1,
200 builder => '_build__data_fh',
201 );
202  
203 2277 ▶sub _get_data_line{
Subroutine _get_data_line: called CC=1 SCAR=0.0
204 2277 my $self = shift;
205 2277 return $self->_data_fh->getline();
206 }
207  
208 #sub name = '_build_' . '_data_fh'
209 9 ▶sub _build__data_fh {
Subroutine _build__data_fh: called CC=2 SCAR=7.1
210 9 my $self = shift;
211 9 ▶ my $fh = IO::File->new( $self->data_filename(), '<' ) or die $@;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->data_filename, "<") or die $@ 9 0 9
212 9 return $fh;
213 }
214  
215 11 has 'sample_ids' => (
216 traits => ['Array'],
217 is => 'ro',
218 isa => 'ArrayRef',
219 lazy => 1,
220 required => 1,
221 builder => '_build_sample_ids',
222 handles => {
223 _ids_from_data_file => 'elements',
224 },
225 );
226  
227 9 ▶sub _build_sample_ids {
Subroutine _build_sample_ids: called CC=4 SCAR=13.9
228 9 my $self = shift;
229  
230 #skip all lines until finding the [Data] label.
231 9 ▶ while ( my $line = $self->_data_fh->getline() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_data_fh->getline) 74 1 75
232 74 ▶ last if $line =~ /\[ data \]/xmsi; #i makes it case insensitive
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\[ data \]/imsx 8 66 74
233 }
234  
235 #read in line containing ids
236 9 my $line_of_ids = $self->_data_fh->getline();
237 9 ▶ if ( !defined $line_of_ids ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line_of_ids) 1 8 9
238 1 my $dying_message =
239 'either no sample ids or no "[Data]" tag found in file: '
240 . $self->data_filename;
241 1 confess $dying_message;
242 }
243  
244 #remove newline(s)
245 8 $line_of_ids = _sans_newlines($line_of_ids);
246  
247 #extract ids from line, ignoring blank field at the beginning
248 8 my ( undef, @ids ) = split /\t/, $line_of_ids;
249 8 return \@ids;
250 }
251  
252 11 has '_columns_for' => (
253 traits => ['Hash'],
254 is => 'ro',
255 isa => 'HashRef',
256 lazy => 1,
257 builder => '_build__columns_for',
258 handles => { _columns_for_id_aref => 'get', },
259 );
260  
261 #getting columns corresponding to given id.
262 7635 ▶sub _get_columns_for{
Subroutine _get_columns_for: called CC=2 SCAR=7.0
263 7635 my $self = shift;
264 7635 my $id = shift;
265 7635 ▶ confess if ! defined($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 7635 7635
266 7635 return @{ $self->_columns_for_id_aref($id) };
267 }
268  
269 8 ▶sub _build__columns_for{
Subroutine _build__columns_for: called CC=6 SCAR=17.9
270 8 my $self = shift;
271  
272 #get all of the sample ids, in their column order
273 8 my @data_ids = $self->_ids_from_data_file();
274  
275 #get all of the column indices
276 8 my @indices = (0 .. $#data_ids);
277  
278 #store column indicdes
279 8 my @columns_aref;
280 8 for my $index (0 .. $#indices){
281 59 push @columns_aref, [ $index ];
282 }
283  
284 #Create hash to hold column info
285 8 my %columns_for = zip(@data_ids, @columns_aref);
286  
287 #-------------------------------------------------------------------------
288 #For each sample that has replicates, add column indices of the replicates
289 # This just checks ids from the manifest file, since they are the only
290 # ones that have replicate information.
291 8 for my $manifest_id ( $self->_sample_ids_from_manifest() ) {
292  
293 340 my $sample = $self->{_sample_for}->{$manifest_id};
294  
295 #if sample has replicates, add column indices of replicates
296 340 ▶ if ( defined $sample->{replicates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sample->{"replicates"}) 20 320 340
297 20 for my $replicate ( @{ $sample->{replicates} } ) {
298 38 ▶ if( defined $columns_for{$replicate}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns_for{$replicate}) 33 5 38
299 33 push @{ $columns_for{$manifest_id} }, @{ $columns_for{$replicate} };
300 }
301 }
302 }
303 }
304 #-------------------------------------------------------------------------
305  
306 8 return \%columns_for;
307 }
308  
309 59 ▶sub _in_group{
Subroutine _in_group: called CC=3 SCAR=11.0
310 59 my $self = shift;
311 59 my $group_regex = shift;
312 59 my $id = shift;
313 59 my $sample = $self->{_sample_for}->{$id};
314 59 ▶ return 0 if(!defined $sample);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sample 0 59 59
315 59 ▶ if($sample->{group} =~ / $group_regex /xms){
Branch: 1 branch
BranchTrueFalseTotal
if ($sample->{'group'} =~ / $group_regex /msx) { } 8 51 59
316 8 return 1;
317 }else{
318 51 return 0;
319 }
320 }
321  
322 8 ▶sub _parented_href{
Subroutine _parented_href: called CC=9 SCAR=22.0
323 8 my $self = shift;
324 8 my %parented = (
325 names => [],
326 ids => [],
327 groups => [],
328 parentA_names => [],
329 parentB_names => [],
330 F1_ancestors => [],
331 );
332  
333  
334 GROUPS_OF_CODES:
335 8 for my $id ($self->_ids_from_data_file()) {
336 59 my $sample = $self->{_sample_for}->{$id};
337  
338 #Skip this id, if no parentA
339 59 ▶ next if ! defined( $sample->{parentA});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sample->{"parentA"} 44 15 59
340  
341 15 ▶ if ( defined( $sample->{parentB} ) || defined( $sample->{F1_ancestor} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sample->{'parentB'} or defined $sample->{'F1_ancestor'}) { } 8 7 15
Condition100%3/3
defined $sample->{"parentB"} or defined $sample->{"F1_ancestor"}
l!l&&r!l&&!r
627
MC/DC100%2/2
defined $sample->{"parentB"} or defined $sample->{"F1_ancestor"}
defined $sample->{"parentB"} defined $sample->{"F1_ancestor"}
Truth table100%3/3
A or B
A= defined $sample->{"parentB"}B= defined $sample->{"F1_ancestor"}
ABresult
1X1
011
000
342  
343 8 my $parentA_id = $sample->{parentA};
344 8 my $parentA_name = $self->{_sample_for}->{$parentA_id}->{name};
345  
346 #Get $parentB_id.
347 #Change it to undef if it is the empty string
348 8 ▶ my $parentB_id = $sample->{parentB} || undef;
Condition100%2/2
$sample->{'parentB'} || undef
l!l
62
MC/DC100%1/1
$sample->{'parentB'} || undef
$sample->{'parentB'}
Truth table100%2/2
A
A= $sample->{'parentB'}
Aresult
11
00
349  
350 8 ▶ my $F1_ancestor_id = $sample->{F1_ancestor} || undef;
Condition100%2/2
$sample->{'F1_ancestor'} || undef
l!l
26
MC/DC100%1/1
$sample->{'F1_ancestor'} || undef
$sample->{'F1_ancestor'}
Truth table100%2/2
A
A= $sample->{'F1_ancestor'}
Aresult
11
00
351  
352 8 my $parentB_name;
353 8 ▶ if ($parentB_id) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parentB_id) { } 6 2 8
elsif ($F1_ancestor_id) { } 2 0 2
354 6 $parentB_name = $self->{_sample_for}->{$parentB_id}->{name};
355 }
356 elsif ($F1_ancestor_id) {
357 $parentB_name =
358 2 '(F1)' . $self->{_sample_for}->{$F1_ancestor_id}->{name};
359 }
360  
361 8 push @{ $parented{ids} }, $id;
362 8 push @{ $parented{parentA_names} }, $parentA_name;
363 8 push @{ $parented{parentB_names} }, $parentB_name;
364 8 push @{ $parented{groups} }, $sample->{group};
365 8 push @{ $parented{names} }, $sample->{name};
366 }
367 else{
368 #Skip this one since it doesn't have a parentB or an F1 ancestor
369 7 next;
370 }
371 }
372 8 return \%parented;
373 }
374  
375 9 ▶sub _output_summary_and_abh_files {
Subroutine _output_summary_and_abh_files: called CC=17 SCAR=28.4
376 9 my $self = shift;
377  
378 #Column headers for Summary output file
379 9 $self->_summary_fh->print($self->_summary_header());
380  
381 9 my @all_sample_ids = $self->_ids_from_data_file;
382  
383 8 my $nam_regex = qr{ NAM [ ]* F1}xms;
384 my @nam_f1_ids =
385 8 grep { $self->_in_group( $nam_regex, $_ ) } @all_sample_ids;
386  
387 8 my %parented = %{ $self->_parented_href() };
388  
389 #Columns headers for ABH output file
390 8 for my $item (qw{ ids parentA_names parentB_names groups names}) {
391 40 my $abh_header_row = "\t" . join "\t", @{ $parented{$item} };
392 40 $self->_abh_fh->print($abh_header_row . "\n");
393 }
394  
395 #Read data from file and analyze it one line at a time
396 # (if already done, this will be skipped due to filehandle
397 # already being at the end of the file)
398 8 ▶ while ( my $line = $self->_get_data_line() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_get_data_line) 2269 8 2277
399  
400 #Remove newline character
401 2269 $line = _sans_newlines($line);
402  
403 #Extract SNP name and the data from this line
404 2269 my ( $snp, @genotypes ) = split /\t/, $line;
405  
406 #Remove whitespace at beginning and end of SNP name
407 2269 $snp = _trim($snp);
408  
409 #Skip blank lines
410 2269 ▶ next if $snp eq $EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
if $snp eq $EMPTY_STRING 0 2269 2269
411  
412 #Print SNP name in first column of current line of ABH output file
413 2269 $self->_abh_fh->print($snp);
414  
415 #Tally occurrences of eacy genotype
416 2269 my @summarized_genotypes = _summarize_genotypes(@genotypes);
417  
418 #Associated data ids with genotypes
419 2269 my %genotype_for = zip( @all_sample_ids, @genotypes );
420  
421 #Initialize counters with zeros. Thus zeros will show up in reports.
422 2269 my $inconsistent_count = 0;
423 2269 my %num_of = (
424 parentA => {
425 $POLYM => 0,
426 $MISSING => 0,
427 },
428 parentB => {
429 $POLYM => 0,
430 $MISSING => 0,
431 },
432 F1 => { $MISSING => 0, },
433 );
434  
435 #----------------------------------------------------------------START
436 #Summarize this one line of data for all of the NAM F1's
437 #---------------------------------------------------------------------
438 NAM_F1_LOOP:
439 2269 for my $sample_id (@nam_f1_ids) {
440  
441 1566 my $sample = $self->_get__sample_for($sample_id);
442 1566 my $sample_gt = $genotype_for{$sample_id};
443  
444 #For our current purposes, we will not evaluate an F1 unless there is data for both parents.
445 my $both_parents_present =
446 1566 ▶ defined $sample->{parentA} && defined $sample->{parentB};
Condition100%3/3
defined $sample->{'parentA'} && defined $sample->{'parentB'}
!ll&&!rl&&r
991548
MC/DC100%2/2
defined $sample->{'parentA'} && defined $sample->{'parentB'}
defined $sample->{'parentA'} defined $sample->{'parentB'}
Truth table100%3/3
A && B
A= defined $sample->{'parentA'}B= defined $sample->{'parentB'}
ABresult
0X0
100
111
447  
448 1566 ▶ next NAM_F1_LOOP if (! $both_parents_present);
Branch: 1 branch
BranchTrueFalseTotal
unless $both_parents_present 18 1548 1566
449  
450 1548 my @genotype_chars;
451 my $parents_missing_data_for_this_sample_count;
452 1548 my %gt_chars_of;
453 1548 for my $parent (qw{ parentA parentB}) {
454 my @parent_columns =
455 3096 $self->_get_columns_for( $sample->{$parent} );
456 3096 my @parent_genotypes = @genotypes[@parent_columns];
457  
458 #_nonredundant_chars
459 3096 my @parent_gt_chars = @{ $gt_chars_of{$parent} } =
460 _nonredundant_chars(@parent_genotypes);
461 3096 push @genotype_chars, @parent_gt_chars;
462  
463 3096 my $num_parent_char = @parent_gt_chars;
464 3096 ▶ if ( $num_parent_char > 1 ) { $num_of{$parent}{$POLYM}++; }
Branch: 3 branches
BranchTrueFalseTotal
if ($num_parent_char > 1) { } 400 2696 3096
elsif ($num_parent_char == 1) { } 2696 0 2696
elsif ($_ < 1) { } 0 0 0
465 elsif ( $num_parent_char == 1 ) {
466 2696 ▶ if ( $parent_gt_chars[0] eq $MISSING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_gt_chars[0] eq $MISSING) 333 2363 2696
467 333 $num_of{$parent}{$MISSING}++;
468 333 $parents_missing_data_for_this_sample_count++;
469 }
470 }
471 elsif ( $_ < 1 ) {
472 0 confess
473 '_nonredundant_chars should return at least one char';
474 }
475 }
476  
477 #Keep track of how many F1s are missing data
478 1548 ▶ if ( $sample_gt eq $MISSING x 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sample_gt eq $MISSING x 2) { } 441 1107 1548
elsif (not _can_be_F1_of($sample_gt, $gt_chars_of{'parentA'}, $gt_chars_of{'parentB'})) { } 202 905 1107
479 441 $num_of{F1}{$MISSING}++;
480 }
481 elsif (
482 !_can_be_F1_of(
483 $sample_gt, $gt_chars_of{parentA},
484 $gt_chars_of{parentB}
485 )
486 )
487 {
488 202 $inconsistent_count++;
489 }
490 }
491 my $sum_of_odds_and_ends = $inconsistent_count
492 + $num_of{parentA}{$POLYM}
493 + $num_of{parentB}{$POLYM}
494 + $num_of{parentA}{$MISSING}
495 2269 + $num_of{parentB}{$MISSING};
496  
497 my $summary_line = join "\t", $snp, @summarized_genotypes,
498 $inconsistent_count, $num_of{parentA}{$POLYM},
499 $num_of{parentB}{$POLYM}, $num_of{parentA}{$MISSING},
500 $num_of{parentB}{$MISSING}, $sum_of_odds_and_ends,
501 2269 $num_of{F1}{$MISSING};
502  
503 2269 $self->_summary_fh->print($summary_line . "\n");
504 #---------------------------------------------------------------------
505 #Summarize this one line of data for all of the NAM F1's
506 #------------------------------------------------------------------END
507  
508  
509 #Output inheritance codes
510 #First step: calculate for those with two parents
511 #Second step: calculate for those with parentA and one F1 ancestor
512 2269 my $abh_line = $EMPTY_STRING;
513  
514 #----------------------------------------------------------------START
515 #Calculate inheritance codes for everything with two parents
516 #---------------------------------------------------------------------
517 2269 for my $sample_id ( @{ $parented{ids} } ) {
518  
519 2269 my $sample = $self->_get__sample_for($sample_id);
520 2269 my $sample_gt = $genotype_for{$sample_id};
521 2269 my $parentA_id = $sample->{parentA};
522 2269 my @parentA_columns = $self->_get_columns_for($parentA_id);
523 2269 my @parentA_genotypes = @genotypes[@parentA_columns];
524  
525 2269 my $sample_abh;
526 2269 ▶ if ( $sample->{parentB} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sample->{'parentB'}) { } 1548 721 2269
elsif ($sample->{'F1_ancestor'}) { } 721 0 721
527 1548 my $parentB_id = $sample->{parentB};
528 1548 my @parentB_columns = $self->_get_columns_for($parentB_id);
529 1548 my @parentB_genotypes = @genotypes[@parentB_columns];
530  
531 1548 $sample_abh = _calculate_abh( $sample_gt, \@parentA_genotypes,
532 \@parentB_genotypes );
533 }elsif($sample->{F1_ancestor}){
534 721 my $F1_ancestor_id = $sample->{F1_ancestor};
535 721 my @F1_columns = $self->_get_columns_for($F1_ancestor_id);
536 721 my @F1_genotypes = @genotypes[@F1_columns];
537 721 $sample_abh = _calculate_abh_from_F1( $sample_gt, \@parentA_genotypes,
538 \@F1_genotypes);
539 }
540 2269 $abh_line .= "\t" . $sample_abh;
541 }
542  
543 #print abh_line to file and add newline
544 2269 $self->_abh_fh->print($abh_line ."\n");
545 #------------------------------------------------------------------END
546  
547  
548 }
549 8 $self->_summary_fh->flush();
550 8 $self->_summary_fh->close();
551 8 $self->_abh_fh->flush();
552 8 $self->_abh_fh->close();
553  
554 8 return;
555 }
556  
557  
558 11 has '_abh_fh' => (
559 is => 'ro',
560 isa => 'IO::File',
561 lazy => 1,
562 required => 1,
563 builder => '_build__abh_fh',
564 );
565  
566 8 ▶sub _build__abh_fh {
Subroutine _build__abh_fh: called CC=2 SCAR=7.0
567 8 my $self = shift;
568 8 my $filename = $self->data_filename() . '_abh.tab';
569 8 ▶ my $fh = IO::File->new( $filename, '>' ) or die $@;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($filename, ">") or die $@ 8 0 8
570 8 return $fh;
571 }
572  
573 11 has '_summary_fh' => (
574 is => 'ro',
575 isa => 'IO::File',
576 lazy => 1,
577 required => 1,
578 builder => '_build__summary_fh',
579 );
580  
581 9 ▶sub _build__summary_fh {
Subroutine _build__summary_fh: called CC=2 SCAR=7.0
582 9 my $self = shift;
583 9 my $filename = $self->data_filename() . '_summary.tab';
584 9 ▶ my $fh = IO::File->new( $filename, '>' ) or die $@;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($filename, ">") or die $@ 9 0 9
585 9 return $fh;
586 }
587  
588 2515 ▶sub _sorted_characters{
Subroutine _sorted_characters: called CC=1 SCAR=0.0
589 2515 my $string = shift;
590 2515 $string =~ s/ \s //xms;
591 #mea culpa
592 2515 return join $EMPTY_STRING, sort split $EMPTY_STRING, $string;
593  
594 }
595  
596 3893 ▶sub _sort_and_join{
Subroutine _sort_and_join: called CC=1 SCAR=0.0
597 3893 my @chars = @_;
598 3893 return join $EMPTY_STRING, sort @chars;
599 }
600  
601 2514 ▶sub _can_be_F1_of{
Subroutine _can_be_F1_of: called CC=7 SCAR=19.5
602 2514 my $sample_gt = shift;
603 2514 my $parentA_aref = shift; # 'aref' means array reference
604 2514 my $parentB_aref = shift;
605 2514 my @parentA_chars = _nonredundant_chars( @{$parentA_aref} );
606 2514 my @parentB_chars = _nonredundant_chars( @{$parentB_aref} );
607  
608 #If parentA or parentB only have missing data, then they could be
609 # anything
610 2514 ▶ if (all { $_ eq $MISSING } @parentA_chars){
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_ eq $MISSING; } , @parentA_chars)) 58 2456 2514
611 58 @parentA_chars = qw{ A C G T - };
612 }
613 2514 ▶ if (all { $_ eq $MISSING } @parentB_chars){
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_ eq $MISSING; } , @parentB_chars)) 1541 973 2514
614 1541 @parentB_chars = qw{ A C G T - };
615 }
616  
617 2514 my $sorted_sample_gt = _sorted_characters($sample_gt);
618 2514 ▶ return 1 if $sorted_sample_gt eq $MISSING x 2;
Branch: 1 branch
BranchTrueFalseTotal
if $sorted_sample_gt eq $MISSING x 2 245 2269 2514
619  
620 2269 for my $parentA_char (@parentA_chars){
621 2383 for my $parentB_char (@parentB_chars){
622 3892 my $expected_gt = _sort_and_join($parentA_char, $parentB_char);
623 3892 ▶ return 1 if($expected_gt eq $sorted_sample_gt);
Branch: 1 branch
BranchTrueFalseTotal
if $expected_gt eq $sorted_sample_gt 2058 1834 3892
624 }
625 }
626  
627 #If nothing matched, return false
628 211 return 0;
629 }
630  
631  
632 732 ▶sub _calculate_abh_from_F1 {
Subroutine _calculate_abh_from_F1: called CC=1 SCAR=0.0
633 732 my $sample_gt = shift;
634 732 my $parentA_aref = shift;
635 732 my $F1_aref = shift;
636  
637 732 _calculate_abh($sample_gt, $parentA_aref, $F1_aref, 'F1');
638 }
639  
640 307 ▶sub _remove_matching_char{
Subroutine _remove_matching_char: called CC=3 SCAR=11.0
641 307 my $char = shift;
642 307 my @array = @_;
643 307 my @final_array;
644 307 for(@array){
645 616 ▶ next if $_ eq $char;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $char 307 309 616
646 309 push @final_array, $_;
647 }
648 307 return @final_array;
649 }
650  
651 2212 ▶sub _equivalent_arrays{
Subroutine _equivalent_arrays: called CC=4 SCAR=13.9
652 2212 my $first_aref = shift;
653 2212 my $second_aref = shift;
654 2212 my @first = @{ $first_aref };
655 2212 my @second = @{ $second_aref };
656  
657 2212 my $same_size = scalar @first == scalar @second;
658 2212 ▶ return if ! $same_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $same_size 609 1603 2212
659  
660 1603 for (0 .. $#first ){
661 1651 ▶ return if $first[$_] ne $second[$_] ;
Branch: 1 branch
BranchTrueFalseTotal
if $first[$_] ne $second[$_] 933 718 1651
662 }
663 670 return 1;
664 }
665  
666 2717 ▶sub _calculate_abh {
Subroutine _calculate_abh: called CC=26 SCAR=32.9
667 2717 my $sample_gt = shift; # 'gt' means genotype
668 2717 my $parentA_aref = shift; # 'aref' means array reference
669 2717 my $parentB_aref = shift;
670 2717 my $parentB_is_F1 = shift;
671 2717 my @parentA_chars = _nonredundant_chars( @{$parentA_aref} );
672 2717 my @parentB_chars = _nonredundant_chars( @{$parentB_aref} );
673  
674 #If genotype has one or more $MISSING alleles, then return $MISSING
675 2717 ▶ return $MISSING if ( $sample_gt =~ m{ $MISSING }xms );
Branch: 1 branch
BranchTrueFalseTotal
if $sample_gt =~ / $MISSING /msx 611 2106 2717
676  
677 #Find out if the parents have exactly the same genotype
678 2106 my $parents_same;
679 2106 ▶ if ( _equivalent_arrays(\@parentA_chars, \@parentB_chars )) {
Branch: 1 branch
BranchTrueFalseTotal
if (_equivalent_arrays(\@parentA_chars, \@parentB_chars)) 564 1542 2106
680 564 $parents_same = 1;
681 }
682  
683 #Return 'missing' if one of the parents have only missing data
684 #Return error code if sample has alleles not found in parents
685 #Must check in this order, otherwise having no parental info will be
686 # misdiagnosed as having a nonparental allele
687 2106 ▶ if ( @parentA_chars == 1 && $parentA_chars[0] eq $MISSING ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parentA_chars == 1 and $parentA_chars[0] eq $MISSING) { } 63 2043 2106
elsif (@parentB_chars == 1 and $parentB_chars[0] eq $MISSING) { } 216 1827 2043
Condition100%3/3
@parentA_chars == 1 and $parentA_chars[0] eq $MISSING
!ll&&!rl&&r
169187463
MC/DC100%2/2
@parentA_chars == 1 and $parentA_chars[0] eq $MISSING
@parentA_chars == 1 $parentA_chars[0] eq $MISSING
Truth table100%3/3
A and B
A= @parentA_chars == 1B= $parentA_chars[0] eq $MISSING
ABresult
0X0
100
111
688 63 return $MISSING_PARENT;
689 }
690 ▶ elsif ( @parentB_chars == 1 && $parentB_chars[0] eq $MISSING ) {
Condition100%3/3
@parentB_chars == 1 and $parentB_chars[0] eq $MISSING
!ll&&!rl&&r
5721255216
MC/DC100%2/2
@parentB_chars == 1 and $parentB_chars[0] eq $MISSING
@parentB_chars == 1 $parentB_chars[0] eq $MISSING
Truth table100%3/3
A and B
A= @parentB_chars == 1B= $parentB_chars[0] eq $MISSING
ABresult
0X0
100
111
691 216 return $MISSING_PARENT;
692 }
693 else {
694  
695 #combine parental character sets
696 1827 my @both_parent_chars =
697 _nonredundant_chars( @parentA_chars, @parentB_chars );
698  
699 #Check if the sample genotype can have descended from parents
700 1827 ▶ if ( !_is_comprised_from( $sample_gt, @both_parent_chars ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_comprised_from($sample_gt, @both_parent_chars)) { } 53 1774 1827
701 53 ▶ if ($parents_same) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parents_same) { } 48 5 53
702 48 return $NONPOLYM_CONFLICT;
703 }
704 else {
705 5 return $NONPARENTAL;
706 }
707 }
708 else {
709  
710 #Good! No errors and no missing data.
711 }
712 }
713  
714 1774 ▶ if ( !$parentB_is_F1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parentB_is_F1) { } 1301 473 1774
715  
716 #If genotype only contains alleles from parents, then assign A, B, or H
717 1301 my $allele_A_count = 0;
718 1301 my $allele_B_count = 0;
719  
720 #Check if both parents are nonpolymorphic
721 1301 ▶ if ( @parentA_chars == 1 && @parentB_chars == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parentA_chars == 1 and @parentB_chars == 1) { } 996 305 1301
Condition100%3/3
@parentA_chars == 1 and @parentB_chars == 1
!ll&&!rl&&r
107198996
MC/DC100%2/2
@parentA_chars == 1 and @parentB_chars == 1
@parentA_chars == 1 @parentB_chars == 1
Truth table100%3/3
A and B
A= @parentA_chars == 1B= @parentB_chars == 1
ABresult
0X0
100
111
722 996 my $parentA_char = $parentA_chars[0];
723 996 my $parentB_char = $parentB_chars[0];
724  
725 #Indicate noninformative if parents are the same
726 996 ▶ return $NONPOLYM if ($parents_same);
Branch: 1 branch
BranchTrueFalseTotal
if $parents_same 348 648 996
727  
728 #Extract characters from sample genotype
729 648 my @sample_chars = _chars_from($sample_gt);
730  
731 #Tally how many are from which parent
732 648 for my $sample_char (@sample_chars) {
733 1296 ▶ if($sample_char eq $parentA_char){
Branch: 2 branches
BranchTrueFalseTotal
if ($sample_char eq $parentA_char) { } 662 634 1296
elsif ($sample_char eq $parentB_char) { } 634 0 634
734 662 $allele_A_count++;
735 }
736 634 elsif ($sample_char eq $parentB_char) { $allele_B_count++;}
737 }
738  
739 648 ▶ if ( $allele_A_count == 2 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($allele_A_count == 2) { } 209 439 648
elsif ($allele_B_count == 2) { } 195 244 439
elsif ($allele_A_count == 1 and $allele_B_count == 1) { } 244 0 244
740 209 return $ALLELE_A;
741 }
742 elsif ( $allele_B_count == 2 ) {
743 195 return $ALLELE_B;
744 }
745 ▶ elsif ( $allele_A_count == 1 && $allele_B_count == 1 ) {
Condition33%1/3
$allele_A_count == 1 and $allele_B_count == 1
!ll&&!rl&&r
00244
MC/DC0%0/2
$allele_A_count == 1 and $allele_B_count == 1
$allele_A_count == 1 $allele_B_count == 1
Truth table33%1/3
A and B
A= $allele_A_count == 1B= $allele_B_count == 1
ABresult
0X0
100
111
746 244 return $HETEROZYGOUS;
747 }
748 else {
749  
750 #Since we already checked for values that were missing or
751 # nonparental, this should be unreachable
752 my $dying_message = "error calculating 'ABH' from:\n"
753 . join "'\n",
754 0 q{parentA => '} . join( "' '", @{$parentA_aref} ),
755 0 q{parentB => '} . join( "' '", @{$parentB_aref} ),
756 q{sample genotype =>'} . $sample_gt;
757  
758 0 confess $dying_message;
759 }
760 }
761 else {
762  
763 #indicate that it is noninformative because of a polymorphic parent
764 305 return $POLYM;
765 }
766 }
767 else {
768 473 for my $F1_gt ( @{$parentB_aref} ) {
769 1399 ▶ return $NONPARENTAL_F1
Branch: 1 branch
BranchTrueFalseTotal
unless _can_be_F1_of($F1_gt, $parentA_aref, [$MISSING]) 5 1394 1399
770 if !_can_be_F1_of( $F1_gt, $parentA_aref, [$MISSING] );
771 }
772  
773 468 ▶ return $POLYM if @parentA_chars > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parentA_chars > 1 57 411 468
774  
775 411 ▶ if (@parentB_chars == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parentB_chars == 2) { } 305 106 411
elsif (@parentB_chars == 1) { } 106 0 106
776 305 my $parentA_char = $parentA_chars[0];
777 305 my @revised_parentB_chars =
778 _remove_matching_char( $parentA_char, @parentB_chars );
779 305 return _calculate_abh( $sample_gt, $parentA_aref,
780 \@revised_parentB_chars );
781 }
782 elsif (@parentB_chars == 1) {
783 106 ▶ if ( _equivalent_arrays(\@parentB_chars, \@parentA_chars )) {
Branch: 1 branch
BranchTrueFalseTotal
if (_equivalent_arrays(\@parentB_chars, \@parentA_chars)) { } 106 0 106
784 106 return _calculate_abh( $sample_gt, $parentA_aref,
785 $parentB_aref );
786 }
787 else {
788 ### $sample_gt
789 ### @parentA_chars
790 ### @parentB_chars
791 0 confess "I didn't think this was reachable";
792 }
793 }
794 }
795 }
796  
797 10 ▶sub _summary_header{
Subroutine _summary_header: called CC=1 SCAR=0.0
798 10 return "\t" . join("\t", qw{ AA AC AG AT CC CG CT GG GT TT --
799 inconsistent parentA_polymorphic
800 parentB_polymorphic parentA_unknown
801 parentB_unknown sum_of_odds_and_ends
802 F1_missing }) . "\n";
803 }
804  
805 2270 ▶sub _summarize_genotypes {
Subroutine _summarize_genotypes: called CC=3 SCAR=11.0
806 2270 my @data = @_;
807 2270 my @genotypes = qw{ AA AC AG AT CC CG CT GG GT TT -- };
808 2270 my %count;
809  
810 #Initialize hash %count, so that zeros show up in the summary
811 2270 for my $genotype (@genotypes) {
812 24970 $count{$genotype} = 0;
813 }
814  
815 #Count occurrences of each genotype
816 2270 for my $datum (@data){
817  
818 #Remove any leading or trailing whitespace
819 16934 $datum = _trim($datum);
820  
821 #Get just the first two characters of each data string
822 16934 my $sorted_char_datum = _sorted_first_two_char($datum);
823  
824 #Add one to the count for this genotype
825 16934 $count{$sorted_char_datum}++;
826 }
827  
828 #Return an array of just the specified genotypes
829 # (this is a 'hash slice')
830 2270 return @count{ @genotypes };
831 }
832 #----------------------------------------------------------------------------#
833 # SUMMARIZE DATA (END)#
834 #****************************************************************************#
835  
836 #****************************************************************************#
837 # GENERAL UTILITIES #
838 #----------------------------------------------------------------------------#
839  
840 15397 ▶sub _nonredundant_chars {
Subroutine _nonredundant_chars: called CC=7 SCAR=19.5
841 15397 my @strings = @_;
842 15397 ▶ confess 'arguments required' if( @strings < 1);
Branch: 1 branch
BranchTrueFalseTotal
if @strings < 1 1 15396 15397
843 15396 for( 0 .. $#strings){
844 42721 $strings[$_] = _sans_newlines($strings[$_]);
845 }
846  
847 #Get just the unique characters out of the strings
848 15396 my $combined = join $EMPTY_STRING, @strings;
849 15396 my @single_chars = uniq _chars_from($combined);
850  
851 #Remove 'missing data' and empty characters
852 my @filtered_chars =
853 15396 ▶ grep { $_ ne $MISSING && $_ ne $EMPTY_STRING } @single_chars;
Condition66%2/3
$_ ne $MISSING && $_ ne $EMPTY_STRING
!ll&&!rl&&r
7150016090
MC/DC50%1/2
$_ ne $MISSING && $_ ne $EMPTY_STRING
$_ ne $MISSING $_ ne $EMPTY_STRING
Truth table66%2/3
A && B
A= $_ ne $MISSINGB= $_ ne $EMPTY_STRING
ABresult
0X0
100
111
854  
855 #Return $MISSING if there aren't any left
856 15396 ▶ return $MISSING if(@filtered_chars == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @filtered_chars == 0 2421 12975 15396
857  
858 #Check that the characters are all DNA bases
859 12975 my $DNA_BASES = qr{ [ACGT] }xms;
860 12975 for my $filtered_char(@filtered_chars){
861  
862 #Throw exception if character is not a valid DNA base
863 16089 ▶ if($filtered_char !~ m{\A $DNA_BASES \z}xms){
Branch: 1 branch
BranchTrueFalseTotal
unless ($filtered_char =~ /\A $DNA_BASES \z/msx) 1 16088 16089
864 1 confess "Unknown character: '$filtered_char'";
865 }
866 }
867  
868 #Return nonredundant characters, now that they've all been checked
869 12974 return @filtered_chars;
870 }
871  
872 # Are all of the characters in a string a subset of the characters found in
873 # this array of strings?
874 1827 ▶sub _is_comprised_from{
Subroutine _is_comprised_from: called CC=5 SCAR=16.1
875 #Get the string to be analyzed, followed by strings with acceptable chars
876 1827 my ($string, @acceptable_strings) = @_;
877  
878 #Put all of the characters from all of these strings into one array
879 1827 my @acceptable_chars;
880 1827 for my $acceptable_string( @acceptable_strings){
881 3154 push @acceptable_chars, uniq _chars_from($acceptable_string);
882 }
883  
884 1827 ▶ confess 'Empty string found'
Branch: 1 branch
BranchTrueFalseTotal
if _is_an_element_of($EMPTY_STRING, @acceptable_chars) 0 1827 1827
885 if _is_an_element_of($EMPTY_STRING, @acceptable_chars);
886  
887 #Get all of the characters out of the string to be analyzed
888 1827 my @chars = uniq _chars_from($string);
889  
890 #If any character is not acceptable, return false
891 1827 for my $char (@chars){
892 2324 ▶ if(! _is_an_element_of($char, @acceptable_chars)){
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_an_element_of($char, @acceptable_chars)) 53 2271 2324
893 53 return 0;
894 }
895 }
896  
897 #Since no unacceptable character was found, return true
898 1774 return 1;
899 }
900  
901 4151 ▶sub _is_an_element_of{
Subroutine _is_an_element_of: called CC=3 SCAR=11.0
902  
903 4151 my $query = shift;
904 4151 my @elements = @_;
905 4151 for my $element( @elements){
906 6347 ▶ return 1 if $query eq $element;
Branch: 1 branch
BranchTrueFalseTotal
if $query eq $element 2271 4076 6347
907 }
908  
909 1880 return 0;
910 }
911  
912 21026 ▶sub _chars_from{
Subroutine _chars_from: called CC=2 SCAR=6.9
913 21026 my @strings = shift;
914  
915 21026 my @string_chars;
916 21026 for my $string (@strings){
917 21026 push @string_chars, split $EMPTY_STRING, $string;
918 }
919  
920 #Return the individual characters from the string as an array
921 21026 return @string_chars;
922 }
923  
924 16934 ▶sub _sorted_first_two_char {
Subroutine _sorted_first_two_char: called CC=1 SCAR=0.0
925 16934 my $string = shift;
926  
927 #Get first two characters from the string
928 16934 my $first_two_char = substr $string, 0, 2;
929  
930 #Sort these two characters alphabetically
931 16934 my @sorted_char = sort { $a cmp $b } split $EMPTY_STRING, $first_two_char;
932  
933 #Concatenate and return the characters
934 16934 return join $EMPTY_STRING, @sorted_char;
935 }
936  
937 45635 ▶sub _sans_newlines{
Subroutine _sans_newlines: called CC=1 SCAR=0.0
938 45635 my $string = shift;
939 45635 chomp $string;
940  
941 45635 $string =~ s{ \r }{}xms;
942  
943 45635 my $string_sans_newlines = $string;
944  
945 45635 return $string_sans_newlines;
946 }
947  
948  
949 11 __PACKAGE__->meta->make_immutable;
950  
951 11 1; # End of Bio::SNP::Inherit
952  
953 __END__
↑ Summary