This file is indexed.

/usr/share/perl5/Bio/Tools/Run/Alignment/Lagan.pm is in libbio-perl-run-perl 1.6.9-2.

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
# BioPerl module for Bio::Tools::Run::Alignment::Lagan
#
# Please direct questions and support issues to <bioperl-l@bioperl.org> 
#
# Cared for by Bioperl
#
# Copyright Bioperl, Stephen Montgomery <smontgom@bcgsc.bc.ca>
#
# Special thanks to Jason Stajich.
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::Tools::Run::Alignment::Lagan - Object for the local execution of the LAGAN suite of tools (including MLAGAN for multiple sequence alignments)

=head1 SYNOPSIS

  use Bio::Tools::Run::Alignment::Lagan;

  @params =
      ('chaos' => "The contents of this string will be passed as args to chaos",

       #Read you chaos README file for more info/This functionality
       #has not been tested and will be integrated in future versions.

       'order' => "\"-gs -7 -gc -2 -mt 2 -ms -1\"",
       #Where gap start penalty of- 7, gap continue of -2, match of 2,
       #and mismatch of -1.

       'recurse' => "\"(12,25),(7,25),(4,30)"\",
       #A list of (wordlength,score cutoff) pairs to be used in the
       #recursive anchoring

       'tree' => "\"(sample1 (sample2 sample3))"\",
       #Used by mlagan / tree can also be passed when calling mlagan directly

       #SCORING PARAMETERS FOR MLAGAN:
       'match' => 12,
       'mismatch' => -8,
       'gapstart' => -50,
       'gapend' => -50,
       'gapcont' => -2,
  );


=head1 DESCRIPTION

To run mlagan/lagan, you must have an environment variable that points to
the executable directory with files lagan.pl etc.
"LAGAN_DIR=/opt/lagan_executables/"

Simply having the executables in your path is not supported because the
executables themselves only work with the environment variable set.

All lagan and mlagan parameters listed in their Readmes can be set
except for the mfa flag which has been turned on by default to prevent
parsing of the alignment format.

TO USE LAGAN:

  my $lagan = Bio::Tools::Run::Alignment::Lagan->new(@params);
  my $report_out = $lagan->lagan($seq1, $seq2);

A SimpleAlign object is returned.

TO USE MLAGAN:

  my $lagan = Bio::Tools::Run::Alignment::Lagan->new();
  my $tree = "(($seqname1 $seqname2) $seqname3)";
  my @sequence_objs; 	#an array of bioperl Seq objects

  ##If you use an unblessed seq array
  my $seq_ref = \@sequence_objs;
  bless $seq_ref, "ARRAY";

  my $report_out = $lagan->mlagan($seq_ref, $tree);

  A SimpleAlign object is returned	

Only basic mlagan/lagan functionality has been implemented due to the
iterative development of their project.  Future maintenance upgrades
will include enhanced features and scoring.

=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://redmine.open-bio.org/projects/bioperl/

=head1 AUTHOR - Stephen Montgomery

Email smontgom@bcgsc.bc.ca

Genome Sciences Centre in beautiful Vancouver, British Columbia CANADA

=head1 CONTRIBUTORS

MLagan/Lagan is the hard work of Michael Brudno et al.

Sendu Bala bix@sendu.me.uk

=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::Alignment::Lagan;

use strict;
use Bio::Root::IO;
use Bio::Seq;
use Bio::SeqIO;
use Bio::AlignIO;
use Bio::SimpleAlign;
use File::Spec;
use Bio::Matrix::IO;
use Cwd;

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

our @LAGAN_PARAMS = qw(chaos order recurse mfa out lazy maskedonly
                       usebounds rc translate draft info fastreject);
our @OTHER_PARAMS = qw(outfile);
our @LAGAN_SWITCHES = qw(silent quiet);
our @MLAGAN_PARAMS = qw(nested postir translate lazy verbose tree match mismatch
                        gapstart gapend gapcont out version);

#Not all of these parameters are useful in this context, care
#should be used in setting only standard ones

#The LAGAN_DIR environment variable must be set
our $PROGRAM_DIR = $ENV{'LAGAN_DIR'} || '';

sub new {
    my($class, @args) = @_;
    my $self = $class->SUPER::new(@args);
    
    $self->_set_from_args(\@args, -methods => [@LAGAN_PARAMS, @OTHER_PARAMS,
                                               @LAGAN_SWITCHES, @MLAGAN_PARAMS],
                                  -create => 1);
    
    my ($tfh, $tempfile) = $self->io->tempfile();
    my $outfile = $self->out || $self->outfile || $tempfile;
    $self->out($outfile);
    close($tfh);
    undef $tfh;
    return $self;
}

=head2 lagan

  Runs the Lagan pairwise alignment algorithm
  Inputs should be two PrimarySeq objects.

  Returns an SimpleAlign object / preloaded with the tmp file of the
  Lagan multifasta output.

=cut

sub lagan {
    my ($self, $input1, $input2) = @_;
    $self->io->_io_cleanup();
    my $executable = 'lagan.pl';
		
    #my (undef, $tempfile) = $self->io->tempfile();
    #$self->out($tempfile);
    my ($infile1, $infile2) = $self->_setinput($executable, $input1, $input2);
    my $lagan_report = &_generic_lagan(	$self,
                                        $executable,
                                        $infile1,
                                        $infile2 );
}

=head2 mlagan

  Runs the Mlagan multiple sequence alignment algorithm.
  Inputs should be an Array of Primary Seq objects and a Phylogenetic Tree in
  String format or as a Bio::Tree::TreeI compliant object.
  Returns an SimpleAlign object / preloaded with the tmp file of the Mlagan
  multifasta output.

=cut

sub mlagan {
    my ($self, $input1, $tree) = @_;
    $self->io->_io_cleanup();
    my $executable = 'mlagan';
    
    if ($tree && ref($tree) && $tree->isa('Bio::Tree::TreeI')) {
        # fiddle tree so mlagan will like it
        my %orig_ids;
        foreach my $node ($tree->get_nodes) {
            my $seq_id = $node->name('supplied');
            $seq_id = $seq_id ? shift @{$seq_id} : ($node->node_name ? $node->node_name : $node->id);
            $orig_ids{$seq_id} = $node->id;
            $node->id($seq_id);
        }
        
        # convert to string
        my $tree_obj = $tree;
        $tree = $tree->simplify_to_leaves_string;
        
        # more fiddling
        $tree =~ s/ /_/g;
        $tree =~ s/"//g;
        $tree =~ s/,/ /g;

        # unfiddle the tree object
        foreach my $node ($tree_obj->get_nodes) {
            $node->id($orig_ids{$node->id});
        }
    }
    
    my $infiles;
    ($infiles, $tree) = $self->_setinput($executable, $input1, $tree);
    my $lagan_report = &_generic_lagan (	$self,
						$executable,
						$infiles,
						$tree );
}

=head2  nuc_matrix

 Title   : nuc_matrix
 Usage   : my $matrix_obj = $obj->nuc_matrix();
           -or-
           $obj->nuc_matrix($matrix_obj);
           -or-
           $obj->nuc_matrix($matrix_file);
 Function: Get/set the substitution matrix for use by mlagan. By default the
           file $LAGAN_DIR/nucmatrix.txt is used by mlagan. By default this
           method returns a corresponding Matrix.
 Returns : Bio::Matrix::Mlagan object
 Args    : none to get, OR to set:
           Bio::Matrix::MLagan object
           OR
           filename of an mlagan substitution matrix file

           NB: due to a bug in mlagan 2.0, the -nucmatrixfile option does not
           work, so this Bioperl wrapper is unable to simply point mlagan to
           your desired matrix file (or to a temp file generated from your
           matrix object). Instead the $LAGAN_DIR/nucmatrix.txt file must
           actually be replaced. This wrapper will make a back-up copy of that
           file, write the new file in its place, then revert things back to the
           way they were after the alignment has been produced. For this reason,
           $LAGAN_DIR must be writable, as must $LAGAN_DIR/nucmatrix.txt.

=cut

sub nuc_matrix {
    my ($self, $thing, $gap_open, $gap_continue) = @_;
    
    if ($thing) {
        if (-e $thing) {
            my $min = Bio::Matrix::IO->new(-format => 'mlagan',
                                           -file   => $thing);
            $self->{_nuc_matrix} = $min->next_matrix;
        }
        elsif (ref($thing) && $thing->isa('Bio::Matrix::Mlagan')) {
            $self->{_nuc_matrix} = $thing;
        }
        else {
            $self->throw("Unknown kind of thing supplied, '$thing'");
        }
        
        $self->{_nuc_matrix_set} = 1;
    }
    
    unless (defined $self->{_nuc_matrix}) {
        # read the program default file
        my $min = Bio::Matrix::IO->new(-format => 'mlagan',
                                       -file   => File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt'));
        $self->{_nuc_matrix} = $min->next_matrix;
    }
    
    $self->{_nuc_matrix_set} = 1 if defined wantarray;
    return $self->{_nuc_matrix};
}

=head2  _setinput

 Title   : _setinput
 Usage   : Internal function, not to be called directly
 Function: Create input file(s) for Lagan executables
 Returns : name of files containing Lagan data input / 
           or array of files and phylo tree for Mlagan data input

=cut

sub _setinput {
    my ($self, $executable, $input1, $input2) = @_;
    my ($fh, $infile1, $infile2, $temp1, $temp2, $seq1, $seq2);

    $self->io->_io_cleanup();
	
  SWITCH: {
        if (ref($input1) =~ /ARRAY/i) {

            ##INPUTS TO MLAGAN / WILL hAVE TO BE CHANGED IF LAGAN EVER
            ##SUPPORTS MULTI-INPUT
            my @infilearr;
            foreach $seq1 (@$input1) {
                ($fh, $infile1) = $self->io->tempfile();
                my $temp = Bio::SeqIO->new(	-fh => $fh,
                                            -format => 'Fasta' );
                unless ($seq1->isa("Bio::PrimarySeqI")) {
                    return 0;
                }
                $temp->write_seq($seq1);
                close $fh;
                undef $fh;
                push @infilearr, $infile1;
            }
            $infile1 = \@infilearr;
            last SWITCH;  
        }
        elsif ($input1->isa("Bio::PrimarySeqI")) {
            ##INPUTS TO LAGAN
            ($fh, $infile1) = $self->io->tempfile();

            #Want to make sure their are no white spaces in sequence.
            #Happens if input1 is taken from an alignment.

            my $sequence = $input1->seq();
            $sequence =~ s/\W+//g;
            $input1->seq($sequence);
            $temp1 = Bio::SeqIO->new(	-fh => $fh,
                                        -format => 'Fasta' );
            $temp1->write_seq($input1);
            close $fh;
            undef $fh;
            last SWITCH;		
        }
    }
  SWITCH2: {
        if (ref($input2)) {
            if ($input2->isa("Bio::PrimarySeqI")) {
                ($fh, $infile2) = $self->io->tempfile();

                #Want to make sure their are no white spaces in
                #sequence.  Happens if input2 is taken from an
                #alignment.

                my $sequence = $input2->seq();
                $sequence =~ s/\W+//g;
                $input2->seq($sequence);

                $temp2 = Bio::SeqIO->new(       -fh => $fh,
                                                -format => 'Fasta' );
                $temp2->write_seq($input2);
                close $fh;
                undef $fh;
                last SWITCH2;
            }
        } else {
            $infile2 = $input2;
            ##A tree as a scalar has been passed, pass it through
        }
    }
    return ($infile1, $infile2);
}

=head2  _generic_lagan

 Title   : _generic_lagan
 Usage   :  internal function not called directly
 Returns :  SimpleAlign object

=cut


sub _generic_lagan {
    my ($self, $executable, $input1, $input2) = @_;
    my $param_string = $self->_setparams($executable);
    my $lagan_report = &_runlagan($self, $executable, $param_string,
                                  $input1, $input2);	
}	

=head2  _setparams

 Title   : _setparams
 Usage   : Internal function, not to be called directly
 Function: Create parameter inputs for (m)Lagan program
 Returns : parameter string to be passed to Lagan
 Args    : Reference to calling object and name of (m)Lagan executable

=cut


sub _setparams {
    my ($self, $executable) = @_;
    
    my (@execparams, $nucmatrixfile);
    if ($executable eq 'lagan.pl') {
        @execparams = @LAGAN_PARAMS;
    }
    elsif ($executable eq 'mlagan') {
        @execparams = @MLAGAN_PARAMS;
        
        if ($self->{_nuc_matrix_set}) {
            # we create this file on every call because we have no way of
            # knowing if user altered the matrix object
            (my $handle, $nucmatrixfile) = $self->io->tempfile();
            my $mout = Bio::Matrix::IO->new(-format => 'mlagan',
                                            -fh => $handle);
            $mout->write_matrix($self->nuc_matrix);
            $self->{_nucmatrixfile} = $nucmatrixfile;
        }
    }
    ##EXPAND OTHER LAGAN SUITE PROGRAMS HERE
    
    my $param_string = $self->SUPER::_setparams(-params => [@execparams],
                                                -dash => 1);
    $param_string .= " -nucmatrixfile $nucmatrixfile" if $nucmatrixfile;
    return $param_string . " -mfa ";
}	


=head2  _runlagan

 Title   :  _runlagan
 Usage   :  Internal function, not to be called directly
 Function:   makes actual system call to (m)Lagan program
 Example :
 Returns : Report object in the SimpleAlign object

=cut

sub _runlagan {
    my ($self, $executable, $param_string, $input1, $input2) = @_;
    my ($lagan_obj, $exe);
    if ( ! ($exe = $self->executable($executable))) {
        return;
    }
    
    my $version = $self->version;
    
    my $command_string;
    if ($executable eq 'lagan.pl') {
        $command_string = $exe . " " . $input1 . " " . $input2 . $param_string;
    }
    if ($executable eq 'mlagan') {
        $command_string = $exe;
        foreach my $tempfile (@$input1) {
            $command_string .= " " . $tempfile;
        }
        if (defined $input2) {
            $command_string .= " -tree " . "\"" . $input2 . "\"";
        }	
        $command_string .= " " . $param_string;
        
        my $matrix_file = $self->{_nucmatrixfile};
        if ($version <= 3 && $matrix_file) {
            # mlagan 2.0 bug-workaround
            my $orig = File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt');
            -e $orig || $self->throw("Strange, $orig doesn't seem to exist");
            system("cp $orig $orig.bk") && $self->throw("Backup of $orig failed: $!");
            system("cp $matrix_file $orig") && $self->throw("Copy of $matrix_file -> $orig failed: $!");
        }
    }

    if (($self->silent ||  $self->quiet) &&
        ($^O !~ /os2|dos|MSWin32|amigaos/)) {
      $command_string .= ' > /dev/null 2> /dev/null';

    }
    
    # will do brute-force clean up of junk files generated by lagan
    my $cwd = cwd();
    opendir(my $cwd_dir, $cwd) || $self->throw("Could not open the current directory '$cwd'!");
    my %ok_files;
    foreach my $thing (readdir($cwd_dir)) {
        if ($thing =~ /anch/) {
            $ok_files{$thing} = 1;
        }
    }
    closedir($cwd_dir);
    
    $self->debug("$command_string\n");
    my $status = system(($version <= 3 ? '_POSIX2_VERSION=1 ' : '').$command_string); # temporary hack whilst lagan script 'rechaos.pl' uses obsolete sort syntax
    
    if ($version <= 1 && $self->{_nucmatrixfile}) {
        my $orig = File::Spec->catfile($PROGRAM_DIR, 'nucmatrix.txt');
        system("mv $orig.bk $orig") && $self->warn("Restore of $orig from $orig.bk failed: $!");
    }
    
    opendir($cwd_dir, $cwd) || $self->throw("Could not open the current directory '$cwd'!");
    foreach my $thing (readdir($cwd_dir)) {
        if ($thing =~ /anch/) {
            unlink($thing) unless $ok_files{$thing};
        }
    }
    closedir($cwd_dir);
    
    my $outfile = $self->out();
    my $align = Bio::AlignIO->new(	'-file' => $outfile,
					'-format' => 'fasta' );
    my $aln = $align->next_aln();

    return $aln;
}

=head2 executable

 Title   : executable
 Usage   : my $exe = $lagan->executable('mlagan');
 Function: Finds the full path to the 'lagan' executable
 Returns : string representing the full path to the exe
 Args    : [optional] name of executable to set path to
           [optional] boolean flag whether or not warn when exe is not found

 Thanks to Jason Stajich for providing the framework for this subroutine

=cut

sub executable {
    my ($self, $exename, $exe, $warn) = @_;
    $exename = 'lagan.pl' unless defined $exename;

    if ( defined $exe && -x $exe ) {
        $self->{'_pathtoexe'}->{$exename} = $exe;
    }
    unless ( defined $self->{'_pathtoexe'}->{$exename} ) {
        my $f = $self->program_path($exename);
        $exe = $self->{'_pathtoexe'}->{$exename} = $f if(-e $f && -x $f );
        
        unless( $exe )  {
            if ( ($exe = $self->io->exists_exe($exename)) && -x $exe ) {
                $self->{'_pathtoexe'}->{$exename} = $exe;
            } else {
                $self->warn("Cannot find executable for $exename") if $warn;
                $self->{'_pathtoexe'}->{$exename} = undef;
            }
        }
    }
    
    # even if its executable, we still need the environment variable to have
    # been set
    if (! $PROGRAM_DIR) {
        $self->warn("Environment variable LAGAN_DIR must be set, even if the lagan executables are in your path");
        $self->{'_pathtoexe'}->{$exename} = undef;
    }
    
    return $self->{'_pathtoexe'}->{$exename};
}

=head2 program_path

 Title   : program_path
 Usage   : my $path = $lagan->program_path();
 Function: Builds path for executable
 Returns : string representing the full path to the exe

 Thanks to Jason Stajich for providing the framework for this subroutine

=cut

sub program_path {
    my ($self,$program_name) = @_;
    my @path;
    push @path, $self->program_dir if $self->program_dir;
   	push @path, $program_name .($^O =~ /mswin/i ?'':'');
	# Option for Windows variants / None so far

    return Bio::Root::IO->catfile(@path);
}

=head2 program_dir

 Title   : program_dir
 Usage   : my $dir = $lagan->program_dir();
 Function: Abstract get method for dir of program. To be implemented
           by wrapper.
 Returns : string representing program directory

 Thanks to Jason Stajich for providing the framework for this subroutine

=cut

sub program_dir {
    $PROGRAM_DIR;
}

=head2 version

 Title   : version
 Usage   : my $version = $lagan->version;
 Function: returns the program version
 Returns : number
 Args    : none

=cut

sub version {
    my $self = shift;
    my $exe = $self->executable('mlagan') || return;
    
    open(my $VER, "$exe -version 2>&1 |") || die "Could not open command '$exe -version'\n";
    my $version;
    while (my $line = <$VER>) {
        ($version) = $line =~ /(\d+\S+)/;
    }
    close($VER) || die "Could not complete command '$exe -version'\n";
    
    return $version;
}

1;