| 1 | |||
| 2 | package Bio::SeqAlignment::Components::SeqMapping::Dataflow::Linear; | ||
| 3 | $Bio::SeqAlignment::Components::SeqMapping::Dataflow::Linear::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::Linear - A role to implement a linear dataflow for a non-generic sequence 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<composed> as a role to the class | ||
| 78 | Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic | ||
| 79 | The module is intended to be B<composed> as a role by a class that | ||
| 80 | provides the necessary logic to implement the methods required by this role. | ||
| 81 | |||
| 82 | The method requires the implementation of the following methods by the class | ||
| 83 | composing the role: | ||
| 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 | |||
| 100 | =head1 METHODS | ||
| 101 | |||
| 102 | =head2 sim_seq_search | ||
| 103 | |||
| 104 | This is the single L<public> method provided by the role. It takes the following | ||
| 105 | arguments: | ||
| 106 | * $workload : an array reference containing the atomic units of work to be | ||
| 107 | processed. The atomic units of work can be any object that can hold a | ||
| 108 | biological sequence, and the format depends entirely on the class composing | ||
| 109 | the role. This gives immense flexibility to the user to define the space | ||
| 110 | the dataflow operates in, e.g. in-memory, on-disk, etc. | ||
| 111 | * %args : a hash containing the following optional arguments, that control | ||
| 112 | parallelization and cleanup after the mapping has been done. The keys are: | ||
| 113 | * max_workers : the number of workers to use for parallelization. Default | ||
| 114 | value is 1, i.e. no parallelization. | ||
| 115 | * chunk_size : the number of atomic units of work to be processed by each | ||
| 116 | worker. Default value is 1. | ||
| 117 | * cleanup : a boolean value that indicates whether to do cleanup after the | ||
| 118 | mapping has been done. Default value is 1, i.e. cleanup after the mess. | ||
| 119 | |||
| 120 | $mapper->sim_seq_search( | ||
| 121 | $workload, | ||
| 122 | max_workers => 4, | ||
| 123 | chunk_size => 10, | ||
| 124 | cleanup => 1 | ||
| 125 | ); | ||
| 126 | |||
| 127 | =head1 TODO | ||
| 128 | |||
| 129 | * Provide examples of non-generic classes that compose this role | ||
| 130 | |||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Christos Argyropoulos, C<< <chrisarg at cpan.org> >> | ||
| 135 | |||
| 136 | =head1 COPYRIGHT AND LICENSE | ||
| 137 | |||
| 138 | This software is copyright (c) 2024 by Christos Argyropoulos. | ||
| 139 | |||
| 140 | This is free software; you can redistribute it and/or modify it under the same | ||
| 141 | terms as the Perl 5 programming language system itself. | ||
| 142 | |||
| 143 | =cut |