lib/File/Generator.pm

filter: Statementstmt 70.2% 59 / 84 Branchbran 50.0% 12 / 24 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 63.6% 84 / 132 CC 16 SCAR 25.7
CC
16
Cov
63%
_generateFasta
42.0
_generateFastq
8.0
generate
4.1
CRAP 13.1
↑ Summary
1 #!/usr/bin/env perl
2  
3 # Generator.pm:
4 # Author: Lee Katz <lkatz@cdc.gov>
5  
6 package File::Generator;
7 2 require 5.12.0;
8 2 our $VERSION=0.2;
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use File::Basename qw/basename fileparse dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Temp qw/tempdir tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 our @EXPORT_OK = qw(
19 );
20  
21 # TODO if 'die' is imported by a script, redefine
22 # sig die in that script as this function.
23 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
24  
25 =pod
26  
27 =head1 NAME
28  
29 File::Generator
30  
31 =head1 SYNOPSIS
32  
33 A module for exporting test files
34  
35 use strict;
36 use warnings;
37 use File::Generator
38
39 my $generator = File::Generator->new();
40 my $fastqFile = $generator->generate("fastq");
41 my $fastqFile2= $generator->generate("fastq");
42 my $largeFastq= $generator->generate("fastq",{maxbytes=>10000});
43  
44 =head1 DESCRIPTION
45  
46 Generate random test files.
47  
48 =pod
49  
50 =head1 METHODS
51  
52 =over
53  
54 =item File::Generator->new(\%options)
55  
56 Create a new instance of the file generator with the following options
57  
58 Applicable arguments:
59 Argument Default Description
60 tempdir A temporary directory. If not supplied,
61 one will be created for you in $TMPDIR
62  
63 =back
64  
65 =cut
66  
67 1 ▶sub new{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
68 1 my($class,$settings)=@_;
69  
70 # Set optional parameter defaults
71 1 ▶ $$settings{tempdir} ||=tempdir("Generator.pm.XXXXXX",TMPDIR=>1,CLEANUP=>1);
Condition33%1/3
$settings->{"tempdir"} ||= tempdir("Generator.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
l!l&&r!l&&!r
010
MC/DC0%0/2
$settings->{"tempdir"} ||= tempdir("Generator.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
$settings->{"tempdir"} tempdir("Generator.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
Truth table33%1/3
A ||= B
A= $settings->{"tempdir"}B= tempdir("Generator.pm.XXXXXX", "TMPDIR", 1, "CLEANUP", 1)
ABresult
1X1
011
000
72  
73 # Initialize the object and then bless it
74 my $self={
75 tempdir => $$settings{tempdir},
76 1 _fileCounter => 0, # how many files this generator has made
77 };
78  
79 1 bless($self);
80  
81 1 return $self;
82 }
83  
84  
85 =pod
86  
87 =over
88  
89 =item $generator->generate($fileType,\%options)
90  
91 Generate a type of file
92  
93 Arguments: $fileType (string) - a type of file to generate
94 Available types: "fastq", "fasta"
95 %options
96 maxbytes - up to how many bytes of the output
97 you want to make.
98  
99 Returns: Path to a file (string)
100  
101 =back
102  
103 =cut
104  
105 5 ▶sub generate{
Subroutine generate: called CC=3 SCAR=14.2
Pod documented
106 5 my($self,$type,$settings)=@_;
107  
108 5 $type=uc($type);
109  
110 5 ▶ if($type eq "FASTQ"){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'FASTQ') { } 5 0 5
elsif ($type eq 'FASTA') { } 0 0 0
111 5 return $self->_generateFastq($settings);
112 } elsif($type eq "FASTA"){
113 0 return $self->_generateFasta($settings);
114 } else {
115 0 die "ERROR: I do not understand type $type";
116 }
117 }
118  
119 5 ▶sub _generateFastq{
Subroutine _generateFastq: called CC=8 SCAR=20.8
120 5 my($self,$settings)=@_;
121 5 ▶ $$settings{maxbytes} ||= 1000;
Condition50%1/2
$settings->{"maxbytes"} ||= 1000
l!l
05
MC/DC0%0/1
$settings->{"maxbytes"} ||= 1000
$settings->{"maxbytes"}
Truth table50%1/2
A
A= $settings->{"maxbytes"}
Aresult
11
00
122  
123 5 my @NT=qw(A C G T);
124  
125 5 $self->{_fileCounter}++;
126 5 my $currentQual=ord("A");
127 5 my $maxQual = ord("I");
128
129 5 my $filename = $self->{tempdir}."/file.".$self->{_fileCounter}.".fastq";
130 5 ▶ open(my $fh, ">", $filename) or die "ERROR: could not write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "ERROR: could not write to ${filename}: $!" 5 0 5
131 5 my $readCounter=0;
132 5 my $numBytes=0;
133 5 ▶ while($numBytes < $$settings{maxbytes}){
Branch: 1 branch
BranchTrueFalseTotal
if ($numBytes < $settings->{"maxbytes"}) 20 5 25
134 #print "$numBytes $$settings{maxbytes}\n";
135 20 $readCounter++;
136 20 my $entry="\@$readCounter\n";
137 20 ▶ for(my $i=0;$i<150;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 150) 3000 20 3020
138 3000 $entry.=$NT[rand(4)];
139 }
140 20 $entry.="\n+\n";
141 20 ▶ for(my $i=0;$i<150;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 150) 3000 20 3020
142 3000 my $nextQual = $currentQual + int(rand(3)) - 1;
143 3000 ▶ $nextQual = $maxQual if($nextQual > $maxQual);
Branch: 1 branch
BranchTrueFalseTotal
if $nextQual > $maxQual 57 2943 3000
144 3000 $entry .= chr($nextQual);
145 3000 $currentQual = $nextQual;
146 }
147 20 $entry.="\n";
148  
149 20 $numBytes+=length($entry);
150 20 ▶ if($numBytes < $$settings{maxbytes}){
Branch: 1 branch
BranchTrueFalseTotal
if ($numBytes < $settings->{"maxbytes"}) 15 5 20
151 15 print $fh $entry;
152 }
153 }
154 5 close $fh;
155  
156 5 return $filename;
157 }
158  
159 0 ▶sub _generateFasta{
Subroutine _generateFasta: not called CC=6 SCAR=37.4
160 0 my($self,$settings)=@_;
161 0 ▶ $$settings{maxbytes} ||= 1000;
Condition0%0/2
$settings->{"maxbytes"} ||= 1000
l!l
00
MC/DC0%0/1
$settings->{"maxbytes"} ||= 1000
$settings->{"maxbytes"}
Truth table0%0/2
A
A= $settings->{"maxbytes"}
Aresult
11
00
162  
163 0 my @NT=qw(A C G T);
164  
165 0 $self->{_fileCounter}++;
166 0 my $currentQual=ord("A");
167
168 0 my $filename = $self->{tempdir}."/file.".$self->{_fileCounter}.".fasta";
169 0 ▶ open(my $fh, ">", $filename) or die "ERROR: could not write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "ERROR: could not write to ${filename}: $!" 0 0 0
170 0 my $readCounter=0;
171 0 my $numBytes=0;
172 0 ▶ while($numBytes < $$settings{maxbytes}){
Branch: 1 branch
BranchTrueFalseTotal
if ($numBytes < $settings->{"maxbytes"}) 0 0 0
173 0 $readCounter++;
174 0 my $entry=">$readCounter\n";
175 0 ▶ for(my $i=0;$i<150;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 150) 0 0 0
176 0 $entry.=$NT[rand(4)];
177 }
178 0 $entry.="\n";
179 0 $numBytes+=length($entry);
180 0 ▶ if($numBytes < $$settings{maxbytes}){
Branch: 1 branch
BranchTrueFalseTotal
if ($numBytes < $settings->{"maxbytes"}) 0 0 0
181 0 print $fh $entry;
182 }
183 }
184 0 close $fh;
185  
186 0 return $filename;
187 }
188  
189 =head1 COPYRIGHT AND LICENSE
190  
191 MIT license. Go nuts.
192  
193 =head1 AUTHOR
194  
195 Author: Lee Katz <lkatz@cdc.gov>
196  
197 For additional help, go to https://github.com/lskatz/File--Generator
198  
199 =cut
200  
201 2 1;
↑ Summary