| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: frameshiftSimul_final.pl | ||
| 3 | # ABSTRACT: Simulation for HmmCleaner | ||
| 4 | |||
| 5 | use Modern::Perl; | ||
| 6 | use Carp; | ||
| 7 | use Path::Class; | ||
| 8 | use File::Basename; | ||
| 9 | #~ use File::Temp qw(tempfile); my $template = 'tmpfile_XXXX'; | ||
| 10 | use List::AllUtils qw(shuffle sum0 any firstidx); | ||
| 11 | use IPC::System::Simple qw(system); | ||
| 12 | use Getopt::Euclid qw(:vars); | ||
| 13 | use Template; | ||
| 14 | |||
| 15 | |||
| 16 | use Bio::MUST::Core; | ||
| 17 | use Bio::MUST::Core::Constants qw(:gaps); | ||
| 18 | use Bio::MUST::Core::Utils qw(change_suffix); | ||
| 19 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 20 | use aliased 'Bio::MUST::Core::Seq'; | ||
| 21 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 22 | use aliased 'Bio::MUST::Core::SeqMask'; | ||
| 23 | use aliased 'Bio::MUST::Core::GeneticCode::Factory'; | ||
| 24 | |||
| 25 | ## no critic (RequireLocalizedPunctuationVars) | ||
| 26 | BEGIN{ | ||
| 27 | $ENV{Smart_Comments} = $ARGV_verbosity | ||
| 28 | ? join q{ }, map { '#' x (2 + $_) } 1..$ARGV_verbosity | ||
| 29 | : q{} | ||
| 30 | ; | ||
| 31 | } | ||
| 32 | ## use critic | ||
| 33 | use Smart::Comments -ENV; | ||
| 34 | |||
| 35 | use Bio::MUST::Apps::HmmCleaner; | ||
| 36 | use aliased 'Bio::MUST::Apps::HmmCleaner'; | ||
| 37 | |||
| 38 | ### %ARGV | ||
| 39 | |||
| 40 | my $sf = $ARGV_sf // undef; | ||
| 41 | my $nf = $ARGV_nf // undef; | ||
| 42 | |||
| 43 | my $base_global_name = join '_',('stat', 'global', 'nf'.((defined $nf) ? $nf : 'rand'), 'sf'.((defined $sf) ? $sf : 'rand'), 'sub'.((defined $ARGV_subset) ? $ARGV_subset : 'all'), 'r'.$ARGV_rep); | ||
| 44 | |||
| 45 | my $outfile_global = $base_global_name.'.out'; | ||
| 46 | my $outfile_frameshift_global = (join '_',($base_global_name, 'frameshift')).'.out'; | ||
| 47 | my $outfile_fp_global = (join '_',($base_global_name, 'fp')).'.out'; | ||
| 48 | my $simul_file_global = file($outfile_global); | ||
| 49 | my $simul_file_frameshift_global = file($outfile_frameshift_global); | ||
| 50 | my $simul_file_fp_global = file($outfile_fp_global); | ||
| 51 | my $out_global = $simul_file_global->openw; | ||
| 52 | my $out_frameshift_global = $simul_file_frameshift_global->openw; | ||
| 53 | my $out_fp_global = $simul_file_fp_global->openw; | ||
| 54 | |||
| 55 | #~ say {$out_global} '#'.join "\t",( | ||
| 56 | #~ qw(File #Replicate #Frameshift Frameshift_size Threshold #Seq Ali_width #AA TruePositive TrueNegative FalsePositive FalseNegative MeanWithAllFrameshift) | ||
| 57 | #~ ); | ||
| 58 | #~ say {$out_frameshift_global} '#'.join "\t",( | ||
| 59 | #~ | ||
| 60 | #~ ); | ||
| 61 | #~ say {$out_fp_global} '#'.join "\t",( | ||
| 62 | #~ | ||
| 63 | #~ ); | ||
| 64 | |||
| 65 | |||
| 66 | my $factory = Factory->new( tax_dir => $ARGV_taxdir ); | ||
| 67 | my $code = $factory->code_for('Standard'); | ||
| 68 | |||
| 69 | for my $f ( @ARGV_infiles ) { | ||
| 70 | |||
| 71 | ### file is : $f | ||
| 72 | my $file = file($f); | ||
| 73 | my $basename = basename($file->stringify, qw(.fasta .fa .ali .fna .faa) ); | ||
| 74 | my $ali = Ali->load($file); | ||
| 75 | |||
| 76 | my @aa_base_seqs; | ||
| 77 | for my $s ($ali->all_seqs) { | ||
| 78 | push @aa_base_seqs, $code->translate($s,'+1'); | ||
| 79 | } | ||
| 80 | #~ my $aa_base_ali = Ali->new( seqs => \@aa_base_seqs , file => $basename.'_aligned.ali'); | ||
| 81 | my $aa_base_ali = Ali->new( seqs => \@aa_base_seqs , file => change_suffix($file, '_aligned.ali')); | ||
| 82 | $aa_base_ali->store($aa_base_ali->file); | ||
| 83 | |||
| 84 | my $gapnseq = get_gapnseq($aa_base_ali); | ||
| 85 | |||
| 86 | ### aligned ref file is : $aa_base_ali->file->stringify | ||
| 87 | |||
| 88 | ### Gblocks... | ||
| 89 | my $gblocks_loose = SeqMask->gblocks_mask($aa_base_ali, 'loose'); | ||
| 90 | my $gblocks_medium = SeqMask->gblocks_mask($aa_base_ali, 'medium'); | ||
| 91 | my $gblocks_strict = SeqMask->gblocks_mask($aa_base_ali, 'strict'); | ||
| 92 | |||
| 93 | # quickest way to fill the mask for now | ||
| 94 | my $gblocks_loose_neg = $gblocks_loose->negative_mask($aa_base_ali); | ||
| 95 | $gblocks_loose = $gblocks_loose_neg->negative_mask($aa_base_ali); | ||
| 96 | my $gblocks_medium_neg = $gblocks_medium->negative_mask($aa_base_ali); | ||
| 97 | $gblocks_medium = $gblocks_medium_neg->negative_mask($aa_base_ali); | ||
| 98 | my $gblocks_strict_neg = $gblocks_strict->negative_mask($aa_base_ali); | ||
| 99 | $gblocks_strict = $gblocks_strict_neg->negative_mask($aa_base_ali); | ||
| 100 | |||
| 101 | undef $gblocks_loose_neg; | ||
| 102 | undef $gblocks_medium_neg; | ||
| 103 | undef $gblocks_strict_neg; | ||
| 104 | ###### mask gblocks loose : join ' ', $gblocks_loose->all_states | ||
| 105 | |||
| 106 | ### BMGE... | ||
| 107 | my $bmge_loose = SeqMask->bmge_mask($aa_base_ali, 'loose'); | ||
| 108 | ###### mask bmge loose : join ' ', $bmge_loose->all_states | ||
| 109 | my $bmge_medium = SeqMask->bmge_mask($aa_base_ali, 'medium'); | ||
| 110 | my $bmge_strict = SeqMask->bmge_mask($aa_base_ali, 'strict'); | ||
| 111 | ### Done... | ||
| 112 | |||
| 113 | my $nseq = $aa_base_ali->count_seqs; | ||
| 114 | #### $nseq | ||
| 115 | my $width = $aa_base_ali->width; | ||
| 116 | #### $width | ||
| 117 | my $frequencies = gap_frequencies($aa_base_ali); | ||
| 118 | ####### $frequencies : join ' ', @$frequencies | ||
| 119 | |||
| 120 | # Missing substitution rates per site, investigating PAML codeml | ||
| 121 | |||
| 122 | ### CODEML... | ||
| 123 | my $rate_file = run_paml($aa_base_ali); | ||
| 124 | my $rates = extract_rates($rate_file); | ||
| 125 | ####### $rates : join ' ', @$rates | ||
| 126 | |||
| 127 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 128 | #~ my @frameshift_size = (30,99,150,300); | ||
| 129 | #~ my @frameshift_n = (1,5,10,$nseq); | ||
| 130 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 131 | |||
| 132 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 133 | ## INITIATING SIMULATION | ||
| 134 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 135 | #~ my $sf = (shuffle(@frameshift_size))[0]; | ||
| 136 | #~ my $nf = (shuffle(@frameshift_n))[0]; | ||
| 137 | |||
| 138 | ### $nf | ||
| 139 | ### $sf | ||
| 140 | |||
| 141 | INITIATION: | ||
| 142 | my $min_start = 0; | ||
| 143 | |||
| 144 | if ($ARGV_rep) { | ||
| 145 | |||
| 146 | |||
| 147 | my $bmge_loose_mean = sum0(($bmge_loose->all_states))/$aa_base_ali->width; | ||
| 148 | my $bmge_medium_mean = sum0(($bmge_medium->all_states))/$aa_base_ali->width; | ||
| 149 | my $bmge_strict_mean = sum0(($bmge_strict->all_states))/$aa_base_ali->width; | ||
| 150 | my $gblocks_loose_mean = sum0(($gblocks_loose->all_states))/$aa_base_ali->width; | ||
| 151 | my $gblocks_medium_mean = sum0(($gblocks_medium->all_states))/$aa_base_ali->width; | ||
| 152 | my $gblocks_strict_mean = sum0(($gblocks_strict->all_states))/$aa_base_ali->width; | ||
| 153 | my $gaps_mean = sum0(@$frequencies)/$aa_base_ali->width; | ||
| 154 | my $rates_mean = sum0(@$rates)/$aa_base_ali->width; | ||
| 155 | |||
| 156 | REPLICATE: | ||
| 157 | for my $nrep (1..$ARGV_rep) { | ||
| 158 | |||
| 159 | my $nf = defined($nf) ? $nf : (shuffle((1..5)))[0]; | ||
| 160 | my $sf = defined($sf) ? $sf : ((shuffle((10..100)))[0]); | ||
| 161 | |||
| 162 | my $simul_info = { | ||
| 163 | nf => $nf, | ||
| 164 | sf => $sf, | ||
| 165 | }; | ||
| 166 | |||
| 167 | # Random pick of subset sequences | ||
| 168 | my $subset = defined( $ARGV_subset ) ? $ARGV_subset : scalar($ali->all_seqs); | ||
| 169 | |||
| 170 | my $ali = Ali->load($file); | ||
| 171 | my $lookup = $ali->new_lookup; | ||
| 172 | my @new = (map { $_->full_id } shuffle($ali->all_seqs))[0..$subset-1]; | ||
| 173 | my $list = IdList->new( ids => \@new); | ||
| 174 | $ali = $list->reordered_ali($ali, $lookup); | ||
| 175 | #### List of picked seq : @new | ||
| 176 | |||
| 177 | ### Replicate number : $nrep | ||
| 178 | $ali->degap_seqs; | ||
| 179 | |||
| 180 | my @pick; | ||
| 181 | goto TRAD unless ($nf); | ||
| 182 | if ($nf < $nseq) { | ||
| 183 | #~ for my $i (1..$nf) { | ||
| 184 | #~ push @pick, int(rand($nseq-1)); | ||
| 185 | #~ } | ||
| 186 | @pick = (shuffle($ali->all_seqs))[0..($nf-1)]; | ||
| 187 | |||
| 188 | } else { | ||
| 189 | #~ @pick = (0..$nseq-1); | ||
| 190 | @pick = $ali->all_seqs; | ||
| 191 | } | ||
| 192 | |||
| 193 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 194 | ## RANDOMLY MODIFYING SEQ | ||
| 195 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 196 | |||
| 197 | for my $p (@pick) { | ||
| 198 | my $seq = $p->clone->degap; | ||
| 199 | my $codons = $seq->codons; | ||
| 200 | |||
| 201 | ### SeqId : $seq->full_id | ||
| 202 | |||
| 203 | my $n_codons = scalar(@$codons); | ||
| 204 | #### $n_codons | ||
| 205 | # !!!!!! Watch out this; should not be able to start the frameshift too early in seq | ||
| 206 | my $start = (shuffle(($min_start..($n_codons-1))))[0]; | ||
| 207 | #### $start | ||
| 208 | my $newframe = (shuffle(('+2','+3')))[0] ; | ||
| 209 | #### $newframe | ||
| 210 | my @range_new = ($newframe eq '+3') ? $start-1..$start-2+$sf : $start..$start-1+$sf; | ||
| 211 | ###### @range_new : join ' ', @range_new | ||
| 212 | my @toChange = @$codons[$start..$start-1+$sf]; | ||
| 213 | ###### @toChange | ||
| 214 | my $frame_codons = $seq->codons($newframe); | ||
| 215 | my @part = @$frame_codons[@range_new]; | ||
| 216 | ###### @part | ||
| 217 | |||
| 218 | # >>>>>>>>>>>>>>>>>>>>>> | ||
| 219 | ## If inserting STOP | ||
| 220 | # <<<<<<<<<<<<<<<<<<<<<< | ||
| 221 | my $haltat = -1; | ||
| 222 | if ($ARGV_noStop) { | ||
| 223 | my $first_undef = firstidx { !defined($_) } @part; | ||
| 224 | ###### $first_undef | ||
| 225 | $haltat = ($first_undef!=0) ? $first_undef-1 : 0; | ||
| 226 | } else { | ||
| 227 | my $first_stop = firstidx { $_ eq 'TAG' || $_ eq 'TAA' || $_ eq 'TGA' } @part; | ||
| 228 | my $first_undef = firstidx { !defined($_) } @part; | ||
| 229 | ###### $first_stop | ||
| 230 | ###### $first_undef | ||
| 231 | $haltat = ($first_stop==-1) ? ( ($first_undef!=0) ? $first_undef-1 : 0 ) : $first_stop; | ||
| 232 | } | ||
| 233 | ### $haltat | ||
| 234 | splice( @$codons, $start, scalar(@part), @part); # @codons modified | ||
| 235 | my $end; | ||
| 236 | if ($haltat >= 0) { | ||
| 237 | ### in halt at | ||
| 238 | #~ $ali->get_seq($i)->_set_seq(join('',@$codons[0..$start+$haltat])); | ||
| 239 | $p->_set_seq(join('',@$codons[0..$start+$haltat])); | ||
| 240 | $end = $start+$haltat; | ||
| 241 | } else { | ||
| 242 | ### not halt at | ||
| 243 | #~ $ali->get_seq($i)->_set_seq(join('',@$codons)); | ||
| 244 | $p->_set_seq(join('',@$codons)); | ||
| 245 | $end = $start+scalar(@part)-1; | ||
| 246 | } | ||
| 247 | |||
| 248 | FRAMESHIFT: | ||
| 249 | ##### BASE : $seq->seq | ||
| 250 | ##### NEW : $p->seq | ||
| 251 | |||
| 252 | warn 'New seq is not the same size. Not an issue if frameshift at end of seq' if ($seq->seq_len != $p->seq_len); | ||
| 253 | ##### AA_BASE : $code->translate( $seq, '+1')->seq | ||
| 254 | ##### AA_NEW : $code->translate( $p, '+1')->seq | ||
| 255 | |||
| 256 | # Should I consider the supposed frameshift position or the actual one due to STOP codon | ||
| 257 | my ($s,$e) = get_real_position($aa_base_ali, $seq->full_id, [$start,$end]); | ||
| 258 | #~ my ($s,$e) = get_real_position($aa_base_ali, $i, [$start,$end); | ||
| 259 | |||
| 260 | #### old start and end : ($start,$end) | ||
| 261 | #### new start and end : ($s,$e) | ||
| 262 | |||
| 263 | # TODO: mask out of blocks so shorter than alignment | ||
| 264 | # It means that it can recover undef value for the sum0 -> warning | ||
| 265 | # but not changing the Mean value | ||
| 266 | my $bmge_loose_fmean = sum0(($bmge_loose->all_states)[$s..$e])/(($e+1-$s)); | ||
| 267 | my $bmge_medium_fmean = sum0(($bmge_medium->all_states)[$s..$e])/(($e+1-$s)); | ||
| 268 | my $bmge_strict_fmean = sum0(($bmge_strict->all_states)[$s..$e])/(($e+1-$s)); | ||
| 269 | my $gblocks_loose_fmean = sum0(($gblocks_loose->all_states)[$s..$e])/(($e+1-$s)); | ||
| 270 | my $gblocks_medium_fmean = sum0(($gblocks_medium->all_states)[$s..$e])/(($e+1-$s)); | ||
| 271 | my $gblocks_strict_fmean = sum0(($gblocks_strict->all_states)[$s..$e])/(($e+1-$s)); | ||
| 272 | my $gaps_fmean = sum0(@$frequencies[$s..$e])/(($e+1-$s)); | ||
| 273 | my $rates_fmean = sum0(@$rates[$s..$e])/(($e+1-$s)); | ||
| 274 | my $gc_fmean = get_gc_mean($seq->edit_seq($start*3,$sf*3)); | ||
| 275 | #### $gc_fmean | ||
| 276 | |||
| 277 | push @{$simul_info->{frameshifts}}, { | ||
| 278 | id=>$seq->full_id, span => [$start+1,$end+1], type => $newframe, stop => ($haltat>=0)?1:0, | ||
| 279 | gaps_mean => $gaps_fmean, rates_mean => $rates_fmean, gc_mean => $gc_fmean, | ||
| 280 | gblocks_loose_mean => $gblocks_loose_fmean, gblocks_medium_mean => $gblocks_medium_fmean, gblocks_strict_mean => $gblocks_strict_fmean, | ||
| 281 | bmge_loose_mean => $bmge_loose_fmean, bmge_medium_mean => $bmge_medium_fmean, bmge_strict_mean => $bmge_strict_fmean, | ||
| 282 | }; | ||
| 283 | #~ say {$out} join "\t", ($f, $seq->full_id, (($start+1).'-'.($start+$sf)) ); | ||
| 284 | } | ||
| 285 | #~ next; | ||
| 286 | |||
| 287 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 288 | ## TRADUCTION | ||
| 289 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 290 | |||
| 291 | TRAD: | ||
| 292 | ### Translating and aligning... | ||
| 293 | my @aa_degap_seqs; | ||
| 294 | for my $s ($ali->all_seqs) { | ||
| 295 | push @aa_degap_seqs, $code->translate($s->clone->degap,'+1'); | ||
| 296 | } | ||
| 297 | my $aa_degap_ali = Ali->new( seqs => \@aa_degap_seqs ); | ||
| 298 | |||
| 299 | my $nuc_gapped_ali = apply_gapnseq($ali, $gapnseq); | ||
| 300 | my @aa_gapped_seqs; | ||
| 301 | for my $s ($nuc_gapped_ali->all_seqs) { | ||
| 302 | push @aa_gapped_seqs, $code->translate($s->clone,'+1'); | ||
| 303 | } | ||
| 304 | my $aa_gapped_ali = Ali->new( seqs => \@aa_gapped_seqs ); | ||
| 305 | |||
| 306 | |||
| 307 | #~ my $simufile = $basename.'-exonAA.fasta'; | ||
| 308 | #~ my $simuNucfile = $basename.'-exonNUC.fasta'; | ||
| 309 | #~ my $simualignedfile = $basename.'-exonAA_aligned.fasta'; | ||
| 310 | my $simufile = change_suffix($file, '-exonAA.fasta'); | ||
| 311 | my $simuNucfile = change_suffix($file, '-exonNUC.fasta'); | ||
| 312 | my $simualignedfile = change_suffix($file, '-exonAA_aligned.fasta'); | ||
| 313 | $ali->store_fasta($simuNucfile); | ||
| 314 | $aa_degap_ali->store_fasta($simufile); | ||
| 315 | |||
| 316 | my $cmd = "mafft --localpair --quiet --maxiterate 1000 --reorder --anysymbol --thread 2 $simufile > $simualignedfile"; | ||
| 317 | |||
| 318 | # try to robustly execute hmmsearch | ||
| 319 | my $ret_code = system( [ 0..127 ], $cmd); | ||
| 320 | if ($ret_code != 0) { | ||
| 321 | carp 'Warning: cannot execute mafft command'; | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | |||
| 325 | my $ali_simu = Ali->load($simualignedfile); | ||
| 326 | |||
| 327 | #~ my $ali_simu = align_ali($aa_ali); | ||
| 328 | |||
| 329 | $simul_info->{width} = $ali_simu->width; | ||
| 330 | $simul_info->{width_gapped} = $aa_gapped_ali->width; | ||
| 331 | $simul_info->{nseq} = $ali_simu->count_seqs; | ||
| 332 | |||
| 333 | # RECOVERING alignement information test | ||
| 334 | |||
| 335 | $ali_simu->degap_seqs; | ||
| 336 | my $count_aa; | ||
| 337 | for my $seq ($ali_simu->all_seqs) { | ||
| 338 | $count_aa += $seq->seq_len; | ||
| 339 | } | ||
| 340 | $simul_info->{count_aa} = $count_aa; | ||
| 341 | |||
| 342 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 343 | ## CLEANER | ||
| 344 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 345 | CLEANER: | ||
| 346 | |||
| 347 | my $t = $ARGV_threshold // 1; | ||
| 348 | my $c1 = $ARGV_cost1 // -0.15; | ||
| 349 | my $c2 = $ARGV_cost2 // -0.08; | ||
| 350 | my $c3 = $ARGV_cost3 // 0.15; | ||
| 351 | my $c4 = $ARGV_cost4 // 0.45; | ||
| 352 | |||
| 353 | my $thresh = $t; | ||
| 354 | my $costs = [$c1, $c2, $c3, $c4]; | ||
| 355 | |||
| 356 | my $args_global = { | ||
| 357 | ali => $simualignedfile, | ||
| 358 | ali_model => $simualignedfile, | ||
| 359 | threshold => $thresh, | ||
| 360 | changeID => 0, | ||
| 361 | delchar => ' ', | ||
| 362 | costs => $costs, | ||
| 363 | consider_X => 1, | ||
| 364 | perseq_profile => 0, | ||
| 365 | }; | ||
| 366 | |||
| 367 | ### Running HmmCleaner global for : $simualignedfile | ||
| 368 | my $cleaner_global = HmmCleaner->new($args_global); | ||
| 369 | |||
| 370 | # For parameter testing | ||
| 371 | |||
| 372 | #~ my @t_list = (1); | ||
| 373 | #~ my @c1_list = (-0.05,-0.075,-0.1,-0.125,-0.15,-0.175,-0.2,-0.225,-0.25); | ||
| 374 | #~ my @c2_list = (-0.02,-0.03,-0.04,-0.05,-0.06,-0.07,-0.08); | ||
| 375 | #~ my @c3_list = (0.05,0.075,0.1,0.125,0.15,0.175,0.2,0.225,0.25); | ||
| 376 | #~ my @c4_list = (0.4,0.45,0.5,0.55,0.6); | ||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | #~ for my $t (@t_list) { | ||
| 381 | #~ for my $c1 (@c1_list) { | ||
| 382 | #~ for my $c2 (@c2_list) { | ||
| 383 | #~ for my $c3 (@c3_list) { | ||
| 384 | #~ for my $c4 (@c4_list) { | ||
| 385 | #~ | ||
| 386 | #~ #### Threshold : $t | ||
| 387 | #~ #### Cost1 ' ': $c1 | ||
| 388 | #~ #### Cost2 '+': $c2 | ||
| 389 | #~ #### Cost3 'a': $c3 | ||
| 390 | #~ #### Cost4 'A': $c4 | ||
| 391 | #~ | ||
| 392 | #~ $cleaner_global->update_cleaners($t, [$c1, $c2, $c3, $c4] ); | ||
| 393 | |||
| 394 | my $log_global = $cleaner_global->get_log_simu; | ||
| 395 | |||
| 396 | unless (defined $log_global) { | ||
| 397 | ## Could not work anymore without the eval but bug was found | ||
| 398 | carp 'Warning: HmmCleaner global failed; Current file '.$f.' replicate '.$nrep.' will be missing'; | ||
| 399 | next REPLICATE; | ||
| 400 | } | ||
| 401 | ##### $log_global | ||
| 402 | for my $clean ($cleaner_global->all_cleaners) { | ||
| 403 | if (any { $_->{id} eq $clean->seq->full_id } @{$simul_info->{frameshifts}}) { | ||
| 404 | ##### Seqid : $clean->seq->full_id | ||
| 405 | ##### nogap_shifts : $clean->nogap_shifts | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | ### Cleaning tmpfile... | ||
| 410 | |||
| 411 | unless ($ARGV_verbosity>5) { | ||
| 412 | my $dir = dir(); | ||
| 413 | my @tmpfiles = grep {substr($_->stringify,0,9) eq "./tmpfile"} $dir->children; | ||
| 414 | $_->remove for (@tmpfiles); | ||
| 415 | } | ||
| 416 | |||
| 417 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 418 | ## COMPARE | ||
| 419 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 420 | |||
| 421 | COMPARE: | ||
| 422 | |||
| 423 | ##### Simulation information : $simul_info | ||
| 424 | if ($nf) { | ||
| 425 | my ($results_file, $results_frameshifts, $fps); | ||
| 426 | |||
| 427 | ## Global | ||
| 428 | ($results_file, $results_frameshifts, $fps) = get_stat_file($simul_info, $log_global, $simualignedfile, $aa_base_ali); | ||
| 429 | # File #Replicate #Frameshift Frameshift_size Threshold cost1 cost2 cost3 cost4 #Seq Ali_width #AA | ||
| 430 | # TruePositive TrueNegative FalsePositive FalseNegative gaps rates | ||
| 431 | # gb_loose gb_medium gb_strict bmge_loose bmge_medium bmge_strict | ||
| 432 | say {$out_global} join "\t", ( | ||
| 433 | $f, $nrep, $nf, $sf, $t, $c1, $c2, $c3, $c4, $nseq, $simul_info->{width}, $count_aa, | ||
| 434 | $results_file->{vp}, $results_file->{vn}, $results_file->{fp}, $results_file->{fn}, $gaps_mean, $rates_mean, | ||
| 435 | $gblocks_loose_mean, $gblocks_medium_mean, $gblocks_strict_mean, $bmge_loose_mean, $bmge_medium_mean, $bmge_strict_mean); | ||
| 436 | for my $frameshift ( @{ $results_frameshifts } ) { | ||
| 437 | |||
| 438 | # File SeqId Threshold cost1 cost2 cost3 cost4 FrameshiftType | ||
| 439 | # #Frameshift TruePositive Frameshift_size FalsePositive | ||
| 440 | # Start End Interrupted | ||
| 441 | # gaps rates GC | ||
| 442 | # gb_loose gb_medium gb_strict bmge_loose bmge_medium bmge_strict | ||
| 443 | say {$out_frameshift_global} join "\t", ( | ||
| 444 | $f, $frameshift->{id}, $t, $c1, $c2, $c3, $c4, $frameshift->{type}, | ||
| 445 | $nf, $frameshift->{vp}, 1+$frameshift->{span}->[1]-$frameshift->{span}->[0], $frameshift->{fp}, | ||
| 446 | $frameshift->{span}->[0], $frameshift->{span}->[1], $frameshift->{stop}, | ||
| 447 | $frameshift->{gaps_mean}, $frameshift->{rates_mean}, $frameshift->{gc_mean}, | ||
| 448 | $frameshift->{gblocks_loose_mean}, $frameshift->{gblocks_medium_mean}, $frameshift->{gblocks_strict_mean}, | ||
| 449 | $frameshift->{bmge_loose_mean}, $frameshift->{bmge_medium_mean}, $frameshift->{bmge_strict_mean}, | ||
| 450 | ); | ||
| 451 | } | ||
| 452 | |||
| 453 | for my $fp (@$fps) { | ||
| 454 | say {$out_fp_global} join "\t", ( | ||
| 455 | $f, $fp->{id}, $fp->{start}, $fp->{end}, $t, $c1, $c2, $c3, $c4, | ||
| 456 | sum0(@$frequencies[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # gaps | ||
| 457 | sum0(@$rates[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # rates | ||
| 458 | sum0(($bmge_loose->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # bmge loose | ||
| 459 | sum0(($bmge_medium->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # bmge medium | ||
| 460 | sum0(($bmge_strict->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # bmge strict | ||
| 461 | sum0(($gblocks_loose->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # gblocks loose | ||
| 462 | sum0(($gblocks_medium->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # gblocks medium | ||
| 463 | sum0(($gblocks_strict->all_states)[$fp->{start}-1..$fp->{end}-1])/(($fp->{end}+1-$fp->{start})), # gblocks strict | ||
| 464 | ); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | } | ||
| 469 | } else { | ||
| 470 | # In case of preflight run without replicate | ||
| 471 | } | ||
| 472 | |||
| 473 | } | ||
| 474 | |||
| 475 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 476 | ## SUB | ||
| 477 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 478 | |||
| 479 | |||
| 480 | sub size_overlap { | ||
| 481 | #~ ### In Size_overlap... | ||
| 482 | my $simu_span = shift; | ||
| 483 | my $clean_spans = shift; | ||
| 484 | |||
| 485 | my $count_overlap; | ||
| 486 | SPAN: | ||
| 487 | for my $cspan (@$clean_spans) { | ||
| 488 | my $ret = _overlap(@$simu_span,@$cspan); | ||
| 489 | #~ ### $ret | ||
| 490 | $count_overlap += $ret; | ||
| 491 | #~ ### $count_overlap | ||
| 492 | } | ||
| 493 | |||
| 494 | return $count_overlap; | ||
| 495 | } | ||
| 496 | |||
| 497 | # Given two segment a-b and c-d, give the common part size | ||
| 498 | # assume a<b and c<d | ||
| 499 | # This sub is coming from the original script | ||
| 500 | sub _overlap { | ||
| 501 | #~ ### In _overlap... | ||
| 502 | my ($a, $b, $c, $d) = @_; | ||
| 503 | my $ret = 0; | ||
| 504 | if( ($c <= $b) and ($b <= $d) ){ | ||
| 505 | $ret = $b-$c+1; | ||
| 506 | } | ||
| 507 | if( ($c <= $a) and ($a <= $d) ){ | ||
| 508 | if($ret == 0){ | ||
| 509 | $ret = $d-$a+1; | ||
| 510 | } | ||
| 511 | else{ | ||
| 512 | $ret-=$a-$c; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | if( ($a < $c) and ($d < $b) ){ | ||
| 516 | $ret = $d-$c+1; | ||
| 517 | } | ||
| 518 | return $ret; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub gap_frequencies { | ||
| 522 | my $ali = shift; | ||
| 523 | |||
| 524 | my @frequencies; | ||
| 525 | for my $pos_idx (0..$ali->width-1) { | ||
| 526 | my @col = map { $_->state_at($pos_idx) } $ali->all_seqs; | ||
| 527 | |||
| 528 | my $freq = (grep{ $_ =~ $GAP }@col)/scalar(@col); | ||
| 529 | |||
| 530 | push @frequencies, $freq; | ||
| 531 | } | ||
| 532 | |||
| 533 | return \@frequencies; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub align_ali { | ||
| 537 | my $ali = shift; | ||
| 538 | |||
| 539 | my $tmpfilename = $ali->filename.'tmp'; | ||
| 540 | |||
| 541 | $ali->store_fasta($tmpfilename); | ||
| 542 | |||
| 543 | my $outfile_name = $ali->file; | ||
| 544 | |||
| 545 | my $cmd = "mafft --localpair --quiet --maxiterate 1000 --reorder --anysymbol --thread 2 $tmpfilename > $outfile_name"; | ||
| 546 | |||
| 547 | # try to robustly execute hmmsearch | ||
| 548 | my $ret_code = system( [ 0, 127 ], $cmd); | ||
| 549 | if ($ret_code == 127) { | ||
| 550 | carp 'Warning: cannot execute mafft command'; | ||
| 551 | return; | ||
| 552 | } | ||
| 553 | |||
| 554 | my $aligned_ali = Ali->load($outfile_name); | ||
| 555 | #~ $aligned_ali->restore_ids($mapper); | ||
| 556 | |||
| 557 | return $aligned_ali; | ||
| 558 | } | ||
| 559 | |||
| 560 | sub get_gc_mean { | ||
| 561 | my $str = shift; | ||
| 562 | #### GC string : $str | ||
| 563 | my @residues = split //, uc($str); | ||
| 564 | my $gc_count = grep { $_ eq 'G' || $_ eq 'C' } @residues; | ||
| 565 | return @residues ? $gc_count/scalar(@residues) : 0; | ||
| 566 | } | ||
| 567 | |||
| 568 | sub get_stat_file { | ||
| 569 | |||
| 570 | my $simul_info = shift; | ||
| 571 | my $log = shift; | ||
| 572 | my $simualignedfile = shift; | ||
| 573 | my $aa_base_ali = shift; | ||
| 574 | |||
| 575 | my %results_file; | ||
| 576 | my @results_frameshifts; | ||
| 577 | |||
| 578 | my $vp = 0; | ||
| 579 | my $simu_aa = 0; | ||
| 580 | my $clean_aa = 0; | ||
| 581 | |||
| 582 | ##### Number of frameshift in simul_info : scalar( @{ $simul_info->{frameshifts} } ) | ||
| 583 | |||
| 584 | for (my $j=0; $j<@{ $simul_info->{frameshifts} }; $j++) { | ||
| 585 | |||
| 586 | my $frameshift = $simul_info->{frameshifts}->[$j]; | ||
| 587 | ##### $frameshift | ||
| 588 | my $simu_span = $frameshift->{span}; | ||
| 589 | ##### $simu_span | ||
| 590 | my $cleaner_spans = $log->{$simualignedfile}->{ $frameshift->{id} }; | ||
| 591 | ##### $cleaner_spans | ||
| 592 | |||
| 593 | # True positive calc | ||
| 594 | my $actual_vp = (defined $cleaner_spans) ? size_overlap($simu_span, $cleaner_spans) : 0 ; | ||
| 595 | |||
| 596 | $frameshift->{vp} = $actual_vp; | ||
| 597 | $frameshift->{sf} = $simul_info->{sf}; | ||
| 598 | $vp += $actual_vp; | ||
| 599 | |||
| 600 | # False negative | ||
| 601 | $simu_aa += 1+$simu_span->[1]-$simu_span->[0]; | ||
| 602 | |||
| 603 | my $frameshift_clean_aa = 0; | ||
| 604 | # False positive | ||
| 605 | for my $span ( @$cleaner_spans ) { | ||
| 606 | $frameshift_clean_aa += 1+$$span[1]-$$span[0]; | ||
| 607 | } | ||
| 608 | my $actual_fp = $frameshift_clean_aa-( 1+$simu_span->[1]-$simu_span->[0] ); | ||
| 609 | $frameshift->{fp} = $actual_fp; | ||
| 610 | |||
| 611 | push @results_frameshifts, $frameshift; | ||
| 612 | } | ||
| 613 | |||
| 614 | # True positive | ||
| 615 | $results_file{vp} = $vp; | ||
| 616 | #### $vp | ||
| 617 | |||
| 618 | # False negative | ||
| 619 | #### $simu_aa | ||
| 620 | $results_file{fn} = $simu_aa-$vp; | ||
| 621 | |||
| 622 | die 'VP is wrong, larger than simu_aa' if ($vp > $simu_aa); | ||
| 623 | |||
| 624 | ### False positive... | ||
| 625 | my @frameshifts = @{$simul_info->{frameshifts}}; | ||
| 626 | |||
| 627 | my @fp; | ||
| 628 | my %all_cleaners_span = %{ $log->{$simualignedfile} }; | ||
| 629 | for my $org (keys %all_cleaners_span) { | ||
| 630 | ### $org | ||
| 631 | if (my @gf = grep {$_->{id} eq $org} @frameshifts) { | ||
| 632 | for my $span ( @{ $all_cleaners_span{$org} } ) { | ||
| 633 | $clean_aa += 1+$$span[1]-$$span[0]; | ||
| 634 | ### $span | ||
| 635 | |||
| 636 | unless (_overlap( @{ $gf[0]->{span} }, @$span)) { | ||
| 637 | ### not overlapping with : $gf[0]->{span} | ||
| 638 | my ($s,$e) = get_real_position($aa_base_ali, $org, $span); | ||
| 639 | push @fp, { | ||
| 640 | id => $org, | ||
| 641 | start => $s, | ||
| 642 | end => $e, | ||
| 643 | }; | ||
| 644 | |||
| 645 | } else { | ||
| 646 | ### overlapping with : $gf[0]->{span} | ||
| 647 | } | ||
| 648 | |||
| 649 | } | ||
| 650 | } else { | ||
| 651 | for my $span ( @{ $all_cleaners_span{$org} } ) { | ||
| 652 | $clean_aa += 1+$$span[1]-$$span[0]; | ||
| 653 | ### $span | ||
| 654 | my ($s,$e) = get_real_position($aa_base_ali, $org, $span); | ||
| 655 | push @fp, { | ||
| 656 | id => $org, | ||
| 657 | start => $s, | ||
| 658 | end => $e, | ||
| 659 | }; | ||
| 660 | } | ||
| 661 | } | ||
| 662 | } | ||
| 663 | #### $clean_aa | ||
| 664 | $results_file{fp} = $clean_aa-$vp; | ||
| 665 | |||
| 666 | # True Negative | ||
| 667 | $results_file{vn} = $simul_info->{count_aa}-( $results_file{vp} + $results_file{fp} + $results_file{fn} ); | ||
| 668 | |||
| 669 | return (\%results_file,\@results_frameshifts, \@fp); | ||
| 670 | } | ||
| 671 | |||
| 672 | |||
| 673 | #~ sub get_stat_falsepositive { | ||
| 674 | #~ | ||
| 675 | #~ my $simul_info = shift; | ||
| 676 | #~ my $log = shift; | ||
| 677 | #~ my $simualignedfile = shift; | ||
| 678 | #~ | ||
| 679 | #~ my @frameshifts = @{$simul_info->{frameshifts}}; | ||
| 680 | #~ | ||
| 681 | #~ for my $org ( sort keys %{$log->{$simualignedfile}} ) { | ||
| 682 | #~ | ||
| 683 | #~ grep { $_->{id} eq $org } @frameshifts; | ||
| 684 | #~ | ||
| 685 | #~ | ||
| 686 | #~ } | ||
| 687 | #~ | ||
| 688 | #~ return (\%results_file,\@results_frameshifts); | ||
| 689 | #~ } | ||
| 690 | |||
| 691 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 692 | ## SUB for gap toggling | ||
| 693 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 694 | |||
| 695 | sub get_gapnseq { | ||
| 696 | |||
| 697 | my $ali = shift; | ||
| 698 | |||
| 699 | my %hash; | ||
| 700 | for my $seq ($ali->all_seqs) { | ||
| 701 | my $id = $seq->full_id; | ||
| 702 | $hash{$id} = get_part_length($seq->seq); | ||
| 703 | } | ||
| 704 | |||
| 705 | return \%hash; | ||
| 706 | } | ||
| 707 | |||
| 708 | sub get_part_length { | ||
| 709 | my $str = shift; | ||
| 710 | |||
| 711 | ## Looking for gaps in full sequence... | ||
| 712 | my @gaps_n_seqs; | ||
| 713 | my $push = 0; | ||
| 714 | foreach my $tab ( split /$GAP/, $str ) { | ||
| 715 | if( length($tab) == 0){ | ||
| 716 | $push++; | ||
| 717 | }else{ | ||
| 718 | push(@gaps_n_seqs, $push); # gap_length | ||
| 719 | push(@gaps_n_seqs, length($tab)); # seq_length | ||
| 720 | $push = 1; | ||
| 721 | } | ||
| 722 | } | ||
| 723 | |||
| 724 | return \@gaps_n_seqs; | ||
| 725 | } | ||
| 726 | |||
| 727 | # Push gap from aa alignment onto nucleotide | ||
| 728 | sub apply_gapnseq { | ||
| 729 | |||
| 730 | my $ali = shift; | ||
| 731 | my $gapnseq = shift; | ||
| 732 | |||
| 733 | my @gap_nuc; | ||
| 734 | for my $seq ($ali->all_seqs) { | ||
| 735 | my $id = $seq->full_id; | ||
| 736 | #~ ### $id | ||
| 737 | |||
| 738 | my $codons = $seq->codons; | ||
| 739 | |||
| 740 | my @new; | ||
| 741 | my @gaps_n_seqs = @{ $$gapnseq{$id} }; | ||
| 742 | |||
| 743 | my $last_start = 0; | ||
| 744 | for (my $i=0; $i < @gaps_n_seqs; $i+=2) { | ||
| 745 | |||
| 746 | my $gap_number = $gaps_n_seqs[$i]; | ||
| 747 | if ($gap_number) { | ||
| 748 | push(@new, '***' ) for 1..$gap_number; | ||
| 749 | } | ||
| 750 | my $codons_number = $gaps_n_seqs[$i+1]; | ||
| 751 | push @new, @$codons[$last_start..($last_start+$codons_number-1)]; | ||
| 752 | |||
| 753 | $last_start += $codons_number; | ||
| 754 | |||
| 755 | #~ ### Seq at the moment: join '', @new | ||
| 756 | } | ||
| 757 | |||
| 758 | push @gap_nuc, Seq->new( seq => join( '', @new), seq_id => $id ); | ||
| 759 | } | ||
| 760 | |||
| 761 | my $new_ali = Ali->new( seqs => \@gap_nuc ); | ||
| 762 | |||
| 763 | return $new_ali; | ||
| 764 | } | ||
| 765 | |||
| 766 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | ||
| 767 | ## SUB for PAML | ||
| 768 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 769 | |||
| 770 | sub run_paml { | ||
| 771 | my $ali = shift; | ||
| 772 | |||
| 773 | my $rates_file = file(change_suffix($ali->file, '.rates')); | ||
| 774 | |||
| 775 | if (-e $rates_file->stringify) { | ||
| 776 | ### Found Rates file, skipping RAxML and PAML... | ||
| 777 | return $rates_file; | ||
| 778 | } else { | ||
| 779 | my $idm = $ali->std_mapper; | ||
| 780 | $ali->shorten_ids($idm); | ||
| 781 | my $idmfile = change_suffix($ali->file, '.idm'); | ||
| 782 | $idm->store($idmfile); | ||
| 783 | |||
| 784 | my $phyfile = substr(change_suffix($ali->file, '.p80'),2); | ||
| 785 | my $treefile = file(change_suffix($ali->file, '.tre')); | ||
| 786 | unless ( -e $treefile) { | ||
| 787 | my $args = { clean => 1, short => 0, chunk => -1 }; | ||
| 788 | $ali->store_phylip($phyfile, $args); | ||
| 789 | $treefile = run_raxml($phyfile); | ||
| 790 | } else { | ||
| 791 | ### Found tree file, skipping RAxML... | ||
| 792 | } | ||
| 793 | my $pamlfile = store_paml($ali); | ||
| 794 | $ali->restore_ids($idm); | ||
| 795 | |||
| 796 | # fill-in template | ||
| 797 | my $vars = { | ||
| 798 | seqfile => $pamlfile->stringify, | ||
| 799 | treefile => $treefile->stringify, | ||
| 800 | }; | ||
| 801 | ### $vars | ||
| 802 | |||
| 803 | my $tt_str = <<'EOT'; | ||
| 804 | seqfile = [% seqfile %] | ||
| 805 | outfile = test_codeml.rst * main result file | ||
| 806 | treefile = [% treefile %] | ||
| 807 | noisy = 0 | ||
| 808 | verbose = 0 | ||
| 809 | runmode = 0 | ||
| 810 | seqtype = 2 | ||
| 811 | aaRatefile = /home/adf/lg.dat | ||
| 812 | model = 3 | ||
| 813 | Mgene = 0 | ||
| 814 | fix_alpha = 0 * 0: estimate gamma shape parameter; 1: fix it at alpha | ||
| 815 | alpha = 0.1 * initial or fixed alpha, 0:infinity (constant rate) | ||
| 816 | Malpha = 0 * different alphas for genes | ||
| 817 | ncatG = 8 * # of categories in dG of NSsites models | ||
| 818 | getSE = 0 * 0: don't want them, 1: want S.E.s of estimates | ||
| 819 | RateAncestor = 1 * (0,1,2): rates (alpha>0) or ancestral states (1 or 2) | ||
| 820 | Small_Diff = .5e-6 | ||
| 821 | cleandata = 0 * remove sites with ambiguity data (1:yes, 0:no)? | ||
| 822 | method = 1 * 0: simultaneous; 1: one branch at a time | ||
| 823 | EOT | ||
| 824 | |||
| 825 | my $tt = Template->new; | ||
| 826 | $tt->process(\$tt_str, $vars, "codeml.ctl"); | ||
| 827 | |||
| 828 | my $cmd = "codeml codeml.ctl"; | ||
| 829 | |||
| 830 | # try to robustly execute codeml | ||
| 831 | my $ret_code = system( [ 0, 127 ], $cmd); | ||
| 832 | if ($ret_code == 127) { | ||
| 833 | carp 'Warning: cannot execute codeml command'; | ||
| 834 | return; | ||
| 835 | } | ||
| 836 | |||
| 837 | $rates_file = file('rates')->move_to($rates_file->stringify); | ||
| 838 | |||
| 839 | return $rates_file; | ||
| 840 | } | ||
| 841 | } | ||
| 842 | |||
| 843 | sub run_raxml { | ||
| 844 | my $file = shift; | ||
| 845 | |||
| 846 | ### RAxML... | ||
| 847 | #~ my $cmd = "raxml -m PROTGAMMALGF -p 12345 -s $file -n tree"; | ||
| 848 | my $cmd = "raxml-thread -m PROTGAMMALGF -p 12345 -s $file -n ".$file." -T 16"; | ||
| 849 | |||
| 850 | # try to robustly execute raxml | ||
| 851 | my $ret_code = system( [ 0, 127 ], $cmd); | ||
| 852 | if ($ret_code == 127) { | ||
| 853 | carp 'Warning: cannot execute RAxML command'; | ||
| 854 | return; | ||
| 855 | } | ||
| 856 | |||
| 857 | my $treefile = 'RAxML_bestTree.'.$file; | ||
| 858 | my $lines = file($treefile)->slurp(iomode => '<:encoding(UTF-8)'); | ||
| 859 | $lines =~ s/Node\d+//g; | ||
| 860 | #~ my $result = system("perl -i.bak -nle 's/Node\d+//g; print;' $treefile"); | ||
| 861 | my $filename = change_suffix($file, '.tre'); | ||
| 862 | my $pamltree = file($filename); | ||
| 863 | $pamltree->spew($lines); | ||
| 864 | |||
| 865 | return $pamltree if -e $pamltree->stringify; | ||
| 866 | } | ||
| 867 | |||
| 868 | sub store_paml { | ||
| 869 | my $self = shift; # BMC::Ali | ||
| 870 | my $args = shift // {}; # HashRef (should not be empty...) | ||
| 871 | |||
| 872 | my $outfile = basename($self->file, qw(.fa .fasta .ali .faa .fna)).'.paml'; | ||
| 873 | my $degap = $args->{degap} // 0; | ||
| 874 | my $clean = $args->{clean} // 0; | ||
| 875 | my $chunk = $args->{chunk} // 60; | ||
| 876 | my $nowrap = $chunk < 0 ? 1 : 0; | ||
| 877 | my $is_aligned = $self->is_aligned; | ||
| 878 | |||
| 879 | open my $out, '>', $outfile; | ||
| 880 | |||
| 881 | say {$out} join ' ', ($self->count_seqs, $self->width); | ||
| 882 | |||
| 883 | for my $seq ($self->all_seqs) { | ||
| 884 | say {$out} $seq->foreign_id; | ||
| 885 | |||
| 886 | # optionally clean and/or degap seq | ||
| 887 | $seq = $seq->clone if $clean || $degap; # clone seq only if needed | ||
| 888 | $seq->gapify('X') if $clean; | ||
| 889 | $seq->degap if $degap; | ||
| 890 | |||
| 891 | my $width = $seq->seq_len; | ||
| 892 | $chunk = $width if $nowrap; # optionally disable wrap | ||
| 893 | |||
| 894 | for (my $site = 0; $site < $width; $site += $chunk) { | ||
| 895 | my $str = $seq->edit_seq($site, $chunk); | ||
| 896 | $str =~ s{$GAP}{-}xmsg if $is_aligned; # restore '-' when aligned | ||
| 897 | say {$out} $str; | ||
| 898 | } | ||
| 899 | } | ||
| 900 | |||
| 901 | return file($outfile); | ||
| 902 | } | ||
| 903 | |||
| 904 | sub extract_rates { | ||
| 905 | my $file = shift; | ||
| 906 | my @lines = grep {$_ =~ m/^\s+\d+/} $file->slurp( chomp => 1); | ||
| 907 | my @rates = map { my @F = split /\s+/, $_; $F[4]; } @lines; | ||
| 908 | |||
| 909 | return \@rates; | ||
| 910 | } | ||
| 911 | |||
| 912 | sub get_real_position { | ||
| 913 | my $ali = shift; | ||
| 914 | my $seq_id = shift; | ||
| 915 | my $span = shift; | ||
| 916 | |||
| 917 | ##### In get Real Position | ||
| 918 | |||
| 919 | my @residues = split //, $ali->get_seq_with_id($seq_id)->seq; | ||
| 920 | my @fromstart = @residues[0..$$span[0]]; | ||
| 921 | |||
| 922 | my $count_gap_s = grep { $_ =~ $GAP } @fromstart; | ||
| 923 | #### $count_gap_s | ||
| 924 | |||
| 925 | if ($count_gap_s) { | ||
| 926 | my $nc = grep { $_ =~ $GAP } @residues[0..$$span[0]+$count_gap_s]; | ||
| 927 | while ( $nc != $count_gap_s ) { | ||
| 928 | $count_gap_s = $nc; | ||
| 929 | #### $count_gap_s | ||
| 930 | $nc = grep { $_ =~ $GAP } @residues[0..$$span[0]+$count_gap_s]; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | |||
| 934 | my $start = $$span[0]+$count_gap_s; | ||
| 935 | |||
| 936 | my $count_gap_e = grep { $_ =~ $GAP } @residues[$start..$$span[1]+$count_gap_s]; | ||
| 937 | #### $count_gap_e | ||
| 938 | if ($count_gap_e && ($$span[1]+$count_gap_s+$count_gap_e)<$#residues) { | ||
| 939 | my $nc = grep { $_ =~ $GAP } @residues[$start..$$span[1]+$count_gap_s+$count_gap_e]; | ||
| 940 | while ( $nc != $count_gap_e ) { | ||
| 941 | $count_gap_e = $nc; | ||
| 942 | #### $count_gap_e | ||
| 943 | $nc = grep { $_ =~ $GAP } @residues[$start..$$span[1]+$count_gap_s+$count_gap_e]; | ||
| 944 | } | ||
| 945 | } | ||
| 946 | |||
| 947 | my $end = ($$span[1]+$count_gap_s+$count_gap_e > $#residues) ? $#residues : $$span[1]+$count_gap_s+$count_gap_e ; | ||
| 948 | |||
| 949 | return ($start , $end); | ||
| 950 | } | ||
| 951 | |||
| 952 | |||
| 953 | ### End of script... | ||
| 954 | |||
| 955 | __END__ | ||
| 956 | |||
| 957 | =pod | ||
| 958 | |||
| 959 | =encoding UTF-8 | ||
| 960 | |||
| 961 | =head1 NAME | ||
| 962 | |||
| 963 | frameshiftSimul_final.pl - Simulation for HmmCleaner | ||
| 964 | |||
| 965 | =head1 VERSION | ||
| 966 | |||
| 967 | version 0.180750 | ||
| 968 | |||
| 969 | =head1 USAGE | ||
| 970 | |||
| 971 | frameshiftSimul_final.pl <infiles> --taxdir=<taxdir> [--noX --noStop -nf <nf> -sf <sf> -rep <rep> --profile=<profile>] | ||
| 972 | |||
| 973 | =head1 REQUIRED ARGUMENTS | ||
| 974 | |||
| 975 | =over | ||
| 976 | |||
| 977 | =item <infiles> | ||
| 978 | |||
| 979 | list of alignment file to check with HMMCleaner | ||
| 980 | |||
| 981 | =for Euclid: infiles.type: readable | ||
| 982 | repeatable | ||
| 983 | |||
| 984 | =item --taxdir=<taxdir> | ||
| 985 | |||
| 986 | Path to local mirror of the NCBI Taxonomy database as obtain from setup-taxdir.pl | ||
| 987 | |||
| 988 | =for Euclid: taxdir.type: string | ||
| 989 | |||
| 990 | =back | ||
| 991 | |||
| 992 | =head1 OPTIONS | ||
| 993 | |||
| 994 | =over | ||
| 995 | |||
| 996 | =item -t[hreshold] <threshold> | ||
| 997 | |||
| 998 | threshold value determining shifts in sequence compare to HMM model | ||
| 999 | |||
| 1000 | =for Euclid: threshold.type: 0+integer | ||
| 1001 | threshold.default: 1 | ||
| 1002 | |||
| 1003 | =item -cost1=<cost1> | ||
| 1004 | |||
| 1005 | cost of blank character in profile alignment | ||
| 1006 | |||
| 1007 | =for Euclid: cost1.type: num | ||
| 1008 | cost1.default: -0.15 | ||
| 1009 | |||
| 1010 | =item -cost2=<cost2> | ||
| 1011 | |||
| 1012 | cost of + character in profile alignment | ||
| 1013 | |||
| 1014 | =for Euclid: cost2.type: num | ||
| 1015 | cost2.default: -0.08 | ||
| 1016 | |||
| 1017 | =item -cost3=<cost3> | ||
| 1018 | |||
| 1019 | cost of lowercase character in profile alignment | ||
| 1020 | |||
| 1021 | =for Euclid: cost3.type: num | ||
| 1022 | cost3.default: 0.15 | ||
| 1023 | |||
| 1024 | =item -cost4=<cost4> | ||
| 1025 | |||
| 1026 | cost of uppercase character in profile alignment | ||
| 1027 | |||
| 1028 | =for Euclid: cost4.type: num | ||
| 1029 | cost4.default: 0.45 | ||
| 1030 | |||
| 1031 | =item -nf <nf> | ||
| 1032 | |||
| 1033 | number of frameshift | ||
| 1034 | |||
| 1035 | =for Euclid: nf.type: 0+int | ||
| 1036 | |||
| 1037 | =item -sf <sf> | ||
| 1038 | |||
| 1039 | size of frameshift | ||
| 1040 | |||
| 1041 | =for Euclid: sf.type: +int | ||
| 1042 | |||
| 1043 | =item -rep <rep> | ||
| 1044 | |||
| 1045 | number of replicate for one file | ||
| 1046 | |||
| 1047 | =for Euclid: rep.type: 0+int | ||
| 1048 | rep.default: 100 | ||
| 1049 | |||
| 1050 | =item -subset <subset> | ||
| 1051 | |||
| 1052 | number of seq to keep in file at random | ||
| 1053 | |||
| 1054 | =for Euclid: subset.type: 0+int | ||
| 1055 | |||
| 1056 | =item --noX | ||
| 1057 | |||
| 1058 | Determine that X characters don't have to be consider by HMMER | ||
| 1059 | |||
| 1060 | =item --noStop | ||
| 1061 | |||
| 1062 | Not stopping translation at STOP codon | ||
| 1063 | |||
| 1064 | =item -v[erbosity]=<level> | ||
| 1065 | |||
| 1066 | Verbosity level for logging to STDERR [default: 0]. Available levels range from | ||
| 1067 | 0 to 5. | ||
| 1068 | |||
| 1069 | =for Euclid: level.type: int, level >= 0 && level <= 5 | ||
| 1070 | level.default: 0 | ||
| 1071 | |||
| 1072 | =item --version | ||
| 1073 | |||
| 1074 | =item --usage | ||
| 1075 | |||
| 1076 | =item --help | ||
| 1077 | |||
| 1078 | =item --man | ||
| 1079 | |||
| 1080 | Print the usual program information | ||
| 1081 | |||
| 1082 | =back | ||
| 1083 | |||
| 1084 | =head1 AUTHOR | ||
| 1085 | |||
| 1086 | Arnaud Di Franco <arnaud.difranco@gmail.fr> | ||
| 1087 | |||
| 1088 | =head1 COPYRIGHT AND LICENSE | ||
| 1089 | |||
| 1090 | This software is copyright (c) 2018 by Arnaud Di Franco. | ||
| 1091 | |||
| 1092 | This is free software; you can redistribute it and/or modify it under | ||
| 1093 | the same terms as the Perl 5 programming language system itself. | ||
| 1094 | |||
| 1095 | =cut |