This file is indexed.

/usr/share/perl5/Bio/Tools/Run/Phylo/Phyml.pm is in libbio-perl-run-perl 1.7.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
#
# BioPerl module for Bio::Tools::Run::Phylo::Phyml
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Heikki Lehvaslaiho
#
# Copyright Heikki Lehvaslaiho
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::Tools::Run::Phylo::Phyml - Wrapper for rapid reconstruction of phylogenies using Phyml

=head1 SYNOPSIS

  use Bio::Tools::Run::Phylo::Phyml;

  #  Make a Phyml factory
  $factory = Bio::Tools::Run::Phylo::Phyml->new(-verbose => 2);
  # it defaults to protein alignment
  # change parameters
  $factory->model('Dayhoff');
  #  Pass the factory an alignment and run
  $inputfilename = 't/data/protpars.phy';
  $tree = $factory->run($inputfilename); # $tree is a Bio::Tree::Tree object.


  # or set parameters at object creation
  my %args = (
      -data_type => 'dna',
      -model => 'HKY',
      -kappa => 4,
      -invar => 'e',
      -category_number => 4,
      -alpha => 'e',
      -tree => 'BIONJ',
      -opt_topology => '0',
      -opt_lengths => '1',
      );
  $factory = Bio::Tools::Run::Phylo::Phyml->new(%args);
  # if you need the output files do
  $factory->save_tempfiles(1);
  $factory->tempdir($workdir);

  # and get a Bio::Align::AlignI (SimpleAlign) object from somewhere
  $tree = $factory->run($aln);

=head1 DESCRIPTION

This is a wrapper for running the phyml application by Stephane
Guindon and Olivier Gascuel. You can download it from:
http://atgc.lirmm.fr/phyml/

=head2 Installing

After downloading, you need to rename a the copy of the program that
runs under your operating system. I.e. C<phyml_linux> into C<phyml>.

You will need to help this Phyml wrapper to find the C<phyml> program.
This can be done in (at least) three ways:

=over

=item 1.

Make sure the Phyml executable is in your path. Copy it to, or create
a symbolic link from a directory that is in your path.

=item 2.

Define an environmental variable PHYMLDIR which is a
directory which contains the 'phyml' application: In bash:

  export PHYMLDIR=/home/username/phyml_v2.4.4/exe

In csh/tcsh:

  setenv PHYMLDIR /home/username/phyml_v2.4.4/exe

=item 3.

Include a definition of an environmental variable PHYMLDIR in
every script that will use this Phyml wrapper module, e.g.:

  BEGIN { $ENV{PHYMLDIR} = '/home/username/phyml_v2.4.4/exe' }
  use Bio::Tools::Run::Phylo::Phyml;

=back

=head2 Running

This wrapper has been tested with PHYML v2.4.4 and v.3.0.  It may work with
recent Phyml releases using a date format for the format, but the wrapper
hasn't been extensively tested in these cases, so for the moment only the
simpler numbered versions are supported.

In its current state, the wrapper supports only input of one MSA and
output of one tree. It can easily be extended to support more advanced
capabilities of C<phyml>.

Two convienience methods have been added on top of the standard
BioPerl WrapperBase ones: stats() and tree_string(). You can call them
to after running the phyml program to retrieve into a string the statistics
and the tree in Newick format.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:

  http://bugzilla.open-bio.org/

=head1 AUTHOR - Heikki Lehvaslaiho

heikki at bioperl dot org

=head1 APPENDIX

The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _

=cut

package Bio::Tools::Run::Phylo::Phyml;
use strict;

use Bio::AlignIO;
use File::Copy;
use File::Spec;
use Bio::TreeIO;

use base qw(Bio::Tools::Run::WrapperBase);

our $PROGRAM_NAME = 'phyml';
our $PROGRAM_DIR  = $ENV{'PHYMLDIR'};

# valid substitution model names
our $models;

# DNA
map { $models->{0}->{$_} = 1 } qw(JC69 K2P F81 HKY F84 TN93 GTR);

# protein
map { $models->{1}->{$_} = 1 } qw(JTT MtREV Dayhoff WAG);

our $models3;

# DNA
map { $models3->{'nt'}->{$_} = 1 } qw(HKY85 JC69 K80 F81 F84 TN93 GTR );

# protein
map { $models3->{'aa'}->{$_} = 1 }
  qw(LG WAG JTT MtREV Dayhoff DCMut RtREV CpREV VT Blosum62 MtMam MtArt HIVw  HIVb );

=head2 new

 Title   : new
 Usage   : $factory = Bio::Tools::Run::Phylo::Phyml->new(@params)
 Function: creates a new Phyml factory
 Returns : Bio::Tools::Run::Phylo::Phyml
 Args    : Optionally, provide any of the following (default in []):
           -data_type       => 'dna' or 'protein',   [protein]
           -dataset_count   => integer,             [1]
           -model           => 'HKY'... ,            [HKY|JTT]
           -kappa           => 'e' or float,         [e]
           -invar           => 'e' or float,         [e]
           -category_number => integer,              [1]
           -alpha           => 'e' or float (int v3),[e]
           -tree            => 'BIONJ' or your own,  [BION]
           -bootstrap       => integer               [123]
           -opt_topology    => boolean               [1]
           -opt_lengths     => boolean               [1]
           -no_memory_check => boolean               [1]
           -program_name    => string

=cut

sub new {
    my ( $class, @args ) = @_;
    my $self = $class->SUPER::new(@args);

    # for consistency with other run modules, allow params to be dashless
    my %args = @args;
    while ( my ( $key, $val ) = each %args ) {
        if ( $key !~ /^-/ ) {
            delete $args{$key};
            $args{ '-' . $key } = $val;
        }
    }

    my (
        $data_type, $data_format,     $dataset_count, $model,
        $freq,      $kappa,           $invar,         $category_number,
        $alpha,     $tree,            $opt_topology,  $opt_lengths,
        $opt,       $search,          $rand_start,    $rand_starts,
        $rand_seed, $no_memory_check, $bootstrap,     $program_name
      )
      = $self->_rearrange(
        [
            qw( DATA_TYPE
              DATA_FORMAT
              DATASET_COUNT
              MODEL
              FREQ
              KAPPA
              INVAR
              CATEGORY_NUMBER
              ALPHA
              TREE
              OPT_TOPOLOGY
              OPT_LENGTHS
              OPT
              SEARCH
              RAND_START
              RAND_STARTS
              RAND_SEED
              NO_MEMORY_CHECK
              BOOTSTRAP
              PROGRAM_NAME
              )
        ],
        %args
      );

    $self->data_type($data_type)             if $data_type;
    $self->data_format($data_format)         if $data_format;
    $self->dataset_count($dataset_count)     if $dataset_count;
    $self->model($model)                     if $model;
    $self->freq($freq)                       if $freq;
    $self->kappa($kappa)                     if $kappa;
    $self->invar($invar)                     if $invar;
    $self->category_number($category_number) if $category_number;
    $self->alpha($alpha)                     if $alpha;
    $self->tree($tree)                       if $tree;
    $self->opt_topology($opt_topology)       if $opt_topology;
    $self->opt_lengths($opt_lengths)         if $opt_lengths;
    $self->opt($opt)                         if $opt;
    $self->search($search)                   if $search;
    $self->rand_start($rand_start)           if $rand_start;
    $self->rand_starts($rand_starts)         if $rand_starts;
    $self->rand_seed($rand_seed)             if $rand_seed;
    $self->no_memory_check($no_memory_check) if $no_memory_check;
    $self->bootstrap($bootstrap)             if $bootstrap;
    $self->program_name($program_name)       if $program_name;

    return $self;
}

=head2 program_name

 Title   : program_name
 Usage   : $factory>program_name()
 Function: holds the program name
 Returns : string
 Args    : None

=cut

sub program_name {
    my ( $self, $value ) = @_;

    if ( defined($value) ) {
      if ( $value =~ /^$PROGRAM_NAME[-a-z]*$/ ) {
        $PROGRAM_NAME = $value;
      } else {
        $self->throw("$value is not a valid program name");
      }
    }

    $PROGRAM_NAME;
}

=head2 program_dir

 Title   : program_dir
 Usage   : $factory->program_dir(@params)
 Function: returns the program directory, obtained from ENV variable.
 Returns : string
 Args    : None

=cut

sub program_dir {
    return $PROGRAM_DIR;
}

=head2  version

 Title   : version
 Usage   : exit if $prog->version < 1.8
 Function: Determine the version number of the program
 Example :
 Returns : float or undef
 Args    : none

Phyml before 3.0 did not display the version. Assume 2.44 when can not
determine it.

Some releases do not state version number, only date, so the
version might have to be inferred from this date.

=cut

sub version {
    my $self = shift;

    return $self->{'_version'} if defined $self->{'_version'};
    my $exe = $self->executable || return;
    my $string = substr `$exe -h`, 0, 40;
    my ($version) = $string =~ /PhyML v([\d+\.]+)/;
    if ( !$version ) {
        $string =~ /PhyML\s+(\d{8})/;

        # 3 was released August 2008
        $version = 3 if ( $1 && $1 >= 20080801 );
    }
    $self->{'_version'} = $version;
    $version ? ( return $version ) : return '2.44';
}

=head2 run

 Title   : run
 Usage   : $factory->run($aln_file);
           $factory->run($align_object);
 Function: Runs Phyml to generate a tree
 Returns : Bio::Tree::Tree object
 Args    : file name for your input alignment in a format
           recognised by AlignIO, OR  Bio::Align::AlignI
           compliant object (eg. Bio::SimpleAlign).

=cut

sub run {
    my ( $self, $in ) = @_;

    if ( ref $in && $in->isa("Bio::Align::AlignI") ) {
        $in = $self->_write_phylip_align_file($in);
    }
    elsif ( !-e $in ) {
        $self->throw( "When not supplying a Bio::Align::AlignI object, "
              . "you must supply a readable filename" );
    }
    elsif ( -e $in ) {
        copy( $in, $self->tempdir );
        my $name =
          File::Spec->splitpath($in);    # name is the last item in the array
        $in = File::Spec->catfile( $self->tempdir, $name );
    }

    return $self->_run($in);
}

=head2 stats

 Title   : stats
 Usage   : $factory->stats;
 Function: Returns the contents of the phyml '_phyml_stat.txt' output file
 Returns : string with statistics about the run, undef before run()
 Args    : none

=cut

sub stats {
    my $self = shift;
    return $self->{_stats};
}

=head2 tree_string

 Title   : tree_string
 Usage   : $factory->tree_string;
           $factory->run($align_object);
 Function: Returns the contents of the phyml '_phyml_tree.txt' output file
 Returns : string with tree in Newick format, undef before run()
 Args    : none

=cut

sub tree_string {
    my $self = shift;
    return $self->{_tree};
}

=head2 Getsetters

These methods are used to set and get program parameters before running.

=head2 data_type

 Title   : data_type
 Usage   : $phyml->data_type('nt');
 Function: Sets sequence alphabet to 'dna' (nt in v3) or 'aa'
           If leaved unset, will be set automatically
 Returns : set value, defaults to  'protein'
 Args    : None to get, 'dna' ('nt') or 'aa' to set.

=cut

sub data_type {
    my ( $self, $value ) = @_;
    if ( $self->version && $self->version >= 3 ) {
        if ( defined $value ) {
            if ( $value eq 'nt' ) {
                $self->{_data_type} = 'nt';
            }
            else {
                $self->{_data_type} = 'aa';
            }
        }
        return 'aa' unless defined $self->{_data_type};
    }
    else {
        if ( defined $value ) {
            if ( $value eq 'dna' ) {
                $self->{_data_type} = '0';
            }
            else {
                $self->{_data_type} = '1';
            }
        }
        return '1' unless defined $self->{_data_type};
    }
    return $self->{_data_type};
}

=head2 data_format

 Title   : data_format
 Usage   : $phyml->data_format('s');
 Function: Sets PHYLIP format to 'i' interleaved or
           's' sequential
 Returns : set value, defaults to  'i'
 Args    : None to get, 'i' or 's' to set.

=cut

sub data_format {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        $self->throw("PHYLIP format must be 'i' or 's'")
          unless $value eq 'i'
              or $value eq 's';
        $self->{_data_format} = $value;
    }
    return $self->{_data_format} || 'i';
}

=head2 dataset_count

 Title   : dataset_count
 Usage   : $phyml->dataset_count(3);
 Function: Sets dataset number to deal with
 Returns : set value, defaults to 1
 Args    : None to get, positive integer to set.

=cut

sub dataset_count {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid positive integer [$value]"
          unless $value =~ /^[-+]?\d*$/ and $value > 0;
        $self->{_dataset_count} = $value;
    }
    return $self->{_dataset_count} || 1;
}

=head2 model

 Title   : model
 Usage   : $phyml->model('HKY');
 Function: Choose the substitution model to use. One of

           JC69 | K2P | F81 | HKY | F84 | TN93 | GTR (DNA)
           JTT | MtREV | Dayhoff | WAG (amino acids)

           v3.0:
           HKY85 (default) | JC69 | K80 | F81 | F84 |
           TN93 | GTR (DNA)
           LG (default) | WAG | JTT | MtREV | Dayhoff | DCMut |
           RtREV | CpREV | VT | Blosum62 | MtMam | MtArt |
           HIVw |  HIVb (amino acids)

 Returns : Name of the model, v2.4.4 defaults to {HKY|JTT}
 Args    : None to get, string to set.

=cut

sub model {
    my ( $self, $value ) = @_;
    if ( defined($value) ) {
        if ( $self->version && $self->version >= 3 ) {
            unless ( $value =~ /\d{6}/ ) {
                $self->throw(
"Not a valid model name [$value] for current data type (alphabet)"
                ) unless $models3->{ $self->data_type }->{$value};
            }
        }
        else {
            $self->throw(
"Not a valid model name [$value] for current data type (alphabet)"
            ) unless $models->{ $self->data_type }->{$value};
        }
        $self->{_model} = $value;
    }

    if ( $self->{_model} ) {
        return $self->{_model};
    }

    if ( $self->version && $self->version >= 3 ) {
        if ( $self->data_type eq 'aa' ) {
            return 'LG';    # protein
        }
        else {
            return 'HKY85';    # DNA
        }
    }
    else {
        if ( $self->data_type ) {
            return 'JTT';      # protein
        }
        else {
            return 'HKY';      # DNA
        }
    }
}

=head2 kappa

 Title   : kappa
 Usage   : $phyml->kappa(4);
 Function: Sets transition/transversion ratio, leave unset to estimate
 Returns : set value, defaults to 'e'
 Args    : None to get, float or integer to set.

=cut

sub kappa {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless $value =~ /^[-+]?\d*\.?\d*$/
              or $value eq 'e';
        $self->{_kappa} = $value;
    }
    return 'e' unless defined $self->{_kappa};
    return 'e' if $self->{_kappa} eq 'e';
    return sprintf( "%.1f", $self->{_kappa} );
}

=head2 invar

 Title   : invar
 Usage   : $phyml->invar(.3);
 Function: Sets proportion of invariable sites, leave unset to estimate
 Returns : set value, defaults to 'e'
 Args    : None to get, float or integer to set.

=cut

sub invar {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless $value =~ /^[-+]?\d*\.\d*$/
              or $value eq 'e';
        $self->{_invar} = $value;
    }
    return 'e' unless defined $self->{_invar};
    return 'e' if $self->{_invar} eq 'e';
    return sprintf( "%.1f", $self->{_invar} );
}

=head2 category_number

 Title   : category_number
 Usage   : $phyml->category_number(4);
 Function: Sets number of relative substitution rate categories
 Returns : set value, defaults to 1
 Args    : None to get, integer to set.

=cut

sub category_number {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid postive integer [$value]"
          unless $value =~ /^[+]?\d*$/ and $value > 0;
        $self->{_category_number} = $value;
    }
    return $self->{_category_number} || 1;
}

=head2 alpha

 Title   : alpha
 Usage   : $phyml->alpha(1.0);
 Function: Sets  gamma distribution parameter, leave unset to estimate
 Returns : set value, defaults to 'e'
 Args    : None to get, float or integer to set.

=cut

sub alpha {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless $value =~ /^[-+]?\d*\.?\d*$/
              or $value eq 'e';
        $self->{_alpha} = $value;
    }
    return 'e' unless defined $self->{_alpha};
    return 'e' if $self->{_alpha} eq 'e';
    return sprintf( "%.1f", $self->{_alpha} ) || 'e';
}

=head2 tree

 Title   : tree
 Usage   : $phyml->tree('/tmp/tree.nwk');
 Function: Sets starting tree, leave unset to estimate a distance tree
 Returns : set value, defaults to 'BIONJ'
 Args    : None to get, newick tree file name to set.

=cut

sub tree {
    my ( $self, $value ) = @_;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless -e $value or $value eq 'BIONJ';
        $self->{_tree} = $value;
    }
    return $self->{_tree} || 'BIONJ';
}

=head2 v2 options

These methods can be used with PhyML v2* only.

=head2 opt_topology

 Title   : opt_topology
 Usage   : $factory->opt_topology(1);
 Function: Choose to optimise the tree topology
 Returns : 1 or 0. Default is 1.
 Args    : None to get, boolean to set.

v2.* only

=cut

sub opt_topology {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [opt_topology] for to PhyML v3")
      if $self->version && $self->version >= 3;
    if ( defined($value) ) {
        if ($value) {
            $self->{_opt_topology} = 1;
        }
        else {
            $self->{_opt_topology} = 0;
        }
    }
    return $self->{_opt_topology} || 1;
}

=head2 opt_lengths

 Title   : opt_lengths
 Usage   : $factory->opt_lengths(0);
 Function: Choose to  optimise branch lengths and rate parameters
 Returns : 1 or 0. Default is 1.
 Args    : None to get, boolean to set.


v2.* only

=cut

sub opt_lengths {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [opt_lengths] for PhyML v3")
      if $self->version && $self->version >= 3;
    if ( defined($value) ) {
        if ($value) {
            $self->{_opt_lengths} = 1;
        }
        else {
            $self->{_opt_lengths} = 0;
        }
    }
    return $self->{_opt_lengths} || 1;
}

=head2 v3 options

These methods can be used with PhyML v3* only.

=head2 freq

 Title   : freq
 Usage   : $phyml->freq(e); $phyml->freq("0.2, 0.6, 0.6, 0.2");
 Function: Sets nucleotide frequences or asks residue to be estimated
            according to two models: e or d
 Returns : set value,
 Args    : None to get, string to set.

v3 only.

=cut

sub freq {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [freq] prior to PhyML v3")
      if $self->version < 3;
    if ( defined $value ) {
        die "Invalid value [$value]"
          unless $value =~ /^[\d\. ]$/
              or $value eq 'e'
              or $value eq 'd';
        $self->{_freq} = $value;
    }
    return $self->{_freq};
}

=head2 opt

 Title   : opt
 Usage   : $factory->opt(1);
 Function: Optimise tree parameters: tlr|tl|tr|l|n
 Returns : {value|n} (default n)
 Args    : None to get, string to set.

v3.* only

=cut

sub opt {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [opt] prior to PhyML v3")
      if $self->version < 3;
    if ( defined($value) ) {
        $self->{_opt} = $value if $value =~ /tlr|tl|tr|l|n/;
    }
    return $self->{_opt} || 'n';
}

=head2 search

 Title   : search
 Usage   : $factory->search(SPR);
 Function: Tree topology search operation algorithm: NNI|SPR|BEST
 Returns : string (defaults to NNI)
 Args    : None to get, string to set.

v3.* only

=cut

sub search {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [search] prior to PhyML v3")
      if $self->version < 3;
    if ( defined($value) ) {
        $self->{_search} = $value if $value =~ /NNI|SPR|BEST/;
    }
    return $self->{_search} || 'NNI';
}

=head2 rand_start

 Title   : rand_start
 Usage   : $factory->rand_start(1);
 Function: Sets the initial SPR tree to random.
 Returns : boolean (defaults to false)
 Args    : None to get, boolean to set.

v3.* only; only meaningful if $prog-E<gt>search is 'SPR'

=cut

sub rand_start {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [rand_start] prior to PhyML v3")
      if $self->version < 3;
    if ( defined($value) ) {
        if ($value) {
            $self->{_rand_start} = 1;
        }
        else {
            $self->{_rand_start} = 0;
        }
    }
    return $self->{_rand_start};
}

=head2 rand_starts

 Title   : rand_starts
 Usage   : $factory->rand_starts(10);
 Function: Sets the number of initial random SPR trees
 Returns : integer (defaults to 1)
 Args    : None to get, integer to set.

v3.* only; only valid if $prog-E<gt>search is 'SPR'

=cut

sub rand_starts {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [rand_starts] prior to PhyML v3")
      if $self->version < 3;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless $value =~ /^[-+]?\d+$/;
        $self->{_rand_starts} = $value;
    }
    return $self->{_rand_starts} || 1;
}

=head2 rand_seed

 Title   : rand_seed
 Usage   : $factory->rand_seed(1769876);
 Function: Seeds the random number generator
 Returns : random integer
 Args    : None to get, integer to set.

v3.* only; only valid if $prog-E<gt>search is 'SPR'

Uses perl rand() to initialize if not explicitely set.

=cut

sub rand_seed {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [rand_seed] prior to PhyML v3")
      if $self->version < 3;
    if ( defined $value ) {
        die "Invalid number [$value]"
          unless $value =~ /^[-+]?\d+$/;
        $self->{_rand_seed} = $value;
    }
    return $self->{_rand_seed} || int rand 1000000;
}

=head2 no_memory_check

 Title   : no_memory_check
 Usage   : $factory->no_memory_check(1);
 Function:
 Returns : boolean (defaults to false)
 Args    : None to get, integer to set.

=cut

sub no_memory_check {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [no_memory_check] prior to PhyML v3")
      if $self->version < 3;
    if ( defined($value) ) {
        if ($value) {
            $self->{_no_memory_check} = 1;
        }
        else {
            $self->{_no_memory_check} = 0;
        }
    }
    return $self->{_no_memory_check} || 0;
}

=head2 bootstrap

 Title   : bootstrap
 Usage   : $factory->bootstrap(100);
 Function: Set number of bootstraps
 Returns :
 Args    : None to get, integer to set.

=cut

sub bootstrap {
    my ( $self, $value ) = @_;
    $self->throw("Not a valid parameter [bootstrap] prior to PhyML v3")
      if $self->version < 3;
    if ( defined $value ) {
        die "Invalid number [$value]" unless $value =~ /^\d+$/;
        $self->{_bootstrap} = $value;
    }
    return $self->{_bootstrap};
}

=head2 command

 Title   : command
 Usage   : $factory->command(...);
 Function:
 Returns : string
 Args    : None to get, integer to set.

=cut

sub command {
    my ( $self, $value ) = @_;
    if ( defined($value) ) {
        if ($value =~ /$PROGRAM_NAME/ ) {
            $self->{_command} = $value;
        }
        else {
            $self->throw("$value is not a $PROGRAM_NAME command");
        }
    }
    return $self->{_command} || '';
}

=head2 Internal methods

These methods are private and should not be called outside this class.

=cut

sub _run {
    my ( $self, $file ) = @_;

    my $exe = $self->executable || return;

    my $command;
    my $output_stat_file;
    if ( $self->version >= 3 ) {
        $command          = $exe . " -i $file" . $self->_setparams;
        $output_stat_file = '_phyml_stats.txt';
    }
    else {
        $command = $exe . " $file " . $self->arguments . $self->_setparams;
        $output_stat_file = '_phyml_stat.txt';
    }

    $self->command($command);
    $self->debug("Phyml command = $command\n");
    `$command`;

    # stats
    {
        my $stat_file = $file . $output_stat_file;
        open( my $FH_STAT, "<", $stat_file )
          || $self->throw(
            "Phyml call ($command) did not give an output [$stat_file]: $?");
        local $/;
        $self->{_stats} .= <$FH_STAT>;
    }

    #print $self->{stats};

    # tree
    my $tree_file = $file . '_phyml_tree.txt';
    {
        open( my $FH_TREE, "<", $tree_file )
          || $self->throw("Phyml call ($command) did not give an output: $?");
        local $/;
        $self->{_tree} .= <$FH_TREE>;
    }

    open( my $FH_TREE, "<", $tree_file )
      || $self->throw("Phyml call ($command) did not give an output: $?");

    my $treeio = Bio::TreeIO->new( -format => 'nhx', -fh => $FH_TREE );
    my $tree = $treeio->next_tree;

    # could be faster to parse the tree only if needed?

    return $tree;
}

=head2 _setparams

 Title   : _setparams
 Usage   : Internal function, not to be called directly
 Function: Creates a string of params to be used in the command string
 Returns : string of params
 Args    : none

=cut

sub _setparams {
    my $self = shift;
    my $param_string;

    if ( $self->version >= 3 ) {

        # version 3 or higher
        $param_string = ' -d ' . $self->data_type;

        $param_string .= ' -q ' if $self->data_format eq 's';
        $param_string .= ' -n ' . $self->dataset_count
          if $self->dataset_count > 1;
        $param_string .= ' -b ' . $self->bootstrap if $self->bootstrap;

        # $param_string .= ' 0';  # no bootstrap sets
        $param_string .= ' -m ' . $self->model;
        $param_string .= ' -f ' . $self->freq if $self->freq;

        if ( $self->data_type eq 'dna' ) {
            $param_string .= ' -t ' . $self->kappa;
        }

        $param_string .= ' -v ' . $self->invar;
        $param_string .= ' -c ' . $self->category_number;
        $param_string .= ' -a ' . $self->alpha;
        $param_string .= ' -u ' . $self->tree if $self->tree ne 'BIONJ';
        $param_string .= ' -o ' . $self->opt if $self->opt;
        $param_string .= ' -s ' . $self->search;

        if ( $self->search eq 'SPR' ) {
            $param_string .= ' --rand_start ' if $self->rand_start;
            $param_string .= ' --n_rand_starts ' . $self->rand_starts
              if $self->rand_starts;
            $param_string .= ' --r_seed ' . $self->rand_seed;
        }

        $param_string .= ' --no_memory_check '
          if $self->no_memory_check;

    }
    else {

        # version 2
        $param_string = ' ' . $self->data_type;
        $param_string .= ' ' . $self->data_format;
        $param_string .= ' ' . $self->dataset_count;
        $param_string .= ' 0';                         # no bootstrap sets
        $param_string .= ' ' . $self->model;

        unless ( $self->data_type ) {                  # only for DNA
            $param_string .= ' ' . $self->kappa;
        }

        $param_string .= ' ' . $self->invar;
        $param_string .= ' ' . $self->category_number;
        $param_string .= ' ' . $self->alpha;
        $param_string .= ' ' . $self->tree;
        $param_string .= ' ' . $self->opt_topology;
        $param_string .= ' ' . $self->opt_lengths;

    }
    return $param_string;
}

=head2 _write_phylip_align_file

 Title   : _write_phylip_align_file
 Usage   : obj->__write_phylip_align_file($aln)
 Function: Internal (not to be used directly)

           Writes the alignment into the tmp directory
           in PHYLIP interlieved format

 Returns : filename
 Args    : Bio::Align::AlignI

=cut

sub _write_phylip_align_file {
    my ( $self, $align ) = @_;

    my $tempfile = File::Spec->catfile( $self->tempdir, "aln$$.phylip" );
    $self->data_format('i');
    my $out = Bio::AlignIO->new(
        -file        => ">$tempfile",
        -format      => 'phylip',
        -interleaved => 1,
        -longid      => 1
    );
    $out->write_aln($align);
    $out->close();
    $out = undef;
    return $tempfile;
}

1;