HackaMol/Roles/RcsbRole.pm

filter: Statementstmt 17.8% 18 / 101 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 30.0% 3 / 10 Podpod 0.0% 0 / 6 totaltotal 14.0% 21 / 150 CC 18 SCAR 36.9
CC
18
Cov
14%
rcsb_ftp_fetch
90.0
rcsb_sync_local
30.0
ftp_connect
20.0
CRAP 40.2
« HackaMol/Roles/ReadZmatRole.pm ↑ Summary HackaMol.pm »
1 package HackaMol::Roles::RcsbRole;
2 11 $HackaMol::Roles::RcsbRole::VERSION = '0.053';
3 # ABSTRACT: Read files with molecular information
4 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsPaths/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 has 'sync_overwrite' => (
8 is => 'ro',
9 isa => 'Bool',
10 default => 0,
11 lazy => 1,
12 );
13  
14 11 has 'local_pdb_path' => (
15 is => 'ro',
16 isa => Path,
17 coerce => 1,
18 default => '~/myPDB/pdb',
19 lazy => 1,
20 );
21  
22 11 has 'local_cif_path' => (
23 is => 'ro',
24 isa => Path,
25 coerce => 1,
26 default => '~/myPDB/cif',
27 lazy => 1,
28 );
29  
30 11 has 'rcsb_rest_addr' => (
31 is => 'ro',
32 isa => 'Str',
33 default => 'http://www.rcsb.org/pdb/rest/',
34 lazy => 1,
35 );
36  
37 11 has 'rcsb_ftp_addr' => (
38 is => 'ro',
39 isa => 'Str',
40 default => 'ftp.rcsb.org',
41 lazy => 1,
42 );
43  
44 11 has 'ftp_user' => (
45 is => 'ro',
46 isa => 'Str',
47 default => 'anonymous',
48 lazy => 1,
49 );
50  
51 11 has 'ftp_password' => (
52 is => 'ro',
53 isa => 'Str',
54 default => 'anonymous',
55 lazy => 1,
56 );
57  
58 0 ▶sub pdbid_local_path {
Subroutine pdbid_local_path: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my $self = shift;
60 0 my $pdbid = lc( shift );
61  
62 0 ▶ die "Invocation: ->pdb_id_local_path(pdbid,[cif|pdb]?)"
Branch: 1 branch
BranchTrueFalseTotal
unless length $pdbid == 4 0 0 0
63 unless length($pdbid) == 4;
64
65 0 ▶ my $type = shift || 'cif';
Condition0%0/2
shift() || 'cif'
l!l
00
MC/DC0%0/1
shift() || 'cif'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
66  
67 0 my $path_method = "local_${type}_path";
68  
69 # may or may not exist
70 0 my $path = $self->$path_method->child(
71 substr( $pdbid, 1, 2 ) . "/$pdbid.$type" );
72  
73 0 return $path;
74  
75 }
76  
77  
78 0 ▶sub local_pdbs { shift->_local_cifs_pdbs('pdb') }
Subroutine local_pdbs: not called CC=1 SCAR=6.9
Pod undocumented
79 0 ▶sub local_cifs { shift->_local_cifs_pdbs('cif') }
Subroutine local_cifs: not called CC=1 SCAR=6.9
Pod undocumented
80  
81 0 ▶sub _local_cifs_pdbs {
Subroutine _local_cifs_pdbs: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83 0 my $type = shift;
84 0 my $path_method = "local_${type}_path";
85 0 my @files = map { $_->children(qr/\.$type$/) }
86 0 grep { $_->is_dir } $self->$path_method->children;
87 0 return @files;
88 }
89  
90  
91 0 ▶sub rcsb_sync_local {
Subroutine rcsb_sync_local: not called CC=5 SCAR=34.0
Pod undocumented
92 0 my $self = shift;
93 0 my $type = shift;
94 0 ▶ die "Invocation-> rcsb_sync_local('cif|pdb')" unless $type =~ /(?:cif|pdb)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /(?:cif|pdb)/ 0 0 0
95 0 my @pdbids = map{ lc($_) } @_;
96  
97 0 my $local_types = "local_${type}s";
98 0 my @local_pdbids = map{ $_->basename(qr/\.$type$/)} $self->$local_types;
99 0 my %seen = map {$_ => 1} @local_pdbids;
100  
101 0 ▶ unless ($self->sync_overwrite){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sync_overwrite) 0 0 0
102 0 my $count = @pdbids;
103 0 @pdbids = grep {! exists($seen{$_})} @pdbids;
104 0 ▶ if ($count != @pdbids){
Branch: 1 branch
BranchTrueFalseTotal
if ($count != @pdbids) 0 0 0
105 0 my $local_path = "local_${type}_path";
106 0 warn "ignoring @{[$count - @pdbids]} files contained in @{[$self->$local_path]}\n";
107 }
108 }
109  
110 0 ▶ return ([],[]) unless @pdbids;
Branch: 1 branch
BranchTrueFalseTotal
unless @pdbids 0 0 0
111 0 print "syncing @{[scalar @pdbids]} $type files\n";
112 0 my ($synced_pdbids,$missed_pdbids) = $self->rcsb_ftp_fetch($type, \@pdbids );
113 0 return ($synced_pdbids,$missed_pdbids);
114 }
115  
116 0 ▶sub rcsb_ftp_fetch {
Subroutine rcsb_ftp_fetch: not called CC=9 SCAR=45.0
Pod undocumented
117  
118 0 require IO::Uncompress::Gunzip;
119 0 my $self = shift;
120 0 my $type = shift;
121 0 ▶ die "Invocation-> rcsb_sync_local('cif|pdb')" unless $type =~ /(?:cif|pdb)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /(?:cif|pdb)/ 0 0 0
122 0 my $pdbids = shift;
123 0 my $parent_path = shift;
124 0 my $local_path = "local_${type}_path";
125 0 ▶ $parent_path = $self->$local_path unless $parent_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_path 0 0 0
126  
127 0 ▶ my $cwd_base = $type eq 'cif' ? 'mmCIF' : 'pdb';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'cif' ? : 0 0 0
128 0 my $ftp = $self->ftp_connect("/pub/pdb/data/structures/divided/$cwd_base");
129  
130 0 my @pdbids = map { lc($_) } @$pdbids;
131 0 my @fetched;
132  
133 my @missed;
134 0 foreach my $pdbid (@pdbids) {
135 0 print "fetching $pdbid\n";
136 0 my $gz = "$pdbid.cif.gz";
137 0 my $subdir = substr( $pdbid, 1, 2 );
138  
139 $ftp->get("$subdir/$gz")
140 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$ftp->get("$subdir/$gz") or do { print "unable to fetch $pdbid\n"; print $ftp->message; die "ftp download problems" if $ftp->message =~ /load was .+ when you connected/; push @missed, $pdbid; next } 0 0 0
141 0 print "unable to fetch $pdbid\n";
142 0 print $ftp->message;
143 0 ▶ die "ftp download problems" if ($ftp->message =~ /load was .+ when you connected/);
Branch: 1 branch
BranchTrueFalseTotal
if $ftp->message =~ /load was .+ when you connected/ 0 0 0
144 0 push @missed,$pdbid;
145 next
146 0 };
147  
148 0 my $dest_par = $parent_path->child("$subdir");
149 0 ▶ $dest_par->mkpath unless $dest_par->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $dest_par->exists 0 0 0
150 0 my $dest = $dest_par->child("$pdbid.$type");
151 0 ▶ IO::Uncompress::Gunzip::gunzip( $gz => $dest->stringify )
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip($gz, $dest->stringify) or die "unable to gunzip $gz" 0 0 0
152 or die "unable to gunzip $gz";
153 0 unlink $gz;
154 0 push @fetched, $pdbid;
155 }
156  
157 0 $ftp->quit;
158 0 return (\@fetched,\@missed);
159 }
160  
161  
162 0 ▶sub ftp_connect {
Subroutine ftp_connect: not called CC=4 SCAR=30.0
Pod undocumented
163 # connects to FTP via rcsb_ftp_addr and sets working directory to path if passed
164 0 require Net::FTP;
165
166 0 my $self = shift;
167 0 my $path = shift;
168 0 my $host = $self->rcsb_ftp_addr;
169 0 my $user = $self->ftp_user;
170 0 my $pass = $self->ftp_password;
171
172 0 my $ftp = Net::FTP->new($host);
173 0 ▶ $ftp->login( $user, $pass ) or die "cannot login to rcsb ftp addr";
Branch: 1 branch
BranchTrueFalseTotal
$ftp->login($user, $pass) or die "cannot login to rcsb ftp addr" 0 0 0
174 0 ▶ if($path){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
175 0 ▶ $ftp->cwd($path) or die "cannont cwd to $path";
Branch: 1 branch
BranchTrueFalseTotal
$ftp->cwd($path) or die "cannont cwd to $path" 0 0 0
176 }
177 0 $ftp->binary();
178 0 return $ftp;
179 }
180  
181 11 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 11 1;
183  
184 __END__
« HackaMol/Roles/ReadZmatRole.pm ↑ Summary HackaMol.pm »