File Coverage

blib/lib/HackaMol/Roles/FileFetchRole.pm
Criterion Covered Total %
statement 18 44 40.9
branch 0 8 0.0
condition 0 3 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 26 66 39.3


line stmt bran cond sub pod time code
1             package HackaMol::Roles::FileFetchRole;
2             $HackaMol::Roles::FileFetchRole::VERSION = '0.051';
3             #ABSTRACT: Role for using LWP::Simple to fetch files from www
4 11     11   6006 use Moose::Role;
  11         42  
  11         79  
5 11     11   57429 use Carp;
  11         54  
  11         661  
6 11     11   93 use Path::Tiny;
  11         44  
  11         586  
7 11     11   7862 use HTTP::Tiny;
  11         403990  
  11         530  
8             #use LWP::Simple;
9 11     11   7066 use Data::Dumper;
  11         69883  
  11         3981  
10              
11             has 'pdbserver', is => 'rw', isa => 'Str', lazy => 1, default => 'https://files.rcsb.org/download/';
12             has 'overwrite', is => 'rw', isa => 'Bool', lazy => 1, default => 0;
13              
14             sub _fix_pdbid{
15 0     0     my $pdbid = shift;
16 0           $pdbid =~ s/\.pdb//; #just in case
17 0           $pdbid .= '.pdb';
18 0           return $pdbid;
19             }
20              
21             sub get_pdbid{
22             #return pdb contents downloaded from pdb.org
23 0     0 1   my $self = shift;
24 0           my $pdbid = _fix_pdbid(shift);
25 0           my ($ok, $why) = HTTP::Tiny->can_ssl;
26 0 0         if ($ok){
27 0           my $pdb = HTTP::Tiny->new->get($self->pdbserver.$pdbid);
28 0           return ( $pdb->{content} );
29             }
30             else {
31 0           warn "$why";
32 0           return 0;
33             }
34             }
35              
36             sub getstore_pdbid{
37             #return array of lines from pdb downloaded from pdb.org
38 0     0 1   my $self = shift;
39 0           my $pdbid = _fix_pdbid(shift);
40 0           my $fpdbid = shift ;
41 0 0         $fpdbid = $pdbid unless defined($fpdbid);
42 0           $fpdbid = path($fpdbid);
43              
44 0 0 0       if ($fpdbid->exists and not $self->overwrite){
45 0           carp "$fpdbid exists, set self->overwrite(1) to overwrite";
46 0           return $fpdbid->stringify;
47             }
48 0           my $pdb = $self->get_pdbid( $pdbid );
49            
50 0 0         if ($pdb){
51 0           $fpdbid->spew($pdb);
52 0           return ( $fpdbid->stringify );
53             }
54             else{
55 0           warn "could not connect, $fpdbid not written\n";
56 0           return 0;
57             }
58             }
59              
60 11     11   103 no Moose::Role;
  11         32  
  11         127  
61             1;
62              
63             __END__
64              
65             =pod
66              
67             =head1 NAME
68              
69             HackaMol::Roles::FileFetchRole - Role for using LWP::Simple to fetch files from www
70              
71             =head1 VERSION
72              
73             version 0.051
74              
75             =head1 SYNOPSIS
76              
77             use HackaMol;
78              
79             my $pdb = $HackaMol->new->get_pdbid("2cba");
80             print $pdb;
81              
82             =head1 DESCRIPTION
83              
84             FileFetchRole provides attributes and methods for pulling files from the internet.
85             Currently, the Role has one method and one attribute for interacting with the Protein Database.
86              
87             =head1 METHODS
88              
89             =head2 get_pdbid
90              
91             fetches a pdb from pdb.org and returns the file in a string.
92              
93             =head2 getstore_pdbid
94              
95             arguments: pdbid and filename for writing (optional).
96             Fetches a pdb from pdb.org and stores it in your working directory unless {it exists and overwrite(0)}. If a filename is not
97             passed to the method, it will write to $pdbid.pdb. use get_pdbid to return contents
98              
99             =head1 ATTRIBUTES
100              
101             =head2 overwrite
102              
103             isa lazy ro Bool that defaults to 0 (false). If overwrite(1), then fetched files will be able to overwrite
104             those of same name in working directory.
105              
106             =head2 pdbserver
107              
108             isa lazy rw Str that defaults to http://pdb.org/pdb/files/
109              
110             =head1 SEE ALSO
111              
112             =over 4
113              
114             =item *
115              
116             L<http://www.pdb.org>
117              
118             =item *
119              
120             L<LWP::Simple>
121              
122             =back
123              
124             =head1 AUTHOR
125              
126             Demian Riccardi <demianriccardi@gmail.com>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2017 by Demian Riccardi.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut