JWS-67 insert Jpred 3.0.1 sources into JABAWS
[jabaws.git] / binaries / src / jpred / lib / Pairwise.pm
1 package Pairwise;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use File::Temp;
7 use base qw(Root Common);
8
9 use Run qw(check);
10
11 sub path {
12   my ( $self, $path ) = @_;
13   if ( defined $path ) {
14     $self->{path} = $path;
15   } else {
16     if ( defined $self->{path} ) {
17       return $self->{path};
18     } else {
19       croak "Pairwise::run: path to pairwise not defined";
20     }
21   }
22 }
23
24 sub run {
25   my ( $self, $fasta ) = @_;
26
27   local $/ = undef;
28   local $? = 0;
29
30   my $f1 = File::Temp->new->filename;
31   my $f2 = File::Temp->new->filename;
32   $fasta->write_file($f1);
33   my $pairwise = $self->path;
34   system("$pairwise $f1 > $f2");
35   check( $pairwise, $? ) or croak "Pairwise::run: $pairwise was naughty\n";
36   open( my $fh, "<", $f2 );
37   my @output = join "\n", split "\n", <$fh>;
38   close $fh;
39   unlink $f1;
40   unlink $f2;
41
42   return @output;
43 }
44
45 sub old_run {
46   my ( $self, $fasta ) = @_;
47
48   local ( $/, $? ) = ( undef, 0 );
49
50   my $f = File::Temp->new->filename;
51   $fasta->write_file($f);
52
53   my $pid = open my $fh, $self->path . " $f |" or die $!;
54
55   my @output = join "\n", split "\n", <$fh>;
56
57   waitpid $pid, 0;
58   check( $self->path, $? ) or die "Pairwise was naughty\n";
59
60   return @output;
61 }
62
63 1;