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