App-SimulateReads
view release on metacpan or search on metacpan
lib/App/SimulateReads/Simulator.pm view on Meta::CPAN
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $counter_ref) = @_;
while (my ($seqid, $count) = each %$counter_ref) {
$counters{$seqid} += $count;
}
}
);
log_msg sprintf ":: Creating %d child %s ...",
$number_of_jobs, $number_of_jobs == 1 ? "job" : "jobs";
for my $tid (1..$number_of_jobs) {
#-------------------------------------------------------------------------------
# Inside parent
#-------------------------------------------------------------------------------
log_msg ":: Creating job $tid ...";
my @files_t = map { "$_.${parent_pid}_part$tid" } @{ $files{$fastq_class} };
push @tmp_files => @files_t;
my $pid = $pm->start and next;
#-------------------------------------------------------------------------------
# Inside child
#-------------------------------------------------------------------------------
# Intelace child/parent processes
my $sig = App::SimulateReads::InterlaceProcesses->new(foreign_pid => [$parent_pid]);
# Set child seed
$self->_set_seed($tid);
# Calculate the number of reads to this job and correct this local index
# to the global index
my $number_of_reads_t = int($number_of_reads/$number_of_jobs);
my $last_read_idx = $number_of_reads_t * $tid;
my $idx = $last_read_idx - $number_of_reads_t + 1;
# If it is the last job, make it work on the leftover reads of int() truncation
$last_read_idx += $number_of_reads % $number_of_jobs
if $tid == $number_of_jobs;
log_msg " => Job $tid: Working on sequences from $idx to $last_read_idx";
# Create temporary files
log_msg " => Job $tid: Creating temporary file: @files_t";
my @fhs = map { $self->my_open_w($_, $self->output_gzip) } @files_t;
# Count the cumulative number of reads for each seqid
my %counter;
# Run simualtion in child
for (my $i = $idx; $i <= $last_read_idx and not $sig->signal_catched; $i++) {
my $id = $seqid->();
my @fastq_entry;
try {
@fastq_entry = $self->sprint_fastq($tid, $i, $id,
\$fasta->{$id}{seq}, $fasta->{$id}{size}, $strand->());
} catch {
die "Not defined entry for seqid '>$id' at job $tid: $_";
} finally {
unless (@_) {
for my $fh_idx (0..$#fhs) {
$counter{$id}++;
$fhs[$fh_idx]->say(${$fastq_entry[$fh_idx]})
or die "Cannot write to $files_t[$fh_idx]: $!\n";
}
}
};
}
log_msg " => Job $tid: Writing and closing file: @files_t";
# Close temporary files
for my $fh_idx (0..$#fhs) {
$fhs[$fh_idx]->close
or die "Cannot write file $files_t[$fh_idx]: $!\n";
}
# Child exit
log_msg " => Job $tid is finished";
$pm->finish(0, \%counter);
}
# Back to parent
# Interlace parent/child(s) processes
my $sig = App::SimulateReads::InterlaceProcesses->new(foreign_pid => \@child_pid);
$pm->wait_all_children;
if ($sig->signal_catched) {
log_msg ":: Termination signal received!";
}
log_msg ":: Saving the work ...";
# Concatenate all temporary files
log_msg ":: Concatenating all temporary files ...";
my @fh = map { $self->my_open_w($self->output_gzip ? "$_.gz" : $_, 0) } @{ $files{$fastq_class} };
for my $i (0..$#tmp_files) {
my $fh_idx = $i % scalar @fh;
cat $tmp_files[$i] => $fh[$fh_idx]
or die "Cannot concatenate $tmp_files[$i] to $files{$fastq_class}[$fh_idx]: $!\n";
}
# Close files
log_msg ":: Writing and closing output file: @{ $files{$fastq_class} }";
for my $fh_idx (0..$#fh) {
$fh[$fh_idx]->close
or die "Cannot write file $files{$fastq_class}[$fh_idx]: $!\n";
}
# Save counts
log_msg ":: Saving count file ...";
my $count_fh = $self->my_open_w($count_file, 0);
log_msg ":; Wrinting counts to $count_file ...";
while (my ($id, $count) = each %counters) {
$count_fh->say("$id\t$count");
}
# Just in case, calculate 'gene' like expression
my $parent_count = $self->_calculate_parent_count(\%counters);
if (defined $parent_count) {
while (my ($id, $count) = each %$parent_count) {
$count_fh->say("$id\t$count");
}
}
# Close $count_file
log_msg ":; Writing and closing $count_file ...";
$count_fh->close
or die "Cannot write file $count_file: $!\n";
# Clean up the mess
log_msg ":: Removing temporary files ...";
for my $file_t (@tmp_files) {
unlink $file_t
or die "Cannot remove temporary file: $file_t: $!\n";
}
}
__END__
=pod
=encoding UTF-8
=head1 NAME
App::SimulateReads::Simulator - Class responsible to make the simulation
=head1 VERSION
version 0.16
=head1 AUTHOR
Thiago L. A. Miller <tmiller@mochsl.org.br>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2018 by Teaching and Research Institute from SÃrio-Libanês Hospital.
This is free software, licensed under:
The GNU General Public License, Version 3, June 2007
=cut
( run in 0.455 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )