Packages.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 76.4% 26 / 34 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 12 / 12 totaltotal 92.5% 173 / 187 CC 29 SCAR 19.7
CC
29
Cov
93%
reverse_dependencies
13.1
search
11.1
get
3.1
CRAP 7.2
« Packages/DB.pm ↑ Summary Packages/Package.pm »
1 package Bioconda::Packages;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Bioconda::Packages::DB ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Bioconda::Packages::Package ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '2026.11.4';
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 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 2 my ($class, %args) = @_;
66 2 my $path = Bioconda::Packages::DB->locate_db($args{db});
67 2 my $data = Bioconda::Packages::DB->load($path);
68 my $self = {
69 db_path => $path,
70 data => $data,
71 packages => $data->{packages},
72 2 };
73 2 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 0 ▶sub db_path { $_[0]{db_path} }
Subroutine db_path: not called CC=1 SCAR=6.9
Pod documented
87 6 ▶sub meta { $_[0]{data}{meta} || {} }
Subroutine meta: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"data"}{"meta"} or {} 6 0 6
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 5 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.1
Pod documented
109 5 my ($self, $name) = @_;
110 5 ▶ return 0 unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5 5
111 5 ▶ return exists $self->{packages}{ _norm($name) } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'packages'}{_norm($name)} ? : 4 1 5
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 4 ▶sub get {
Subroutine get: called CC=3 SCAR=11.4
Pod documented
123 4 my ($self, $name) = @_;
124 4 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
125 4 ▶ my $rec = $self->{packages}{ _norm($name) } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->{"packages"}{_norm($name)} or return undef 4 0 4
126 4 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 4 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
139 4 my ($self, %opt) = @_;
140 4 my $inc = $opt{include_removed};
141 my @names = sort grep {
142 $inc || !$self->{packages}{$_}{is_removed}
143 4 ▶ } keys %{ $self->{packages} };
Condition100%3/3
$inc || !$self->{'packages'}{$_}{'is_removed'}
l!l&&r!l&&!r
642
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 4 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 3 ▶sub removed {
Subroutine removed: called CC=1 SCAR=0.0
Pod documented
168 3 my ($self) = @_;
169 11527 my @r = sort grep { $self->{packages}{$_}{is_removed} }
170 3 keys %{ $self->{packages} };
171 3 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 4 ▶sub search {
Subroutine search: called CC=11 SCAR=24.0
Pod documented
187 4 my ($self, $query, %opt) = @_;
188 4 ▶ croak "search() needs a query" unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 4 4
189 4 ▶ my $re = ref $query eq 'Regexp' ? $query : qr/\Q$query\E/i;
Branch: 1 branch
BranchTrueFalseTotal
ref $query eq 'Regexp' ? : 0 4 4
190 4 ▶ my @fields = @{ $opt{fields} || ['name', 'summary'] };
Condition100%2/2
$opt{'fields'} || ['name', 'summary']
l!l
13
MC/DC100%1/1
$opt{'fields'} || ['name', 'summary']
$opt{'fields'}
Truth table100%2/2
A
A= $opt{'fields'}
Aresult
11
00
191 4 my $inc = $opt{include_removed};
192  
193 4 my @hits;
194 4 for my $name (keys %{ $self->{packages} }) {
195 12 my $rec = $self->{packages}{$name};
196 12 ▶ next if $rec->{is_removed} && !$inc;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"is_removed"} and not $inc 3 9 12
Condition100%3/3
$rec->{"is_removed"} and not $inc
!ll&&!rl&&r
813
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 9 for my $f (@fields) {
198 15 ▶ my $val = $f eq 'name' ? $name : $rec->{$f};
Branch: 1 branch
BranchTrueFalseTotal
$f eq 'name' ? : 7 8 15
199 15 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 15 15
200 15 ▶ if ($val =~ $re) { push @hits, $name; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /$re/) 4 11 15
201 }
202 }
203 4 @hits = sort @hits;
204 4 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 4 ▶sub reverse_dependencies {
Subroutine reverse_dependencies: called CC=13 SCAR=25.7
Pod documented
219 4 my ($self, $dep, %opt) = @_;
220 4 ▶ croak "reverse_dependencies() needs a dependency name" unless defined $dep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dep 0 4 4
221 4 ▶ my $kind = $opt{kind} || 'run';
Condition50%1/2
$opt{'kind'} || 'run'
l!l
40
MC/DC0%0/1
$opt{'kind'} || 'run'
$opt{'kind'}
Truth table50%1/2
A
A= $opt{'kind'}
Aresult
11
00
222 4 my $inc = $opt{include_removed};
223 4 my $target = lc $dep;
224  
225 4 my @hits;
226 4 for my $name (keys %{ $self->{packages} }) {
227 11530 my $rec = $self->{packages}{$name};
228 11530 ▶ next if $rec->{is_removed} && !$inc;
Branch: 1 branch
BranchTrueFalseTotal
if $rec->{"is_removed"} and not $inc 2 11528 11530
Condition100%3/3
$rec->{"is_removed"} and not $inc
!ll&&!rl&&r
1152712
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 11528 ▶ my @kinds = $kind eq 'all' ? qw(build host run) : ($kind);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'all' ? : 11526 2 11528
230 11528 my $found = 0;
231 11528 for my $k (@kinds) {
232 32778 ▶ for my $d (@{ $rec->{depends}{$k} || [] }) {
Condition50%1/2
$rec->{'depends'}{$k} || []
l!l
327780
MC/DC0%0/1
$rec->{'depends'}{$k} || []
$rec->{'depends'}{$k}
Truth table50%1/2
A
A= $rec->{'depends'}{$k}
Aresult
11
00
233 135372 ▶ if (lc $d->{name} eq $target) { $found = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $d->{"name"} eq $target) 1838 133534 135372
234 }
235 32778 ▶ last if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 1838 30940 32778
236 }
237 11528 ▶ push @hits, $name if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 1838 9690 11528
238 }
239 4 @hits = sort @hits;
240 4 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 2 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
253 2 my ($self) = @_;
254 2 my $total = scalar keys %{ $self->{packages} };
255 2 my @removed = $self->removed;
256 2 my $removed = scalar @removed;
257 2 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 2 );
267 }
268  
269 # Normalise a package name for lookup (conda names are lower-case).
270 9 ▶sub _norm {
Subroutine _norm: called CC=1 SCAR=0.0
271 9 my ($n) = @_;
272 9 $n = lc $n;
273 9 $n =~ s/^\s+|\s+$//g;
274 9 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 =cut
287  
288 3 1;
« Packages/DB.pm ↑ Summary Packages/Package.pm »