blib/lib/Chemistry/File/OPTIMADE.pm

filter: Statementstmt 87.1% 61 / 70 Branchbran 44.4% 8 / 18 Conditioncond 34.4% 10 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 63.0% 92 / 146 CC 22 SCAR 36.1
CC
22
Cov
68%
parse_string
55.0
__ANON__
1.1
__ANON__
1.0
CRAP 37.0
↑ Summary
1 package Chemistry::File::OPTIMADE;
2  
3 1 our $VERSION = '0.11'; # VERSION
4 # $Id$
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base 'Chemistry::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Chemistry::Mol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URL::Encode qw( url_params_multi );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my @mandatory_fields = qw( cartesian_site_positions species species_at_sites );
17  
18 =head1 NAME
19  
20 Chemistry::File::OPTIMADE - OPTIMADE reader
21  
22 =head1 SYNOPSIS
23  
24 use Chemistry::File::OPTIMADE;
25  
26 # read a molecule
27 my $file = Chemistry::File::OPTIMADE->new( file => 'myfile.json' );
28 my $mol = $file->read();
29  
30 =cut
31  
32 # Format is not registered, as OPTIMADE does not have proper file extension.
33 # .json is an option, but not sure if it will not clash with anything else.
34  
35 =head1 DESCRIPTION
36  
37 OPTIMADE structure representation reader.
38  
39 =cut
40  
41 1 ▶sub parse_string {
Subroutine parse_string: called CC=22 SCAR=40.1
Pod documented
42 1 my ($self, $s, %opts) = @_;
43  
44 1 ▶ my $mol_class = $opts{mol_class} || 'Chemistry::Mol';
Condition50%1/2
$opts{'mol_class'} || 'Chemistry::Mol'
l!l
01
MC/DC0%0/1
$opts{'mol_class'} || 'Chemistry::Mol'
$opts{'mol_class'}
Truth table50%1/2
A
A= $opts{'mol_class'}
Aresult
11
00
45 1 ▶ my $atom_class = $opts{atom_class} || $mol_class->atom_class;
Condition33%1/3
$opts{'atom_class'} || $mol_class->atom_class
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'atom_class'} || $mol_class->atom_class
$opts{'atom_class'} $mol_class->atom_class
Truth table33%1/3
A || B
A= $opts{'atom_class'}B= $mol_class->atom_class
ABresult
1X1
011
000
46 1 ▶ my $bond_class = $opts{bond_class} || $mol_class->bond_class;
Condition33%1/3
$opts{'bond_class'} || $mol_class->bond_class
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'bond_class'} || $mol_class->bond_class
$opts{'bond_class'} $mol_class->bond_class
Truth table33%1/3
A || B
A= $opts{'bond_class'}B= $mol_class->bond_class
ABresult
1X1
011
000
47  
48 1 my $json = decode_json $s;
49  
50 1 ▶ if( $json->{meta} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"meta"} and $json->{"meta"}{"api_version"} and $json->{"meta"}{"api_version"} =~ /^[^01]\./) 0 1 1
Condition33%2/6
$json->{"meta"} and $json->{"meta"}{"api_version"} and $json->{"meta"}{"api_version"} =~ /^[^01]\./
!ll&&!rl&&r
100
$json->{"meta"} and $json->{"meta"}{"api_version"}
!ll&&!rl&&r
010
MC/DC0%0/3
$json->{"meta"} and $json->{"meta"}{"api_version"} and $json->{"meta"}{"api_version"} =~ /^[^01]\./
$json->{"meta"} $json->{"meta"}{"api_version"} $json->{"meta"}{"api_version"} =~ /^[^01]\./
Truth table25%1/4
A and B and C
A= $json->{"meta"}B= $json->{"meta"}{"api_version"}C= $json->{"meta"}{"api_version"} =~ /^[^01]\./
ABCresult
0XX0
10X0
1100
1111
51 $json->{meta}{api_version} &&
52 $json->{meta}{api_version} =~ /^[^01]\./ ) {
53 warn 'OPTIMADE API version ' . $json->{meta}{api_version} .
54 0 ' encountered, this module supports versions 0 and 1, ' .
55 'later versions may not work as expected' . "\n";
56 }
57  
58 1 my $required_fields_selected;
59 1 ▶ if( $json->{meta} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"meta"} and $json->{"meta"}{"query"} and $json->{"meta"}{"query"}{"representation"}) 1 0 1
Condition33%2/6
$json->{"meta"} and $json->{"meta"}{"query"} and $json->{"meta"}{"query"}{"representation"}
!ll&&!rl&&r
001
$json->{"meta"} and $json->{"meta"}{"query"}
!ll&&!rl&&r
001
MC/DC0%0/3
$json->{"meta"} and $json->{"meta"}{"query"} and $json->{"meta"}{"query"}{"representation"}
$json->{"meta"} $json->{"meta"}{"query"} $json->{"meta"}{"query"}{"representation"}
Truth table25%1/4
A and B and C
A= $json->{"meta"}B= $json->{"meta"}{"query"}C= $json->{"meta"}{"query"}{"representation"}
ABCresult
0XX0
10X0
1100
1111
60 $json->{meta}{query} &&
61 $json->{meta}{query}{representation} ) {
62 1 ▶ if( $json->{meta}{query}{representation} =~ /\?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'meta'}{'query'}{'representation'} =~ /\?/) { } 1 0 1
63 1 my( $query ) = reverse split /\?/, $json->{meta}{query}{representation};
64 1 $query = url_params_multi $query;
65 1 ▶ if( $query->{response_fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{'response_fields'}) { } 1 0 1
66 1 my @response_fields = split ',', $query->{response_fields}[0];
67 $required_fields_selected =
68 1 ▶ (any { $_ eq 'cartesian_site_positions' } @response_fields) &&
Subroutine __ANON__: called CC=1 SCAR=0.0
69 3 ▶ (any { $_ eq 'species' } @response_fields) &&
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 ▶ (any { $_ eq 'species_at_sites' } @response_fields);
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%2/6
&any(sub { $_ eq 'cartesian_site_positions'; } , @response_fields) && &any(sub { $_ eq 'species'; } , @response_fields) && &any(sub { $_ eq 'species_at_sites'; } , @response_fields)
!ll&&!rl&&r
001
&any(sub { $_ eq "cartesian_site_positions"; } , @response_fields) and &any(sub { $_ eq "species"; } , @response_fields)
!ll&&!rl&&r
001
MC/DC0%0/3
&any(sub { $_ eq 'cartesian_site_positions'; } , @response_fields) && &any(sub { $_ eq 'species'; } , @response_fields) && &any(sub { $_ eq 'species_at_sites'; } , @response_fields)
&any(sub { $_ eq "cartesian_site_positions"; } , @response_fields) &any(sub { $_ eq "species"; } , @response_fields) &any(sub { $_ eq 'species_at_sites'; } , @response_fields)
Truth table25%1/4
A and B && C
A= &any(sub { $_ eq "cartesian_site_positions"; } , @response_fields)B= &any(sub { $_ eq "species"; } , @response_fields)C= &any(sub { $_ eq 'species_at_sites'; } , @response_fields)
ABCresult
0XX0
10X0
1100
1111
71 } else {
72 0 $required_fields_selected = ''; # false
73 }
74 } else {
75 0 $required_fields_selected = ''; # false
76 }
77 }
78  
79 1 ▶ return () unless $json->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless $json->{"data"} 0 1 1
80  
81 1 my @molecule_descriptions;
82 1 ▶ if( ref $json->{data} eq 'HASH' && $json->{data}{attributes} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $json->{'data'} eq 'HASH' and $json->{'data'}{'attributes'}) { } 1 0 1
elsif (ref $json->{'data'} eq 'ARRAY') { } 0 0 0
Condition33%1/3
ref $json->{"data"} eq "HASH" and $json->{"data"}{"attributes"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $json->{"data"} eq "HASH" and $json->{"data"}{"attributes"}
ref $json->{"data"} eq "HASH" $json->{"data"}{"attributes"}
Truth table33%1/3
A and B
A= ref $json->{"data"} eq "HASH"B= $json->{"data"}{"attributes"}
ABresult
0X0
100
111
83 1 @molecule_descriptions = ( $json->{data} );
84 } elsif( ref $json->{data} eq 'ARRAY' ) {
85 0 @molecule_descriptions = @{$json->{data}};
86 } else {
87 0 return ();
88 }
89  
90 1 my @molecules;
91 1 for my $description (@molecule_descriptions) {
92 1 my $mol = $mol_class->new( name => $description->{id} );
93 1 my $attributes = $description->{attributes};
94  
95 # TODO: Warn about disorder
96  
97 1 ▶ if( any { !exists $attributes->{$_} } @mandatory_fields ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not exists $attributes->{$_}; } , @mandatory_fields)) 0 1 1
98 warn 'one or more of the mandatory fields (' .
99 0 join( ', ', map { "'$_'" } @mandatory_fields ) .
100 'not found in input for molecule \'' .
101 0 $description->{id} . '\', skipping' . "\n";
102 }
103  
104 1 my %species = map { $_->{name} => $_ } @{$attributes->{species}};
105 1 for my $site (0..$#{$attributes->{cartesian_site_positions}}) {
106 1 my $species = $species{$attributes->{species_at_sites}[$site]};
107  
108 # FIXME: For now we are taking the first chemical symol.
109 # PerlMol is not capable to represent mixture sites.
110 my $atom = $mol->new_atom( coords => $attributes->{cartesian_site_positions}[$site],
111 1 symbol => $species->{chemical_symbols}[0] );
112 1 ▶ if( exists $species->{mass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $species->{"mass"}) 0 1 1
113 0 $atom->mass( $species->{mass}[0] );
114 }
115 }
116 1 push @molecules, $mol;
117 }
118 1 return @molecules;
119 }
120  
121 1 1;
122  
123 =head1 SOURCE CODE REPOSITORY
124  
125 L<https://github.com/perlmol/Chemistry-File-OPTIMADE>
126  
127 =head1 SEE ALSO
128  
129 L<Chemistry::Mol>, L<Chemistry::File>
130  
131 The OPTIMADE Home Page at https://www.optimade.org
132  
133 =head1 AUTHOR
134  
135 Andrius Merkys <merkys@cpan.org>
136  
137 =head1 COPYRIGHT
138  
139 Copyright (c) 2022 Andrius Merkys. All rights reserved. This program is
140 free software; you can redistribute it and/or modify it under the same terms as
141 Perl itself.
142  
143 =cut
↑ Summary