File Coverage

blib/lib/WebService/FuncNet/Results.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WebService::FuncNet::Results;
2              
3 5     5   10165 use strict;
  5         12  
  5         169  
4 5     5   27 use warnings;
  5         9  
  5         124  
5              
6 5     5   10791 use Math::BigFloat;
  5         111456  
  5         42  
7 5     5   238525 use XML::Simple;
  0            
  0            
8             use Text::CSV;
9             use Carp;
10              
11             our $VERSION = '0.2';
12              
13             =head1 NAME
14              
15             WebService::FuncNet::Results - object encapsulating results
16              
17             =head1 FUNCTIONS
18              
19             =head2 new
20              
21             Creates a new instance of the results object.
22              
23             Do not use this function directly.
24              
25             =cut
26              
27             sub new {
28             my $class = shift;
29             my $rah_data = shift;
30             my $self = { };
31            
32             bless $self, $class;
33             $self->{'data'} = $rah_data;
34            
35             return $self;
36             }
37              
38             =head2 prepare_data
39              
40             Internal function used by all output format-specific functions in order to
41             prepare the data. Essentially, it sorts the output the service returns on
42             the raw pairwise score in descending order.
43              
44             Do not use this function directly.
45              
46             =cut
47              
48             sub prepare_data {
49             my $self = shift;
50             my $rah_raw = shift;
51            
52             return
53             unless ( $rah_raw && ref $rah_raw eq 'ARRAY' );
54            
55             my $rah_unsorted = [ ];
56              
57             foreach my $rh ( @$rah_raw ) {
58             my $p1 = $rh->{p1}; ## query protein
59             my $p2 = $rh->{p2}; ## reference protein
60             my $rs_mbfo = Math::BigFloat->new( $rh->{'rs'} ); ## raw score
61             my $pv_mbfo = Math::BigFloat->new( $rh->{'pv'} ); ## p-value
62              
63             my $rh = {
64             p1 => $p1,
65             p2 => $p2,
66             rs => $rs_mbfo->bstr,
67             pv => $pv_mbfo->bstr,
68             };
69              
70             push ( @$rah_unsorted, $rh );
71             }
72            
73             ## sort by rs value descending
74             my @sorted = sort { $b->{rs} <=> $a->{rs} } @$rah_unsorted ;
75              
76             return \@sorted;
77             }
78              
79              
80             =head2 as_csv
81              
82             Formats and returns the data in CSV format. The data is returned as a reference
83             to an array where each line is a comma separated string. The elements are:
84             query protein, reference protein, raw score, p-value. The data is sorted by raw score
85             in descending order.
86              
87             my $ra_data = $R->as_csv;
88              
89             More information on the output of the function can be found at
90             L
91              
92             This function returns I on error.
93              
94             =cut
95              
96             sub as_csv {
97             my $self = shift;
98             my $rah_data = $self->{'data'};
99            
100             return
101             unless ( defined $rah_data && ref $rah_data eq 'ARRAY' );
102            
103             my $rah_sorted_data =
104             $self->prepare_data( $rah_data );
105            
106             my $csv = Text::CSV->new();
107             my $ra_output = [ ];
108            
109             foreach my $rh ( @$rah_sorted_data ) {
110             my $ra = [ $rh->{p1}, $rh->{p2}, $rh->{rs}, $rh->{pv} ];
111             my $status = $csv->combine( @$ra );
112             if ( $status ) {
113             push (@$ra_output, $csv->string );
114             } else {
115             carp 'Failed to create CSV output';
116             return;
117             }
118             }
119              
120             return $ra_output;
121             }
122              
123             =head2 as_tsv
124              
125             Formats and returns the data in TSV format. The data is returned as a reference
126             to an array where each line is a tab separated string. The elements are:
127             query protein, reference protein, raw score, p-value. The data is sorted by raw score
128             in descending order.
129              
130             my $ra_data = $R->as_tsv;
131              
132             More information on the output of the function can be found at
133             L
134              
135             This function returns I on error.
136              
137             =cut
138              
139             sub as_tsv {
140             my $self = shift;
141             my $rah_data = $self->{'data'};
142            
143             return
144             unless ( defined $rah_data && ref $rah_data eq 'ARRAY' );
145            
146             my $rah_sorted_data =
147             $self->prepare_data( $rah_data );
148            
149             my $tsv = Text::CSV->new({ sep_char => "\t" });
150             my $ra_output = [ ];
151            
152             foreach my $rh ( @$rah_sorted_data ) {
153             my $ra = [ $rh->{p1}, $rh->{p2}, $rh->{rs}, $rh->{pv} ];
154             my $status = $tsv->combine( @$ra );
155             if ( $status ) {
156             push (@$ra_output, $tsv->string );
157             } else {
158             carp 'Failed to create TSV output';
159             return;
160             }
161             }
162              
163             return $ra_output;
164             }
165              
166             =head2 as_xml
167              
168             Formats and returns the data in CSV format. Returns a scalar which contains the
169             XML markup. The root node is named 'results'.
170              
171             B: each pairwise result is stored
172             under an XML element named 'anon'. This way, you can use XMLin from XML::Simple
173             and reconstruct a reference to a array of anonymos hashes easily.
174              
175             my $xml = $R->as_xml;
176            
177             More information on the output of the function can be found at
178             L
179              
180             Example output:
181              
182            
183            
184             Q9H8H3
185             O75865
186             0.8059660198021762
187             1.615708908613666
188            
189            
190             P22676
191             A3EXL0
192             0.9246652723089276
193             0.8992717754263188
194            
195            
196             Q5SR05
197             A3EXL0
198             0.9739920871688543
199             0.49493596412217056
200            
201            
202              
203             This function returns I on error.
204              
205             =cut
206              
207             sub as_xml {
208             my $self = shift;
209             my $rah_data = $self->{'data'};
210            
211             return
212             unless ( defined $rah_data && ref $rah_data eq 'ARRAY' );
213              
214             my $rah_sorted_data =
215             $self->prepare_data( $rah_data );
216            
217             my $xml =
218             XML::Simple::XMLout(
219             $rah_sorted_data,
220             'AttrIndent' => 1,
221             'NoAttr' => 1,
222             'RootName' => 'results' );
223            
224             if ( $xml ) {
225             return $xml;
226             } else {
227             carp 'Failed to create XML output';
228             return;
229             }
230             }
231              
232             1;
233              
234             =head1 REVISION INFO
235              
236             Revision: $Rev: 64 $
237             Last editor: $Author: andrew_b_clegg $
238             Last updated: $Date: 2009-07-06 16:12:20 +0100 (Mon, 06 Jul 2009) $
239              
240             The latest source code for this project can be checked out from:
241              
242             https://funcnet.svn.sf.net/svnroot/funcnet/trunk
243              
244             =cut