blib/lib/Bioconda/Packages.pm

filter: Statementstmt 100% 99 / 99 Branchbran 79.4% 27 / 34 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 18 / 18 Podpod 100% 12 / 12 totaltotal 94.1% 176 / 187 CC 29 SCAR 19.7
CC
29
Cov
94%
reverse_dependencies
13.1
search
11.1
exists
3.0
CRAP 7.2
« bin/bioconda-packages ↑ Summary blib/lib/Bioconda/Packages/Package.pm »
1 package Bioconda::Packages;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use Bioconda::Packages::DB ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Bioconda::Packages::Package ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 our $VERSION = '2026.12.1';
11  
12 =head1 NAME
13  
14 Bioconda::Packages - Query a bundled snapshot of Bioconda package metadata.
15  
16 =head1 SYNOPSIS
17  
18 use Bioconda::Packages;
19  
20 my $bp = Bioconda::Packages->new; # loads the bundled database
21  
22 if ($bp->exists('samtools')) {
23 my $pkg = $bp->get('samtools');
24 print $pkg->current_version, "\n"; # 1.25
25 print "$_\n" for $pkg->dependency_names('run');
26 }
27  
28 my @hits = $bp->search('aligner'); # name/summary substring match
29 my @users = $bp->reverse_dependencies('zlib');
30 my @gone = $bp->removed; # packages dropped from bioconda
31  
32 =head1 DESCRIPTION
33  
34 C<Bioconda::Packages> gives offline, read-only access to a snapshot of Bioconda
35 package metadata: versions, dependencies, licenses, summaries and homepage/URLs.
36 The snapshot is a JSON database shipped with the distribution and rebuilt by the
37 maintainer tooling (see L<Bioconda::Packages::Updater> and the C<maintainer/>
38 scripts). No network access is needed at query time.
39  
40 Packages that disappear from C<bioconda-recipes> are B<retained> in the database
41 and flagged as removed (see L</removed> and C<is_removed> on
42 L<Bioconda::Packages::Package>), so historical lookups keep working.
43  
44 =head1 METHODS
45  
46 =head2 new
47  
48 my $bp = Bioconda::Packages->new;
49 my $bp = Bioconda::Packages->new(db => '/path/to/db.json');
50  
51 Loads the database. Options:
52  
53 =over 4
54  
55 =item C<db>
56  
57 Explicit path to a JSON database. Defaults to the bundled database (see
58 L<Bioconda::Packages::DB/locate_db>).
59  
60 =back
61  
62 =cut
63  
64 19 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 19 my ($class, %args) = @_;
66 19 my $path = Bioconda::Packages::DB->locate_db($args{db});
67 19 my $data = Bioconda::Packages::DB->load($path);
68 my $self = {
69 db_path => $path,
70 data => $data,
71 packages => $data->{packages},
72 19 };
73 19 return bless $self, $class;
74 }
75  
76 =head2 db_path
77  
78 Path to the database file that was loaded.
79  
80 =head2 meta
81  
82 The database C<meta> hashref (generation time, commit, counts).
83  
84 =cut
85  
86 1 ▶sub db_path { $_[0]{db_path} }
Subroutine db_path: called CC=1 SCAR=0.0
Pod documented
87 12 ▶sub meta { $_[0]{data}{meta} || {} }
Subroutine meta: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"data"}{"meta"} or {} 12 0 12
88  
89 =head2 db_version
90  
91 The database snapshot version, a CalVer/SemVer string like C<2026.11.1>
92 (year.month.progressive). Sortable under SemVer rules. Returns C<undef> for a
93 database built before versions were stamped.
94  
95 =cut
96  
97 1 ▶sub db_version { $_[0]{data}{meta}{db_version} }
Subroutine db_version: called CC=1 SCAR=0.0
Pod documented
98  
99 =head2 exists
100  
101 $bp->exists('samtools');
102  
103 True if the (case-insensitive) package name is in the database, including removed
104 packages.
105  
106 =cut
107  
108 10 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.1
Pod documented
109 10 my ($self, $name) = @_;
110 10 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 10 10
111 10 ▶ return exists $self->{packages}{ _norm($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'packages'}{_norm($name)} ? : 9 1 10
112 }
113  
114 =head2 get
115  
116 my $pkg = $bp->get('samtools');
117  
118 Returns a L<Bioconda::Packages::Package> object, or C<undef> if unknown.
119  
120 =cut
121  
122 17 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
123 17 my ($self, $name) = @_;
124 17 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 17 17
125 17 ▶ my $rec = $self->{packages}{ _norm($name) } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->{"packages"}{_norm($name)} or return undef 16 1 17
126 16 return Bioconda::Packages::Package->new($rec);
127 }
128  
129 =head2 list
130  
131 my @names = $bp->list; # all current (non-removed)
132 my @names = $bp->list(include_removed => 1); # everything
133  
134 Returns a sorted list of package names.
135  
136 =cut
137  
138 6 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
139 6 my ($self, %opt) = @_;
140 6 my $inc = $opt{include_removed};
141 my @names = sort grep {
142 $inc || !$self->{packages}{$_}{is_removed}
143 6 ▶ } keys %{ $self->{packages} };
Condition100%3/3
$inc || !$self->{'packages'}{$_}{'is_removed'}
l!l&&r!l&&!r
6104
MC/DC100%2/2
$inc || !$self->{'packages'}{$_}{'is_removed'}
$inc !$self->{'packages'}{$_}{'is_removed'}
Truth table100%3/3
A || not(B)
A= $incB= !$self->{'packages'}{$_}{'is_removed'}
ABresult
1X1
011
000
144 6 return @names;
145 }
146  
147 =head2 count
148  
149 my $n = $bp->count; # current packages
150 my $n = $bp->count(include_removed => 1);
151  
152 =cut
153  
154 2 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
155 2 my ($self, %opt) = @_;
156 2 return scalar $self->list(%opt);
157 }
158  
159 =head2 removed
160  
161 my @names = $bp->removed;
162  
163 Sorted list of packages flagged as removed from bioconda-recipes.
164  
165 =cut
166  
167 6 ▶sub removed {
Subroutine removed: called CC=1 SCAR=0.0
Pod documented
168 6 my ($self) = @_;
169 11545 my @r = sort grep { $self->{packages}{$_}{is_removed} }
170 6 keys %{ $self->{packages} };
171 6 return @r;
172 }
173  
174 =head2 search
175  
176 my @names = $bp->search('aligner');
177 my @names = $bp->search(qr/align/i);
178 my @names = $bp->search('align', fields => ['name']);
179 my @names = $bp->search('align', include_removed => 1);
180  
181 Case-insensitive substring (or regex) search over package name and summary by
182 default. Returns a sorted list of matching package names.
183  
184 =cut
185  
186 7 ▶sub search {
Subroutine search: called CC=11 SCAR=24.0
Pod documented
187 7 my ($self, $query, %opt) = @_;
188 7 ▶ croak "search() needs a query" unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 7 7
189 7 ▶ my $re = ref $query eq 'Regexp' ? $query : qr/\Q$query\E/i;
Branch: 1 branch
BranchTrueFalseTotal
ref $query eq 'Regexp' ? : 0 7 7
190 7 ▶ my @fields = @{ $opt{fields} || ['name', 'summary'] };
Condition100%2/2
$opt{'fields'} || ['name', 'summary']
l!l
16
MC/DC100%1/1
$opt{'fields'} || ['name', 'summary']
$opt{'fields'}
Truth table100%2/2
A
A= $opt{'fields'}
Aresult
11
00
191 7 my $inc = $opt{include_removed};
192  
193 7 my @hits;
194 7 for my $name (keys %{ $self->{packages} }) {
195 21 my $rec = $self->{packages}{$name};
196 21 ▶ next if $rec->{is_removed} && !$inc;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"is_removed"} and not $inc 5 16 21
Condition100%3/3
$rec->{"is_removed"} and not $inc
!ll&&!rl&&r
1425
MC/DC100%2/2
$rec->{"is_removed"} and not $inc
$rec->{"is_removed"} not $inc
Truth table100%3/3
A and not(B)
A= $rec->{"is_removed"}B= not $inc
ABresult
0X0
100
111
197 16 for my $f (@fields) {
198 29 ▶ my $val = $f eq 'name' ? $name : $rec->{$f};
Branch: 1 branch
BranchTrueFalseTotal
$f eq 'name' ? : 14 15 29
199 29 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 29 29
200 29 ▶ if ($val =~ $re) { push @hits, $name; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /$re/) 6 23 29
201 }
202 }
203 7 @hits = sort @hits;
204 7 return @hits;
205 }
206  
207 =head2 reverse_dependencies
208  
209 my @users = $bp->reverse_dependencies('zlib');
210 my @users = $bp->reverse_dependencies('zlib', kind => 'host');
211 my @users = $bp->reverse_dependencies('zlib', kind => 'all');
212  
213 Returns the sorted names of packages that depend on the given package. C<kind>
214 is C<run> (default), C<build>, C<host>, or C<all>.
215  
216 =cut
217  
218 5 ▶sub reverse_dependencies {
Subroutine reverse_dependencies: called CC=13 SCAR=25.7
Pod documented
219 5 my ($self, $dep, %opt) = @_;
220 5 ▶ croak "reverse_dependencies() needs a dependency name" unless defined $dep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dep 0 5 5
221 5 ▶ my $kind = $opt{kind} || 'run';
Condition50%1/2
$opt{'kind'} || 'run'
l!l
50
MC/DC0%0/1
$opt{'kind'} || 'run'
$opt{'kind'}
Truth table50%1/2
A
A= $opt{'kind'}
Aresult
11
00
222 5 my $inc = $opt{include_removed};
223 5 my $target = lc $dep;
224  
225 5 my @hits;
226 5 for my $name (keys %{ $self->{packages} }) {
227 11540 my $rec = $self->{packages}{$name};
228 11540 ▶ next if $rec->{is_removed} && !$inc;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"is_removed"} and not $inc 3 11537 11540
Condition100%3/3
$rec->{"is_removed"} and not $inc
!ll&&!rl&&r
1153613
MC/DC100%2/2
$rec->{"is_removed"} and not $inc
$rec->{"is_removed"} not $inc
Truth table100%3/3
A and not(B)
A= $rec->{"is_removed"}B= not $inc
ABresult
0X0
100
111
229 11537 ▶ my @kinds = $kind eq 'all' ? qw(build host run) : ($kind);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'all' ? : 11533 4 11537
230 11537 my $found = 0;
231 11537 for my $k (@kinds) {
232 32800 ▶ for my $d (@{ $rec->{depends}{$k} || [] }) {
Condition50%1/2
$rec->{'depends'}{$k} || []
l!l
328000
MC/DC0%0/1
$rec->{'depends'}{$k} || []
$rec->{'depends'}{$k}
Truth table50%1/2
A
A= $rec->{'depends'}{$k}
Aresult
11
00
233 135428 ▶ if (lc $d->{name} eq $target) { $found = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $d->{"name"} eq $target) 1840 133588 135428
234 }
235 32800 ▶ last if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 1840 30960 32800
236 }
237 11537 ▶ push @hits, $name if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 1840 9697 11537
238 }
239 5 @hits = sort @hits;
240 5 return @hits;
241 }
242  
243 =head2 stats
244  
245 my %s = $bp->stats;
246  
247 Returns a hash of summary counts: C<total>, C<current>, C<removed>, C<noarch>,
248 plus the database C<generated_at> and C<recipes_commit> when present.
249  
250 =cut
251  
252 3 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
253 3 my ($self) = @_;
254 3 my $total = scalar keys %{ $self->{packages} };
255 3 my @removed = $self->removed;
256 3 my $removed = scalar @removed;
257 3 my $noarch = grep { $_->{noarch} } values %{ $self->{packages} };
258 return (
259 total => $total,
260 current => $total - $removed,
261 removed => $removed,
262 noarch => $noarch,
263 db_version => $self->meta->{db_version},
264 generated_at => $self->meta->{generated_at},
265 recipes_commit => $self->meta->{recipes_commit},
266 3 );
267 }
268  
269 # Normalise a package name for lookup (conda names are lower-case).
270 27 ▶sub _norm {
Subroutine _norm: called CC=1 SCAR=0.0
271 27 my ($n) = @_;
272 27 $n = lc $n;
273 27 $n =~ s/^\s+|\s+$//g;
274 27 return $n;
275 }
276  
277 =head1 SEE ALSO
278  
279 L<Bioconda::Packages::Package>, L<Bioconda::Packages::Updater>,
280 L<Bioconda::Packages::RecipeParser>, L<https://bioconda.github.io>.
281  
282 =head1 AUTHOR
283  
284 Andrea Telatin
285  
286 =head1 LICENSE
287  
288 This is free software; you can redistribute it and/or modify it under the
289 same terms as Perl 5 itself. See L<perlartistic> and L<perlgpl> for the
290 license terms.
291  
292 =cut
293  
294 18 1;
« bin/bioconda-packages ↑ Summary blib/lib/Bioconda/Packages/Package.pm »