SeqMapping/Dataflow/LinearLinearGeneric.pm untested

filter: Statementstmt 0.0% 0 / 55 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 / 81 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::LinearLinearGeneric;
3 $Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric::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->@*; ## append to the results
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 my @chunk_results;
50 foreach my $chunk ( @{$chunk_ref} ) {
51 my $seq_align = $self->seq_align->($chunk);
52 my $sim_metric = $self->extract_sim_metric->($seq_align);
53 my $reduced_metric =
54 $self->reduce_sim_metric->($sim_metric);
55 push @chunk_results, $reduced_metric->@*;
56 $self->cleanup->( $seq_align, $sim_metric, $reduced_metric )
57 if $cleanup;
58 }
59 $mce->gather( $chunk_id, @chunk_results );
60 }
61 );
62 $mce->process($workload);
63 $mce->shutdown();
64 }
65 return \@results;
66 }
67  
68 1;
69  
70 =head1 NAME
71  
72 Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric - A role to implement a linear-linear dataflow for sequence mapping
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 multi 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<applied> as a role to the class
102 Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic
103 The latter is pluggable Moose mapper that provides the necessary logic to
104 implement the methods required by this role module.
105  
106 The method requires the implementation of the following methods by the class
107 composing the role:
108  
109 * seq_align : aligns an atomic unit of work provided as the single argument
110 and returns the similarity metric. It is passed only one argument:
111 1. The atomic unit of work
112 The method may return any object that can hold the similarity metric for
113 each sequence in the atomic unit of work. This could be for example a file,
114 a hashref, an arrayref, etc. A specialized extraction method will then be
115 used to extract the similarity metric from the output of this method.
116 * extract_sim_metric : extracts the similarity metric for each B<sequence> in the
117 atomic unit of work from the search results. It is passed only one argument:
118 1. The output of the seq_align method
119 The method should return a hashref where the keys are the sequence IDs and
120 the values are references to arrays that contain the similarity metric for
121 each sequence in an atomic unit of work.
122 * reduce_sim_metric : reduces the similarity metric for each sequence to a
123 single value. It receives one argument:
124 1. A hashref containing the similarity metric for each sequence in the
125 atomic unit of work. The keys are the sequence IDs and the values
126 are references to arrays that contain the similarity metric for each
127 sequence against each reference sequence in the database used for
128 searching. All the values of the hash are hashrefs themselves.
129 This function returns a reference to an array containing the sequence ID,
130 the reference sequence that was mapped to and the similarity metric used to
131 decide which reference sequence to map to.
132 * cleanup : does any cleanup necessary after the mapping has been done if the
133 user desires so. The function will be provided with three arguments:
134 1. the output of the seq_align method
135 2. the output of the extract_sim_metric method
136 3. the output of the reduce_sim_metric method
137  
138 Note that these functions are provided as code references to the constructor of
139 the class that composes the role. The role does not provide any default
140 implementation for these methods, as they are highly dependent on the
141 implementation of the similarity search and the similarity metric reduction
142 steps. The user is free to implement these methods in any way they see fit,
143 e.g. using BioPerl, BioX, BioPython, command line and use Perl to wrap them
144 into functions that are provided as arguments to:
145 Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic
146  
147  
148 =head1 METHODS
149  
150 =head2 sim_seq_search
151  
152 This is the single L<public> method provided by the role. It takes the following
153 arguments:
154 * $workload : an array reference containing the atomic units of work to be
155 processed. The atomic units of work can be any object that can hold a
156 biological sequence, and the format depends entirely on the class composing
157 the role. This gives immense flexibility to the user to define the space
158 the dataflow operates in, e.g. in-memory, on-disk, etc.
159 * %args : a hash containing the following optional arguments, that control
160 parallelization and cleanup after the mapping has been done. The keys are:
161 * max_workers : the number of workers to use for parallelization. Default
162 value is 1, i.e. no parallelization.
163 * chunk_size : the number of atomic units of work to be processed by each
164 worker. Default value is 1.
165 * cleanup : a boolean value that indicates whether to do cleanup after the
166 mapping has been done. Default value is 1, i.e. cleanup after the mess.
167  
168 $mapper->sim_seq_search(
169 $workload,
170 max_workers => 4,
171 chunk_size => 10,
172 cleanup => 1
173 );
174  
175 =head1 SEE ALSO
176  
177 =over 4
178  
179 =item * L<Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic|https://metacpan.org/pod/Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic>
180  
181 Documentation of the Generic Mapper class that uses this role.
182  
183 =item * L<Bio::SeqAlignment::Examples::EnhancingEdlib|https://metacpan.org/pod/Bio::SeqAlignment::Examples::EnhancingEdlib>
184  
185 Example of how to use the Generic Mapper with the LinearLinearGeneric and the
186 LinearGeneric Dataflow roles, along with the Edlib alignment library.
187  
188 =back
189  
190 =head1 AUTHOR
191  
192 Christos Argyropoulos, C<< <chrisarg at cpan.org> >>
193  
194 =head1 COPYRIGHT AND LICENSE
195  
196 This software is copyright (c) 2024 by Christos Argyropoulos.
197  
198 This is free software; you can redistribute it and/or modify it under the same
199 terms as the Perl 5 programming language system itself.
200  
201 =cut