| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Kmer.pm: a kmer counting module | ||||||||||||||||||||||||||||
| 4 | # Author: Lee Katz <lkatz@cdc.gov> | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Bio::Kmer; | ||||||||||||||||||||||||||||
| 7 | 4 | require 5.10.0; | |||||||||||||||||||||||||||
| 8 | 4 | our $VERSION=0.55; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use List::Util qw/max/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use File::Basename qw/basename fileparse dirname/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use File::Temp qw/tempdir tempfile/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use File::Path qw/remove_tree/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use IO::Uncompress::Gunzip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use File::Which qw/which/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Carp qw/croak carp confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | our $iThreads; # boolean for whether threads are loaded | |||||||||||||||||||||||||||
| 23 | 4 | ▶ | BEGIN{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 24 | 4 | eval{ | |||||||||||||||||||||||||||
| 25 | 4 | require threads; | |||||||||||||||||||||||||||
| 26 | 0 | require threads::shared; | |||||||||||||||||||||||||||
| 27 | 0 | $iThreads = 1; | |||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | 4 | ▶ | if($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 4 | $iThreads = 0; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | our @fastqExt=qw(.fastq.gz .fastq .fq .fq.gz); | |||||||||||||||||||||||||||
| 39 | 4 | our @fastaExt=qw(.fasta .fna .faa .mfa .fas .fa); | |||||||||||||||||||||||||||
| 40 | 4 | our @bamExt=qw(.sorted.bam .bam); | |||||||||||||||||||||||||||
| 41 | 4 | our @vcfExt=qw(.vcf.gz .vcf); | |||||||||||||||||||||||||||
| 42 | 4 | our @richseqExt=qw(.gbk .gbf .gb .embl); | |||||||||||||||||||||||||||
| 43 | 4 | our @sffExt=qw(.sff); | |||||||||||||||||||||||||||
| 44 | 4 | our @samExt=qw(.sam .bam); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | ▶ | our $fhStick :shared; # Helps us open only one file at a time | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | our $enqueueStick :shared; # Helps control access to the kmer queue | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # TODO if 'die' is imported by a script, redefine | ||||||||||||||||||||||||||||
| 51 | # sig die in that script as this function. | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | local $SIG{'__DIE__'} = sub { my $e = $_[0]; $e =~ s/(at [^\s]+? line \d+\.$)/\nStopped $1/; die("$0: ".(caller(1))[3].": ".$e); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | my $startTime = time(); | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub logmsg{ | ||||||||||||||||||||||||||
|
Subroutine
logmsg: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | local $0 = basename $0; | |||||||||||||||||||||||||||
| 57 | 0 | my $tid = 0; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if($iThreads){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $tid = threads->tid; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | my $elapsedTime = time() - $startTime; | |||||||||||||||||||||||||||
| 62 | 0 | print STDERR "$0.$tid $elapsedTime @_\n"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =pod | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 NAME | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Bio::Kmer - Helper module for Kmer Analysis. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | A module for helping with kmer analysis. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | use strict; | ||||||||||||||||||||||||||||
| 76 | use warnings; | ||||||||||||||||||||||||||||
| 77 | use Bio::Kmer; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my $kmer=Bio::Kmer->new("file.fastq.gz",{kmercounter=>"jellyfish",numcpus=>4}); | ||||||||||||||||||||||||||||
| 80 | my $kmerHash=$kmer->kmers(); | ||||||||||||||||||||||||||||
| 81 | my $countOfCounts=$kmer->histogram(); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | my $minimizers = $kmer->minimizers(); | ||||||||||||||||||||||||||||
| 84 | my $minimizerCluster = $kmer->minimizerCluster(); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | The BioPerl way | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | use strict; | ||||||||||||||||||||||||||||
| 89 | use warnings; | ||||||||||||||||||||||||||||
| 90 | use Bio::SeqIO; | ||||||||||||||||||||||||||||
| 91 | use Bio::Kmer; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Load up any Bio::SeqIO object. Quality values will be | ||||||||||||||||||||||||||||
| 94 | # faked internally to help with compatibility even if | ||||||||||||||||||||||||||||
| 95 | # a fastq file is given. | ||||||||||||||||||||||||||||
| 96 | my $seqin = Bio::SeqIO->new(-file=>"input.fasta"); | ||||||||||||||||||||||||||||
| 97 | my $kmer=Bio::Kmer->new($seqin); | ||||||||||||||||||||||||||||
| 98 | my $kmerHash=$kmer->kmers(); | ||||||||||||||||||||||||||||
| 99 | my $countOfCounts=$kmer->histogram(); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | A module for helping with kmer analysis. The basic methods help count kmers and can produce a count of counts. Currently this module only supports fastq format. Although this module can count kmers with pure perl, it is recommended to give the option for a different kmer counter such as Jellyfish. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | * BioPerl | ||||||||||||||||||||||||||||
| 108 | * Jellyfish >=2 | ||||||||||||||||||||||||||||
| 109 | * Perl threads | ||||||||||||||||||||||||||||
| 110 | * Perl >=5.10 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 VARIABLES | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =over | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item $Bio::Kmer::iThreads | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Boolean describing whether the module instance is using threads | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =back | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 METHODS | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =over | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item Bio::Kmer->new($filename, \%options) | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Create a new instance of the kmer counter. One object per file. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Filename can be either a file path or a Bio::SeqIO object. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Applicable arguments for \%options: | ||||||||||||||||||||||||||||
| 133 | Argument Default Description | ||||||||||||||||||||||||||||
| 134 | kmercounter perl What kmer counter software to use. | ||||||||||||||||||||||||||||
| 135 | Choices: Perl, Jellyfish. | ||||||||||||||||||||||||||||
| 136 | kmerlength|k 21 Kmer length | ||||||||||||||||||||||||||||
| 137 | numcpus 1 This module uses perl | ||||||||||||||||||||||||||||
| 138 | multithreading with pure perl or | ||||||||||||||||||||||||||||
| 139 | can supply this option to other | ||||||||||||||||||||||||||||
| 140 | software like jellyfish. | ||||||||||||||||||||||||||||
| 141 | gt 1 If the count of kmers is fewer | ||||||||||||||||||||||||||||
| 142 | than this, ignore the kmer. This | ||||||||||||||||||||||||||||
| 143 | might help speed analysis if you | ||||||||||||||||||||||||||||
| 144 | do not care about low-count kmers. | ||||||||||||||||||||||||||||
| 145 | sample 1 Retain only a percentage of kmers. | ||||||||||||||||||||||||||||
| 146 | 1 is 100%; 0 is 0% | ||||||||||||||||||||||||||||
| 147 | Only works with the perl kmer counter. | ||||||||||||||||||||||||||||
| 148 | verbose 0 Print more messages. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Examples: | ||||||||||||||||||||||||||||
| 151 | my $kmer=Bio::Kmer->new("file.fastq.gz",{kmercounter=>"jellyfish",numcpus=>4}); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | ▶ | sub new{ | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=19 SCAR=43.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 1 | my($class,$seqfile,$settings)=@_; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | ▶ | croak "ERROR: need a sequence file or a Bio::SeqIO object" if(!$seqfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Set optional parameter defaults | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | $$settings{kmerlength} = $$settings{kmerlength} || $$settings{k} || 21; | ||||||||||||||||||||||||||
|
Condition40%2/5
$settings->{'kmerlength'} || $settings->{'k'} || 21
$settings->{"kmerlength"} or $settings->{"k"}
MC/DC0%0/2
$settings->{'kmerlength'} || $settings->{'k'} || 21
$settings->{"kmerlength"}
$settings->{"k"}
Truth table33%1/3
A or B
A= $settings->{"kmerlength"}B= $settings->{"k"}
| |||||||||||||||||||||||||||||
| 164 | 1 | ▶ | $$settings{numcpus} ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$settings->{"numcpus"} ||= 1
MC/DC0%0/1
$settings->{"numcpus"} ||= 1
$settings->{"numcpus"}
Truth table50%1/2
A
A= $settings->{"numcpus"}
| |||||||||||||||||||||||||||||
| 165 | 1 | ▶ | $$settings{gt} ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$settings->{"gt"} ||= 1
MC/DC0%0/1
$settings->{"gt"} ||= 1
$settings->{"gt"}
Truth table50%1/2
A
A= $settings->{"gt"}
| |||||||||||||||||||||||||||||
| 166 | 1 | ▶ | $$settings{kmercounter} ||= "perl"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$settings->{"kmercounter"} ||= "perl"
MC/DC0%0/1
$settings->{"kmercounter"} ||= "perl"
$settings->{"kmercounter"}
Truth table50%1/2
A
A= $settings->{"kmercounter"}
| |||||||||||||||||||||||||||||
| 167 | 1 | ▶ | $$settings{tempdir} ||= tempdir("Kmer.pm.XXXXXX",TMPDIR=>1,CLEANUP=>1); | ||||||||||||||||||||||||||
|
Condition33%1/3
$settings->{"tempdir"} ||= tempdir("Kmer.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
MC/DC0%0/2
$settings->{"tempdir"} ||= tempdir("Kmer.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
$settings->{"tempdir"}
tempdir("Kmer.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
Truth table33%1/3
A ||= B
A= $settings->{"tempdir"}B= tempdir("Kmer.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
| |||||||||||||||||||||||||||||
| 168 | 1 | ▶ | $$settings{sample} = 1 if(!defined($$settings{sample})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 1 | ▶ | $$settings{verbose} ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$settings->{"verbose"} ||= 0
MC/DC0%0/1
$settings->{"verbose"} ||= 0
$settings->{"verbose"}
Truth table50%1/2
A
A= $settings->{"verbose"}
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # If the first parameter $seqfile is a Bio::SeqIO object, | ||||||||||||||||||||||||||||
| 172 | # then send it to a file to dovetail with the rest of | ||||||||||||||||||||||||||||
| 173 | # this module. | ||||||||||||||||||||||||||||
| 174 | 1 | ▶ | if(ref($seqfile) && $seqfile->isa("Bio::SeqIO")){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $seqfile and $seqfile->isa("Bio::SeqIO")
MC/DC0%0/2
ref $seqfile and $seqfile->isa("Bio::SeqIO")
ref $seqfile
$seqfile->isa("Bio::SeqIO")
Truth table33%1/3
A and B
A= ref $seqfileB= $seqfile->isa("Bio::SeqIO")
| |||||||||||||||||||||||||||||
| 175 | # BioPerl isn't required at compile tile but is | ||||||||||||||||||||||||||||
| 176 | # required if the user starts with a BioPerl object. | ||||||||||||||||||||||||||||
| 177 | 0 | eval { | |||||||||||||||||||||||||||
| 178 | 0 | require Bio::SeqIO; | |||||||||||||||||||||||||||
| 179 | 0 | require Bio::Seq::Quality; | |||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | croak "ERROR: cannot load Bio::SeqIO and Bio::Seq::Quality, but I need to because you supplied a Bio::SeqIO object"; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | my $tempfile="$$settings{tempdir}/bioperl_input.fastq"; | |||||||||||||||||||||||||||
| 185 | 0 | my $out=Bio::SeqIO->new(-file=>">".$tempfile); | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | while(my $seq=$seqfile->next_seq){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my $qual = $seq->qual || "I" x $seq->length; | ||||||||||||||||||||||||||
|
Condition0%0/3
$seq->qual || 'I' x $seq->length
MC/DC0%0/2
$seq->qual || 'I' x $seq->length
$seq->qual
'I' x $seq->length
Truth table0%0/3
A || B
A= $seq->qualB= 'I' x $seq->length
| |||||||||||||||||||||||||||||
| 188 | 0 | my $seqWithQual = Bio::Seq::Quality->new( | |||||||||||||||||||||||||||
| 189 | # TODO preserve qual values if they exist, but | ||||||||||||||||||||||||||||
| 190 | # for now, it doesn't really matter. | ||||||||||||||||||||||||||||
| 191 | -qual=> $qual, | ||||||||||||||||||||||||||||
| 192 | -seq => $seq->seq, | ||||||||||||||||||||||||||||
| 193 | -id => $seq->id, | ||||||||||||||||||||||||||||
| 194 | -verbose => -1, | ||||||||||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||||||||||
| 196 | 0 | $out->write_seq($seqWithQual); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | $out->close; # make sure output is flushed | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # now redefine the seqfile as the new file on disk. | ||||||||||||||||||||||||||||
| 201 | 0 | $seqfile=$tempfile; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # Check if we have a valid sequence file path or at | ||||||||||||||||||||||||||||
| 205 | # the very least, double check that the file path we just | ||||||||||||||||||||||||||||
| 206 | # made from the Bio::SeqIO object is valid. | ||||||||||||||||||||||||||||
| 207 | 1 | ▶ | croak "ERROR: could not locate the sequence file $seqfile" if(!-e $seqfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # Initialize the object and then bless it | ||||||||||||||||||||||||||||
| 210 | my $self={ | ||||||||||||||||||||||||||||
| 211 | seqfile =>$seqfile, | ||||||||||||||||||||||||||||
| 212 | kmerlength =>$$settings{kmerlength}, | ||||||||||||||||||||||||||||
| 213 | numcpus =>$$settings{numcpus}, | ||||||||||||||||||||||||||||
| 214 | tempdir =>$$settings{tempdir}, | ||||||||||||||||||||||||||||
| 215 | gt =>$$settings{gt}, | ||||||||||||||||||||||||||||
| 216 | kmercounter=>$$settings{kmercounter}, | ||||||||||||||||||||||||||||
| 217 | sample =>$$settings{sample}, | ||||||||||||||||||||||||||||
| 218 | verbose =>$$settings{verbose}, | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # Jellyfish-specific | ||||||||||||||||||||||||||||
| 221 | 1 | jellyfish =>scalar(which("jellyfish")), | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # Values that will be filled in after analysis | ||||||||||||||||||||||||||||
| 224 | _kmers =>{}, # hash of kmer=>count | ||||||||||||||||||||||||||||
| 225 | _hist =>[], # histogram. 0th element is counts of kmers occuring no times, 1st is elements occuring exactly once, etc | ||||||||||||||||||||||||||||
| 226 | _ntcount =>0, # Total number of nucleotides | ||||||||||||||||||||||||||||
| 227 | }; | ||||||||||||||||||||||||||||
| 228 | # Add in some other temporary files | ||||||||||||||||||||||||||||
| 229 | #($$self{kmerfileFh},$$self{kmerfile}) = tempfile("KMER.XXXXXX", DIR=>$$self{tempdir}, SUFFIX=>".tsv"); | ||||||||||||||||||||||||||||
| 230 | 1 | ($$self{histfileFh},$$self{histfile}) = tempfile("HIST.XXXXXX", DIR=>$$self{tempdir}, SUFFIX=>".tsv"); | |||||||||||||||||||||||||||
| 231 | 1 | ($$self{jellyfishdbFh},$$self{jellyfishdb})= tempfile("JF.XXXXXX", DIR=>$$self{tempdir}, SUFFIX=>".jf"); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # Make some values lc | ||||||||||||||||||||||||||||
| 234 | 1 | $$self{$_}=lc($$self{$_}) for(qw(kmercounter)); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # Set an exact parameter for the kmer counter | ||||||||||||||||||||||||||||
| 237 | 1 | ▶ | if($$self{kmercounter}=~ /(pure)?.*perl/){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 238 | 1 | $$self{kmercounter}="perl"; | |||||||||||||||||||||||||||
| 239 | } elsif($self->{kmercounter} =~ /jellyfish/){ | ||||||||||||||||||||||||||||
| 240 | 0 | $$self{kmercounter}="jellyfish"; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 1 | bless($self); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 1 | $self->count; # start off the kmer counting ASAP | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | return $self; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =pod | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =over | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =item $kmer->ntcount() | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Returns the number of base pairs counted. | ||||||||||||||||||||||||||||
| 257 | In some cases such as when counting with Jellyfish, | ||||||||||||||||||||||||||||
| 258 | that number is not calculated; instead the length | ||||||||||||||||||||||||||||
| 259 | is calculated by the total length of kmers. | ||||||||||||||||||||||||||||
| 260 | Internally, this number is stored as $kmer->{_ntcount}. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | Note: internally runs $kmer->histogram() if | ||||||||||||||||||||||||||||
| 263 | $kmer->{_ntcount} is not initially found. | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | Arguments: None | ||||||||||||||||||||||||||||
| 266 | Returns: integer | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =back | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =cut | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | sub ntcount{ | ||||||||||||||||||||||||||
|
Subroutine
ntcount: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 273 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # Get the histogram just in case it's needed. | ||||||||||||||||||||||||||||
| 276 | # Do not wait until later to get the histogram | ||||||||||||||||||||||||||||
| 277 | # because I want to make sure this sub is | ||||||||||||||||||||||||||||
| 278 | # streamlined despite which underlying mechanism | ||||||||||||||||||||||||||||
| 279 | # is used. | ||||||||||||||||||||||||||||
| 280 | 0 | my $hist = $self->histogram(); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | # If the length was already calculated, return it | ||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if($$self{_ntcount}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | return $$self{_ntcount}; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # Approximate the length by just counting | ||||||||||||||||||||||||||||
| 288 | # how many unique kmers there are. | ||||||||||||||||||||||||||||
| 289 | 0 | my $length = 0; | |||||||||||||||||||||||||||
| 290 | 0 | $length += $_ for(@$hist); | |||||||||||||||||||||||||||
| 291 | 0 | $$self{_ntcount} = $length; | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | if($length < 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 0 | croak "ERROR: could not calculate total length for some reason (internal error)"; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 0 | return $length; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =pod | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =over | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =item $kmer->count() | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | Count kmers. This method is called as soon as new() is called | ||||||||||||||||||||||||||||
| 305 | and so you should never have to run this method. | ||||||||||||||||||||||||||||
| 306 | Internally caches the kmer counts to ram. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Arguments: None | ||||||||||||||||||||||||||||
| 309 | Returns: None | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =back | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 1 | ▶ | sub count{ | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=8 SCAR=34.8
Pod
documented
| |||||||||||||||||||||||||||||
| 316 | 1 | my($self)=@_; | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 1 | my $seqfile=$self->{seqfile}; | |||||||||||||||||||||||||||
| 319 | 1 | my $kmerlength=$self->{kmerlength}; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 1 | my $kmerHash={}; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 1 | ▶ | if($self->{kmercounter} eq "perl"){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 324 | 1 | ▶ | if(!$iThreads){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 1 | croak("Warning: perl iThreads are not enabled in this version of perl, but you requested iThreads when running ". ref($self)); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 0 | $self->countKmersPurePerl($seqfile,$kmerlength); | |||||||||||||||||||||||||||
| 328 | } elsif($self->{kmercounter} eq "jellyfish"){ | ||||||||||||||||||||||||||||
| 329 | # TODO make JF DB file $self->{jellyfishDb} and do not return | ||||||||||||||||||||||||||||
| 330 | # a kmer count | ||||||||||||||||||||||||||||
| 331 | 0 | $self->countKmersJellyfish($seqfile,$kmerlength); | |||||||||||||||||||||||||||
| 332 | } else { | ||||||||||||||||||||||||||||
| 333 | 0 | croak "ERROR: I do not understand the kmer counter $self->{kmercounter}"; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if($$self{sample} < 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 0 | my $sample = $$self{sample}; | |||||||||||||||||||||||||||
| 339 | 0 | my $kmers = $self->kmers(); | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | my %subsampledKmers; | |||||||||||||||||||||||||||
| 342 | 0 | for my $kmer(keys(%$kmers)){ | |||||||||||||||||||||||||||
| 343 | 0 | my $count = $$kmers{$kmer}; | |||||||||||||||||||||||||||
| 344 | 0 | ▶ | for(my $i=0;$i<$count;$i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if($sample >= rand(1)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 0 | $subsampledKmers{$kmer}++; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 0 | $self->clearCache(); | |||||||||||||||||||||||||||
| 352 | 0 | $$self{_kmers} = \%subsampledKmers; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =pod | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =over | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =item $kmer->clearCache | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Clears kmer counts and histogram counts. You should probably never use | ||||||||||||||||||||||||||||
| 363 | this method. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Arguments: None | ||||||||||||||||||||||||||||
| 366 | Returns: None | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =back | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =cut | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub clearCache{ | ||||||||||||||||||||||||||
|
Subroutine
clearCache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | $$self{_kmers} = {}; | |||||||||||||||||||||||||||
| 376 | 0 | $$self{_hist} = []; | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =pod | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =over | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =item $kmer->query($queryString) | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | Query the set of kmers with your own query | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Arguments: query (string) | ||||||||||||||||||||||||||||
| 388 | Returns: Count of kmers. | ||||||||||||||||||||||||||||
| 389 | 0 indicates that the kmer was not found. | ||||||||||||||||||||||||||||
| 390 | -1 indicates an invalid kmer (e.g., invalid length) | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =back | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =cut | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | ▶ | sub query{ | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 397 | 0 | my($self,$query)=@_; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 0 | ▶ | if(length($query) != $self->{kmerlength}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 0 | return -1; | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 0 | my $count=0; | |||||||||||||||||||||||||||
| 404 | 0 | ▶ | if($self->{kmercounter} eq "perl"){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 405 | 0 | my $kmers=$self->kmers(); | |||||||||||||||||||||||||||
| 406 | 0 | ▶ | $count=$$kmers{uc($query)} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$$kmers{uc $query} || 0
MC/DC0%0/1
$$kmers{uc $query} || 0
$$kmers{uc $query}
Truth table0%0/2
A
A= $$kmers{uc $query}
| |||||||||||||||||||||||||||||
| 407 | } elsif($self->{kmercounter} eq "jellyfish"){ | ||||||||||||||||||||||||||||
| 408 | 0 | ▶ | open(my $queryFh, "$$self{jellyfish} query ".$self->{jellyfishdb}." |") or croak "ERROR: could not run jellyfish query: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | 0 | my $db=$self->{jellyfishdb}; | |||||||||||||||||||||||||||
| 410 | 0 | my $tmp=`jellyfish query $db $query`; | |||||||||||||||||||||||||||
| 411 | 0 | ▶ | croak "ERROR: could not run jellyfish query" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | 0 | chomp($tmp); | |||||||||||||||||||||||||||
| 413 | 0 | (undef,$count)=split(/\s+/,$tmp); | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | return $count; | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =pod | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =over | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =item $kmer->histogram() | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Count the frequency of kmers. Internally caches the histogram to ram. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | Arguments: none | ||||||||||||||||||||||||||||
| 428 | Returns: Reference to an array of counts. The index of | ||||||||||||||||||||||||||||
| 429 | the array is the frequency. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =back | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =cut | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 0 | ▶ | sub histogram{ | ||||||||||||||||||||||||||
|
Subroutine
histogram: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 436 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if(@{ $$self{_hist} } > 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 0 | return $$self{_hist}; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | if($self->{kmercounter} eq "jellyfish"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | 0 | return $self->histogramJellyfish(); | |||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||
| 445 | 0 | return $self->histogramPerl(); | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 0 | ▶ | sub histogramJellyfish{ | ||||||||||||||||||||||||||
|
Subroutine
histogramJellyfish: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 450 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 0 | close $self->{histfileFh}; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # Run jellyfish histo | ||||||||||||||||||||||||||||
| 455 | 0 | my $jellyfishXopts = join(" ","-t", $self->{numcpus}, "-o", $self->{histfile}, $self->{jellyfishdb}); | |||||||||||||||||||||||||||
| 456 | 0 | system("$$self{jellyfish} histo $jellyfishXopts"); | |||||||||||||||||||||||||||
| 457 | 0 | ▶ | croak "ERROR with jellyfish histo" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | # Read the output file | ||||||||||||||||||||||||||||
| 460 | 0 | my @hist=(0); | |||||||||||||||||||||||||||
| 461 | 0 | ▶ | open(my $fh, $self->{histfile}) or croak "ERROR: reading $self->{histfile}: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 462 | 0 | ▶ | while(<$fh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 0 | s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 464 | 0 | my($count, $countOfCounts)=split /\s+/; | |||||||||||||||||||||||||||
| 465 | 0 | $hist[$count]=$countOfCounts; | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | 0 | close $fh; | |||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | # Fill in gaps in the histogram | ||||||||||||||||||||||||||||
| 470 | 0 | for(@hist){ | |||||||||||||||||||||||||||
| 471 | 0 | ▶ | $_||=0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_ ||= 0
MC/DC0%0/1
$_ ||= 0
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 0 | $self->{_hist} = \@hist; | |||||||||||||||||||||||||||
| 475 | 0 | return \@hist; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 0 | ▶ | sub histogramPerl{ | ||||||||||||||||||||||||||
|
Subroutine
histogramPerl: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 479 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 480 | 0 | my %hist=(); | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 0 | my @hist=(0); # initialize the histogram with a count of zero kmers happening zero times | |||||||||||||||||||||||||||
| 483 | #$hist[0]=4**$self->{kmerlength}; # or maybe it should be initialized to the total number of possibilities. | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | for my $kmercount(values(%{ $self->kmers() } )){ | |||||||||||||||||||||||||||
| 486 | 0 | $hist{$kmercount}++; | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | # Turn this hash into an array | ||||||||||||||||||||||||||||
| 490 | 0 | for(1..max(keys(%hist))){ | |||||||||||||||||||||||||||
| 491 | 0 | ▶ | $hist[$_] = $hist{$_} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$hist{$_} || 0
MC/DC0%0/1
$hist{$_} || 0
$hist{$_}
Truth table0%0/2
A
A= $hist{$_}
| |||||||||||||||||||||||||||||
| 492 | #$hist[0]=$hist[0] - $hist[$_]; # subtract off from the total space of possible kmers | ||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 0 | $self->{_hist}=\@hist; | |||||||||||||||||||||||||||
| 496 | 0 | return \@hist; | |||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | # Pure perl to make this standalone... the only reason | ||||||||||||||||||||||||||||
| 500 | # we are counting kmers in Perl instead of C. | ||||||||||||||||||||||||||||
| 501 | 0 | ▶ | sub countKmersPurePerl{ | ||||||||||||||||||||||||||
|
Subroutine
countKmersPurePerl: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 502 | 0 | my($self,$seqfile,$kmerlength)=@_; | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 0 | my @allSeqs; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | # Record the total number of nucleotides | ||||||||||||||||||||||||||||
| 507 | 0 | my $ntCount = 0; | |||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | # Save all seqs to an array, for passing out to individual threads. | ||||||||||||||||||||||||||||
| 510 | 0 | my $fastqFh=$self->openFastq($seqfile); | |||||||||||||||||||||||||||
| 511 | 0 | my $i=0; | |||||||||||||||||||||||||||
| 512 | 0 | my @buffer=(); | |||||||||||||||||||||||||||
| 513 | 0 | ▶ | while(<$fastqFh>){ # burn the read ID line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 0 | $i++; | |||||||||||||||||||||||||||
| 515 | 0 | my $seq=<$fastqFh>; | |||||||||||||||||||||||||||
| 516 | 0 | chomp($seq); | |||||||||||||||||||||||||||
| 517 | 0 | push(@allSeqs, uc($seq)); | |||||||||||||||||||||||||||
| 518 | 0 | $ntCount += length($seq); | |||||||||||||||||||||||||||
| 519 | # Burn the quality score lines | ||||||||||||||||||||||||||||
| 520 | 0 | <$fastqFh>; | |||||||||||||||||||||||||||
| 521 | 0 | <$fastqFh>; | |||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | 0 | close $fastqFh; | |||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 0 | $$self{_ntcount}=$ntCount; | |||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | # The number of sequences per thread is divided evenly but cautions | ||||||||||||||||||||||||||||
| 528 | # toward having one extra sequence per thread in the first threads | ||||||||||||||||||||||||||||
| 529 | # rather than accidentally leaving some off at the end. | ||||||||||||||||||||||||||||
| 530 | 0 | my $numSeqsPerThread = int(scalar(@allSeqs)/$self->{numcpus}) + 1; | |||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | # Multithreading | ||||||||||||||||||||||||||||
| 533 | 0 | my @thr; | |||||||||||||||||||||||||||
| 534 | 0 | for(0..$self->{numcpus}-1){ | |||||||||||||||||||||||||||
| 535 | # Get a set of sequences to kmerize | ||||||||||||||||||||||||||||
| 536 | 0 | my @threadSeqs = splice(@allSeqs, 0, $numSeqsPerThread); | |||||||||||||||||||||||||||
| 537 | # Set up a place for kmers to land | ||||||||||||||||||||||||||||
| 538 | 0 | $thr[$_]=threads->new(\&_countKmersPurePerlWorker,$kmerlength,\@threadSeqs,$self->{sample}); | |||||||||||||||||||||||||||
| 539 | #logmsg "Kicking off thread ".$thr[$_]->tid." with ".scalar(@threadSeqs)." sequences"; | ||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | # Join the threads and put everything into a large kmer hash | ||||||||||||||||||||||||||||
| 543 | 0 | my %kmer; | |||||||||||||||||||||||||||
| 544 | 0 | for(@thr){ | |||||||||||||||||||||||||||
| 545 | 0 | my $kmerArr = $_->join; | |||||||||||||||||||||||||||
| 546 | 0 | for my $kmer(@$kmerArr){ | |||||||||||||||||||||||||||
| 547 | 0 | $kmer{$kmer}++; | |||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 0 | my($fh, $filename) = tempfile("KMER.XXXXXX", DIR=>$$self{tempdir}, SUFFIX=>".tsv"); | |||||||||||||||||||||||||||
| 552 | 0 | ($$self{kmerfileFh},$$self{kmerfile}) = ($fh, $filename); | |||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | # Write everything to file. The FH should still be open. | ||||||||||||||||||||||||||||
| 555 | # Do not return the kmer. | ||||||||||||||||||||||||||||
| 556 | # Make a new method that returns the kmer hash | ||||||||||||||||||||||||||||
| 557 | # Do the same for jellyfish | ||||||||||||||||||||||||||||
| 558 | 0 | ▶ | while(my($kmer,$count)=each(%kmer)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | # Filtering step | ||||||||||||||||||||||||||||
| 560 | 0 | ▶ | if($count < $self->{gt}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | #delete($kmer{$kmer}); | ||||||||||||||||||||||||||||
| 562 | 0 | next; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 0 | print $fh "$kmer\t$count\n"; | |||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | 0 | close $fh; | |||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | 0 | return 1; | |||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =pod | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | =over | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =item $kmer->kmers | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | Return actual kmers | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | Arguments: None | ||||||||||||||||||||||||||||
| 581 | Returns: Reference to a hash of kmers and their counts | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | =back | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | =cut | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 0 | ▶ | sub kmers{ | ||||||||||||||||||||||||||
|
Subroutine
kmers: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 589 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | # Look for the cached results before trying to read the file. | ||||||||||||||||||||||||||||
| 592 | 0 | ▶ | return $self->{_kmers} if(keys(%{ $self->{_kmers} }) > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | # Dump the kmers to a tab delimited file if we are using | ||||||||||||||||||||||||||||
| 595 | # jellyfish and the user invokes this method. | ||||||||||||||||||||||||||||
| 596 | 0 | ▶ | if($self->{kmercounter} eq "jellyfish"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 597 | 0 | $self->_dumpKmersJellyfish(); | |||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 0 | my %kmer; | |||||||||||||||||||||||||||
| 601 | 0 | ▶ | open(my $fh, $self->{kmerfile}) or croak "ERROR: could not read the kmer file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 602 | 0 | ▶ | while(<$fh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 603 | 0 | chomp; | |||||||||||||||||||||||||||
| 604 | 0 | my($kmer,$count)=split /\t/; | |||||||||||||||||||||||||||
| 605 | 0 | $kmer{$kmer}=$count; | |||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | 0 | close $fh; | |||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | 0 | $self->{_kmers}=\%kmer; | |||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | 0 | return \%kmer; | |||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | =pod | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =over | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =item $kmer->minimizers(5) | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | Finds minimizer of each kmer | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | Arguments: length of minimizer (default: 5) | ||||||||||||||||||||||||||||
| 623 | returns: hash ref, e.g., $hash = {AAAAA=>AAA, TAGGGT=>AGG,...} | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | =back | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | =cut | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 0 | ▶ | sub minimizers{ | ||||||||||||||||||||||||||
|
Subroutine
minimizers: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 630 | 0 | my($self, $l) = @_; | |||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 0 | ▶ | if($self->{minimizer}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 633 | 0 | return $self->{minimizer}; | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 0 | ▶ | $l //= 5; | ||||||||||||||||||||||||||
|
Condition0%0/2
$l //= 5
MC/DC0%0/1
$l //= 5
$l
Truth table0%0/2
A
A= $l
| |||||||||||||||||||||||||||||
| 637 | 0 | $$self{l} = $l; | |||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | 0 | my %minimizer; | |||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 0 | my $numLmersPerKmer = $$self{kmerlength}-$l+1; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 0 | for my $kmer(keys(%{ $self->kmers})){ | |||||||||||||||||||||||||||
| 644 | # Start findig the minimizer by seeding it with the first l-mer | ||||||||||||||||||||||||||||
| 645 | 0 | my $minimizer = substr($kmer, 0, $l); | |||||||||||||||||||||||||||
| 646 | 0 | ▶ | for(my $i=1; $i<$numLmersPerKmer; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 647 | 0 | my $candidate = substr($kmer, $i, $l); | |||||||||||||||||||||||||||
| 648 | 0 | ▶ | if($candidate lt $minimizer){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 649 | 0 | $minimizer = $candidate; | |||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | 0 | $minimizer{$kmer} = $minimizer; | |||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | 0 | $self->{minimizer} = \%minimizer; | |||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | 0 | return \%minimizer; | |||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =pod | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =over | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | =item $kmer->minimizerCluster(5) | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | Finds minimizer of each kmer | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | Arguments: length of minimizer (default: 5). | ||||||||||||||||||||||||||||
| 669 | Internally, calls $kmer->minimizer($l) | ||||||||||||||||||||||||||||
| 670 | If $kmer->minimizer has already been called, this parameter will be ignored. | ||||||||||||||||||||||||||||
| 671 | returns: hash ref, e.g., $hash = {AAA=>[TAAAT, AAAGG,...], ATT=>[GATTC,...]}} | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | =back | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | =cut | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | 0 | ▶ | sub minimizerCluster{ | ||||||||||||||||||||||||||
|
Subroutine
minimizerCluster: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 678 | 0 | my($self, $l) = @_; | |||||||||||||||||||||||||||
| 679 | 0 | ▶ | if($$self{l}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 680 | 0 | $l = $$self{l}; | |||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | 0 | my %cluster; | |||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | 0 | my $minimizer = $self->minimizers($l); | |||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | 0 | for my $kmer(keys(%$minimizer)){ | |||||||||||||||||||||||||||
| 688 | 0 | push(@{ $cluster{$$minimizer{$kmer}} }, $kmer); | |||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||
| 690 | 0 | return \%cluster; | |||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | =pod | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | =over | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | =item $kmer->union($kmer2) | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | Finds the union between two sets of kmers | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | Arguments: Another Bio::Kmer object | ||||||||||||||||||||||||||||
| 702 | Returns: List of kmers | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | =back | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | =cut | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | 0 | ▶ | sub union{ | ||||||||||||||||||||||||||
|
Subroutine
union: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 709 | 0 | my($self,$other)=@_; | |||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | 0 | ▶ | if(!$self->_checkCompatibility($other)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 712 | 0 | croak "ERROR: two objects are not compatible in a union: ".ref($self)." / ".ref($other); | |||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | # See what kmers are in common using hashes | ||||||||||||||||||||||||||||
| 716 | 0 | my %union; | |||||||||||||||||||||||||||
| 717 | 0 | my $kmer1 = $self->kmers; | |||||||||||||||||||||||||||
| 718 | 0 | my $kmer2 = $other->kmers; | |||||||||||||||||||||||||||
| 719 | 0 | for my $kmer(keys(%{ $self->kmers }), keys(%{ $other->kmers })){ | |||||||||||||||||||||||||||
| 720 | 0 | $union{$kmer}=1; | |||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | 0 | return [keys(%union)]; | |||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | =pod | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | =over | ||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | =item $kmer->intersection($kmer2) | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | Finds the intersection between two sets of kmers | ||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||
| 734 | Arguments: Another Bio::Kmer object | ||||||||||||||||||||||||||||
| 735 | Returns: List of kmers | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | =back | ||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||
| 739 | =cut | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | 0 | ▶ | sub intersection{ | ||||||||||||||||||||||||||
|
Subroutine
intersection: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 742 | 0 | my($self,$other)=@_; | |||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | 0 | ▶ | if(!$self->_checkCompatibility($other)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 745 | 0 | croak "ERROR: two objects are not compatible in an intersection: ".ref($self)." / ".ref($other); | |||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||
| 748 | 0 | my @intersection; | |||||||||||||||||||||||||||
| 749 | 0 | my $kmer2 = $other->kmers; | |||||||||||||||||||||||||||
| 750 | 0 | for my $kmer(keys(%{ $self->kmers })){ | |||||||||||||||||||||||||||
| 751 | 0 | ▶ | if($$kmer2{$kmer}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 752 | 0 | push(@intersection, $kmer); | |||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | 0 | return \@intersection; | |||||||||||||||||||||||||||
| 757 | } | ||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | =pod | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | =over | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | =item $kmer->subtract($kmer2) | ||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||
| 765 | Finds the set of kmers unique to this Bio::Kmer object. | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | Arguments: Another Bio::Kmer object | ||||||||||||||||||||||||||||
| 768 | Returns: List of kmers | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | =back | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | =cut | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | 0 | ▶ | sub subtract{ | ||||||||||||||||||||||||||
|
Subroutine
subtract: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 775 | 0 | my($self,$other)=@_; | |||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||
| 777 | 0 | ▶ | if(!$self->_checkCompatibility($other)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 778 | 0 | croak "ERROR: trying to subtract two incompatible ".ref($self)." objects."; | |||||||||||||||||||||||||||
| 779 | } | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | 0 | my %subtractKmers = %{ $self->kmers }; | |||||||||||||||||||||||||||
| 782 | 0 | for my $kmer(keys(%{ $other->kmers })){ | |||||||||||||||||||||||||||
| 783 | 0 | delete($subtractKmers{$kmer}); | |||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | 0 | return [keys(%subtractKmers)]; | |||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||
| 791 | # See if another Bio::Kmer is the same kind as this one. | ||||||||||||||||||||||||||||
| 792 | # Return Boolean | ||||||||||||||||||||||||||||
| 793 | 0 | ▶ | sub _checkCompatibility{ | ||||||||||||||||||||||||||
|
Subroutine
_checkCompatibility: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 794 | 0 | my($self,$other)=@_; | |||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | 0 | ▶ | if($self->{kmerlength} != $other->{kmerlength}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 797 | 0 | ▶ | carp "WARNING: kmer lengths do not match\n" if($self->{verbose}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 798 | 0 | return 0; | |||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | 0 | return 1; | |||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||
| 804 | 0 | ▶ | sub _countKmersPurePerlWorker{ | ||||||||||||||||||||||||||
|
Subroutine
_countKmersPurePerlWorker: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 805 | 0 | my($kmerlength,$seqArr,$sample)=@_; | |||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | 0 | my $outKmerArray=[]; | |||||||||||||||||||||||||||
| 808 | 0 | for my $seq(@$seqArr){ | |||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||
| 810 | 0 | my $numKmersInRead=length($seq)-$kmerlength; | |||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||
| 812 | # Count kmers in a sliding window. | ||||||||||||||||||||||||||||
| 813 | # We must keep this loop optimized for speed. | ||||||||||||||||||||||||||||
| 814 | 0 | ▶ | for(my $j=0;$j<$numKmersInRead;$j++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 815 | #next if($sample < rand(1)); # subsample | ||||||||||||||||||||||||||||
| 816 | #$kmer{substr($seq,$j,$kmerlength)}++; | ||||||||||||||||||||||||||||
| 817 | 0 | push(@$outKmerArray, substr($seq,$j,$kmerlength)); | |||||||||||||||||||||||||||
| 818 | } | ||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||
| 820 | } | ||||||||||||||||||||||||||||
| 821 | 0 | return $outKmerArray; | |||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||
| 825 | 0 | ▶ | sub countKmersJellyfish{ | ||||||||||||||||||||||||||
|
Subroutine
countKmersJellyfish: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 826 | 0 | my($self,$seqfile,$kmerlength)=@_; | |||||||||||||||||||||||||||
| 827 | 0 | my $basename=basename($seqfile); | |||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | # Version checking | ||||||||||||||||||||||||||||
| 830 | 0 | my $jfVersion=`jellyfish --version`; chomp($jfVersion); | |||||||||||||||||||||||||||
| 831 | # e.g., jellyfish 2.2.6 | ||||||||||||||||||||||||||||
| 832 | 0 | ▶ | if($jfVersion =~ /(jellyfish\s+)?(\d+)?/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 833 | 0 | my $majorVersion=$2; | |||||||||||||||||||||||||||
| 834 | 0 | ▶ | if($majorVersion < 2){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 835 | 0 | croak "ERROR: Jellyfish v2 or greater is required for ".ref($self); | |||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||
| 837 | } | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | 0 | my $outfile=$self->{jellyfishdb}; | |||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||
| 841 | # Counting | ||||||||||||||||||||||||||||
| 842 | 0 | my $jellyfishCountOptions="-s 10000000 -m $kmerlength -o $outfile -t $self->{numcpus}"; | |||||||||||||||||||||||||||
| 843 | 0 | my $uncompressedFastq="$self->{tempdir}/$basename.fastq"; | |||||||||||||||||||||||||||
| 844 | 0 | ▶ | if($seqfile=~/\.gz$/i){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 845 | 0 | my $inFh = $self->openFastq($seqfile); | |||||||||||||||||||||||||||
| 846 | 0 | ▶ | open(my $outFh, ">", $uncompressedFastq) or croak "ERROR: could not write decompressed fastq to $uncompressedFastq: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 847 | 0 | ▶ | while(<$inFh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 848 | 0 | print $outFh $_; | |||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||
| 850 | 0 | close $outFh; | |||||||||||||||||||||||||||
| 851 | 0 | close $inFh; | |||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||
| 853 | 0 | system("$$self{jellyfish} count $jellyfishCountOptions \Q$uncompressedFastq\E"); | |||||||||||||||||||||||||||
| 854 | } else { | ||||||||||||||||||||||||||||
| 855 | 0 | system("$$self{jellyfish} count $jellyfishCountOptions \Q$seqfile\E"); | |||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||
| 857 | 0 | close $self->{jellyfishdbFh}; | |||||||||||||||||||||||||||
| 858 | 0 | ▶ | croak "Error: problem with jellyfish" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 859 | } | ||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||
| 861 | 0 | ▶ | sub _dumpKmersJellyfish{ | ||||||||||||||||||||||||||
|
Subroutine
_dumpKmersJellyfish: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 862 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 863 | 0 | my $kmerTsv=$self->{kmerfile}; | |||||||||||||||||||||||||||
| 864 | 0 | my $jfDb=$self->{jellyfishdb}; | |||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | 0 | ▶ | return if(-s $kmerTsv > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | # Dump the kmers to a tab-delimited file if it doesn't | ||||||||||||||||||||||||||||
| 869 | # already have contents | ||||||||||||||||||||||||||||
| 870 | 0 | ▶ | if(-s $kmerTsv < 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 871 | 0 | my $lowerCount=$self->{gt}-1; | |||||||||||||||||||||||||||
| 872 | 0 | system("$$self{jellyfish} dump --lower-count=$lowerCount --column --tab -o \Q$kmerTsv\E \Q$jfDb\E"); | |||||||||||||||||||||||||||
| 873 | 0 | ▶ | croak "ERROR dumping kmers from jellyfish database $jfDb" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 874 | } | ||||||||||||||||||||||||||||
| 875 | } | ||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||
| 877 | # Opens a fastq file in a thread-safe way. | ||||||||||||||||||||||||||||
| 878 | # Returns a file handle. | ||||||||||||||||||||||||||||
| 879 | 0 | ▶ | sub openFastq{ | ||||||||||||||||||||||||||
|
Subroutine
openFastq: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 880 | 0 | my($self,$fastq)=@_; | |||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||
| 882 | 0 | my $fh; | |||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||
| 884 | 0 | my($name,$dir,$ext)=fileparse($fastq,@fastqExt); | |||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||
| 886 | 0 | ▶ | if(!grep(/$ext/,@fastqExt)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 887 | 0 | croak "ERROR: could not read $fastq as a fastq file"; | |||||||||||||||||||||||||||
| 888 | } | ||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||
| 890 | # Open the file in different ways, depending on if it | ||||||||||||||||||||||||||||
| 891 | # is gzipped or if the user has gzip installed. | ||||||||||||||||||||||||||||
| 892 | 0 | lock($fhStick); | |||||||||||||||||||||||||||
| 893 | 0 | ▶ | if($ext =~/\.gz$/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 894 | # use binary gzip if we can... why not take advantage | ||||||||||||||||||||||||||||
| 895 | # of the compiled binary's speedup? | ||||||||||||||||||||||||||||
| 896 | 0 | my $gzip = which('gzip'); | |||||||||||||||||||||||||||
| 897 | 0 | ▶ | if(-e $gzip){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 898 | 0 | ▶ | open($fh,"$gzip -cd \Q$fastq\E | ") or croak "ERROR: could not open $fastq for reading!: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 899 | }else{ | ||||||||||||||||||||||||||||
| 900 | 0 | ▶ | $fh=new IO::Uncompress::Gunzip($fastq) or croak "ERROR: could not read $fastq using native perl module IO::Uncompress::Gunzip: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||
| 902 | } else { | ||||||||||||||||||||||||||||
| 903 | 0 | ▶ | open($fh,"<",$fastq) or croak "ERROR: could not open $fastq for reading!: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | 0 | return $fh; | |||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||
| 908 | # In case I accidentally do $Kmer->closeFastq without thinking | ||||||||||||||||||||||||||||
| 909 | # how ridiculous that is, let's just avoid that problem with | ||||||||||||||||||||||||||||
| 910 | # this subroutine. | ||||||||||||||||||||||||||||
| 911 | 0 | ▶ | sub closeFastq{ | ||||||||||||||||||||||||||
|
Subroutine
closeFastq: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 912 | 0 | my($self,$fastq)=@_; | |||||||||||||||||||||||||||
| 913 | 0 | close $fastq; | |||||||||||||||||||||||||||
| 914 | 0 | return 1; | |||||||||||||||||||||||||||
| 915 | } | ||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | =pod | ||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||
| 919 | =over | ||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||
| 921 | =item $kmer->close() | ||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||
| 923 | Cleans the temporary directory and removes this object from | ||||||||||||||||||||||||||||
| 924 | RAM. Good for when you might be counting kmers for many | ||||||||||||||||||||||||||||
| 925 | things but want to keep your overhead low. | ||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||
| 927 | Arguments: None | ||||||||||||||||||||||||||||
| 928 | Returns: 1 | ||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||
| 930 | =back | ||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||
| 932 | =cut | ||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||
| 934 | 0 | ▶ | sub close{ | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 935 | 0 | my($self)=@_; | |||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||
| 937 | 0 | remove_tree($self->{tempdir}); | |||||||||||||||||||||||||||
| 938 | 0 | undef($self); | |||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||
| 940 | 0 | return 1; | |||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||
| 943 | =pod | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | MIT license. Go nuts. | ||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||
| 949 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||
| 951 | Author: Lee Katz <lkatz@cdc.gov> | ||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||
| 953 | For additional help, go to https://github.com/lskatz/Bio--Kmer | ||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||
| 955 | CPAN module at http://search.cpan.org/~lskatz/Bio-Kmer/ | ||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||
| 957 | =cut | ||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||
| 959 | 4 | 1; | |||||||||||||||||||||||||||