| 1 | package Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic; | ||
| 2 | $Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic::VERSION = '0.03'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | #ABSTRACT: A generic sequence mapper for sequence mapping | ||
| 7 | use Carp; | ||
| 8 | use Module::Runtime 'use_module'; | ||
| 9 | use Module::Find; | ||
| 10 | use Scalar::Util 'blessed'; | ||
| 11 | use Moose; | ||
| 12 | use namespace::autoclean; | ||
| 13 | ############################################################################### | ||
| 14 | ## Searching/Mapping related methods | ||
| 15 | has 'init_sim_search' => ( | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'CodeRef', | ||
| 18 | default => sub { | ||
| 19 | sub { } | ||
| 20 | }, | ||
| 21 | trigger => sub { $_[0]->_nondefault_set( 'init_sim_search', @_ ) }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'seq_align' => ( | ||
| 25 | is => 'rw', | ||
| 26 | isa => 'CodeRef', | ||
| 27 | default => sub { | ||
| 28 | sub { } | ||
| 29 | }, | ||
| 30 | trigger => sub { $_[0]->_nondefault_set( 'seq_align', @_ ) }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has 'extract_sim_metric' => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'CodeRef', | ||
| 36 | default => sub { | ||
| 37 | sub { } | ||
| 38 | }, | ||
| 39 | trigger => sub { $_[0]->_nondefault_set( 'extract_sim_metric', @_ ) }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has 'reduce_sim_metric' => ( | ||
| 43 | is => 'rw', | ||
| 44 | isa => 'CodeRef', | ||
| 45 | default => sub { | ||
| 46 | sub { } | ||
| 47 | }, | ||
| 48 | trigger => sub { $_[0]->_nondefault_set( 'reduce_sim_metric', @_ ) }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has 'cleanup' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'CodeRef', | ||
| 54 | default => sub { | ||
| 55 | sub { } | ||
| 56 | }, | ||
| 57 | trigger => sub { $_[0]->_nondefault_set( 'cleanup', @_ ) }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | ############################################################################### | ||
| 61 | ## Reference Database related methods | ||
| 62 | has 'create_refDB' => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'CodeRef', | ||
| 65 | default => sub { | ||
| 66 | sub { } | ||
| 67 | }, | ||
| 68 | trigger => sub { $_[0]->_nondefault_set( 'create_refDB', @_ ) }, | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'use_refDB' => ( | ||
| 72 | is => 'rw', | ||
| 73 | isa => 'CodeRef', | ||
| 74 | default => sub { | ||
| 75 | sub { } | ||
| 76 | }, | ||
| 77 | trigger => sub { $_[0]->_nondefault_set( 'use_refDB', @_ ) }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | ## around modifiers to allow $self to be passed as an argument | ||
| 81 | around qw( | ||
| 82 | create_refDB | ||
| 83 | use_refDB | ||
| 84 | cleanup | ||
| 85 | reduce_sim_metric | ||
| 86 | seq_align | ||
| 87 | init_sim_search | ||
| 88 | extract_sim_metric | ||
| 89 | ) => sub { | ||
| 90 | my $orig = shift; | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | # Modify the behavior (if needed) | ||
| 94 | return sub { | ||
| 95 | ## may put code to modify the logic prior to returning | ||
| 96 | return $self->$orig->( $self, @_ ); | ||
| 97 | }; | ||
| 98 | }; | ||
| 99 | ############################################################################### | ||
| 100 | ## Parameters used for database creation/access, similarity search & mapping | ||
| 101 | has 'refDB_access_params' => ( | ||
| 102 | is => 'rw', | ||
| 103 | isa => 'HashRef', | ||
| 104 | default => sub { {} }, | ||
| 105 | trigger => sub { $_[0]->_nondefault_set( 'refDB_access_params', @_ ) }, | ||
| 106 | ); | ||
| 107 | |||
| 108 | has 'sim_search_params' => ( | ||
| 109 | is => 'rw', | ||
| 110 | isa => 'HashRef', | ||
| 111 | default => sub { {} }, | ||
| 112 | trigger => sub { $_[0]->_nondefault_set( 'sim_search_params', @_ ) }, | ||
| 113 | ); | ||
| 114 | |||
| 115 | has 'extract_sim_metric_params' => ( | ||
| 116 | is => 'rw', | ||
| 117 | isa => 'HashRef', | ||
| 118 | default => sub { {} }, | ||
| 119 | trigger => | ||
| 120 | sub { $_[0]->_nondefault_set( 'extract_sim_metric_params', @_ ) }, | ||
| 121 | ); | ||
| 122 | |||
| 123 | has 'seqmap_params' => ( | ||
| 124 | is => 'rw', | ||
| 125 | isa => 'HashRef', | ||
| 126 | default => sub { {} }, | ||
| 127 | trigger => sub { $_[0]->_nondefault_set( 'seqmap_params', @_ ) }, | ||
| 128 | ); | ||
| 129 | ############################################################################### | ||
| 130 | ## around accessors to allow updating of parameters | ||
| 131 | around qw( | ||
| 132 | refDB_access_params | ||
| 133 | sim_search_params | ||
| 134 | extract_sim_metric_params | ||
| 135 | seqmap_params | ||
| 136 | ) => sub { | ||
| 137 | my $orig = shift; | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | #current prameter value | ||
| 141 | my $current_param_value = $self->$orig; | ||
| 142 | |||
| 143 | # my new parameter value | ||
| 144 | my $new_param_value = shift; | ||
| 145 | |||
| 146 | #return current value if no new value is provided | ||
| 147 | return $current_param_value | ||
| 148 | unless $new_param_value; | ||
| 149 | |||
| 150 | #update current value with new value, if new value is non empty | ||
| 151 | if ($current_param_value) { | ||
| 152 | return ( keys %$new_param_value ) | ||
| 153 | ? $self->$orig( { %$current_param_value, %$new_param_value } ) | ||
| 154 | : $self->$orig( {} ) | ||
| 155 | ; #reset to empty hash if new value is the empty hash | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | #set current value to new value if current value is not defined | ||
| 159 | return $self->$orig($new_param_value); | ||
| 160 | } | ||
| 161 | |||
| 162 | }; | ||
| 163 | ############################################################################### | ||
| 164 | ## Keep track of what has been explicitly set by the user | ||
| 165 | |||
| 166 | has '_has_nondefault_value' => ( | ||
| 167 | traits => ['Hash'], | ||
| 168 | is => 'ro', | ||
| 169 | isa => 'HashRef', | ||
| 170 | default => sub { {} }, | ||
| 171 | handles => { | ||
| 172 | is_nondefault => 'exists', | ||
| 173 | get_nondefault_value => 'get', | ||
| 174 | set_nondefault_value => 'set', | ||
| 175 | }, | ||
| 176 | ); | ||
| 177 | |||
| 178 | sub _nondefault_set { | ||
| 179 | my ( $self, $attribute, $new_value, $old_value ) = @_; | ||
| 180 | $self->set_nondefault_value( $attribute, 1 ); | ||
| 181 | } | ||
| 182 | ############################################################################### | ||
| 183 | ## This may be used by the init_sim_search method to store the code for each | ||
| 184 | ## external function (or program) that one may want to interface with | ||
| 185 | |||
| 186 | has '_code_for' => ( | ||
| 187 | traits => ['Hash'], | ||
| 188 | is => 'ro', | ||
| 189 | isa => 'HashRef', | ||
| 190 | default => sub { {} }, | ||
| 191 | handles => { | ||
| 192 | has_function_for => 'exists', | ||
| 193 | get_function_for => 'get', | ||
| 194 | set_function_for => 'set', | ||
| 195 | }, | ||
| 196 | ); | ||
| 197 | ############################################################################### | ||
| 198 | |||
| 199 | __PACKAGE__->meta->make_immutable; | ||
| 200 | |||
| 201 | 1; | ||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | =head1 NAME | ||
| 206 | |||
| 207 | Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic Generic Sequence Mapper | ||
| 208 | |||
| 209 | =head1 VERSION | ||
| 210 | |||
| 211 | version 0.03 | ||
| 212 | |||
| 213 | =head1 SYNOPSIS | ||
| 214 | |||
| 215 | To use the module one must do *at least* the following: | ||
| 216 | |||
| 217 | use Module::Find; | ||
| 218 | use Moose::Util qw( apply_all_roles ); | ||
| 219 | use Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic; | ||
| 220 | |||
| 221 | The rest depends on the dataflow role applied to the mapper | ||
| 222 | The example under USAGE utilizes the LinearLinearGeneric role | ||
| 223 | to demonstrate the usage of the Generic module with the most | ||
| 224 | code that needs to be implemented by the user. | ||
| 225 | |||
| 226 | |||
| 227 | =head1 DESCRIPTION | ||
| 228 | |||
| 229 | This module loads all the components that can actually map sequences to a reference | ||
| 230 | database. If you don't want to nuke your namespace with all the components, you can | ||
| 231 | load them as needed by using the specific component name, e.g.: | ||
| 232 | |||
| 233 | use Bio::SeqAlignment::Components::SeqMapping::Mapper::ComponentName; | ||
| 234 | |||
| 235 | where ComponentName is the name of the component you need. | ||
| 236 | If you choose violence, you can load all the components at once by using: | ||
| 237 | |||
| 238 | use Bio::SeqAlignment::Components::SeqMapping::Mapper; | ||
| 239 | |||
| 240 | =head1 USAGE | ||
| 241 | |||
| 242 | |||
| 243 | use Module::Find; | ||
| 244 | use Moose::Util qw( apply_all_roles ); | ||
| 245 | use Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic; | ||
| 246 | |||
| 247 | ## apply the LinearLinearGeneric role to the Generic module | ||
| 248 | use Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric; | ||
| 249 | use Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic; | ||
| 250 | |||
| 251 | my $mapper = Bio::SeqAlignment::Components::SeqMapping::Mapper::Generic->new( | ||
| 252 | create_refDB => \&create_db, | ||
| 253 | use_refDB => \&use_refDB, | ||
| 254 | init_sim_search => \&init_sim_search, | ||
| 255 | seq_align => \&seq_align, | ||
| 256 | extract_sim_metric => \&extract_sim_metric, | ||
| 257 | reduce_sim_metric => \&reduce_sim_metric | ||
| 258 | ); | ||
| 259 | |||
| 260 | ## db_location : where to create the database | ||
| 261 | ## dbname : name of the database | ||
| 262 | ## dbfiles : array ref of the files that hold the reference sequences | ||
| 263 | $mapper->create_refDB->( $db_location, $dbname, \@dbfiles ); | ||
| 264 | my $ref_DB = $mapper->use_refDB->( $db_location, 'Hsapiens.cds.sample' ); | ||
| 265 | $mapper->sim_search_params( { ... } ); | ||
| 266 | |||
| 267 | ## apply the LinearLinearGeneric Dataflow role to the mapper | ||
| 268 | apply_all_roles( $mapper, | ||
| 269 | 'Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric' | ||
| 270 | ); | ||
| 271 | |||
| 272 | ## workload : array ref of the sequences to be mapped | ||
| 273 | ## max_workers : number of workers to use for process level parallelism through MCE | ||
| 274 | my @workload = ... ; | ||
| 275 | $results = $mapper->sim_seq_search( \@workload, max_workers => 4 ); | ||
| 276 | |||
| 277 | ## a combo of the the following methods will be required to be implemented | ||
| 278 | ## by the user depending on the dataflow role applied to the mapper | ||
| 279 | sub init_sim_search { | ||
| 280 | my ( $self, %params ) = @_; | ||
| 281 | ... | ||
| 282 | } | ||
| 283 | |||
| 284 | sub reduce_sim_metric { | ||
| 285 | my ( $self, $sim_metric, %params ) = @_; | ||
| 286 | ... | ||
| 287 | } | ||
| 288 | |||
| 289 | sub seq_align { | ||
| 290 | my ( $self, $query_fname ) = @_; | ||
| 291 | ... | ||
| 292 | } | ||
| 293 | |||
| 294 | sub extract_sim_metric { | ||
| 295 | my ( $self, $seq_align ) = @_; | ||
| 296 | ... | ||
| 297 | } | ||
| 298 | |||
| 299 | sub create_db { | ||
| 300 | my ( $self, $dbloc, $dbname, $files_aref ) = @_; | ||
| 301 | ... | ||
| 302 | } | ||
| 303 | |||
| 304 | sub use_refDB { | ||
| 305 | my $self = shift; | ||
| 306 | ... | ||
| 307 | } | ||
| 308 | |||
| 309 | =head1 ATTRIBUTES | ||
| 310 | |||
| 311 | =head2 cleanup | ||
| 312 | |||
| 313 | A code reference that cleans up the data after the mapping process. This method | ||
| 314 | is required to be implemented by the user. If you don't implement it, the default | ||
| 315 | is a coderef to an empty subroutine. If you leave it unimplemented, you probably | ||
| 316 | don't need this. | ||
| 317 | |||
| 318 | =head2 create_refDB | ||
| 319 | |||
| 320 | A code reference that creates the reference database. This method is required to | ||
| 321 | be implemented by the user. If you don't implement it, the default is a coderef to | ||
| 322 | an empty subroutine. If you leave it unimplemented, you probably don't need this, | ||
| 323 | e.g. you have already created your database of reference sequences somehow. | ||
| 324 | Perhaps the database was created by someone else, or you are using a pre-existing | ||
| 325 | database, or you created the database through this interface at some time in the | ||
| 326 | past. If none of the above hold true, then you probably DO need to provide the | ||
| 327 | code for this method, through the created_refDB attribute. | ||
| 328 | |||
| 329 | =head2 extract_sim_metric | ||
| 330 | |||
| 331 | A code reference that extracts the similarity metric from the search. This method | ||
| 332 | is required to be implemented by the user. If you don't implement it, the default | ||
| 333 | is a coderef to an empty subroutine. If you leave it unimplemented, you probably | ||
| 334 | don't need this, e.g. you are NOT using the LinearLinearGeneric dataflow role. | ||
| 335 | |||
| 336 | =head2 extract_sim_metric_params | ||
| 337 | |||
| 338 | A hash reference that holds the parameters for extracting the similarity metric. This | ||
| 339 | attribute is required to be set by the user. If you don't set it, the default is an | ||
| 340 | empty hash reference. If you leave it unimplemented, you probably don't need this, e.g. | ||
| 341 | your similarity extraction is not parameterized, and the extraction is hardwired into | ||
| 342 | the extract_sim_metric method. However, DO consider scenarios in which the extraction | ||
| 343 | is parameterized, e.g. you have to filter out some sequences from the search results | ||
| 344 | based on some criteria, that can be controlled by the user. | ||
| 345 | |||
| 346 | =head2 init_sim_search | ||
| 347 | |||
| 348 | A code reference that initializes the similarity search. This method is required | ||
| 349 | to be implemented by the user. If not implemented, the default is a coderef to | ||
| 350 | an empty subroutine. If you leave it unimplemented, you probably don't need this. | ||
| 351 | |||
| 352 | =head2 reduce_sim_metric | ||
| 353 | |||
| 354 | A code reference that reduces the similarity metric to a single value. This method | ||
| 355 | is required to be implemented by the user. If you don't implement it, the default | ||
| 356 | is a coderef to an empty subroutine. If you leave it unimplemented, you probably | ||
| 357 | don't need this, e.g. you are NOT using the LinearLinearGeneric dataflow role. | ||
| 358 | |||
| 359 | =head2 refDB_access_params | ||
| 360 | |||
| 361 | A hash reference that holds the parameters for accessing the reference database. | ||
| 362 | This attribute is required to be set by the user. If you don't set it, the default | ||
| 363 | is an empty hash reference. If you leave it unimplemented, you probably don't need | ||
| 364 | this, i.e. you will not be (for example) accessing the database over the network. | ||
| 365 | |||
| 366 | =head2 seq_align | ||
| 367 | |||
| 368 | A code reference that performs the sequence alignment. This method is required | ||
| 369 | to be implemented by the user. If you don't implement it, the default is a coderef | ||
| 370 | to an empty subroutine. If you leave it unimplemented, you probably don't mind | ||
| 371 | having a non-working code. | ||
| 372 | |||
| 373 | =head2 seqmap_params | ||
| 374 | |||
| 375 | A hash reference that holds the parameters for the sequence mapping. This attribute | ||
| 376 | is required to be set by the user. If you don't set it, the default is an empty hash | ||
| 377 | reference. If you leave it unimplemented, you probably don't need this, e.g. you | ||
| 378 | have hardwired the mapping parameters into the seq_align method. However, DO consider | ||
| 379 | scenarios in which the mapping is parameterized, e.g. you have to filter out some | ||
| 380 | sequences before the search based on some criteria, that can be controlled by the user. | ||
| 381 | Another scenario that you may (or rather SHOULD) use this parameter for is to control | ||
| 382 | the similarity search parameters, e.g. provide match/mismatch scores, gap penalties, | ||
| 383 | etc. that are used by the similarity search algorithm. | ||
| 384 | |||
| 385 | =head2 sim_search_params | ||
| 386 | |||
| 387 | A hash reference that holds the parameters for the similarity search. This attribute | ||
| 388 | is required to be set by the user. If you don't set it, the default is an empty hash | ||
| 389 | reference. If you leave it unimplemented, you probably don't need this, e.g. you are | ||
| 390 | performing a similarity search with a default you have somehow hardwired into the | ||
| 391 | similarity search code (that would be the init_sim_search or seq_align methods). | ||
| 392 | |||
| 393 | =head2 sim_seq_search | ||
| 394 | |||
| 395 | This is a B<role> method that is applied to the generic mapper. It is provided | ||
| 396 | by a dataflow role that is composed into the generic mapper. This method is used | ||
| 397 | to perform the sequence mapping. This method is implemented in one of the generic | ||
| 398 | mapper's dataflow roles, e.g. LinearLinearGeneric, LinearGeneric, etc. The user | ||
| 399 | only needs to apply this to the generic mapper, and then call it with the | ||
| 400 | appropriate arguments. See under USAGE for how to do this in general, and | ||
| 401 | under the B<EnhancingEdlib> example for a specific example. | ||
| 402 | |||
| 403 | =head2 use_refDB | ||
| 404 | |||
| 405 | A code reference that accesses the reference database. This method is required to | ||
| 406 | be implemented by the user. If you don't implement it, the default is a coderef to | ||
| 407 | an empty subroutine. If you leave it unimplemented, you probably don't mind have | ||
| 408 | a code that does not work. | ||
| 409 | |||
| 410 | |||
| 411 | =head1 METHODS | ||
| 412 | |||
| 413 | =head2 _nondefault_set | ||
| 414 | |||
| 415 | A method that sets the _has_nondefault_value attribute. This method is used internally | ||
| 416 | to keep track of what has been explicitly set by the user. | ||
| 417 | |||
| 418 | =head2 _code_for | ||
| 419 | |||
| 420 | A method that sets the _code_for attribute. This method is used internally to keep | ||
| 421 | track of the code for each external function (or program) that one may want to interface | ||
| 422 | with. | ||
| 423 | |||
| 424 | |||
| 425 | =head1 SEE ALSO | ||
| 426 | |||
| 427 | =over 4 | ||
| 428 | |||
| 429 | =item * L<Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric|https://metacpan.org/pod/Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearLinearGeneric> | ||
| 430 | |||
| 431 | LinearLinear Generic Dataflow role that can be composed into the Generic Mapper. | ||
| 432 | |||
| 433 | =item * L<Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearGeneric|https://metacpan.org/pod/Bio::SeqAlignment::Components::SeqMapping::Dataflow::LinearGeneric> | ||
| 434 | |||
| 435 | Linear Generic Dataflow role that can be composed into the Generic Mapper. | ||
| 436 | |||
| 437 | =item * L<Bio::SeqAlignment::Examples::EnhancingEdlib|https://metacpan.org/pod/Bio::SeqAlignment::Examples::EnhancingEdlib> | ||
| 438 | |||
| 439 | Example of how to use the Generic Mapper with the LinearLinearGeneric and the | ||
| 440 | LinearGeneric Dataflow roles, along with the Edlib alignment library. | ||
| 441 | |||
| 442 | =back | ||
| 443 | |||
| 444 | =head1 AUTHOR | ||
| 445 | |||
| 446 | Christos Argyropoulos <chrisarg@cpan.org> | ||
| 447 | |||
| 448 | =head1 COPYRIGHT AND LICENSE | ||
| 449 | |||
| 450 | This software is copyright (c) 2024 by Christos Argyropoulos. | ||
| 451 | |||
| 452 | This is free software; you can redistribute it and/or modify it under | ||
| 453 | the same terms as the Perl 5 programming language system itself. | ||
| 454 | |||
| 455 | =cut |