SeqMapping/Dataflow/LinearLinear.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 80 CC 5 SCAR 26.1
CC
5
Cov
0%
sim_seq_search
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 13.7
1  
2 package Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinear;
3 $Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinear::VERSION = '0.03';
4 use strict;
5 use warnings;
6  
7 use Moose::Role;
8 use Carp;
9 use MCE;
10 use MCE::Candy;
11 use namespace::autoclean;
12  
13 requires 'seq_align'; ## the method that does (pseudo)alignment
14 requires 'extract_sim_metric'; ## extract the similarity metric from the search
15 requires 'reduce_sim_metric'; ## reduce the similarity metric to a single value
16 requires 'cleanup';
17  
18 sub sim_seq_search {
19  
20 my ( $self, $workload, %args ) = @_;
21  
22 croak 'expect an array(ref) as workload' unless ref $workload eq 'ARRAY';
23  
24 my $max_workers = $args{max_workers}
25 // 1; # set default value if not defined
26 my $chunk_size = $args{chunk_size} // 1; # set default value if not defined
27 my $cleanup = $args{cleanup} // 1; # set default value if not defined
28 my @results;
29  
30 if ( $max_workers == 1 ) {
31 foreach my $chunk ( @{$workload} ) {
32 my $seq_align = $self->seq_align($chunk);
33 my $sim_metric = $self->extract_sim_metric($seq_align);
34 my $reduced_metric = $self->reduce_sim_metric($sim_metric);
35 push @results, $reduced_metric;
36 $self->cleanup( $seq_align, $sim_metric, $reduced_metric )
37 if $cleanup;
38 }
39 return \@results;
40 }
41 else {
42 my $mce = MCE->new(
43 max_workers => $max_workers,
44 chunk_size => $chunk_size,
45 gather => MCE::Candy::out_iter_array( \@results ),
46 posix_exit => 1,
47 user_func => sub {
48 my ( $mce, $chunk_ref, $chunk_id ) = @_;
49 foreach my $chunk ( @{$chunk_ref} ) {
50 my $seq_align = $self->seq_align($chunk);
51 my $sim_metric = $self->extract_sim_metric($seq_align);
52 my $reduced_metric =
53 $self->reduce_sim_metric($sim_metric);
54 push @results, $reduced_metric;
55 $self->cleanup( $seq_align, $sim_metric, $reduced_metric )
56 if $cleanup;
57 }
58 $mce->gather( $chunk_id, @results );
59 }
60 );
61 $mce->process($workload);
62 $mce->shutdown();
63 }
64 return \@results;
65 }
66  
67  
68 1;
69  
70 =head1 NAME
71  
72 Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinear - A role to implement a linear-linear dataflow for a non-generic sequence mapper.
73  
74 =head1 VERSION
75  
76 version 0.03
77  
78 =head1 DESCRIPTION
79  
80 This role provides a linear-linear dataflow for sequence mapping. By that we
81 mean that the input, i.e. a reference to a (list) of any object that can hold
82 biological sequences such as BioX::Seq objects, FASTA files, etc) undergoes a
83 two step process for mapping. The first step is to carry out a similarity
84 search against a database of reference sequences, using (pseudo)alignment and
85 extract a similarity metric. The second step is to reduce the similarity metric
86 to a single value, that identifies the reference sequence each user provided
87 sequence is most similar to. The output is a list containing the sequence ID,
88 the reference sequence that was mapped to and the similarity metric used to
89 decide which reference sequence to map to. The "linear-linear" part of the
90 dataflow refers to the fact that each atomic unit of work is processed
91 independently of all others for both the alignment and the reduction steps,
92 and that Perl is given access to the intermediate results of the similarity
93 search, before directing them to the reduction step. Due to the lack of any
94 dependencies between the atomic units of work, the dataflow can be parallelized
95 if the user desires so, using the MCE module. Parallelization is optional and
96 the user can choose to run the dataflow in a single thread if desired. This
97 feature is useful when the user wants to run the dataflow consuming the
98 resources of a single core, and leaving the rest of the cores free for e.g.
99 multithreaded logic that implements the similarity search and the similarity
100 metric reduction steps.
101 The module is intended to be B<composed> as a role by a class that
102 provides the necessary logic to implement the methods required by this role.
103  
104 The method requires the implementation of the following methods by the class
105 composing the role:
106  
107 * seq_align : aligns an atomic unit of work provided as the single argument
108 and returns the similarity metric. It is passed only one argument:
109 1. The atomic unit of work
110 The method may return any object that can hold the similarity metric for
111 each sequence in the atomic unit of work. This could be for example a file,
112 a hashref, an arrayref, etc. A specialized extraction method will then be
113 used to extract the similarity metric from the output of this method.
114 * extract_sim_metric : extracts the similarity metric for each B<sequence> in the
115 atomic unit of work from the search results. It is passed only one argument:
116 1. The output of the seq_align method
117 The method should return a hashref where the keys are the sequence IDs and
118 the values are references to arrays that contain the similarity metric for
119 each sequence in an atomic unit of work.
120 * reduce_sim_metric : reduces the similarity metric for each sequence to a
121 single value. It receives one argument:
122 1. A hashref containing the similarity metric for each sequence in the
123 atomic unit of work. The keys are the sequence IDs and the values
124 are references to arrays that contain the similarity metric for each
125 sequence against each reference sequence in the database used for
126 searching. All the values of the hash are hashrefs themselves.
127 This function returns a reference to an array containing the sequence ID,
128 the reference sequence that was mapped to and the similarity metric used to
129 decide which reference sequence to map to.
130 * cleanup : does any cleanup necessary after the mapping has been done if the
131 user desires so. The function will be provided with three arguments:
132 1. the output of the seq_align method
133 2. the output of the extract_sim_metric method
134 3. the output of the reduce_sim_metric method
135  
136 =head1 METHODS
137  
138 =head2 sim_seq_search
139  
140 This is the single L<public> method provided by the role. It takes the following
141 arguments:
142 * $workload : an array reference containing the atomic units of work to be
143 processed. The atomic units of work can be any object that can hold a
144 biological sequence, and the format depends entirely on the class composing
145 the role. This gives immense flexibility to the user to define the space
146 the dataflow operates in, e.g. in-memory, on-disk, etc.
147 * %args : a hash containing the following optional arguments, that control
148 parallelization and cleanup after the mapping has been done. The keys are:
149 * max_workers : the number of workers to use for parallelization. Default
150 value is 1, i.e. no parallelization.
151 * chunk_size : the number of atomic units of work to be processed by each
152 worker. Default value is 1.
153 * cleanup : a boolean value that indicates whether to do cleanup after the
154 mapping has been done. Default value is 1, i.e. cleanup after the mess.
155  
156 $mapper->sim_seq_search(
157 $mapper,
158 $workload,
159 max_workers => 4,
160 chunk_size => 10,
161 cleanup => 1
162 );
163  
164 =head1 TODO
165  
166 * Provide examples of non-generic classes that compose this role
167  
168  
169 =head1 AUTHOR
170  
171 Christos Argyropoulos, C<< <chrisarg at cpan.org> >>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2024 by Christos Argyropoulos.
176  
177 This is free software; you can redistribute it and/or modify it under the same
178 terms as the Perl 5 programming language system itself.
179  
180 =cut