blib/lib/Bio/EBI/RNAseqAPI.pm untested

filter: Statementstmt 0.0% 0 / 237 Branchbran 0.0% 0 / 80 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 361 CC 41 SCAR 26.9
CC
41
Cov
0%
get_runs_by_list
30.0
get_runs_by_organism
20.0
get_runs_by_organism_condition
20.0
CRAP 14.7
1 =head1 NAME
2  
3 Bio::EBI::RNAseqAPI - A Perl interface to the EMBL-EBI RNA-seq analysis API.
4  
5 =head1 DESCRIPTION
6  
7 This module provides a Perl-based interface to the L<EMBL-EBI|http://www.ebi.ac.uk> L<RNA-seq analysis API|http://www.ebi.ac.uk/fg/rnaseq/api/>.
8  
9 The RNA-seq Analysis API enables access to analysis results for thousands of
10 publicly available gene expression datasets. This module provides functions to
11 access each endpoint provided by the API.
12  
13 For more information about the API, see its L<documentation|http://www.ebi.ac.uk/fg/rnaseq/api/doc>.
14  
15 =head1 SYNOPSIS
16  
17 use 5.10.0;
18 use Bio::EBI::RNAseqAPI;
19  
20 my $rnaseqAPI = Bio::EBI::RNAseqAPI->new;
21  
22 my $runInfo = $rnaseqAPI->get_runs_by_study(
23 study => "E-MTAB-513",
24 minimum_mapped_reads => 0
25 );
26  
27 =cut
28  
29 package Bio::EBI::RNAseqAPI;
30  
31 use 5.10.0;
32  
33 use Moose;
34 use MooseX::FollowPBP;
35 use LWP::UserAgent;
36 use Log::Log4perl qw( :easy );
37 use JSON::Parse qw( parse_json );
38  
39 our $VERSION = 1.03;
40  
41 =head1 ATTRIBUTES
42  
43 =over 2
44  
45 =cut
46  
47 #=item B<api_base>
48  
49 #Base URL for the EBI RNA-seq API. Default value, can be overridden.
50  
51 #=cut
52  
53 has 'api_base' => (
54 is => 'rw',
55 isa => 'Str',
56 default => "http://www.ebi.ac.uk/fg/rnaseq/api"
57 );
58  
59 #=item B<user_agent>
60  
61 #LWP::UserAgent object with current user's proxy settings. Lazy build.
62  
63 #=cut
64  
65 has 'user_agent' => (
66 is => 'rw',
67 isa => 'LWP::UserAgent',
68 lazy_build => 1
69 );
70  
71 #=item B<log_writer>
72  
73 #Log::Log4perl::Logger object, used for logging error messages. Lazy build.
74  
75 #=cut
76  
77 has 'log_writer' => (
78 is => 'rw',
79 isa => 'Log::Log4perl::Logger',
80 lazy_build => 1
81 );
82  
83 =item B<run_organism_list>
84  
85 An anonymous hash containing allowed organism names for downloading run
86 information as keys. Access the contents like this:
87  
88 my $runOrganisms = $rnaseqAPI->get_run_organism_list;
89  
90 =cut
91  
92 has 'run_organism_list' => (
93 is => 'rw',
94 isa => 'HashRef',
95 lazy_build => 1
96 );
97  
98 =item B<expression_organism_list>
99  
100 An anonymous hash containing allowed organism names for downloading gene
101 expression information as keys. Access the contents like this:
102  
103 my $expressionOrganisms = $rnaseqAPI->get_expression_organism_list;
104  
105 =cut
106  
107 has 'expression_organism_list' => (
108 is => 'rw',
109 isa => 'HashRef',
110 lazy_build => 1
111 );
112  
113 =back
114  
115 =head1 METHODS
116  
117 =head2 Analysis results per sequencing run
118  
119 These functions take arguments in the form of a hash. These usually
120 consist of a study accession, or one or more run accessions, plus a value for
121 "minimum_mapped_reads". This value represents the minimum percentage of mapped
122 reads to allow for each run in the results. Only information for runs with a
123 percentage of mapped reads greater than or equal to this value will be
124 returned. To get all available information, set "minimum_mapped_reads" to zero.
125  
126 Analysis information for each run is returned in an anonymous hash. Some
127 functions return anonymous arrays with one anonymous hash per run found. See
128 below for examples and more information about the results.
129  
130  
131 =over 2
132  
133 =item B<get_run>
134  
135 Accesses the API's C<getRun> JSON endpoint and returns analysis information for
136 a single run, passed in the arguments.
137  
138 Arguments should be passed as a hash containing values for "run" and
139 "minimum_mapped_reads", e.g.:
140  
141 my $runInfo = $rnaseqAPI->get_run(
142 run => "ERR030885",
143 minimum_mapped_reads => 0
144 );
145  
146 Run analysis information is returned in an anonymous hash. Returns C<undef> (and
147 logs errors) if errors are encountered.
148  
149 An example of the hash returned is as follows:
150  
151 {
152 'BIOREP_ID' => 'ERR030885',
153 'RUN_IDS' => 'ERR030885',
154 'REFERENCE_ORGANISM' => 'homo_sapiens',
155 'MAPPING_QUALITY' => 96,
156 'ASSEMBLY_USED' => 'GRCh38',
157 'ORGANISM' => 'homo_sapiens',
158 'BEDGRAPH_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/ERR030/ERR030885/ERR030885.bedgraph',
159 'ENA_LAST_UPDATED' => 'Mon Aug 18 2014 13:40:46',
160 'STUDY_ID' => 'ERP000546',
161 'BIGWIG_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/ERR030/ERR030885/ERR030885.bw',
162 'CRAM_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/ERR030/ERR030885/ERR030885.cram',
163 'LAST_PROCESSED_DATE' => 'Sun Jul 12 2015 23:31:47',
164 'STATUS' => 'Complete',
165 'SAMPLE_IDS' => 'SAMEA962348'
166 }
167  
168 =cut
169  
170 sub get_run {
171  
172 my ( $self, %args ) = @_;
173  
174 my $logger = $self->get_log_writer;
175
176 unless( $self->_hash_arguments_ok( \%args, "run", "minimum_mapped_reads" ) ) {
177  
178 $logger->error(
179 "Problem with arguments to \"get_run\" function."
180 );
181  
182 return;
183 }
184  
185 my $restResult = $self->_run_rest_call(
186 {
187 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
188 function_name => "getRun",
189 function_argument => $args{ "run" }
190 }
191 );
192  
193 if( $restResult ) {
194  
195 my ( $runInfo ) = @{ $restResult };
196  
197 return $runInfo;
198 }
199 else {
200  
201 $logger->error(
202 "No result found for run ",
203 $args{ "run" }
204 );
205  
206 return;
207 }
208 }
209  
210  
211 =item B<get_runs_by_list>
212  
213 This function takes an anonymous array of run accessions and sequentially
214 accesses the API's C<getRun> JSON endpoint to collect the analysis information
215 for each run in the list provided.
216  
217 my $runInfo = $rnaseqAPI->get_runs_by_list(
218 runs => [ "ERR030885", "ERR030886" ],
219 minimum_mapped_reads => 0
220 );
221  
222 Run analysis information is returned as an anonymous array containing one
223 anonymous hash per run (see L</get_run> documentation for an example of what the
224 anonymous hash looks like). Returns C<undef> (and logs errors) if errors are
225 encountered.
226  
227 =cut
228  
229 sub get_runs_by_list {
230  
231 my ( $self, %args ) = @_;
232  
233 my $logger = $self->get_log_writer;
234
235 unless( $self->_hash_arguments_ok( \%args, "runs", "minimum_mapped_reads" ) ) {
236  
237 $logger->error(
238 "Problem with arguments to \"get_runs_by_list\" function."
239 );
240  
241 return;
242 }
243  
244 # Make sure the run accessions are in an array ref.
245 eval { my @runAccessions = @{ $args{ "runs" } } };
246  
247 if( $@ ) {
248
249 $logger->error(
250 "get_runs_by_list requires run accession(s) to be provided as an array reference."
251 );
252  
253 return;
254 }
255  
256 my @allRunInfo = ();
257  
258 foreach my $runAcc ( @{ $args{ "runs" } } ) {
259  
260 my $restResult = $self->get_run(
261 run => $runAcc,
262 minimum_mapped_reads => $args{ "minimum_mapped_reads" }
263 );
264
265 if( $restResult ) {
266
267 push @allRunInfo, $restResult;
268 }
269 }
270
271 unless( scalar @allRunInfo ) {
272  
273 $logger->error(
274 "No results found for any runs."
275 );
276  
277 return;
278 }
279  
280 return \@allRunInfo;
281 }
282  
283  
284 =item B<get_runs_by_study>
285  
286 Accesses the API's C<getRunsByStudy> JSON endpoint, and returns an anonymous array
287 containing an anonymous hash for each run found (see L</get_run> docs for an example).
288  
289 my $runInfo = $rnaseqAPI->get_runs_by_study(
290 study => "E-MTAB-513",
291 minimum_mapped_reads => 0
292 );
293  
294 Study accession can be either an L<ENA|http://www.ebi.ac.uk/ena>,
295 L<SRA|http://www.ncbi.nlm.nih.gov/sra>, L<DDBJ|http://www.ddbj.nig.ac.jp/> or
296 L<ArrayExpress|http://www.ebi.ac.uk/arrayexpress> study accession. The example
297 above uses an ArrayExpress experiment accession. Examples of ENA, SRA or DDBJ
298 accessions are "ERP000546" or "SRP013533" or "DRP000391", respectively.
299  
300 Returns C<undef> (and logs errors) if errors are encountered.
301  
302 =cut
303  
304 sub get_runs_by_study {
305  
306 my ( $self, %args ) = @_;
307  
308 my $logger = $self->get_log_writer;
309
310 unless( $self->_hash_arguments_ok( \%args, "study", "minimum_mapped_reads" ) ) {
311  
312 $logger->error(
313 "Problem with arguments to \"get_runs_by_study\" function."
314 );
315  
316 return;
317 }
318
319 my $restResult = $self->_run_rest_call(
320 {
321 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
322 function_name => "getRunsByStudy",
323 function_argument => $args{ "study" }
324 }
325 );
326  
327 if( $restResult ) {
328  
329 return $restResult;
330 }
331 else {
332  
333 $logger->error(
334 "Problem retrieving runs for ",
335 $args{ "study" }
336 );
337 }
338 }
339  
340  
341 =item B<get_runs_by_organism>
342  
343 Accesses the API's C<getRunsByOrganism> JSON endpoint, and returns an anonymous
344 array containing an anonymous hash for each run found.
345  
346 my $runInfo = $rnaseqAPI->get_runs_by_organism(
347 organism => "homo_sapiens",
348 minimum_mapped_reads => 70
349 );
350  
351 Value for "organism" attribute is a species scientific name, in lower case,
352 with underscores instead of spaces. E.g. "homo_sapiens",
353 "canis_lupus_familiaris", "oryza_sativa_japonica_group". To ensure your
354 organism name is allowed, check against the L</run_organism_list> attribute:
355  
356 my $organism = "oryctolagus_cuniculus";
357 my $organismList = $rnaseqAPI->get_run_organism_list;
358 if( $organismList->{ $organism } ) {
359 say "Found $organism!";
360 }
361  
362 Results are returned as an anonymous array containing an anonymous hash for each
363 run found. Returns C<undef> (and logs errors) if errors are encountered.
364  
365 =cut
366  
367 sub get_runs_by_organism {
368  
369 my ( $self, %args ) = @_;
370  
371 my $logger = $self->get_log_writer;
372  
373 unless( $self->_hash_arguments_ok( \%args, "organism", "minimum_mapped_reads" ) ) {
374  
375 $logger->error(
376 "Problem with arguments to \"get_runs_by_organism\" function."
377 );
378  
379 return;
380 }
381
382 # Fail if the organism isn't recognised.
383 unless( $self->_organism_name_ok( $args{ "organism" }, "run" ) ) {
384  
385 return;
386 }
387
388 my $restResult = $self->_run_rest_call(
389 {
390 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
391 function_name => "getRunsByOrganism",
392 function_argument => $args{ "organism" }
393 }
394 );
395  
396 if( $restResult ) {
397  
398 return $restResult;
399 }
400 else {
401  
402 $logger->error(
403 "Problem retrieving runs for ",
404 $args{ "organism" }
405 );
406 }
407 }
408  
409  
410 =item B<get_runs_by_organism_condition>
411  
412 Accesses the API's C<getRunsByOrganismCondition> JSON endpoint, and returns an
413 anonymous array containing an anonymous hash for each run found. An organism
414 name and a "condition" -- meaning a sample attribute -- are passed in the
415 arguments. The condition must exist in the L<Experimental Factor Ontology (EFO)|http://www.ebi.ac.uk/efo>; this can
416 be checked via the EFO website or via the L<Ontology Lookup Service (OLS) API|http://www.ebi.ac.uk/ols/docs/api>.
417  
418  
419 my $runInfo = $rnaseqAPI->get_runs_by_organism_condition(
420 organism => "homo_sapiens",
421 condition => "central nervous system",
422 minimum_mapped_reads => 70
423 );
424  
425 See L</get_runs_by_organism> docs for how to check organism name format and availability.
426  
427 Returns C<undef> (and logs errors) if errors are encountered.
428  
429 =cut
430  
431 sub get_runs_by_organism_condition {
432  
433 my ( $self, %args ) = @_;
434  
435 my $logger = $self->get_log_writer;
436
437 unless( $self->_hash_arguments_ok( \%args, "organism", "minimum_mapped_reads", "condition" ) ) {
438  
439 $logger->error(
440 "Problem with arguments to \"get_runs_by_organism_condition\" function."
441 );
442  
443 return;
444 }
445
446 # Fail if the organism isn't recognised.
447 unless( $self->_organism_name_ok( $args{ "organism" }, "run" ) ) {
448  
449 return;
450 }
451
452 my $restResult = $self->_run_rest_call(
453 {
454 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
455 function_name => "getRunsByOrganismCondition",
456 function_argument => $args{ "organism" } . "/" . $args{ "condition" }
457 }
458 );
459  
460 if( $restResult ) {
461
462 return $restResult;
463 }
464 else {
465  
466 $logger->error(
467 "Problem retrieving runs for organism \"",
468 $args{ "organism" },
469 "\" with condition \"",
470 $args{ "condition" }
471 );
472 }
473 }
474  
475  
476 =back
477  
478 =head2 Analysis results per study
479  
480 These functions take an L<ENA|http://www.ebi.ac.uk/ena>, L<SRA|http://www.ncbi.nlm.nih.gov/sra>, L<DDBJ|http://www.ddbj.nig.ac.jp/> or L<ArrayExpress|http://www.ebi.ac.uk/arrayexpress> accession or species name and return information about the corresponding dataset(s).
481  
482 =over 2
483  
484 =item B<get_study>
485  
486 Accesses the API's C<getStudy> JSON endpoint. Single argument is a study
487 accession (L<ENA|http://www.ebi.ac.uk/ena>, L<SRA|http://www.ncbi.nlm.nih.gov/sra>, L<DDBJ|http://www.ddbj.nig.ac.jp/> or L<ArrayExpress|http://www.ebi.ac.uk/arrayexpress>). Returns an anonymous hash
488 containing the results for the matching study. Returns C<undef> (and logs
489 errors) if errors are encountered. If you try an ArrayExpress accession and it
490 doesn't work, try the corresponding sequencing archive study accession instead.
491  
492 my $studyInfo = $rnaseqAPI->get_study( "SRP033494" );
493  
494 An example of the anonymous hash returned is as follows:
495  
496 {
497 'GTF_USED' => 'Arabidopsis_thaliana.TAIR10.26.gtf.gz',
498 'ORGANISM' => 'arabidopsis_thaliana',
499 'STATUS' => 'Complete',
500 'ASSEMBLY_USED' => 'TAIR10',
501 'LAST_PROCESSED_DATE' => 'Thu Jun 30 2016 19:55:56',
502 'GENES_FPKM_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/genes.rpkm.tsv',
503 'EXONS_FPKM_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/exons.rpkm.tsv',
504 'GENES_TPM_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/genes.tpm.tsv',
505 'SOFTWARE_VERSIONS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/irap.versions.tsv',
506 'REFERENCE_ORGANISM' => 'arabidopsis_thaliana',
507 'STUDY_ID' => 'SRP033494',
508 'EXONS_RAW_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/exons.raw.tsv',
509 'GENES_RAW_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/genes.raw.tsv',
510 'EXONS_TPM_COUNTS_FTP_LOCATION' => 'ftp://ftp.ebi.ac.uk/pub/databases/arrayexpress/data/atlas/rnaseq/studies/ena/SRP033494/exons.tpm.tsv'
511 }
512  
513 =cut
514  
515 sub get_study {
516  
517 my ( $self, $studyAcc ) = @_;
518  
519 my $logger = $self->get_log_writer;
520  
521 unless( $studyAcc ) {
522  
523 $logger->error(
524 "get_study requires a study accession as an argument."
525 );
526  
527 return;
528 }
529  
530 my $restResult = $self->_run_rest_call(
531 {
532 function_name => "getStudy",
533 function_argument => $studyAcc
534 }
535 );
536  
537 if( $restResult ) {
538  
539 my ( $studyInfo ) = @{ $restResult };
540  
541 return $studyInfo;
542 }
543 else {
544  
545 $logger->error(
546 "Problem retrieving study ",
547 $studyAcc
548 );
549 }
550 }
551  
552 =item B<get_studies_by_organism>
553  
554 Accesses the API's C<getStudiesByOrganism> JSON endpoint. Single argument is the
555 name of an organism (see the L</run_organism_list> attribute for allowed names).
556 Returns an anonymous array containing one anonymous hash per study found. See
557 L</get_study> docs for an example of an anonymous hash.
558  
559 my $studies = $rnaseqAPI->get_studies_by_organism( "arabidopsis_thaliana" );
560  
561 =cut
562  
563 sub get_studies_by_organism {
564  
565 my ( $self, $organism ) = @_;
566  
567 my $logger = $self->get_log_writer;
568  
569 unless( $organism ) {
570  
571 $logger->error(
572 "get_studies_by_organism requires organism as an argument."
573 );
574  
575 return;
576 }
577  
578 # Fail if the organism isn't recognised.
579 unless( $self->_organism_name_ok( $organism, "run" ) ) {
580  
581 return;
582 }
583
584 my $restResult = $self->_run_rest_call(
585 {
586 function_name => "getStudiesByOrganism",
587 function_argument => $organism
588 }
589 );
590  
591 if( $restResult ) {
592  
593 return $restResult;
594 }
595 else {
596  
597 $logger->error(
598 "Problem retrieving study information for ",
599 $organism
600 );
601 }
602 }
603  
604  
605 =back
606  
607 =head2 Sample attributes per run
608  
609 These functions return information about the sample attributes that runs are
610 annotated with. Sample attributes have a "type", e.g. "organism", and a
611 "value", e.g. "Homo sapiens". Where possible, the URI of the matching ontology term
612 is also annotated.
613  
614 =over 2
615  
616 =item B<get_sample_attributes_by_run>
617  
618 Accesses the API's C<getSampleAttributesByRun> JSON endpoint. Single argument is
619 the accession of the run. Returns an anonymous array containing one anonymous
620 hash per sample attribute found.
621  
622 my $sampleAttributes = $rnaseqAPI->get_sample_attributes_by_run( "SRR805786" );
623  
624 An example of the results returned is as follows:
625  
626 [
627 {
628 'VALUE' => 'peripheral blood mononuclear cells (PBMCs)',
629 'EFO_URL' => 'NA',
630 'STUDY_ID' => 'SRP020492',
631 'TYPE' => 'cell type',
632 'RUN_ID' => 'SRR805786'
633 },
634 {
635 'STUDY_ID' => 'SRP020492',
636 'TYPE' => 'organism',
637 'RUN_ID' => 'SRR805786',
638 'VALUE' => 'Homo sapiens',
639 'EFO_URL' => 'http://purl.obolibrary.org/obo/NCBITaxon_9606'
640 },
641 ]
642  
643 EFO_URL is not always present and will be "NA" if it is not.
644  
645 Returns C<undef> (and logs errors) if errors are encountered.
646  
647 =cut
648  
649 sub get_sample_attributes_by_run {
650  
651 my ( $self, $runAcc ) = @_;
652  
653 my $logger = $self->get_log_writer;
654  
655 unless( $runAcc ) {
656  
657 $logger->error(
658 "get_sample_attributes_by_run requires a run accession as an argument."
659 );
660  
661 return;
662 }
663  
664 my $restResult = $self->_run_rest_call(
665 {
666 function_name => "getSampleAttributesByRun",
667 function_argument => $runAcc
668 }
669 );
670  
671 if( $restResult ) {
672  
673 return $restResult;
674 }
675 else {
676  
677 $logger->error(
678 "Problem retrieving sample attributes for run ",
679 $runAcc
680 );
681 }
682 }
683  
684 =item B<get_sample_attributes_per_run_by_study>
685  
686 Accesses the API's C<getSampleAttributesPerRunByStudy> JSON endpoint. Single
687 argument is a study accession. Returns an array ref containing one anonymous
688 hash per sample attribute. See L</get_sample_attributes_by_run> docs for an
689 example. Returns C<undef> (and logs errors) if errors are encountered.
690  
691 my $sampleAttributes = $rnaseqAPI->get_sample_attributes_per_run_by_study( "DRP000391" );
692  
693 =cut
694  
695 sub get_sample_attributes_per_run_by_study {
696  
697 my ( $self, $studyAcc ) = @_;
698  
699 my $logger = $self->get_log_writer;
700  
701 unless( $studyAcc ) {
702  
703 $logger->error(
704 "get_sample_attributes_per_run_by_study requires a study accession as an argument."
705 );
706  
707 return;
708 }
709  
710 my $restResult = $self->_run_rest_call(
711 {
712 function_name => "getSampleAttributesPerRunByStudy",
713 function_argument => $studyAcc
714 }
715 );
716  
717 if( $restResult ) {
718  
719 return $restResult;
720 }
721 else {
722
723 $logger->error(
724 "Problem retrieving sample attributes for ",
725 $studyAcc
726 );
727 }
728 }
729  
730 =item B<get_sample_attributes_coverage_by_study>
731  
732 Accesses the API's C<getSampleAttributesCoverageByStudy> endpoint. Single argument
733 is a study accession. Returns an anonymous array containing one anonymous hash
734 per sample attribute. Returns C<undef> (and logs errors) if errors are
735 encountered.
736  
737 my $sampleAttributeCoverage = $rnaseqAPI->get_sample_attributes_coverage_by_study( "DRP000391" );
738  
739 An example of the results is as follows:
740  
741 [
742 {
743 'VALUE' => 'Nipponbare',
744 'STUDY_ID' => 'DRP000391',
745 'PCT_OF_ALL_RUNS' => 100,
746 'NUM_OF_RUNS' => 28,
747 'TYPE' => 'cultivar'
748 },
749 {
750 'VALUE' => 'Oryza sativa Japonica Group',
751 'STUDY_ID' => 'DRP000391',
752 'PCT_OF_ALL_RUNS' => 100,
753 'NUM_OF_RUNS' => 28,
754 'TYPE' => 'organism'
755 },
756 {
757 'VALUE' => '7 days after germination',
758 'STUDY_ID' => 'DRP000391',
759 'PCT_OF_ALL_RUNS' => 29,
760 'NUM_OF_RUNS' => 8,
761 'TYPE' => 'developmental stage'
762 }
763 ]
764  
765 =cut
766  
767 sub get_sample_attributes_coverage_by_study {
768  
769 my ( $self, $studyAcc ) = @_;
770  
771 my $logger = $self->get_log_writer;
772  
773 unless( $studyAcc ) {
774  
775 $logger->error(
776 "get_sample_attributes_coverage_by_study requires a study accession as an argument."
777 );
778  
779 return;
780 }
781  
782 my $restResult = $self->_run_rest_call(
783 {
784 function_name => "getSampleAttributesCoverageByStudy",
785 function_argument => $studyAcc
786 }
787 );
788  
789 if( $restResult ) {
790  
791 return $restResult;
792 }
793 else {
794
795 $logger->error(
796 "Problem retrieving sample attributes for ",
797 $studyAcc
798 );
799 }
800 }
801  
802 =back
803  
804 =head2 Baseline gene expression per tissue, cell type, developmental stage, sex, and strain
805  
806 =over 2
807  
808 =item B<get_expression_by_organism_genesymbol>
809  
810 Accesses the API's C<getExpression> endpoint. Provide arguments as a hash,
811 passing an organism name and a gene symbol, as well as a value for the minimum
812 percentage of mapped reads to allow:
813  
814 my $geneExpressionInfo = $rnaseqAPI->get_expression(
815 minimum_mapped_reads => 0,
816 organism => "oryza_sativa",
817 gene_symbol => "BURP7"
818 );
819  
820 Results are returned as an anonymous array of anonymous hashes, with one
821 anonymous hash per unique combination of tissue, cell type, developmental
822 stage, sex, and strain. The median expression level of all runs is given in TPM
823 (transcripts per million). Returns C<undef> (and logs errors) if errors are
824 encountered.
825  
826 An example of the results returned is as follows:
827  
828 [
829 {
830 'COEFFICIENT_OF_VARIATION' => '0.3',
831 'STRAIN' => 'NA',
832 'DEVELOPMENTAL_STAGE' => 'seedling, two leaves visible, three leaves visible',
833 'CELL_TYPE' => 'NA',
834 'SEX' => 'NA',
835 'GENE_ID' => 'OS05G0217700',
836 'MEDIAN_EXPRESSION' => '831.1',
837 'NUMBER_OF_RUNS' => 60,
838 'ORGANISM' => 'oryza_sativa',
839 'ALL_SAMPLE_ATTRIBUTES' => 'http://www.ebi.ac.uk/fg/rnaseq/api/tsv/getSampleAttributesByCondition/3238',
840 'ORGANISM_PART' => 'shoot, vascular leaf'
841 },
842 {
843 'STRAIN' => 'NA',
844 'DEVELOPMENTAL_STAGE' => '20 days after sowing',
845 'COEFFICIENT_OF_VARIATION' => '0.3',
846 'CELL_TYPE' => 'NA',
847 'GENE_ID' => 'OS05G0217700',
848 'SEX' => 'NA',
849 'ORGANISM' => 'oryza_sativa',
850 'NUMBER_OF_RUNS' => 4,
851 'MEDIAN_EXPRESSION' => '433.5',
852 'ALL_SAMPLE_ATTRIBUTES' => 'http://www.ebi.ac.uk/fg/rnaseq/api/tsv/getSampleAttributesByCondition/3192',
853 'ORGANISM_PART' => 'leaf'
854 },
855  
856 =cut
857  
858 sub get_expression_by_organism_genesymbol {
859  
860 my ( $self, %args ) = @_;
861  
862 my $logger = $self->get_log_writer;
863
864 unless( $self->_hash_arguments_ok( \%args, "minimum_mapped_reads", "organism", "gene_symbol" ) ) {
865  
866 $logger->error(
867 "Problem with arguments to \"get_expression_by_organism_genesymbol\" function."
868 );
869  
870 return;
871 }
872 # Fail if the organism isn't recognised.
873 unless( $self->_organism_name_ok( $args{ "organism" }, "expression" ) ) {
874  
875 return;
876 }
877
878 my $restResult = $self->_run_rest_call(
879 {
880 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
881 function_name => "getExpression",
882 function_argument => $args{ "organism" } . "/" . $args{ "gene_symbol" }
883 }
884 );
885  
886 if( $restResult ) {
887
888 return $restResult;
889 }
890 else {
891  
892 $logger->error(
893 "Problem retrieving expression information for gene \"",
894 $args{ "gene_symbol" },
895 "\" in organism \"",
896 $args{ "organism" }
897 );
898 }
899 }
900  
901  
902  
903 =item B<get_expression_by_gene_id>
904  
905 Accesses the API's C<getExpression> endpoint, but instead of querying by
906 organism and gene symbol (see L</get_expression_by_organism_genesymbol>), this
907 function queries by gene identifier. Also expects a value for the minimum
908 percentage of mapped reads to allow.
909  
910 my $geneExpressionInfo = $rnaseqAPI->get_expression(
911 gene_identifer => "ENSG00000172023",
912 minimum_mapped_reads => 0
913 );
914  
915 Results are returned as an anonymous array of anonymous hashes, with one
916 anonymous hash per unique combination of tissue, cell type, developmental
917 stage, sex, and strain. See L</get_expression_by_organism_genesymbol> for an
918 example. The median expression level of all runs is given in TPM (transcripts
919 per million). Returns C<undef> (and logs errors) if errors are encountered.
920  
921 =cut
922  
923 sub get_expression_by_gene_id {
924  
925 my ( $self, %args ) = @_;
926  
927 my $logger = $self->get_log_writer;
928  
929 unless( $self->_hash_arguments_ok( \%args, "minimum_mapped_reads", "gene_identifier" ) ) {
930  
931 $logger->error(
932 "Problem with arguments to \"get_expression_by_gene_id\" function."
933 );
934  
935 return;
936 }
937
938 my $restResult = $self->_run_rest_call(
939 {
940 minimum_mapped_reads => $args{ "minimum_mapped_reads" },
941 function_name => "getExpression",
942 function_argument => $args{ "gene_identifier" }
943 }
944 );
945  
946 if( $restResult ) {
947  
948 return $restResult;
949 }
950 else {
951  
952 $logger->error(
953 "Problem retrieving expression information for gene \"",
954 $args{ "gene_identifier" },
955 "\"."
956 );
957 }
958 }
959  
960 =back
961  
962 =cut
963  
964  
965 # Logger builder.
966 sub _build_log_writer {
967  
968 Log::Log4perl->easy_init(
969 {
970 level => $INFO,
971 layout => '%-5p - %m%n'
972 }
973 );
974  
975 return Log::Log4perl::get_logger;
976 }
977  
978 # User Agent builder.
979 sub _build_user_agent {
980  
981 my $userAgent = LWP::UserAgent->new;
982  
983 $userAgent->env_proxy;
984  
985 return $userAgent;
986 }
987  
988 # Run organisms list builder. This is built by accessing the API's endpoints for
989 # the various genome reference resources it uses. Most of these are the
990 # divisions of Ensembl (http://www.ensembl.org and http://ensemblgenomes.org)
991 # -- core, plants, fungi, metazoa, and protists, as well as WormBase ParaSite
992 # (http://parasite.wormbase.org/). The endpoint for each resource provides
993 # key-value pairs of sample organism and reference organism, the reference
994 # organism being the name of the reference genome that was used in the
995 # alignment of RNA-seq reads, and the sample organism being the species the RNA
996 # sample was taken from. Here we collect all the sample organisms, add them as
997 # keys in an anonymous hash (pointing at 1), and return the anonymous hash.
998 sub _build_run_organism_list {
999  
1000 my ( $self ) = @_;
1001  
1002 my $logger = $self->get_log_writer;
1003  
1004 my $organismList = {};
1005  
1006 my @genomeResources = (
1007 "ensembl",
1008 "plants",
1009 "fungi",
1010 "metazoa",
1011 "protists",
1012 "wbps"
1013 );
1014  
1015 # Download the lists of organisms from the API.
1016 foreach my $resource ( @genomeResources ) {
1017
1018 my $restResult = $self->_run_rest_call(
1019 {
1020 minimum_mapped_reads => 0,
1021 function_name => "getOrganisms",
1022 function_argument => $resource
1023 }
1024 );
1025  
1026 if( $restResult ) {
1027  
1028 foreach my $record ( @{ $restResult } ) {
1029
1030 # The sample organism is found under the key "ORGANISM".
1031 $organismList->{ $record->{ "ORGANISM" } } = 1;
1032 }
1033 }
1034  
1035 else {
1036  
1037 $logger->error(
1038 "Problem collecting ",
1039 $resource,
1040 " organism names from API."
1041 );
1042 }
1043 }
1044
1045 return $organismList;
1046 }
1047  
1048  
1049 # Expression organisms list builder.
1050 sub _build_expression_organism_list {
1051  
1052 my ( $self ) = @_;
1053  
1054 my $logger = $self->get_log_writer;
1055  
1056 my $exprOrganismList = {};
1057  
1058 my $restResult = $self->_run_rest_call(
1059 {
1060 function_name => "getExpressionOrganisms"
1061 }
1062 );
1063  
1064 if( $restResult ) {
1065  
1066 foreach my $record ( @{ $restResult } ) {
1067  
1068 $exprOrganismList->{ $record->{ "ORGANISM" } } = 1;
1069 }
1070 }
1071 else {
1072  
1073 $logger->error(
1074 "Problem collecting expression organism names from API."
1075 );
1076 }
1077  
1078 return $exprOrganismList;
1079 }
1080  
1081 # Check that arguments to functions that require a hash ref of named arguments
1082 # are OK. This means:
1083 # - Check the variable is actually a hash.
1084 # - Make sure the arguments required by the calling function are present.
1085 # - Warn about any arguments that were provided that are not recognised by the
1086 # calling function.
1087 sub _hash_arguments_ok {
1088  
1089 my $self = shift;
1090  
1091 my $logger = $self->get_log_writer;
1092  
1093 # Get the arguments hash from the first argument.
1094 my $argsHash = shift;
1095  
1096 # First, make sure the arguments are in a hash.
1097 unless( ref( $argsHash ) eq "HASH" ) {
1098  
1099 $logger->error(
1100 "Arguments should be provided as a hash. See POD for examples."
1101 );
1102  
1103 return;
1104 }
1105 # The rest of the @_ array is the names or the keys that should be present.
1106 my %wantedArgNames = map { $_ => 1 } @_;
1107
1108 # Create a flag to unset if at least one wanted argument is missing.
1109 my $allWantedPresent = 1;
1110
1111 # Check that all the arguments we want are present.
1112 foreach my $wantedArgName ( sort keys %wantedArgNames ) {
1113  
1114 unless( defined( $argsHash->{ $wantedArgName } ) ) {
1115  
1116 $logger->error(
1117 "Required argument \"",
1118 $wantedArgName,
1119 "\" is missing."
1120 );
1121  
1122 $allWantedPresent = 0;
1123 }
1124 }
1125
1126 # Next, check whether there are any unrecognised arguments. Just warn about
1127 # them if so.
1128 foreach my $argName ( sort keys %{ $argsHash } ) {
1129  
1130 unless( $wantedArgNames{ $argName } ) {
1131  
1132 $logger->warn(
1133 "Argument \"",
1134 $argName,
1135 "\" is not recognised."
1136 );
1137 }
1138 }
1139
1140 # Now return the flag to show whether all wanted arguments are present or
1141 # not.
1142 return $allWantedPresent;
1143 }
1144  
1145  
1146 # REST call running -- common to all the querying functions.
1147 sub _run_rest_call {
1148  
1149 my ( $self, $args ) = @_;
1150  
1151 my $logger = $self->get_log_writer;
1152  
1153 my $userAgent = $self->get_user_agent;
1154  
1155 # Start building the query URL.
1156 my $url = $self->get_api_base . "/json/";
1157
1158 # If we're passed a minimum percentage of mapped reads, add this to the URL
1159 # next.
1160 if( defined( $args->{ "minimum_mapped_reads" } ) ) {
1161  
1162 $url .= $args->{ "minimum_mapped_reads" } . "/";
1163 }
1164
1165 # Add the function name and argument to the end of the URL.
1166 $url .= $args->{ "function_name" };
1167  
1168 if( $args->{ "function_argument" } ) {
1169  
1170 $url .= "/" . $args->{ "function_argument" };
1171 }
1172  
1173 # Run HTTP GET request.
1174 my $response = $userAgent->get( $url );
1175  
1176 # If the request was successful, return the parsed JSON.
1177 if( $response->is_success ) {
1178  
1179 return parse_json( $response->decoded_content );
1180 }
1181 # Otherwise, log an error and return undef.
1182 else {
1183
1184 $logger->error(
1185 "Problem retrieving URL: ",
1186 $url,
1187 " . Response from server: ",
1188 $response->status_line
1189 );
1190  
1191 return;
1192 }
1193 }
1194  
1195  
1196 # Check that the run organism name is allowed. This checks the passed string
1197 # against the keys of the hash stored in the "run_organism_list" attribute.
1198 sub _organism_name_ok {
1199  
1200 my ( $self, $organism, $type ) = @_;
1201  
1202 my $logger = $self->get_log_writer;
1203  
1204 my $organismList = ( $type eq "run" ? $self->get_run_organism_list : $self->get_expression_organism_list );
1205  
1206 if( $organismList->{ $organism } ) {
1207  
1208 return 1;
1209 }
1210 else {
1211  
1212 $logger->error(
1213 "Organism \"",
1214 $organism,
1215 "\" is not an allowed organism. Check organisms against the run_organism_list attribute."
1216 );
1217  
1218 return;
1219 }
1220 }
1221  
1222  
1223 =head1 AUTHOR
1224  
1225 Maria Keays <maria.keays@gmail.com>
1226  
1227 The above email should be used for feedback about the Perl module only. All
1228 mail regarding the RNA-seq analysis API itself should be directed to
1229 <rnaseq@ebi.ac.uk>.
1230  
1231 =cut
1232  
1233 1;
1234