JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Index / FastaCMD.pm
1 package Index::FastaCMD;
2
3 our $VERSION = '0.2';
4
5 =head1 NAME
6
7 Index::FastaCMD - module to use the fastacmd program to read BLAST formatted databases
8
9 =head1 DESCRIPTION
10
11 This module was written as a replacement for Index::EMBOSS as EMBOSS needed to be statically linked to libgd, whcih wasn't a very sysadmin-friendly way of installing the software. As BLAST is pretty ubiquitous the fastacmd was deemed to be a better option. Copied heavily from Index::EMBOSS.
12
13 =head1 SYNOPSIS
14
15 =head1 CHANGES
16
17 =over 8
18
19 =item 0.2
20
21 Added 'use Path' module to ensure that the correct fastacmd binary is used. Previously on the cluster the wrong fastacmd was used, which resulted in a failure. Now is more robust although the path to fastacmd is hardcoded in Paths.pm
22
23 =head1 AUTHOR
24
25 Chris Cole <christian@cole.name>
26
27 =cut
28
29
30 use strict;
31 use warnings;
32
33
34 use Carp;
35 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
36 use IPC::Open3;
37
38 use base qw(Root);
39
40 use FASTA::File;
41 use Paths qw($fastacmd);
42
43 sub get_sequence {
44    my ($self, $key, $db) = @_;
45    
46    my $appName = $fastacmd;
47    
48    croak "No sequence code given for get_sequence" unless $key;
49    croak "No database given for get_sequence" unless $db;
50    
51    my @cmd = split / /, "$appName -s $key -d $db";
52
53    my $pid = open3(undef, \*RD, \*ERR, @cmd);
54
55    my $seq = FASTA::File->new(read => \*RD);
56    my @err = <ERR>;
57
58    pop @err;
59    pop @err;
60
61    close RD; close ERR;
62    waitpid $pid, 0;
63    
64    # Everything was okay...
65    if (WIFEXITED($?) and not WEXITSTATUS($?)) { return $seq }
66    # Non-zero exit
67    elsif (WIFEXITED($?) and WEXITSTATUS($?)) {
68       carp "Command: '@cmd' had a problem: $?; $!";
69       carp @err;
70    }
71    # Was it stopped by an external program
72    elsif (WIFSIGNALED($?)) {
73       carp "$appName halted by external signal ".WTERMSIG($?)
74    }
75    else {
76       carp "$appName suffered from a random pantwetting event"
77    }
78
79    return undef;
80 }
81
82 1;