line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Generator.pm: |
4
|
|
|
|
|
|
|
# Author: Lee Katz |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package File::Generator; |
7
|
|
|
|
|
|
|
require 5.12.0; |
8
|
|
|
|
|
|
|
our $VERSION=0.2; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
1527
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
59
|
|
11
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
11
|
use File::Basename qw/basename fileparse dirname/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
156
|
|
14
|
2
|
|
|
2
|
|
1521
|
use File::Temp qw/tempdir tempfile/; |
|
2
|
|
|
|
|
35430
|
|
|
2
|
|
|
|
|
146
|
|
15
|
2
|
|
|
2
|
|
1666
|
use Data::Dumper qw/Dumper/; |
|
2
|
|
|
|
|
14030
|
|
|
2
|
|
|
|
|
150
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
20
|
use Exporter qw/import/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1639
|
|
18
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
local $SIG{'__DIE__'} = sub { my $e = $_[0]; $e =~ s/(at [^\s]+? line \d+\.$)/\nStopped $1/; die("$0: ".(caller(1))[3].": ".$e); }; |
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
|
|
|
|
|
|
|
sub new{ |
68
|
1
|
|
|
1
|
1
|
594
|
my($class,$settings)=@_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Set optional parameter defaults |
71
|
1
|
|
33
|
|
|
10
|
$$settings{tempdir} ||=tempdir("Generator.pm.XXXXXX",TMPDIR=>1,CLEANUP=>1); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Initialize the object and then bless it |
74
|
|
|
|
|
|
|
my $self={ |
75
|
|
|
|
|
|
|
tempdir => $$settings{tempdir}, |
76
|
1
|
|
|
|
|
673
|
_fileCounter => 0, # how many files this generator has made |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
3
|
bless($self); |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
4
|
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
|
|
|
|
|
|
|
sub generate{ |
106
|
5
|
|
|
5
|
1
|
4826
|
my($self,$type,$settings)=@_; |
107
|
|
|
|
|
|
|
|
108
|
5
|
|
|
|
|
12
|
$type=uc($type); |
109
|
|
|
|
|
|
|
|
110
|
5
|
50
|
|
|
|
16
|
if($type eq "FASTQ"){ |
|
|
0
|
|
|
|
|
|
111
|
5
|
|
|
|
|
16
|
return $self->_generateFastq($settings); |
112
|
|
|
|
|
|
|
} elsif($type eq "FASTA"){ |
113
|
0
|
|
|
|
|
0
|
return $self->_generateFasta($settings); |
114
|
|
|
|
|
|
|
} else { |
115
|
0
|
|
|
|
|
0
|
die "ERROR: I do not understand type $type"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _generateFastq{ |
120
|
5
|
|
|
5
|
|
36
|
my($self,$settings)=@_; |
121
|
5
|
|
50
|
|
|
36
|
$$settings{maxbytes} ||= 1000; |
122
|
|
|
|
|
|
|
|
123
|
5
|
|
|
|
|
15
|
my @NT=qw(A C G T); |
124
|
|
|
|
|
|
|
|
125
|
5
|
|
|
|
|
11
|
$self->{_fileCounter}++; |
126
|
5
|
|
|
|
|
33
|
my $currentQual=ord("A"); |
127
|
5
|
|
|
|
|
13
|
my $maxQual = ord("I"); |
128
|
|
|
|
|
|
|
|
129
|
5
|
|
|
|
|
16
|
my $filename = $self->{tempdir}."/file.".$self->{_fileCounter}.".fastq"; |
130
|
5
|
50
|
|
|
|
351
|
open(my $fh, ">", $filename) or die "ERROR: could not write to $filename: $!"; |
131
|
5
|
|
|
|
|
16
|
my $readCounter=0; |
132
|
5
|
|
|
|
|
8
|
my $numBytes=0; |
133
|
5
|
|
|
|
|
16
|
while($numBytes < $$settings{maxbytes}){ |
134
|
|
|
|
|
|
|
#print "$numBytes $$settings{maxbytes}\n"; |
135
|
20
|
|
|
|
|
31
|
$readCounter++; |
136
|
20
|
|
|
|
|
44
|
my $entry="\@$readCounter\n"; |
137
|
20
|
|
|
|
|
44
|
for(my $i=0;$i<150;$i++){ |
138
|
3000
|
|
|
|
|
5062
|
$entry.=$NT[rand(4)]; |
139
|
|
|
|
|
|
|
} |
140
|
20
|
|
|
|
|
24
|
$entry.="\n+\n"; |
141
|
20
|
|
|
|
|
41
|
for(my $i=0;$i<150;$i++){ |
142
|
3000
|
|
|
|
|
4147
|
my $nextQual = $currentQual + int(rand(3)) - 1; |
143
|
3000
|
100
|
|
|
|
5113
|
$nextQual = $maxQual if($nextQual > $maxQual); |
144
|
3000
|
|
|
|
|
3729
|
$entry .= chr($nextQual); |
145
|
3000
|
|
|
|
|
5065
|
$currentQual = $nextQual; |
146
|
|
|
|
|
|
|
} |
147
|
20
|
|
|
|
|
27
|
$entry.="\n"; |
148
|
|
|
|
|
|
|
|
149
|
20
|
|
|
|
|
26
|
$numBytes+=length($entry); |
150
|
20
|
100
|
|
|
|
45
|
if($numBytes < $$settings{maxbytes}){ |
151
|
15
|
|
|
|
|
105
|
print $fh $entry; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
5
|
|
|
|
|
177
|
close $fh; |
155
|
|
|
|
|
|
|
|
156
|
5
|
|
|
|
|
52
|
return $filename; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _generateFasta{ |
160
|
0
|
|
|
0
|
|
|
my($self,$settings)=@_; |
161
|
0
|
|
0
|
|
|
|
$$settings{maxbytes} ||= 1000; |
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
|
0
|
|
|
|
|
open(my $fh, ">", $filename) or die "ERROR: could not write to $filename: $!"; |
170
|
0
|
|
|
|
|
|
my $readCounter=0; |
171
|
0
|
|
|
|
|
|
my $numBytes=0; |
172
|
0
|
|
|
|
|
|
while($numBytes < $$settings{maxbytes}){ |
173
|
0
|
|
|
|
|
|
$readCounter++; |
174
|
0
|
|
|
|
|
|
my $entry=">$readCounter\n"; |
175
|
0
|
|
|
|
|
|
for(my $i=0;$i<150;$i++){ |
176
|
0
|
|
|
|
|
|
$entry.=$NT[rand(4)]; |
177
|
|
|
|
|
|
|
} |
178
|
0
|
|
|
|
|
|
$entry.="\n"; |
179
|
0
|
|
|
|
|
|
$numBytes+=length($entry); |
180
|
0
|
0
|
|
|
|
|
if($numBytes < $$settings{maxbytes}){ |
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 |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
For additional help, go to https://github.com/lskatz/File--Generator |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |