Cn-Bioinf
view release on metacpan or search on metacpan
} admix_fit;
/*The parameters, q (n rows of k) and then p (m rows of k), held to the
bounds: each q row clamped and made to sum to 1 again, each p clamped.*/
static void admix_bound(const admix_fit *a, NV *th) {
const size_t k = a->k;
for (size_t i = 0; i < a->n; i++) {
NV *q = th + i * k;
NV sum = 0;
for (size_t l = 0; l < k; l++) {
if (!(q[l] >= ADMIX_BOUND)) q[l] = ADMIX_BOUND;// also NaN, which an extrapolation can make
else if (q[l] > 1 - ADMIX_BOUND) q[l] = 1 - ADMIX_BOUND;
sum += q[l];
}
for (size_t l = 0; l < k; l++) q[l] /= sum;
}
NV *p = th + a->n * k;
for (size_t x = 0; x < a->m * k; x++) {
if (!(p[x] >= ADMIX_BOUND)) p[x] = ADMIX_BOUND;
else if (p[x] > 1 - ADMIX_BOUND) p[x] = 1 - ADMIX_BOUND;
}
}
NV next;
const NV *keep = t2;// the parameters this iteration ends at
if (sv > 0) {
NV alpha = -nv_sqrt(sr / sv);
if (alpha > -1) alpha = -1;
for (size_t x = 0; x < len; x++) t3[x] = th[x] - 2 * alpha * (t1[x] - th[x]) + alpha * alpha * (t2[x] - 2 * t1[x] + th[x]);
admix_bound(a, t3);
admix_em(a, t3, t1);// t1 is not needed again
next = admix_loglik(a, t1);
if (next >= ll) keep = t1;// not NaN, and no lower
else next = admix_loglik(a, t2);
} else {
next = admix_loglik(a, t2);
}
Copy(keep, th, len, NV);
const NV rise = next - ll;
ll = next;
if (rise < tol) {
*converged = TRUE;
break;
category's intercept. A category whose only row is an intercept of 0 is the
reference category, which is how a model of *k* categories is written with
*k* - 1 sets of betas; every category that should appear in the result needs at
least one row. Traits and their categories come out in the order the table
first names them. A frame's rows and columns must be of one length, as
`pgs_score`'s must, and a gzipped file that ends early dies.
For each category the linear predictor is its intercept plus the sum of its
betas times the genotypes, and the probabilities are `exp` of those over their
sum. The largest is taken out before `exp`, so a linear predictor large enough
to overflow gives 1 rather than a NaN.
### Arguments
- `genotypes` (required): what `hirisplex_genotypes` returned, or a plain hash
ref of sample => { marker => 0, 1 or 2 }. A dosage may be any number from 0
to 2, as imputation gives, and `undef` is no genotype; anything else, `NA`
included, dies naming the sample and the marker. Only the markers the model
uses are looked at.
- `model` (required): the table above, as a file name or a data frame.
- `force`: 0 (the default) makes no prediction for a trait that the genotypes
lib/Cn/Bioinf.pm view on Meta::CPAN
# mitochondrial chromosomes and UCSC's "M" named as Ensembl names them, and with
# X's pseudo-autosomal regions, which PLINK 2 names PAR1 and PAR2 (or XY, its
# 25) once --split-par has split them off, taken as X.
my %Chrom_alias = (23 => 'X', 24 => 'Y', 25 => 'X', 26 => 'MT', M => 'MT', PAR1 => 'X', PAR2 => 'X', XY => 'X');
sub _chrom {
my $c = uc $_[0];
$c =~ s/\ACHR//;
$Chrom_alias{$c} // $c;
}
# A decimal number, as an effect_weight must be. Infinity and NaN, which
# looks_like_number would take, are left out, as either would make every score
# the same non-number.
my $Number = qr/\A[-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?\z/;
# The rows of the option $what ("weights" for pgs_score, "model" for
# trait_predict) one at a time, each a hash ref with how a message names it,
# and the column names: from a data frame (see _frame_rows), or read from a
# tab-separated file a line at a time, so that a score of millions of variants
# is never held whole. An empty field of a file is '', where read_table gives
# undef; every check below treats the two alike.
lib/Cn/Bioinf.pm view on Meta::CPAN
$beta{$t}{$c}{$m} = $b;
}
croak "$sub: \"model\" has no rows" unless @traits;
(\%beta, \@traits, \%category);
}
# exp() of each of @$eta, divided by their sum: the probabilities a multinomial
# logistic regression gives its categories. The largest is taken out first, so
# that a linear predictor big enough to overflow exp() (about 710 on an IEEE
# double, and larger on a long-double or quadmath perl) gives 1 and the rest 0
# rather than inf/inf, which is NaN.
sub _softmax {
my ($eta) = @_;
my $max = $eta->[0];
foreach my $e (@{ $eta }) { $max = $e if $e > $max }
my @exp = map { exp($_ - $max) } @{ $eta };
my $sum = 0;
$sum += $_ foreach @exp;
[map { $_ / $sum } @exp];
}
lib/Cn/Bioinf.pm view on Meta::CPAN
category's intercept. A category whose only row is an intercept of 0 is the
reference category, which is how a model of I<k> categories is written with
I<k> - 1 sets of betas; every category that should appear in the result needs at
least one row. Traits and their categories come out in the order the table
first names them. A frame's rows and columns must be of one length, as
C<pgs_score>'s must, and a gzipped file that ends early dies.
For each category the linear predictor is its intercept plus the sum of its
betas times the genotypes, and the probabilities are C<exp> of those over their
sum. The largest is taken out before C<exp>, so a linear predictor large enough
to overflow gives 1 rather than a NaN.
=head3 Arguments
=over
=item * C<genotypes> (required): what C<hirisplex_genotypes> returned, or a plain hash
ref of sample => { marker => 0, 1 or 2 }. A dosage may be any number from 0
to 2, as imputation gives, and C<undef> is no genotype; anything else, C<NA>
included, dies naming the sample and the marker. Only the markers the model
uses are looked at.
# the shape is taken from whichever column perl's hash order gives first
[[vcf => $v, weights => { a => [1], b => 2 }],
qr/^pgs_score: (?:column "b" of "weights" must be an array ref, as "a" is, not "2"|"weights" must be .+, not a hash whose "b" is "2")/,
'a string column of a hoa'],
[[vcf => $v, weights => [1]], qr/^pgs_score: "weights" must be .+, not an array whose first row is "1"/, 'an array of strings'],
[[vcf => { a => 1 }, weights => $w], qr/^pgs_score: "vcf" must be .+, not a hash whose "a" is "1"/, 'a hash of strings'],
[[vcf => $v, weights => [{ map { $_ => $wrow->{$_} } qw(rsID effect_allele effect_weight) }]],
qr/^pgs_score: "weights" has no "chr_name", "chr_position" columns, which a PGS Catalog scoring file has/, 'no positions'],
[[vcf => $v, weights => [{ map { $_ => $wrow->{$_} } qw(chr_name chr_position effect_allele effect_weight) }], match => 'id'],
qr/^pgs_score: "weights" has no "rsID" column/, 'no rsID to match by ID'],
[[vcf => $v, weights => [{ %{ $wrow }, effect_weight => 'NaN' }]], qr/^pgs_score: row 1 of "weights" has effect_weight "NaN", which isn't a number/, 'a NaN weight'],
[[vcf => $v, weights => [$wrow, { %{ $wrow }, effect_weight => undef }]], qr/^pgs_score: row 2 of "weights" has effect_weight undef, which isn't a number/, 'an undef weight'],
[[vcf => $v, weights => { k => { %{ $wrow }, effect_weight => '0,5' } }], qr/^pgs_score: row "k" of "weights" has effect_weight "0,5", which isn't a number/, 'a decimal comma, in a hoh'],
[[vcf => $v, weights => [{ %{ $wrow }, effect_allele => '' }]], qr/^pgs_score: row 1 of "weights" has no effect_allele/, 'an empty effect allele'],
[[vcf => $v, weights => [{ %{ $wrow }, chr_position => '1e5' }]], qr/^pgs_score: row 1 of "weights" has chr_position "1e5", which isn't a position/, 'a position in exponent form'],
[[vcf => $v, weights => [{ %{ $wrow }, hm_chr => '1', hm_pos => 0 }]], qr/^pgs_score: row 1 of "weights" has hm_pos "0", which isn't a position/, 'position 0, in hm_pos'],
[[vcf => $v, weights => [{ %{ $wrow }, is_dominant => 'True' }]], qr/^pgs_score: row 1 of "weights" has is_dominant "True", and only additive weights can be scored/, 'a dominant weight'],
[[vcf => $v, weights => [{ %{ $wrow }, is_haplotype => 'TRUE' }]], qr/^pgs_score: row 1 of "weights" has is_haplotype "TRUE", and only/, 'a haplotype'],
[[vcf => $v, weights => [{ %{ $wrow }, dosage_1_weight => '0.2' }]], qr/^pgs_score: row 1 of "weights" has a dosage_1_weight, and only additive weights can be scored/, 'a dosage weight'],
[[vcf => { k => { CHROM => 'chr1', POS => 5, REF => 'A', ALT => 'G', FORMAT => 'GT', S => '0/1' } }, weights => $w],
qr/^pgs_score: "vcf" has no <sample>\.GT column; read the VCF with read_table's default of explode => 1/, 'a VCF read with explode => 0'],
open STDOUT, '>', File::Spec->devnull or die $!;
my $r = eval { sex_check($v) };
open STDOUT, '>&', $save or die $!;
is_deeply $r, { S => sex_check(vcf => $v, report => 0)->{S} }, 'sex_check: or alone, as sex_check($file), rather than dying on an odd number of arguments';
}
my @cases = (
[[], qr/^sex_check: needs "vcf"/, 'no vcf'],
[[{ vcf => $v }], qr/^sex_check: takes name => value pairs, not a hash ref/, 'a hash ref'],
[[vcf => $v, depth => 7], qr/^sex_check: doesn't know "depth"; it takes cutoff, min_depth, report, vcf/, 'a misspelled option'],
[[vcf => $v, cutoff => 0], qr/^sex_check: "cutoff" must be a number above 0, not "0"/, 'cutoff 0'],
[[vcf => $v, cutoff => 'NaN'], qr/^sex_check: "cutoff" must be a number above 0, not "NaN"/, 'cutoff NaN'],
[[vcf => $v, cutoff => [0.6]], qr/^sex_check: "cutoff" must be a number above 0, not an ARRAY ref/, 'cutoff an array ref'],
[[vcf => $v, min_depth => -1], qr/^sex_check: "min_depth" must be a whole number, 0 or more, not "-1"/, 'min_depth -1'],
[[vcf => $v, min_depth => 7.5], qr/^sex_check: "min_depth" must be a whole number, 0 or more, not "7\.5"/, 'min_depth 7.5'],
[[vcf => $v, report => 2], qr/^sex_check: "report" must be 0 or 1, not "2"/, 'report 2'],
[['cutoff', 0.5, 'min_depth'], qr/^sex_check: takes name => value pairs, and was given an odd number of arguments/, 'an odd list that starts with an option'],
[[$v, 'report'], qr/^sex_check: ".+sex\.vcf" is the file, so the options after it must be name => value pairs, and are an odd number/, 'the file and an option with no value, which is read as a pair'],
[[vcf => $v, report => 'yes'], qr/^sex_check: "report" must be 0 or 1, not "yes"/, 'report "yes"'],
[[vcf => \'calls'], qr/^sex_check: "vcf" must be a file name, not a SCALAR ref/, 'a scalar ref'],
[[vcf => catfile($dir, 'none.vcf.bz2')], qr/^sex_check: "vcf" file ".+none\.vcf\.bz2" doesn't exist or isn't readable/, 'a VCF that is not there'],
[[vcf => $dir], qr/^sex_check: "vcf" file ".+" doesn't exist or isn't readable/, 'a directory'],
[[vcf => { a => $a }], qr/^relatedness: "vcf" must be a file name, not a HASH ref/, 'a hash ref as "vcf"'],
[[vcf => [$a, undef]], qr/^relatedness: "vcf" must be a file name, not undef/, 'an undef among the files'],
[[vcf => [$a, \'b']], qr/^relatedness: "vcf" must be a file name, not a SCALAR ref/, 'a scalar ref among the files'],
[[$a, $none], qr/^relatedness: "vcf" file "\Q$none\E" doesn't exist or isn't readable/, 'a file that is not there'],
[[$a, $dir], qr/^relatedness: "vcf" file ".+" doesn't exist or isn't readable/, 'a directory'],
[[$a, spew('b.bcf', '')], qr/^relatedness: "vcf" file .+b\.bcf must end in \.vcf, \.vcf\.gz, \.vcf\.bgz or \.vcf\.bz2/, 'a BCF'],
[[$a, $b, $a], qr/^relatedness: "\Q$a\E" is given twice/, 'one file twice'],
[[$two, min_depth => -1], qr/^relatedness: "min_depth" must be a whole number, 0 or more, not "-1"/, 'min_depth -1'],
[[$two, min_depth => 7.5], qr/^relatedness: "min_depth" must be a whole number, 0 or more, not "7\.5"/, 'min_depth 7.5'],
[[$two, max_ibs0 => 0], qr/^relatedness: "max_ibs0" must be a number above 0, not "0"/, 'max_ibs0 0'],
[[$two, max_ibs0 => 'NaN'], qr/^relatedness: "max_ibs0" must be a number above 0, not "NaN"/, 'max_ibs0 NaN'],
[[$two, max_ibs0 => [0.01]], qr/^relatedness: "max_ibs0" must be a number above 0, not an ARRAY ref/, 'max_ibs0 an array ref'],
[[$two, report => 'yes'], qr/^relatedness: "report" must be 0 or 1, not "yes"/, 'report "yes"'],
);
foreach my $c (@cases) {
my ($args, $re, $name) = @{ $c };
dies_like sub { relatedness(@{ $args }) }, $re, "relatedness: $name dies";
}
}
# The XSUB's own checks of what perl hands it, which relatedness never gets
( run in 2.609 seconds using v1.01-cache-2.11-cpan-60884a8df76 )