SeqMapping/Dataflow/LinearGeneric.pm untested

filter: Statementstmt 0.0% 0 / 49 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 / 75 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::LinearGeneric;
3 $Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearGeneric::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 mapping & reductions
14 requires 'cleanup';
15  
16 sub sim_seq_search {
17  
18 my ( $self, $workload, %args ) = @_;
19  
20 croak 'expect an array(ref) as workload' unless ref $workload eq 'ARRAY';
21  
22 my $max_workers = $args{max_workers}
23 // 1; # set default value if not defined
24 my $chunk_size = $args{chunk_size} // 1; # set default value if not defined
25 my $cleanup = $args{cleanup} // 1; # set default value if not defined
26 my @results;
27  
28 if ( $max_workers == 1 ) {
29 foreach my $chunk ( @{$workload} ) {
30 my $reduced_metric = $self->seq_align->($chunk);
31 push @results, $reduced_metric->@*; ## append to the results
32 $self->cleanup->($reduced_metric) if $cleanup;
33 }
34 return \@results;
35 }
36 else {
37 my $mce = MCE->new(
38 max_workers => $max_workers,
39 chunk_size => $chunk_size,
40 gather => MCE::Candy::out_iter_array( \@results ),
41 posix_exit => 1,
42 user_func => sub {
43 my ( $mce, $chunk_ref, $chunk_id ) = @_;
44 my @chunk_results;
45 foreach my $chunk ( @{$chunk_ref} ) {
46 my $reduced_metric = $self->seq_align->($chunk);
47 push @chunk_results, $reduced_metric->@*;
48 $self->cleanup->($reduced_metric) if $cleanup;
49 }
50 $mce->gather( $chunk_id, @chunk_results );
51 }
52 );
53 $mce->process($workload);
54 $mce->shutdown();
55 }
56 return \@results;
57 }
58  
59 1;
60  
61 =head1 NAME
62  
63 Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearGeneric - A role to implement a linear dataflow for a Generic Mapper
64  
65 =head1 VERSION
66  
67 version 0.03
68  
69 =head1 DESCRIPTION
70  
71 This role provides a linear dataflow for sequence mapping. By that we
72 mean that the input, i.e. a reference to a (list) of any object that can hold
73 biological sequences such as BioX::Seq objects, FASTA files, etc) undergoes a
74 single step process for mapping, i.e. similarity search, usually through a
75 (pseudo)alignment method and mapping to a single best candidate, which is the
76 final (?definitive) step of similarity search.
77 The module is intended to be B<applied> as a role to the class
78 Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic
79 The latter is pluggable Moose mapper that provides the necessary logic to
80 implement the methods required by this role module.
81  
82 The method requires the implementation of the following methods by the
83 GenericMapper class to which the role will be applied:
84  
85 * seq_align : aligns an atomic unit of work provided as the single argument
86 and returns the similarity metric and the match result. This method is
87 passed only one argument:
88 1. The atomic unit of work
89 This function returns a reference to an array containing the sequence ID,
90 the reference sequence that was mapped to and the similarity metric used to
91 decide which reference sequence to map to. In essence this method performs
92 ALL the steps of the similarity search and mapping in one go (as opposed to
93 the linear-linear dataflow where the similarity search, extraction of
94 similarity measures, their reduction and mapping are distinct steps.
95 * cleanup : does any cleanup necessary after the mapping has been done if the
96 user desires so. The function will be provided with a single argument:
97 1. the output of the seq_align method
98  
99 Note that these functions are provided as code references to the constructor of
100 the class that composes the role. The role does not provide any default
101 implementation for these methods, as they are highly dependent on the
102 implementation of the similarity search and the similarity metric reduction
103 steps. The user is free to implement these methods in any way they see fit,
104 e.g. using BioPerl, BioX, BioPython, command line and use Perl to wrap them
105 into functions that are provided as arguments to:
106 Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic
107  
108  
109 =head1 METHODS
110  
111 =head2 sim_seq_search
112  
113 This is the single L<public> method provided by the role. It takes the following
114 arguments:
115 * $workload : an array reference containing the atomic units of work to be
116 processed. The atomic units of work can be any object that can hold a
117 biological sequence, and the format depends entirely on the class composing
118 the role. This gives immense flexibility to the user to define the space
119 the dataflow operates in, e.g. in-memory, on-disk, etc.
120 * %args : a hash containing the following optional arguments, that control
121 parallelization and cleanup after the mapping has been done. The keys are:
122 * max_workers : the number of workers to use for parallelization. Default
123 value is 1, i.e. no parallelization.
124 * chunk_size : the number of atomic units of work to be processed by each
125 worker. Default value is 1.
126 * cleanup : a boolean value that indicates whether to do cleanup after the
127 mapping has been done. Default value is 1, i.e. cleanup after the mess.
128  
129 $mapper->sim_seq_search(
130 $workload,
131 max_workers => 4,
132 chunk_size => 10,
133 cleanup => 1
134 );
135  
136 =head1 SEE ALSO
137  
138 =over 4
139  
140 =item * L<Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic|https://metacpan.org/pod/Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic>
141  
142 Documentation of the Generic Mapper class that uses this role.
143  
144 =item * L<Bio::SeqAlignment::Examples::EnhancingEdlib|https://metacpan.org/pod/Bio::SeqAlignment::Examples::EnhancingEdlib>
145  
146 Example of how to use the Generic Mapper with the LinearLinearGeneric and the
147 LinearGeneric Dataflow roles, along with the Edlib alignment library.
148  
149 =back
150  
151 =head1 AUTHOR
152  
153 Christos Argyropoulos, C<< <chrisarg at cpan.org> >>
154  
155 =head1 COPYRIGHT AND LICENSE
156  
157 This software is copyright (c) 2024 by Christos Argyropoulos.
158  
159 This is free software; you can redistribute it and/or modify it under the same
160 terms as the Perl 5 programming language system itself.
161  
162 =cut