| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: ali2phylip.pl | ||
| 3 | # ABSTRACT: Convert (and filter) ALI files to PHYLIP files for tree building | ||
| 4 | # CONTRIBUTOR: Arnaud DI FRANCO <arnaud.difranco@gmail.com> | ||
| 5 | # CONTRIBUTOR: Raphael LEONARD <rleonard@doct.uliege.be> | ||
| 6 | |||
| 7 | use Modern::Perl '2011'; | ||
| 8 | use autodie; | ||
| 9 | |||
| 10 | use Getopt::Euclid qw(:vars); | ||
| 11 | use List::Compare; | ||
| 12 | use Smart::Comments; | ||
| 13 | |||
| 14 | use Bio::MUST::Core; | ||
| 15 | use Bio::MUST::Core::Utils qw(change_suffix); | ||
| 16 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 17 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 18 | use aliased 'Bio::MUST::Core::IdMapper'; | ||
| 19 | use aliased 'Bio::MUST::Core::SeqMask'; | ||
| 20 | |||
| 21 | |||
| 22 | # optionally setup test outfile | ||
| 23 | my $out; | ||
| 24 | if ($ARGV_test_out) { | ||
| 25 | open $out, '>', $ARGV_test_out; | ||
| 26 | say {$out} join "\t", qw( | ||
| 27 | file gb bmge pars max min | ||
| 28 | Q0.len Q1.len Q2.len Q3.len Q4.len | ||
| 29 | in.seqs in.sites in.miss | ||
| 30 | out.seqs out.sites out.miss | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | for my $infile (@ARGV_infiles) { | ||
| 35 | |||
| 36 | ### Processing: $infile | ||
| 37 | my $ali = Ali->load($infile); | ||
| 38 | $ali->gapify_seqs if $ARGV_from_scafos; | ||
| 39 | |||
| 40 | dump_stats($infile, $ali, 'in'); | ||
| 41 | |||
| 42 | # remove shared gaps (and more if asked to do so) | ||
| 43 | _apply_mask( $ali, SeqMask->ideal_mask($ali, $ARGV_max_res_drop_site) ); | ||
| 44 | |||
| 45 | # TODO: allow deleting #NEW# sequences made identical to existing seqs | ||
| 46 | # of the same org after mask application (to handle 42 mini-inserts) | ||
| 47 | |||
| 48 | # apply Gblocks mask | ||
| 49 | _apply_mask( $ali, SeqMask->gblocks_mask($ali, $ARGV_gb_mask) ) | ||
| 50 | if $ARGV_gb_mask; | ||
| 51 | |||
| 52 | # apply BMGE mask | ||
| 53 | _apply_mask( $ali, SeqMask->bmge_mask($ali, $ARGV_bmge_mask) ) | ||
| 54 | if $ARGV_bmge_mask; | ||
| 55 | |||
| 56 | # apply parsimony mask | ||
| 57 | _apply_mask( $ali, SeqMask->parsimony_mask($ali) ) | ||
| 58 | if $ARGV_pars_mask; | ||
| 59 | |||
| 60 | # discard partial sequences and report their ids | ||
| 61 | if ($ARGV_min_res_seq) { | ||
| 62 | my @ali_list = map { $_->full_id } $ali->all_seq_ids; | ||
| 63 | $ali->apply_list( $ali->complete_seq_list($ARGV_min_res_seq) ); | ||
| 64 | my @phy_list = map { $_->full_id } $ali->all_seq_ids; | ||
| 65 | my $lc = List::Compare->new( { lists => [\@ali_list, \@phy_list] } ); | ||
| 66 | for my $full_id ($lc->get_unique) { | ||
| 67 | ### Discarding seq: $full_id | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # generate mapping file and map ids | ||
| 72 | if ($ARGV_map_ids) { | ||
| 73 | my $idm = $ali->std_mapper; | ||
| 74 | my $idmfile = change_suffix($infile, '.idm'); | ||
| 75 | $idm->store($idmfile); | ||
| 76 | $ali->shorten_ids($idm); | ||
| 77 | } | ||
| 78 | |||
| 79 | # optionally delete constant sites | ||
| 80 | _apply_mask( $ali, SeqMask->variable_mask($ali) ) | ||
| 81 | if $ARGV_del_const; | ||
| 82 | |||
| 83 | # export Ali to phylip format (or ALI) format | ||
| 84 | my $outfile = change_suffix($infile, | ||
| 85 | $ARGV_ali ? '-a2p.ali' : | ||
| 86 | $ARGV_p80 ? '.p80' : | ||
| 87 | '.phy' | ||
| 88 | ); | ||
| 89 | |||
| 90 | dump_stats($outfile, $ali, 'out'); | ||
| 91 | |||
| 92 | # only write actual phylip file if not in test mode | ||
| 93 | unless ($out) { | ||
| 94 | my $method = $ARGV_ali ? 'store' : 'store_phylip'; | ||
| 95 | my $args = { clean => 1, $ARGV_p80 ? (short => 0, chunk => -1) : () }; | ||
| 96 | $ali->$method($outfile, $args); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | # wrapper to native methods to transparently handle codon_mask | ||
| 101 | sub _apply_mask { | ||
| 102 | my $ali = shift; | ||
| 103 | my $mask = shift; | ||
| 104 | |||
| 105 | if ($ARGV_keep_codons) { | ||
| 106 | $mask = $mask->codon_mask( { | ||
| 107 | frame => $ARGV_coding_frame, | ||
| 108 | max => $ARGV_codon_max_nt_drop, | ||
| 109 | } ); | ||
| 110 | |||
| 111 | # frame should be fixed only once | ||
| 112 | $ARGV_coding_frame = 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | $ali->apply_mask($mask); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub dump_stats { | ||
| 120 | my $file = shift; | ||
| 121 | my $ali = shift; | ||
| 122 | my $mode = shift; | ||
| 123 | |||
| 124 | # output tabular stats line when in test mode | ||
| 125 | if ($out) { | ||
| 126 | print {$out} join "\t", ( | ||
| 127 | $file, | ||
| 128 | $ARGV_gb_mask // q{} , $ARGV_bmge_mask // q{}, | ||
| 129 | $ARGV_pars_mask // q{}, | ||
| 130 | $ARGV_max_res_drop_site, $ARGV_min_res_seq, | ||
| 131 | $ali->seq_len_stats | ||
| 132 | ) if $mode eq 'in'; | ||
| 133 | print {$out} "\t" . join "\t", | ||
| 134 | $ali->count_seqs, $ali->width, $ali->perc_miss; | ||
| 135 | print {$out} "\n" if $mode eq 'out'; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | # output regular Smart::Comments stats line | ||
| 140 | my $str = "$file has " . $ali->count_seqs . ' seqs x ' . $ali->width | ||
| 141 | . ' sites (' . sprintf('%.2f', $ali->perc_miss) . '% missing states)'; | ||
| 142 | if ($mode eq 'in') { | ||
| 143 | ### [ALI] file: $str | ||
| 144 | } else { | ||
| 145 | ### [PHY] file: $str | ||
| 146 | } | ||
| 147 | |||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | __END__ |