| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: two-scalp.pl | ||
| 3 | # ABSTRACT: Align or re-align sequences using various strategies | ||
| 4 | # CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be> | ||
| 5 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@doct.uliege.be> | ||
| 6 | |||
| 7 | use Modern::Perl '2011'; | ||
| 8 | use Getopt::Euclid qw(:vars); | ||
| 9 | |||
| 10 | ## no critic (RequireLocalizedPunctuationVars) | ||
| 11 | BEGIN{ | ||
| 12 | $ENV{Smart_Comments} = $ARGV_verbosity | ||
| 13 | ? join q{ }, map { '#' x (2 + $_) } 1..$ARGV_verbosity | ||
| 14 | : q{} | ||
| 15 | ; | ||
| 16 | } | ||
| 17 | ## use critic | ||
| 18 | |||
| 19 | use Smart::Comments -ENV; | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | use Const::Fast; | ||
| 23 | use File::Basename; | ||
| 24 | use List::AllUtils qw(part uniq count_by); | ||
| 25 | use Path::Class qw(file); | ||
| 26 | use Tie::IxHash; | ||
| 27 | |||
| 28 | use Bio::MUST::Core; | ||
| 29 | use Bio::MUST::Core::Utils qw(secure_outfile); | ||
| 30 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 31 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 32 | |||
| 33 | use Bio::MUST::Drivers; | ||
| 34 | use aliased 'Bio::MUST::Drivers::Blast::Database'; | ||
| 35 | use aliased 'Bio::MUST::Drivers::Blast::Database::Temporary'; | ||
| 36 | |||
| 37 | use Bio::MUST::Apps::TwoScalp; | ||
| 38 | use aliased 'Bio::MUST::Apps::TwoScalp::AlignAll'; | ||
| 39 | use aliased 'Bio::MUST::Apps::TwoScalp::Seq2Seq'; | ||
| 40 | use aliased 'Bio::MUST::Apps::TwoScalp::Seqs2Profile'; | ||
| 41 | use aliased 'Bio::MUST::Apps::TwoScalp::Profile2Profile'; | ||
| 42 | |||
| 43 | |||
| 44 | const my $DEF_FAM => ':default'; | ||
| 45 | |||
| 46 | # set up mafft options | ||
| 47 | my %opt; | ||
| 48 | $opt{ '--keeplength' } = () if $ARGV_keep_length; | ||
| 49 | $opt{ '--thread' } = $ARGV_threads if $ARGV_threads > 1; | ||
| 50 | if ($ARGV_linsi) { | ||
| 51 | $opt{ '--maxiterate' } = 1000; | ||
| 52 | $opt{ '--localpair' } = undef; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $master_profile; | ||
| 56 | my $ref_prefix; | ||
| 57 | |||
| 58 | # TODO: check and unifomize error messages with other Bio::MUST::Apps | ||
| 59 | # TODO: decide between 'master' and 'reference' | ||
| 60 | |||
| 61 | if ($ARGV_p_ref) { | ||
| 62 | ### Loading master profile: $ARGV_p_ref | ||
| 63 | my $ref_profile = Ali->load($ARGV_p_ref); | ||
| 64 | # change ids to have no conflict when removing redundant seqs | ||
| 65 | my ($temp_file, $temp_mapper) | ||
| 66 | = $ref_profile->temp_fasta( {id_prefix => 'pref-'}); | ||
| 67 | $ref_prefix = Ali->load($temp_file); | ||
| 68 | $master_profile = $ref_prefix; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $basename = $ARGV_in_seqs[0]; | ||
| 72 | my $len = @ARGV_in_seqs; | ||
| 73 | #### number of specified files: $len | ||
| 74 | |||
| 75 | my @ord_fams; | ||
| 76 | my %seqs_for; | ||
| 77 | |||
| 78 | ### Check if more than one in-seqs file | ||
| 79 | if ($len > 1) { | ||
| 80 | @ord_fams = (1..$len); | ||
| 81 | for my $fam (@ord_fams) { | ||
| 82 | my $file = shift @ARGV_in_seqs; | ||
| 83 | #### file: $file | ||
| 84 | my $ali = Ali->load( $file ); | ||
| 85 | @{ $seqs_for{$fam} } = map { $_ } $ali->all_seqs; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # TODO: check alternative here; too complex | ||
| 90 | |||
| 91 | ### Check if family asked | ||
| 92 | if (@ord_fams && @ARGV_fam) { | ||
| 93 | warn <<'EOT'; | ||
| 94 | Warning: --fam option specified but there is more than one file. | ||
| 95 | --fam option will be ignored and I will follow infile order instead!" | ||
| 96 | EOT | ||
| 97 | } | ||
| 98 | |||
| 99 | elsif (@ARGV_fam) { | ||
| 100 | @ord_fams = @ARGV_fam; | ||
| 101 | my %exist_for = map { $_ => 1 } @ord_fams; | ||
| 102 | my $ali = Ali->load(@ARGV_in_seqs); | ||
| 103 | for my $seq ($ali->all_seqs) { | ||
| 104 | my $fam = $seq->family // $DEF_FAM; | ||
| 105 | |||
| 106 | # TODO: streamline this using either // or ? : within @{ ... } | ||
| 107 | if ( exists $exist_for{$fam} ) { | ||
| 108 | push @{ $seqs_for{$fam} }, $seq; | ||
| 109 | } | ||
| 110 | |||
| 111 | else { | ||
| 112 | push @{ $seqs_for{other} }, $seq; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | push @ord_fams, 'other' if $seqs_for{other}; | ||
| 116 | } | ||
| 117 | |||
| 118 | unless (@ord_fams) { | ||
| 119 | ### One file with no family specified | ||
| 120 | my $ali = Ali->load(@ARGV_in_seqs); | ||
| 121 | # TODO: move this to Ali introspective methods | ||
| 122 | my ($unaligned_seqs, $aligned_seqs) | ||
| 123 | = part { $_->is_aligned ? 1 : 0 } $ali->all_seqs; | ||
| 124 | @ord_fams = qw(aligned unaligned); | ||
| 125 | $seqs_for{ aligned} = $aligned_seqs if $aligned_seqs; | ||
| 126 | $seqs_for{unaligned} = $unaligned_seqs if $unaligned_seqs; | ||
| 127 | } | ||
| 128 | #### fam: @ord_fams | ||
| 129 | ##### %seqs_for | ||
| 130 | |||
| 131 | FAM: | ||
| 132 | for my $fam (@ord_fams) { | ||
| 133 | ### Check if seqs are aligned from part: $fam | ||
| 134 | |||
| 135 | # TODO: fix this as it is very dangerous to have my depending on if | ||
| 136 | # https://metacpan.org/pod/Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations | ||
| 137 | ## no critic (ProhibitConditionalDeclarations) | ||
| 138 | my $ali = Ali->new( seqs => $seqs_for{$fam}, guessing => 1 ) | ||
| 139 | if $seqs_for{$fam}; | ||
| 140 | ## use critic | ||
| 141 | |||
| 142 | unless ($ali) { | ||
| 143 | carp "Warning: no sequence found for family: $fam"; | ||
| 144 | next FAM; | ||
| 145 | } | ||
| 146 | |||
| 147 | if ($fam eq 'other') { | ||
| 148 | ### Degap seqs from families not specified | ||
| 149 | $ali->degap_seqs; | ||
| 150 | } | ||
| 151 | |||
| 152 | my ($unaligned_seqs, $aligned_seqs) | ||
| 153 | = part { $_->is_aligned ? 1 : 0 } $ali->all_seqs; | ||
| 154 | #= part { $_->is_aligned ? 1 : 0 } $seqs_for{$fam}; | ||
| 155 | |||
| 156 | my $p2; | ||
| 157 | |||
| 158 | # TODO: check how to simplify complex alternatives here | ||
| 159 | |||
| 160 | if ($aligned_seqs) { | ||
| 161 | my $aligned = Ali->new( | ||
| 162 | seqs => $aligned_seqs, | ||
| 163 | guessing => 1, | ||
| 164 | ); | ||
| 165 | |||
| 166 | if ($unaligned_seqs) { | ||
| 167 | my $unaligned = Ali->new( | ||
| 168 | seqs => $unaligned_seqs, | ||
| 169 | guessing => 1, | ||
| 170 | ); | ||
| 171 | ### Align non aligned seqs on aligned seqs from the same family | ||
| 172 | $p2 = align_on_profile($aligned, $unaligned); | ||
| 173 | } | ||
| 174 | $p2 = $aligned unless $p2; | ||
| 175 | } | ||
| 176 | |||
| 177 | elsif ($unaligned_seqs && $master_profile) { | ||
| 178 | ### There are only unaligned seqs in this family | ||
| 179 | my $unaligned = Ali->new( | ||
| 180 | seqs => $unaligned_seqs, | ||
| 181 | guessing => 1, | ||
| 182 | ); | ||
| 183 | $p2 = $unaligned; | ||
| 184 | } | ||
| 185 | |||
| 186 | if ($master_profile) { | ||
| 187 | my $new_master_profile = align_on_profile($master_profile, $p2); | ||
| 188 | $master_profile = $new_master_profile; | ||
| 189 | } | ||
| 190 | |||
| 191 | # align from scratch only if only unaligned seqs and no other family | ||
| 192 | # TODO: make this more explicit: too subtle combining ? : and postfix unless | ||
| 193 | $master_profile = $p2 ? $p2 : align_from_scratch($unaligned_seqs) | ||
| 194 | unless $master_profile; | ||
| 195 | } | ||
| 196 | |||
| 197 | if ($ARGV_p_ref) { | ||
| 198 | ### prune seq from reference profile | ||
| 199 | my @ids2prune = map { $_->full_id } $ref_prefix->all_seq_ids; | ||
| 200 | my $list = IdList->new( ids => \@ids2prune ); | ||
| 201 | $list = $list->negative_list($master_profile); | ||
| 202 | my $pruned_ali = $list->filtered_ali($master_profile); | ||
| 203 | $master_profile = $pruned_ali; | ||
| 204 | } | ||
| 205 | |||
| 206 | # add suffix | ||
| 207 | my $outfile = secure_outfile($basename, $ARGV_out_suffix); | ||
| 208 | ### Store fasta outfile: $outfile | ||
| 209 | $master_profile->store($outfile); | ||
| 210 | |||
| 211 | |||
| 212 | sub align_on_profile { | ||
| 213 | # TODO choose better name here (reused from main code) | ||
| 214 | ## no critic (ProhibitReusedNames) | ||
| 215 | my $master_profile = shift; | ||
| 216 | ## use critic | ||
| 217 | my $other = shift; | ||
| 218 | ##### profile sequences: $master_profile | ||
| 219 | |||
| 220 | my $new_profile = $master_profile; | ||
| 221 | |||
| 222 | unless ($new_profile->has_uniq_ids) { | ||
| 223 | ### non uniq seq id in master profile | ||
| 224 | $new_profile = uniq_ids($new_profile); | ||
| 225 | } | ||
| 226 | |||
| 227 | my $toalign = $other; | ||
| 228 | ##### sequence to add: $toalign | ||
| 229 | |||
| 230 | unless ($toalign->has_uniq_ids) { | ||
| 231 | ### non uniq seq id p2 | ||
| 232 | $toalign = uniq_ids($toalign); | ||
| 233 | } | ||
| 234 | |||
| 235 | my ($toalign_file, $toalign_mapper) | ||
| 236 | = $toalign->temp_fasta( {id_prefix => 'toalign-'} ); | ||
| 237 | |||
| 238 | my $profile = $new_profile; | ||
| 239 | my ($profile_file, $profile_mapper) | ||
| 240 | = $profile->temp_fasta( {id_prefix => 'profile-'} ); | ||
| 241 | |||
| 242 | my %mapper = ( profile => $profile_mapper, toalign => $toalign_mapper ); | ||
| 243 | |||
| 244 | my $type = $other->is_aligned ? 'prof' : 'seqs'; | ||
| 245 | ### type of sequence to add: $type | ||
| 246 | |||
| 247 | # set up new (expendable) hash to change used options | ||
| 248 | tie my %reduced_opt, 'Tie::IxHash'; | ||
| 249 | %reduced_opt = %opt; | ||
| 250 | |||
| 251 | if ($type eq 'prof') { | ||
| 252 | delete $reduced_opt{ '--keeplength' }; | ||
| 253 | $new_profile = Profile2Profile->new( file1 => $toalign_file, | ||
| 254 | file2 => $profile_file, | ||
| 255 | options => \%reduced_opt ); | ||
| 256 | } | ||
| 257 | |||
| 258 | elsif ($type eq 'seqs') { | ||
| 259 | delete @reduced_opt{ qw( --maxiterate --localpair ) } | ||
| 260 | if $ARGV_keep_length; | ||
| 261 | $new_profile = Seqs2Profile->new( file1 => $toalign_file, | ||
| 262 | file2 => $profile_file, | ||
| 263 | options => \%reduced_opt ); | ||
| 264 | } | ||
| 265 | |||
| 266 | $new_profile->restore_ids( $mapper{profile} ); | ||
| 267 | $new_profile->restore_ids( $mapper{toalign} ); | ||
| 268 | |||
| 269 | unless ($new_profile->has_uniq_ids) { | ||
| 270 | ### non uniq ids between parts to align | ||
| 271 | $new_profile = uniq_ids($new_profile); | ||
| 272 | } | ||
| 273 | |||
| 274 | return $new_profile; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | sub align_from_scratch { | ||
| 279 | my $seqs = shift; | ||
| 280 | |||
| 281 | my $toalign = Ali->new( | ||
| 282 | seqs => $seqs, | ||
| 283 | guessing => 1, | ||
| 284 | ); | ||
| 285 | $toalign->degap_seqs; | ||
| 286 | |||
| 287 | unless ($toalign->has_uniq_ids) { | ||
| 288 | ### non uniq seq id | ||
| 289 | $toalign = uniq_ids($toalign); | ||
| 290 | } | ||
| 291 | |||
| 292 | my ($toalign_file, $toalign_mapper) = $toalign->temp_fasta; | ||
| 293 | |||
| 294 | # set up new (expendable) hash to change used options | ||
| 295 | tie my %reduced_opt, 'Tie::IxHash'; | ||
| 296 | %reduced_opt = %opt; | ||
| 297 | delete $reduced_opt{ '--keeplength' }; | ||
| 298 | |||
| 299 | my $new_profile = AlignAll->new( file => $toalign_file, | ||
| 300 | options => \%reduced_opt ); | ||
| 301 | $new_profile->restore_ids($toalign_mapper); | ||
| 302 | |||
| 303 | return $new_profile; | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | sub uniq_ids { | ||
| 308 | # TODO: modify way to check ids and seq to keep seq from master profile | ||
| 309 | # with ids slightly different if subseq | ||
| 310 | my $ali = shift; | ||
| 311 | |||
| 312 | my @ids = map { $_->full_id } $ali->all_seq_ids; | ||
| 313 | |||
| 314 | my %count_for = count_by { $_ } @ids; | ||
| 315 | my @duplicates = grep { $count_for{ $_ } > 1 } @ids; | ||
| 316 | #### non uniq ids: @duplicates | ||
| 317 | |||
| 318 | for my $dup ( uniq @duplicates ) { | ||
| 319 | my @seqs = grep { $_->full_id eq $dup } $ali->all_seqs; | ||
| 320 | my $len_seq = @seqs; | ||
| 321 | |||
| 322 | for my $i ( 1..($len_seq-1) ) { | ||
| 323 | |||
| 324 | unless ( $seqs[$i]->is_subseq_of($seqs[$i-1]) | ||
| 325 | || ( $seqs[$i-1]->is_subseq_of($seqs[$i]) | ||
| 326 | && $seqs[$i]->is_subseq_of($seqs[$i-1]) ) ) { | ||
| 327 | carp "Warning: duplicate ids but different sequences for: $dup"; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | my @uniq_ids = uniq @ids; | ||
| 333 | my @seq_uniq_ids = map { $ali->get_seq_with_id($_) } @uniq_ids; | ||
| 334 | my $ali2 = Ali->new( | ||
| 335 | seqs => \@seq_uniq_ids, | ||
| 336 | guessing => 1, | ||
| 337 | ); | ||
| 338 | |||
| 339 | return $ali2; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | # TODO: check if args are better served with 'repeatable' or '...' | ||
| 344 | |||
| 345 | __END__ | ||
| 346 | |||
| 347 | =pod | ||
| 348 | |||
| 349 | =head1 NAME | ||
| 350 | |||
| 351 | two-scalp.pl - Align or re-align sequences using various strategies | ||
| 352 | |||
| 353 | =head1 VERSION | ||
| 354 | |||
| 355 | version 0.231010 | ||
| 356 | |||
| 357 | =head1 USAGE | ||
| 358 | |||
| 359 | two-scalp.pl --in-seqs=<infiles>... [optional arguments] | ||
| 360 | |||
| 361 | =head1 REQUIRED ARGUMENTS | ||
| 362 | |||
| 363 | =over | ||
| 364 | |||
| 365 | =item --in-seqs=<infiles>... | ||
| 366 | |||
| 367 | Path to input ALI file(s) with sequences to align. If only one file is | ||
| 368 | specified, the script aligns all non-aligned sequences on the already aligned | ||
| 369 | sequences (but see option C<--fam> below). If several files are specified, | ||
| 370 | each file is aligned on the previous one(s) in the given order. | ||
| 371 | |||
| 372 | =for Euclid: infiles.type: readable | ||
| 373 | |||
| 374 | =back | ||
| 375 | |||
| 376 | =head1 OPTIONAL ARGUMENTS | ||
| 377 | |||
| 378 | =over | ||
| 379 | |||
| 380 | =item --p-ref=<infile> | ||
| 381 | |||
| 382 | Path to input ALI file with already aligned sequences [default: none]. When | ||
| 383 | such a master profile is specified, its sequences are only used as references | ||
| 384 | for aligning the sequences of the other input file(s). Therefore, they will | ||
| 385 | not appear in the final output. | ||
| 386 | |||
| 387 | =for Euclid: infile.type: readable | ||
| 388 | |||
| 389 | =item --fam=<family>... | ||
| 390 | |||
| 391 | Family (or families) to consider when aligning sequences (already aligned or | ||
| 392 | not) [default: none]. Families are processed in the given order, i.e., the | ||
| 393 | first specified family is the first aligned and will serve as a master profile | ||
| 394 | for the second one, etc. If sequences from additional families (or devoid of | ||
| 395 | family) are also present, these are degapped and aligned on the profile | ||
| 396 | obtained after aligning the specified families. | ||
| 397 | |||
| 398 | =for Euclid: family.type: string | ||
| 399 | |||
| 400 | =item --keep-length | ||
| 401 | |||
| 402 | Alignment length remains unchanged when adding sequences from the same family | ||
| 403 | [default: no]. This is achieved by clipping sequence regions not fitting in the | ||
| 404 | existing alignment. | ||
| 405 | |||
| 406 | =item --linsi | ||
| 407 | |||
| 408 | Run C<mafft-linsi> instead of the default C<mafft> executable for more accurate | ||
| 409 | alignment [default: no]. If the C<--keep-length> option is specified, the | ||
| 410 | default C<mafft> will still be run when aligning sequences from the same family. | ||
| 411 | |||
| 412 | =item --threads=<n> | ||
| 413 | |||
| 414 | Number of threads to allocate to the aligner [default: n.default]. Mostly useful | ||
| 415 | when the C<--linsi> option is specified. | ||
| 416 | |||
| 417 | =for Euclid: n.type: +int | ||
| 418 | n.default: 1 | ||
| 419 | |||
| 420 | =item --out[-suffix]=<suffix> | ||
| 421 | |||
| 422 | Suffix to append to infile basename for deriving outfile name [default: | ||
| 423 | suffix.default]. The infile giving the basename is the first given to --in-seqs. | ||
| 424 | |||
| 425 | =for Euclid: suffix.type: string | ||
| 426 | suffix.default: '-ts' | ||
| 427 | |||
| 428 | =item --verbosity=<level> | ||
| 429 | |||
| 430 | Verbosity level for logging to STDERR [default: level.default]. Available levels | ||
| 431 | range from 0 to 6. Level 6 corresponds to debugging mode. | ||
| 432 | |||
| 433 | =for Euclid: level.type: int, level >= 0 && level <= 6 | ||
| 434 | level.default: 0 | ||
| 435 | |||
| 436 | =item --version | ||
| 437 | |||
| 438 | =item --usage | ||
| 439 | |||
| 440 | =item --help | ||
| 441 | |||
| 442 | =item --man | ||
| 443 | |||
| 444 | Print the usual program information | ||
| 445 | |||
| 446 | =back | ||
| 447 | |||
| 448 | =head1 AUTHOR | ||
| 449 | |||
| 450 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 451 | |||
| 452 | =head1 CONTRIBUTORS | ||
| 453 | |||
| 454 | =for stopwords Amandine BERTRAND Valerian LUPO | ||
| 455 | |||
| 456 | =over 4 | ||
| 457 | |||
| 458 | =item * | ||
| 459 | |||
| 460 | Amandine BERTRAND <amandine.bertrand@doct.uliege.be> | ||
| 461 | |||
| 462 | =item * | ||
| 463 | |||
| 464 | Valerian LUPO <valerian.lupo@doct.uliege.be> | ||
| 465 | |||
| 466 | =back | ||
| 467 | |||
| 468 | =head1 COPYRIGHT AND LICENSE | ||
| 469 | |||
| 470 | This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 471 | |||
| 472 | This is free software; you can redistribute it and/or modify it under | ||
| 473 | the same terms as the Perl 5 programming language system itself. | ||
| 474 | |||
| 475 | =cut |