| 1 |
|
| /*FASTA reading and writing for Cn::Bioinf, the genotype counts behind its |
| 2 |
|
| sex_check, parent, relatedness, concordance, hirisplex_genotypes and |
| 3 |
|
| blood_type, and (since 2026-11-11) the codon and k-mer loops behind translate, |
| 4 |
|
| translate_six_frames, find_orfs and kmer_count. |
| 5 |
|
| |
| 6 |
|
| fasta2hash() and hash2fasta_file() used to be pure perl in ~/Scripts/bioinf.pm: |
| 7 |
|
| one readline, chomp and string append per line in, and one substr-sized SV per |
| 8 |
|
| 80-column line out. Here a file is read in large blocks and every line of a |
| 9 |
|
| record is appended straight onto that record's SV, so no per-line SV is ever |
| 10 |
|
| made, and each finished sequence is shrunk to its length, which is the RAM a |
| 11 |
|
| proteome or genome actually needs rather than whatever the last buffer growth |
| 12 |
|
| left. On the file described at FASTA_BLOCK below, bioinf.pm read it in 3.83 s |
| 13 |
|
| with a peak RSS of 1,098 MB, and this in 0.80 s and 967 MB, 900 MB of which is |
| 14 |
|
| the sequences themselves; writing it back took 44.44 s and takes 0.97 s, and |
| 15 |
|
| the two files are byte-identical. Dropping the whitespace in sequences, which |
| 16 |
|
| began on 2026-10-17, costs about 15%: a made-up file of the same shape (938 MB, |
| 17 |
|
| 400,000 records of 2,280 residues in 60-column lines, in the page cache) took |
| 18 |
|
| 0.85 s to read before and 0.98 s after, best of five timed inside perl. The |
| 19 |
|
| perl side (lib/Cn/Bioinf.pm) opens and closes the files. |
| 20 |
|
| |
| 21 |
|
| Every croak() and warn() here starts with C99's __func__, so a message names |
| 22 |
|
| the C function that raised it (read_fasta, write_fasta, translate_codons, |
| 23 |
|
| kmer_tally, or one of the vcf_, sex_, parent_, rel_, conc_, hps_ and bt_ ones) |
| 24 |
|
| and stays right if one is renamed.*/ |
| 25 |
|
| #define PERL_NO_GET_CONTEXT |
| 26 |
|
| #include "EXTERN.h" |
| 27 |
|
| #include "perl.h" |
| 28 |
|
| #include "XSUB.h" |
| 29 |
|
| #include <string.h> |
| 30 |
|
| #include <stdint.h> |
| 31 |
|
| /*`restrict` is spelled bare, which is C99 syntax. |
| 32 |
|
| |
| 33 |
|
| It is carried only by read_fasta()'s block buffer, which this file allocates |
| 34 |
|
| itself and reaches through nothing but that pointer and the ones derived from |
| 35 |
|
| it. Every other pointer here is perl's: an SV, AV or HV, or the PV inside one, |
| 36 |
|
| which the perl API may alias behind the code's back -- in write_fasta() a |
| 37 |
|
| defline and its sequence can be COW copies of the one buffer -- so the |
| 38 |
|
| qualifier would be a false promise. perl is built with -fno-strict-aliasing, |
| 39 |
|
| where `restrict` is the only disambiguation there is, but it earns nothing even |
| 40 |
|
| on the buffer: the loop over it stores only through memchr() and sv_catpvn(), |
| 41 |
|
| which are opaque calls the compiler must assume write anywhere. The generated |
| 42 |
|
| code for this file was byte-identical with and without it (gcc -O2 -std=gnu99, |
| 43 |
|
| 2026-09-30), so the one annotation records the contract rather than buying |
| 44 |
|
| speed. |
| 45 |
|
| |
| 46 |
|
| Makefile.PL probes for a C99 flag but cannot always find one. MSVC accepts the |
| 47 |
|
| keyword only under /std:c11 or later and otherwise spells it __restrict, older |
| 48 |
|
| gcc spells it __restrict__, and a strict C89 compiler has no equivalent. Map it |
| 49 |
|
| where a spelling exists and define it away where none does -- the same block as |
| 50 |
|
| ~/Scripts/stats/LikeR.xs. It sits below every #include so that it cannot |
| 51 |
|
| rewrite a parameter named `restrict` inside a system header.*/ |
| 52 |
|
| #if !defined(__cplusplus) && !defined(restrict) |
| 53 |
|
| # if defined(_MSC_VER) |
| 54 |
|
| # define restrict __restrict |
| 55 |
|
| # elif !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L |
| 56 |
|
| # if defined(__GNUC__) |
| 57 |
|
| # define restrict __restrict__ |
| 58 |
|
| # else |
| 59 |
|
| # define restrict /*no spelling available; annotation only*/ |
| 60 |
|
| # endif |
| 61 |
|
| # endif |
| 62 |
|
| #endif |
| 63 |
|
| /*PerlIO_read() request size. Reading a 928 MB FASTA of 400,000 records in |
| 64 |
|
| 60-column lines, on this machine on 2026-09-29, took 0.84 s with 16 KiB blocks, |
| 65 |
|
| 0.80 s with 64 KiB, 0.81 s with 256 KiB and 0.82 s with 1 MiB (best of three |
| 66 |
|
| each, file in the page cache), so nothing is gained past 64 KiB. Build with |
| 67 |
|
| DEFINE=-DFASTA_BLOCK=<bytes> to try another.*/ |
| 68 |
|
| #ifndef FASTA_BLOCK |
| 69 |
|
| #define FASTA_BLOCK 65536 |
| 70 |
|
| #endif |
| 71 |
|
| |
| 72 |
|
| /*Whether c is whitespace as Python has it, which is what Biopython strips: ' ' |
| 73 |
|
| or one of '\t' (9), '\v' (11), '\f' (12) and '\r' (13), which is the range |
| 74 |
|
| 9..13 less the '\n' (10) that ends every line, and so is never tested. perl's |
| 75 |
|
| isSPACE() is not used, since that took in '\v' only from 5.18.*/ |
| 76 |
|
| #define FASTA_SPACE(c) ((c) == ' ' || (unsigned char)((c) - '\t') < 5) |
| 77 |
|
| |
| 78 |
|
| /*Whether p..e holds any FASTA_SPACE() byte, 8 bytes at a time. |
| 79 |
|
| |
| 80 |
|
| With a byte-at-a-time loop, the 938 MB file described at the top of this file |
| 81 |
|
| took 1.46 s to read rather than 0.85 s (best of five each, 2026-10-17): gcc |
| 82 |
|
| -O2 does not vectorise a loop of unknown length. This takes 8 bytes at once: a space is |
| 83 |
|
| a zero byte of w ^ 0x2020...20, found by Alan Mycroft's haszero(), and a byte |
| 84 |
|
| in 9..13 is found by hasbetween(w, 8, 14), both from Sean Eron Anderson's "Bit |
| 85 |
|
| Twiddling Hacks" (graphics.stanford.edu/~seander/bithacks.html, sections |
| 86 |
|
| ZeroInWord and HasBetweenInWord). Both are exact, and work byte by byte with no |
| 87 |
|
| carry between bytes, so the byte order does not matter.*/ |
| 88 |
15315 |
| static bool any_space(const char *p, const char *e) { |
| 89 |
15315 |
| const uint64_t ones = UINT64_C(0x0101010101010101), low7 = ones * 0x7f, high = ones * 0x80; |
| 90 |
113595 |
▶ | for (; e - p >= 8; p += 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; e - p >= 8; p += 8) { |
98355 |
15240 |
113595 |
|
| 91 |
|
| uint64_t w; |
| 92 |
98355 |
| memcpy(&w, p, 8); // not a cast to uint64_t *, since SPARC and some ARM fault on an unaligned load |
| 93 |
98355 |
| const uint64_t sp = w ^ (ones * ' '); |
| 94 |
98415 |
▶ | if ((sp - ones) & ~sp & high) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sp - ones) & ~sp & high) return 1; |
15 |
98340 |
98355 |
|
| 95 |
98340 |
▶ | if ((ones * (127 + 14) - (w & low7)) & ~w & ((w & low7) + ones * (127 - 8)) & high) return TRUE; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ones * (127 + 14) - (w & low7)) & ~w & ((w & low7) + ones * (127 - 8)) & high) return TRUE; |
60 |
98280 |
98340 |
|
| 96 |
|
| } |
| 97 |
76436 |
▶ | for (; p < e; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < e; p++) { |
61233 |
15203 |
76436 |
|
| 98 |
61233 |
▶ | if (FASTA_SPACE(*p)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (FASTA_SPACE(*p)) return 1; |
61225 |
8 |
61233 |
| if (FASTA_SPACE(*p)) return 1; |
29 |
61196 |
61225 |
|
| 99 |
|
| } |
| 100 |
15203 |
| return 0; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| /*Drop the whitespace at the end of a defline, the '\r' of a CRLF file |
| 104 |
|
| included, as Biopython does. It is chopped when the line ends rather than as it |
| 105 |
|
| is read, because a block can end inside it. Only SvCUR moves: the bytes |
| 106 |
|
| dropped stay in the buffer with no NUL after SvCUR, so the SV is read by its |
| 107 |
|
| length (sv_eq, hv_*_ent, SVf) and never as a C string, as a warning in |
| 108 |
|
| read_fasta() once read it and printed them.*/ |
| 109 |
459 |
| static void chop_space(SV *sv) { |
| 110 |
459 |
| STRLEN n = SvCUR(sv); |
| 111 |
477 |
▶ | while (n && FASTA_SPACE(SvPVX(sv)[n - 1])) n--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (n && FASTA_SPACE(SvPVX(sv)[n - 1])) n--; |
476 |
1 |
477 |
| while (n && FASTA_SPACE(SvPVX(sv)[n - 1])) n--; |
6 |
470 |
476 |
| while (n && FASTA_SPACE(SvPVX(sv)[n - 1])) n--; |
12 |
458 |
470 |
|
| 112 |
459 |
| SvCUR_set(sv, n); |
| 113 |
459 |
| } |
| 114 |
|
| |
| 115 |
|
| /*Append the residues in p..e to seq, leaving out the whitespace. Biopython |
| 116 |
|
| drops the spaces in a sequence line and the whitespace at its end; a tab inside |
| 117 |
|
| a line is dropped here too. A line with none, which is nearly every line, is |
| 118 |
|
| appended whole; otherwise each run between whitespace is one sv_catpvn().*/ |
| 119 |
15315 |
| static void cat_residues(pTHX_ SV *seq, const char *p, const char *e) { |
| 120 |
15315 |
▶ | if (!any_space(p, e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!any_space(p, e)) { |
15203 |
112 |
15315 |
|
| 121 |
15203 |
| sv_catpvn(seq, p, (STRLEN)(e - p)); |
| 122 |
15203 |
| return; |
| 123 |
|
| } |
| 124 |
319 |
▶ | while (p < e) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < e) { |
207 |
112 |
319 |
|
| 125 |
207 |
| const char *run = p; |
| 126 |
133110 |
▶ | while (p < e && !FASTA_SPACE(*p)) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < e && !FASTA_SPACE(*p)) p++; |
133018 |
92 |
133110 |
| while (p < e && !FASTA_SPACE(*p)) p++; |
132993 |
25 |
133018 |
| while (p < e && !FASTA_SPACE(*p)) p++; |
132903 |
90 |
132993 |
|
| 127 |
207 |
▶ | if (p > run) sv_catpvn(seq, run, (STRLEN)(p - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > run) sv_catpvn(seq, run, (STRLEN)(p - run)); |
205 |
2 |
207 |
|
| 128 |
322 |
▶ | while (p < e && FASTA_SPACE(*p)) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < e && FASTA_SPACE(*p)) p++; |
210 |
112 |
322 |
| while (p < e && FASTA_SPACE(*p)) p++; |
25 |
185 |
210 |
| while (p < e && FASTA_SPACE(*p)) p++; |
90 |
95 |
185 |
|
| 129 |
|
| } |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| /*Whether p..e holds nothing but whitespace.*/ |
| 133 |
5 |
| static bool all_space(const char *p, const char *e) { |
| 134 |
9 |
▶ | for (; p < e; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < e; p++) { |
6 |
3 |
9 |
|
| 135 |
6 |
▶ | if (!FASTA_SPACE(*p)) return FALSE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FASTA_SPACE(*p)) return FALSE; |
5 |
1 |
6 |
| if (!FASTA_SPACE(*p)) return FALSE; |
2 |
3 |
5 |
|
| 136 |
|
| } |
| 137 |
3 |
| return TRUE; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| /*Every record of fh as a hash of defline => sequence, or with key given only |
| 141 |
|
| that record's sequence (undef if it is absent), reading no further than the |
| 142 |
|
| defline after it. name is the file's name, for messages. order, when not NULL, |
| 143 |
|
| gets each defline in file order. |
| 144 |
|
| |
| 145 |
|
| A repeated defline is warned about and its sequences concatenated, which is |
| 146 |
|
| what bioinf.pm did: it is the file that is wrong, and nothing here can say |
| 147 |
|
| which of the two records was meant. With key given, reading stops at the |
| 148 |
|
| defline after key's record, so the only repeat seen, and concatenated, is one |
| 149 |
|
| that follows it directly; a second record of key further on is never read, |
| 150 |
|
| and the one sequence returned is the first's alone. That is the price of |
| 151 |
|
| stopping early, and README.md says so. A repeat of any other defline is not |
| 152 |
|
| looked for either: finding it would mean keeping every defline read on the way |
| 153 |
|
| to key, a hash of 400,000 keys for one record of a proteome. |
| 154 |
|
| |
| 155 |
|
| A defline with a CR inside it, once the whitespace at its end is gone, dies: |
| 156 |
|
| it is what a file whose lines end in CR alone (classic Mac OS) has, since with |
| 157 |
|
| no LF the whole file is one line, and up to 2026-11-10 such a file came back as |
| 158 |
|
| one record named by all of it, with no sequence. Biopython, which opens a file |
| 159 |
|
| in Python's universal-newlines mode, reads one; here it is refused, by one |
| 160 |
|
| memchr() a defline, rather than every CR being tested for as the file is |
| 161 |
|
| read.*/ |
| 162 |
61 |
| static SV *read_fasta(pTHX_ PerlIO *fh, SV *key, const char *name, AV *order) { |
| 163 |
61 |
| HV *out = (HV*)sv_2mortal((SV*)newHV()); |
| 164 |
61 |
| SV *hdr = sv_2mortal(newSVpvs("")); |
| 165 |
61 |
| SV *seq = NULL; // record being filled; NULL before the first defline and while skipping |
| 166 |
61 |
| bool have_def = FALSE, in_hdr = FALSE, bol = TRUE, done = FALSE, fed = FALSE; // fed: the '\n' that ends an unterminated last line has been supplied |
| 167 |
61 |
| UV line = 0; // 1-based, as $. would count it |
| 168 |
|
| char *restrict buf; |
| 169 |
61 |
| Newx(buf, FASTA_BLOCK, char); |
| 170 |
61 |
| SAVEFREEPV(buf); // freed at the caller's LEAVE, croak or not |
| 171 |
146 |
▶ | while (!done) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!done) { |
139 |
7 |
146 |
|
| 172 |
139 |
| SSize_t n = PerlIO_read(fh, buf, FASTA_BLOCK); |
| 173 |
139 |
▶ | if (n < 0 || PerlIO_error(fh)) croak("%s: error reading %s: %s", __func__, name, Strerror(errno)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 || PerlIO_error(fh)) croak("%s: error reading %s: %s", __func__, name, Strerror(errno)); |
139 |
0 |
139 |
| if (n < 0 || PerlIO_error(fh)) croak("%s: error reading %s: %s", __func__, name, Strerror(errno)); |
0 |
139 |
139 |
|
| 174 |
139 |
▶ | if (n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { |
56 |
83 |
139 |
|
| 175 |
56 |
▶ | if (bol || fed) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (bol || fed) break; |
6 |
50 |
56 |
| if (bol || fed) break; |
6 |
0 |
6 |
|
| 176 |
6 |
| buf[0] = '\n'; |
| 177 |
6 |
| n = 1; |
| 178 |
6 |
| fed = 1; |
| 179 |
|
| } |
| 180 |
89 |
| const char *p = buf, *end = buf + n; |
| 181 |
26195 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
26146 |
49 |
26195 |
|
| 182 |
26146 |
▶ | if (bol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bol) { |
25658 |
488 |
26146 |
|
| 183 |
25658 |
| line++; |
| 184 |
25658 |
| bol = FALSE; |
| 185 |
25658 |
▶ | if (*p == '>') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '>') { |
459 |
25199 |
25658 |
|
| 186 |
459 |
| in_hdr = 1; |
| 187 |
459 |
| SvCUR_set(hdr, 0); |
| 188 |
459 |
| p++; |
| 189 |
459 |
| continue; |
| 190 |
|
| } |
| 191 |
|
| } |
| 192 |
25687 |
| const char *nl = (const char*)memchr(p, '\n', (size_t)(end - p)); |
| 193 |
25687 |
▶ | const char *e = nl ? nl : end; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *e = nl ? nl : end; |
25658 |
29 |
25687 |
|
| 194 |
25687 |
▶ | if (in_hdr) sv_catpvn(hdr, p, (STRLEN)(e - p)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_hdr) sv_catpvn(hdr, p, (STRLEN)(e - p)); |
463 |
25224 |
25687 |
|
| 195 |
25224 |
▶ | else if (seq) cat_residues(aTHX_ seq, p, e); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (seq) cat_residues(aTHX_ seq, p, e); |
15315 |
9909 |
25224 |
|
| 196 |
9909 |
▶ | else if (!have_def && !all_space(p, e)) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!have_def && !all_space(p, e)) |
5 |
9904 |
9909 |
| else if (!have_def && !all_space(p, e)) |
2 |
3 |
5 |
|
| 197 |
2 |
| croak("%s: %s line %" UVuf " is sequence, but no defline (\">...\") has come before it", __func__, name, line); |
| 198 |
25685 |
▶ | if (!nl) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nl) break; |
29 |
25656 |
25685 |
|
| 199 |
25656 |
| p = nl + 1; |
| 200 |
25656 |
| bol = 1; |
| 201 |
25656 |
▶ | if (!in_hdr) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_hdr) continue; |
25197 |
459 |
25656 |
|
| 202 |
459 |
| in_hdr = FALSE; |
| 203 |
459 |
| chop_space(hdr); |
| 204 |
459 |
▶ | if (!SvCUR(hdr)) croak("%s: %s line %" UVuf " is a defline with no name", __func__, name, line); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvCUR(hdr)) croak("%s: %s line %" UVuf " is a defline with no name", __func__, name, line); |
1 |
458 |
459 |
|
| 205 |
458 |
▶ | if (memchr(SvPVX(hdr), '\r', SvCUR(hdr))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(SvPVX(hdr), '\r', SvCUR(hdr))) |
1 |
457 |
458 |
|
| 206 |
1 |
| croak("%s: %s line %" UVuf " has a carriage return inside its defline, as a file whose lines end in CR alone (classic Mac OS) does; convert its line endings to LF first", __func__, name, line); |
| 207 |
457 |
▶ | if (seq) SvPV_shrink_to_cur(seq); |
Branch: 4 branches
| Branch | True | False | Total |
| if (seq) SvPV_shrink_to_cur(seq); |
387 |
70 |
457 |
| if (seq) SvPV_shrink_to_cur(seq); |
335 |
52 |
387 |
| if (seq) SvPV_shrink_to_cur(seq); |
0 |
335 |
335 |
| if (seq) SvPV_shrink_to_cur(seq); |
284 |
103 |
387 |
|
| 208 |
457 |
▶ | bool is_key = key && sv_eq(hdr, key); |
Branch: 2 branches
| Branch | True | False | Total |
| bool is_key = key && sv_eq(hdr, key); |
37 |
420 |
457 |
| bool is_key = key && sv_eq(hdr, key); |
13 |
24 |
37 |
|
| 209 |
457 |
▶ | if (key && !is_key && hv_exists_ent(out, key, 0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (key && !is_key && hv_exists_ent(out, key, 0)) { |
37 |
420 |
457 |
| if (key && !is_key && hv_exists_ent(out, key, 0)) { |
24 |
13 |
37 |
| if (key && !is_key && hv_exists_ent(out, key, 0)) { |
7 |
17 |
24 |
|
| 210 |
7 |
| done = 1; // the wanted record is complete |
| 211 |
7 |
| break; |
| 212 |
|
| } |
| 213 |
450 |
| have_def = 1; |
| 214 |
450 |
▶ | if (key && !is_key) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key && !is_key) { |
30 |
420 |
450 |
| if (key && !is_key) { |
17 |
13 |
30 |
|
| 215 |
17 |
| seq = NULL; |
| 216 |
17 |
| continue; |
| 217 |
|
| } |
| 218 |
433 |
▶ | if (hv_exists_ent(out, hdr, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(out, hdr, 0)) |
4 |
429 |
433 |
|
| 219 |
4 |
| warn("%s: \"%" SVf "\" appears more than once in %s (line %" UVuf "); its sequences will be concatenated", __func__, SVfARG(hdr), name, line); |
| 220 |
429 |
▶ | else if (order) av_push(order, newSVsv(hdr)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (order) av_push(order, newSVsv(hdr)); |
0 |
429 |
429 |
|
| 221 |
433 |
| seq = HeVAL(hv_fetch_ent(out, hdr, 1, 0)); |
| 222 |
433 |
▶ | if (!SvOK(seq)) sv_setpvs(seq, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(seq)) sv_setpvs(seq, ""); |
429 |
4 |
433 |
|
| 223 |
|
| } |
| 224 |
|
| } |
| 225 |
57 |
▶ | if (seq) SvPV_shrink_to_cur(seq); |
Branch: 4 branches
| Branch | True | False | Total |
| if (seq) SvPV_shrink_to_cur(seq); |
52 |
5 |
57 |
| if (seq) SvPV_shrink_to_cur(seq); |
11 |
41 |
52 |
| if (seq) SvPV_shrink_to_cur(seq); |
0 |
11 |
11 |
| if (seq) SvPV_shrink_to_cur(seq); |
6 |
46 |
52 |
|
| 226 |
57 |
▶ | if (!key) return newRV_inc((SV*)out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) return newRV_inc((SV*)out); |
41 |
16 |
57 |
|
| 227 |
16 |
| HE *he = hv_fetch_ent(out, key, 0, 0); |
| 228 |
16 |
▶ | return he ? SvREFCNT_inc_simple_NN(HeVAL(he)) : newSV(0); // not a copy: out is mortal, so this becomes the only reference |
Branch: 1 branch
| Branch | True | False | Total |
| return he ? SvREFCNT_inc_simple_NN(HeVAL(he)) : newSV(0); // not a copy: out is mortal, so this becomes the only reference |
12 |
4 |
16 |
|
| 229 |
|
| } |
| 230 |
|
| |
| 231 |
|
| /*The bytes of sv for a FASTA file, as print to a :raw handle writes them: a |
| 232 |
|
| string perl holds as UTF-8 is written as the characters it holds, so that how |
| 233 |
|
| perl happened to store it cannot change the file. Up to 2026-10-17 it was |
| 234 |
|
| written as its UTF-8, and "caf\xe9" came out as one byte or two. scratch holds |
| 235 |
|
| that downgraded copy. A character above 255 has no byte, and gets NULL, for |
| 236 |
|
| the caller to die naming the string; print would write UTF-8 and warn.*/ |
| 237 |
56 |
| static const char *fasta_bytes(pTHX_ SV *sv, SV *scratch, STRLEN *len) { |
| 238 |
56 |
▶ | if (SvUTF8(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(sv)) { |
10 |
46 |
56 |
|
| 239 |
10 |
| sv_setsv(scratch, sv); |
| 240 |
10 |
▶ | if (!sv_utf8_downgrade(scratch, TRUE)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_utf8_downgrade(scratch, TRUE)) return NULL; |
4 |
6 |
10 |
|
| 241 |
6 |
| sv = scratch; |
| 242 |
|
| } |
| 243 |
52 |
| return SvPV_const(sv, *len); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /*Each key of h named in order, as ">key" and then its sequence broken into |
| 247 |
|
| lines of width characters; width 0 puts each sequence on one line. |
| 248 |
|
| |
| 249 |
|
| A key that is empty or holds a line feed dies, since read_fasta() could not |
| 250 |
|
| read its record back: the one is a defline with no name, and the other ends |
| 251 |
|
| the defline at the line feed and makes the rest of the key sequence. So does a |
| 252 |
|
| sequence that is a reference, unless it is an object that overloads its own |
| 253 |
|
| text. Up to 2026-11-10 all three were written, the reference as |
| 254 |
|
| "ARRAY(0x5ea732db32c8)".*/ |
| 255 |
23 |
| static void write_fasta(pTHX_ PerlIO *fh, HV *h, AV *order, STRLEN width) { |
| 256 |
23 |
| SV *kbuf = sv_newmortal(), *sbuf = sv_newmortal(); // fasta_bytes()'s scratch copies, of the key and of its sequence |
| 257 |
46 |
▶ | for (SSize_t i = 0; i <= av_len(order); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (SSize_t i = 0; i <= av_len(order); i++) { |
33 |
13 |
46 |
|
| 258 |
33 |
| SV **k = av_fetch(order, i, 0); |
| 259 |
33 |
▶ | if (!k || !SvOK(*k)) croak("%s: element %" IVdf " of the order is undefined", __func__, (IV)i); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !SvOK(*k)) croak("%s: element %" IVdf " of the order is undefined", __func__, (IV)i); |
33 |
0 |
33 |
| if (!k || !SvOK(*k)) croak("%s: element %" IVdf " of the order is undefined", __func__, (IV)i); |
1 |
32 |
33 |
|
| 260 |
32 |
| HE *he = hv_fetch_ent(h, *k, 0, 0); |
| 261 |
|
| STRLEN kl, sl; |
| 262 |
|
| // kp and sp not restrict: a defline and its sequence may share one COW buffer |
| 263 |
32 |
| const char *kp = fasta_bytes(aTHX_ *k, kbuf, &kl); |
| 264 |
35 |
▶ | if (!kp) croak("%s: \"%s\" has a character above 255, so it has to be encoded (with Encode::encode('UTF-8', ...), say) before it is written", __func__, SvPV_nolen(*k)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!kp) croak("%s: \"%s\" has a character above 255, so it has to be encoded (with Encode::encode('UTF-8', ...), say) before it is written", __func__, SvPV_nolen(*k)); |
3 |
29 |
32 |
|
| 265 |
29 |
▶ | if (!kl) croak("%s: element %" IVdf " of the order is an empty name, which a defline can't be", __func__, (IV)i); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!kl) croak("%s: element %" IVdf " of the order is an empty name, which a defline can't be", __func__, (IV)i); |
1 |
28 |
29 |
|
| 266 |
28 |
▶ | if (memchr(kp, '\n', kl)) croak("%s: \"%s\" has a line feed in it, which would end the defline there", __func__, kp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(kp, '\n', kl)) croak("%s: \"%s\" has a line feed in it, which would end the defline there", __func__, kp); |
1 |
27 |
28 |
|
| 267 |
27 |
▶ | if (!he || !SvOK(HeVAL(he))) croak("%s: \"%s\" has no sequence in the hash", __func__, kp); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!he || !SvOK(HeVAL(he))) croak("%s: \"%s\" has no sequence in the hash", __func__, kp); |
25 |
2 |
27 |
| if (!he || !SvOK(HeVAL(he))) croak("%s: \"%s\" has no sequence in the hash", __func__, kp); |
0 |
25 |
25 |
|
| 268 |
25 |
▶ | if (SvROK(HeVAL(he)) && !SvAMAGIC(HeVAL(he))) croak("%s: the sequence of \"%s\" is a reference, not a string of residues", __func__, kp); |
Branch: 4 branches
| Branch | True | False | Total |
| if (SvROK(HeVAL(he)) && !SvAMAGIC(HeVAL(he))) croak("%s: the sequence of \"%s\" is a reference, not a string of residues", __func__, kp); |
2 |
23 |
25 |
| if (SvROK(HeVAL(he)) && !SvAMAGIC(HeVAL(he))) croak("%s: the sequence of \"%s\" is a reference, not a string of residues", __func__, kp); |
2 |
0 |
2 |
| if (SvROK(HeVAL(he)) && !SvAMAGIC(HeVAL(he))) croak("%s: the sequence of \"%s\" is a reference, not a string of residues", __func__, kp); |
1 |
1 |
2 |
| if (SvROK(HeVAL(he)) && !SvAMAGIC(HeVAL(he))) croak("%s: the sequence of \"%s\" is a reference, not a string of residues", __func__, kp); |
0 |
1 |
1 |
|
| 269 |
24 |
| const char *sp = fasta_bytes(aTHX_ HeVAL(he), sbuf, &sl); |
| 270 |
24 |
▶ | if (!sp) croak("%s: the sequence of \"%s\" has a character above 255, so it has to be encoded before it is written", __func__, kp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sp) croak("%s: the sequence of \"%s\" has a character above 255, so it has to be encoded before it is written", __func__, kp); |
1 |
23 |
24 |
|
| 271 |
23 |
| PerlIO_putc(fh, '>'); |
| 272 |
23 |
| PerlIO_write(fh, kp, kl); |
| 273 |
23 |
| PerlIO_putc(fh, '\n'); |
| 274 |
23 |
▶ | STRLEN w = width ? width : sl; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN w = width ? width : sl; |
20 |
3 |
23 |
|
| 275 |
47 |
▶ | for (STRLEN o = 0; o < sl; o += w) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (STRLEN o = 0; o < sl; o += w) { |
24 |
23 |
47 |
|
| 276 |
24 |
| PerlIO_write(fh, sp + o, sl - o < w ? sl - o : w); |
| 277 |
24 |
| PerlIO_putc(fh, '\n'); |
| 278 |
|
| } |
| 279 |
|
| } |
| 280 |
13 |
▶ | if (PerlIO_error(fh)) croak("%s: error writing FASTA: %s", __func__, Strerror(errno)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (PerlIO_error(fh)) croak("%s: error writing FASTA: %s", __func__, Strerror(errno)); |
0 |
13 |
13 |
|
| 281 |
13 |
| } |
| 282 |
|
| |
| 283 |
|
| /*Reading a VCF, for sex_check(), parent(), relatedness(), concordance(), |
| 284 |
|
| hirisplex_genotypes() and blood_type(). |
| 285 |
|
| |
| 286 |
|
| The file is read by perl, which knows gzip and bzip2 through IO::Uncompress |
| 287 |
|
| (linking zlib and libbz2 here would be a build dependency on every platform), |
| 288 |
|
| and handed over a block at a time. vcf_read() cuts the blocks into lines and |
| 289 |
|
| gives each record to the reader's on_record, keeping nothing itself but the |
| 290 |
|
| one line a block can end inside; what each caller keeps is its own.*/ |
| 291 |
|
| |
| 292 |
|
| typedef struct vcf_reader vcf_reader; |
| 293 |
|
| struct vcf_reader { |
| 294 |
|
| const char *name; // the file's, for messages |
| 295 |
|
| UV line; // 1-based, as $. would count it |
| 296 |
|
| UV min_depth; // a genotype whose DP is below this is not counted |
| 297 |
|
| size_t n; // samples; 0 until the #CHROM line |
| 298 |
|
| bool header; // the #CHROM line has been read |
| 299 |
|
| bool stop; // set by on_header to read no further, as concordance() does to learn the truth's samples |
| 300 |
|
| HV *samples; // sample => its 0-based column among the samples |
| 301 |
|
| AV *names; // the samples in file order |
| 302 |
|
| void (*on_header)(pTHX_ vcf_reader *r); // once the #CHROM line has been read |
| 303 |
|
| void (*on_record)(pTHX_ vcf_reader *r, const char *p, const char *e); // each record, its '\n' gone |
| 304 |
|
| }; |
| 305 |
|
| |
| 306 |
|
| /*The end of the tab-separated field that starts at p, before e.*/ |
| 307 |
4436326 |
| static const char *vcf_field_end(const char *p, const char *e) { |
| 308 |
4436326 |
| const char *t = (const char*)memchr(p, '\t', (size_t)(e - p)); |
| 309 |
4436326 |
▶ | return t ? t : e; |
Branch: 1 branch
| Branch | True | False | Total |
| return t ? t : e; |
4092839 |
343487 |
4436326 |
|
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| /*Sub-field k (0-based) of the colon-separated p..e, in *s..*f, or FALSE when |
| 313 |
|
| there are fewer.*/ |
| 314 |
1238775 |
| static bool vcf_subfield(const char *p, const char *e, size_t k, const char **s, const char **f) { |
| 315 |
1840990 |
▶ | for (; k; k--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; k; k--) { |
602420 |
1238570 |
1840990 |
|
| 316 |
602420 |
| p = (const char*)memchr(p, ':', (size_t)(e - p)); |
| 317 |
602420 |
▶ | if (!p) return FALSE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return FALSE; |
205 |
602215 |
602420 |
|
| 318 |
602215 |
| p++; |
| 319 |
|
| } |
| 320 |
1238570 |
| *s = p; |
| 321 |
1238570 |
| const char *c = (const char*)memchr(p, ':', (size_t)(e - p)); |
| 322 |
1238570 |
▶ | *f = c ? c : e; |
Branch: 1 branch
| Branch | True | False | Total |
| *f = c ? c : e; |
554114 |
684456 |
1238570 |
|
| 323 |
1238570 |
| return TRUE; |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| /*The chromosome that CHROM p..f names, numbered as PLINK numbers them: 1 to |
| 327 |
|
| 22, 23 for X and 24 for Y, and 0 for any other (MT, an unplaced contig). A |
| 328 |
|
| "chr" prefix, in any case, is dropped.*/ |
| 329 |
439803 |
| static unsigned short int vcf_chrom(const char *p, const char *f) { |
| 330 |
439803 |
▶ | if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
Branch: 7 branches
| Branch | True | False | Total |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
245890 |
193913 |
439803 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
245890 |
245890 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
0 |
0 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
245890 |
245890 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
0 |
0 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
245890 |
245890 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
0 |
0 |
0 |
|
| 331 |
439803 |
▶ | if (f - p == 1 && (*p == 'X' || *p == 'x')) return 23; |
Branch: 3 branches
| Branch | True | False | Total |
| if (f - p == 1 && (*p == 'X' || *p == 'x')) return 23; |
230029 |
209774 |
439803 |
| if (f - p == 1 && (*p == 'X' || *p == 'x')) return 23; |
228399 |
1630 |
230029 |
| if (f - p == 1 && (*p == 'X' || *p == 'x')) return 23; |
0 |
228399 |
228399 |
|
| 332 |
438173 |
▶ | if (f - p == 1 && (*p == 'Y' || *p == 'y')) return 24; |
Branch: 3 branches
| Branch | True | False | Total |
| if (f - p == 1 && (*p == 'Y' || *p == 'y')) return 24; |
228399 |
209774 |
438173 |
| if (f - p == 1 && (*p == 'Y' || *p == 'y')) return 24; |
228196 |
203 |
228399 |
| if (f - p == 1 && (*p == 'Y' || *p == 'y')) return 24; |
0 |
228196 |
228196 |
|
| 333 |
437970 |
▶ | if (f - p < 1 || f - p > 2 || *p < '1' || *p > '9') return 0; // "01" is not chromosome 1 |
Branch: 4 branches
| Branch | True | False | Total |
| if (f - p < 1 || f - p > 2 || *p < '1' || *p > '9') return 0; // "01" is not chromosome 1 |
437970 |
0 |
437970 |
| if (f - p < 1 || f - p > 2 || *p < '1' || *p > '9') return 0; // "01" is not chromosome 1 |
437839 |
131 |
437970 |
| if (f - p < 1 || f - p > 2 || *p < '1' || *p > '9') return 0; // "01" is not chromosome 1 |
437708 |
131 |
437839 |
| if (f - p < 1 || f - p > 2 || *p < '1' || *p > '9') return 0; // "01" is not chromosome 1 |
131 |
437577 |
437708 |
|
| 334 |
437577 |
| unsigned short int c = (unsigned short int)(*p - '0'); |
| 335 |
437577 |
▶ | if (f - p == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f - p == 2) { |
209512 |
228065 |
437577 |
|
| 336 |
209512 |
▶ | if (p[1] < '0' || p[1] > '9') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p[1] < '0' || p[1] > '9') return 0; |
209512 |
0 |
209512 |
| if (p[1] < '0' || p[1] > '9') return 0; |
0 |
209512 |
209512 |
|
| 337 |
209512 |
| c = (unsigned short int)(c * 10 + (p[1] - '0')); |
| 338 |
|
| } |
| 339 |
437577 |
▶ | return c <= 24 ? c : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return c <= 24 ? c : 0; |
437577 |
0 |
437577 |
|
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
|
| /*The index of base c, 0 to 3 for A, C, G and T in either case, or 4 for |
| 343 |
|
| anything else.*/ |
| 344 |
1010044 |
| static unsigned short int vcf_base_index(char c) { |
| 345 |
1010044 |
| switch (c) { |
| 346 |
310732 |
| case 'A': case 'a': return 0; |
| 347 |
195462 |
| case 'C': case 'c': return 1; |
| 348 |
308014 |
| case 'G': case 'g': return 2; |
| 349 |
195602 |
| case 'T': case 't': return 3; |
| 350 |
234 |
| default: return 4; |
| 351 |
|
| } |
| 352 |
|
| } |
| 353 |
|
| |
| 354 |
|
| /*Whether p..e is one base, as a SNV's REF and ALT are. An ALT of ".", "*" or |
| 355 |
|
| "<NON_REF>", or with a comma, is not.*/ |
| 356 |
663986 |
| static bool vcf_base(const char *p, const char *e) { |
| 357 |
663986 |
▶ | return e - p == 1 && vcf_base_index(*p) < 4; |
Branch: 2 branches
| Branch | True | False | Total |
| return e - p == 1 && vcf_base_index(*p) < 4; |
663716 |
270 |
663986 |
| return e - p == 1 && vcf_base_index(*p) < 4; |
663482 |
234 |
663716 |
|
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
|
| /*Where each of the 9 fixed columns of the record p..e starts, in col, given |
| 361 |
|
| that CHROM ends at f; returns where FORMAT ends, so that col[i + 1] - 1 ends |
| 362 |
|
| column i.*/ |
| 363 |
343878 |
| static const char *vcf_columns(pTHX_ const vcf_reader *r, const char *p, const char *f, const char *e, const char **col) { |
| 364 |
343878 |
| col[0] = p; |
| 365 |
3094893 |
▶ | for (unsigned short int i = 1; i < 9; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int i = 1; i < 9; i++) { |
2751017 |
343876 |
3094893 |
|
| 366 |
2751017 |
▶ | if (f >= e) croak("%s: %s line %" UVuf " has %u columns, and a record needs 9 and a sample", __func__, r->name, r->line, (unsigned)i); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f >= e) croak("%s: %s line %" UVuf " has %u columns, and a record needs 9 and a sample", __func__, r->name, r->line, (unsigned)i); |
2 |
2751015 |
2751017 |
|
| 367 |
2751015 |
| col[i] = f + 1; |
| 368 |
2751015 |
| f = vcf_field_end(col[i], e); |
| 369 |
|
| } |
| 370 |
343876 |
| return f; |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
|
| /*POS, the field p..f.*/ |
| 374 |
186428 |
| static UV vcf_pos(pTHX_ const vcf_reader *r, const char *p, const char *f) { |
| 375 |
186428 |
▶ | if (p == f) croak("%s: %s line %" UVuf " has an empty POS", __func__, r->name, r->line); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == f) croak("%s: %s line %" UVuf " has an empty POS", __func__, r->name, r->line); |
1 |
186427 |
186428 |
|
| 376 |
186427 |
| UV pos = 0; |
| 377 |
1377134 |
▶ | for (const char *q = p; q < f; q++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *q = p; q < f; q++) { |
1190711 |
186423 |
1377134 |
|
| 378 |
1190711 |
▶ | if (*q < '0' || *q > '9' || pos > (UV_MAX - 9) / 10) croak("%s: %s line %" UVuf " has POS \"%.*s\", which isn't a position", __func__, r->name, r->line, (int)(f - p), p); |
Branch: 3 branches
| Branch | True | False | Total |
| if (*q < '0' || *q > '9' || pos > (UV_MAX - 9) / 10) croak("%s: %s line %" UVuf " has POS \"%.*s\", which isn't a position", __func__, r->name, r->line, (int)(f - p), p); |
1190711 |
0 |
1190711 |
| if (*q < '0' || *q > '9' || pos > (UV_MAX - 9) / 10) croak("%s: %s line %" UVuf " has POS \"%.*s\", which isn't a position", __func__, r->name, r->line, (int)(f - p), p); |
1190708 |
3 |
1190711 |
| if (*q < '0' || *q > '9' || pos > (UV_MAX - 9) / 10) croak("%s: %s line %" UVuf " has POS \"%.*s\", which isn't a position", __func__, r->name, r->line, (int)(f - p), p); |
1 |
1190707 |
1190708 |
|
| 379 |
1190707 |
| pos = pos * 10 + (UV)(*q - '0'); |
| 380 |
|
| } |
| 381 |
186423 |
| return pos; |
| 382 |
|
| } |
| 383 |
|
| |
| 384 |
|
| #define VCF_ABSENT ((size_t)-1) // a key that FORMAT doesn't name |
| 385 |
|
| |
| 386 |
|
| /*GT's and DP's places in FORMAT, the field p..e, in *gt and *dp, or |
| 387 |
|
| VCF_ABSENT.*/ |
| 388 |
343096 |
| static void vcf_format(const char *p, const char *e, size_t *gt, size_t *dp) { |
| 389 |
343096 |
| *gt = *dp = VCF_ABSENT; |
| 390 |
343096 |
| size_t k = 0; |
| 391 |
958686 |
▶ | for (const char *s = p; s <= e; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *s = p; s <= e; k++) { |
615590 |
343096 |
958686 |
|
| 392 |
615590 |
| const char *c = (const char*)memchr(s, ':', (size_t)(e - s)); |
| 393 |
615590 |
▶ | const char *ce = c ? c : e; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *ce = c ? c : e; |
272494 |
343096 |
615590 |
|
| 394 |
615590 |
▶ | if (ce - s == 2 && s[0] == 'G' && s[1] == 'T') *gt = k; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ce - s == 2 && s[0] == 'G' && s[1] == 'T') *gt = k; |
615590 |
0 |
615590 |
| if (ce - s == 2 && s[0] == 'G' && s[1] == 'T') *gt = k; |
342922 |
272668 |
615590 |
| if (ce - s == 2 && s[0] == 'G' && s[1] == 'T') *gt = k; |
342922 |
0 |
342922 |
|
| 395 |
272668 |
▶ | else if (ce - s == 2 && s[0] == 'D' && s[1] == 'P') *dp = k; |
Branch: 3 branches
| Branch | True | False | Total |
| else if (ce - s == 2 && s[0] == 'D' && s[1] == 'P') *dp = k; |
272668 |
0 |
272668 |
| else if (ce - s == 2 && s[0] == 'D' && s[1] == 'P') *dp = k; |
272668 |
0 |
272668 |
| else if (ce - s == 2 && s[0] == 'D' && s[1] == 'P') *dp = k; |
272668 |
0 |
272668 |
|
| 396 |
615590 |
| s = ce + 1; |
| 397 |
|
| } |
| 398 |
343096 |
| } |
| 399 |
|
| |
| 400 |
|
| //What vcf_call() found |
| 401 |
|
| enum { |
| 402 |
|
| VCF_SHALLOW, // a DP below min_depth: the genotype is not counted at all |
| 403 |
|
| VCF_NO_CALL, // missing, partly missing ("./1"), cut short ("1/"), more than two alleles, or an allele the site lacks |
| 404 |
|
| VCF_CALL // one or two alleles, each REF or the one ALT |
| 405 |
|
| }; |
| 406 |
|
| |
| 407 |
|
| /*Whether the sample field p..e, whose FORMAT has DP at dp, has a DP below |
| 408 |
|
| r->min_depth. A DP that is absent, "." or not a whole number is not.*/ |
| 409 |
684456 |
| static bool vcf_shallow(const vcf_reader *r, const char *p, const char *e, size_t dp) { |
| 410 |
|
| const char *s, *sf; |
| 411 |
684456 |
▶ | if (dp == VCF_ABSENT || !vcf_subfield(p, e, dp, &s, &sf) || s == sf || *s == '.') return FALSE; |
Branch: 4 branches
| Branch | True | False | Total |
| if (dp == VCF_ABSENT || !vcf_subfield(p, e, dp, &s, &sf) || s == sf || *s == '.') return FALSE; |
602420 |
82036 |
684456 |
| if (dp == VCF_ABSENT || !vcf_subfield(p, e, dp, &s, &sf) || s == sf || *s == '.') return FALSE; |
602215 |
205 |
602420 |
| if (dp == VCF_ABSENT || !vcf_subfield(p, e, dp, &s, &sf) || s == sf || *s == '.') return FALSE; |
602215 |
0 |
602215 |
| if (dp == VCF_ABSENT || !vcf_subfield(p, e, dp, &s, &sf) || s == sf || *s == '.') return FALSE; |
164 |
602051 |
602215 |
|
| 412 |
602051 |
| UV depth = 0; |
| 413 |
1757650 |
▶ | for (; s < sf; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; s < sf; s++) { |
1155599 |
602051 |
1757650 |
|
| 414 |
1155599 |
▶ | if (*s < '0' || *s > '9' || depth > (UV_MAX - 9) / 10) return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (*s < '0' || *s > '9' || depth > (UV_MAX - 9) / 10) return FALSE; |
1155599 |
0 |
1155599 |
| if (*s < '0' || *s > '9' || depth > (UV_MAX - 9) / 10) return FALSE; |
1155599 |
0 |
1155599 |
| if (*s < '0' || *s > '9' || depth > (UV_MAX - 9) / 10) return FALSE; |
0 |
1155599 |
1155599 |
|
| 415 |
1155599 |
| depth = depth * 10 + (UV)(*s - '0'); |
| 416 |
|
| } |
| 417 |
602051 |
| return depth < r->min_depth; |
| 418 |
|
| } |
| 419 |
|
| |
| 420 |
|
| /*The call in the sample field p..e of a biallelic site, whose FORMAT has GT |
| 421 |
|
| at gt and DP at dp: one of the values above, and for VCF_CALL the number of |
| 422 |
|
| alleles in *alleles and how many of them are the ALT in *alt. A DP that is |
| 423 |
|
| absent, "." or not a whole number leaves the genotype counted.*/ |
| 424 |
669473 |
| static short int vcf_call(const vcf_reader *r, const char *p, const char *e, size_t gt, size_t dp, unsigned short int *alleles, unsigned short int *alt) { |
| 425 |
|
| const char *s, *sf; |
| 426 |
669473 |
▶ | if (vcf_shallow(r, p, e, dp)) return VCF_SHALLOW; |
Branch: 1 branch
| Branch | True | False | Total |
| if (vcf_shallow(r, p, e, dp)) return VCF_SHALLOW; |
47983 |
621490 |
669473 |
|
| 427 |
621490 |
| *alleles = *alt = 0; |
| 428 |
621490 |
▶ | if (!vcf_subfield(p, e, gt, &s, &sf) || s == sf) return VCF_NO_CALL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!vcf_subfield(p, e, gt, &s, &sf) || s == sf) return VCF_NO_CALL; |
621490 |
0 |
621490 |
| if (!vcf_subfield(p, e, gt, &s, &sf) || s == sf) return VCF_NO_CALL; |
0 |
621490 |
621490 |
|
| 429 |
2465551 |
▶ | while (s < sf) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < sf) { |
1232775 |
611286 |
1844061 |
|
| 430 |
1232775 |
▶ | if (++*alleles > 2 || *s == '.') return VCF_NO_CALL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (++*alleles > 2 || *s == '.') return VCF_NO_CALL; |
1232666 |
109 |
1232775 |
| if (++*alleles > 2 || *s == '.') return VCF_NO_CALL; |
9983 |
1222683 |
1232666 |
|
| 431 |
1222683 |
▶ | if (*s == '1') ++*alt; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '1') ++*alt; |
688336 |
534347 |
1222683 |
|
| 432 |
534347 |
▶ | else if (*s != '0') return VCF_NO_CALL; // no other allele exists at a biallelic site |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s != '0') return VCF_NO_CALL; // no other allele exists at a biallelic site |
110 |
534237 |
534347 |
|
| 433 |
1222573 |
| s++; |
| 434 |
1222573 |
▶ | if (s < sf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s < sf) { |
611286 |
611287 |
1222573 |
|
| 435 |
611287 |
▶ | if (*s != '/' && *s != '|') return VCF_NO_CALL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s != '/' && *s != '|') return VCF_NO_CALL; |
106 |
611181 |
611287 |
| if (*s != '/' && *s != '|') return VCF_NO_CALL; |
0 |
106 |
106 |
|
| 436 |
611287 |
▶ | if (++s == sf) return VCF_NO_CALL; // "1/": a separator with no allele after it is a call cut short, not a haploid one |
Branch: 1 branch
| Branch | True | False | Total |
| if (++s == sf) return VCF_NO_CALL; // "1/": a separator with no allele after it is a call cut short, not a haploid one |
2 |
611285 |
611287 |
|
| 437 |
|
| } |
| 438 |
|
| } |
| 439 |
611286 |
| return VCF_CALL; |
| 440 |
|
| } |
| 441 |
|
| |
| 442 |
|
| /*The end of sample i's field, which starts at p; dies when the record ends |
| 443 |
|
| before it.*/ |
| 444 |
1228100 |
| static const char *vcf_sample(pTHX_ const vcf_reader *r, const char *p, const char *e, size_t i) { |
| 445 |
1228100 |
▶ | if (p > e) croak("%s: %s line %" UVuf " has %" UVuf " samples, and the #CHROM line names %" UVuf, __func__, r->name, r->line, (UV)i, (UV)r->n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > e) croak("%s: %s line %" UVuf " has %" UVuf " samples, and the #CHROM line names %" UVuf, __func__, r->name, r->line, (UV)i, (UV)r->n); |
10 |
1228090 |
1228100 |
|
| 446 |
1228090 |
| return vcf_field_end(p, e); |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
|
| /*Dies when anything is left of the record after its last sample, which would |
| 450 |
|
| start at p.*/ |
| 451 |
342988 |
| static void vcf_samples_end(pTHX_ const vcf_reader *r, const char *p, const char *e) { |
| 452 |
342988 |
▶ | if (p <= e) croak("%s: %s line %" UVuf " has more samples than the %" UVuf " its #CHROM line names", __func__, r->name, r->line, (UV)r->n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p <= e) croak("%s: %s line %" UVuf " has more samples than the %" UVuf " its #CHROM line names", __func__, r->name, r->line, (UV)r->n); |
14 |
342974 |
342988 |
|
| 453 |
342974 |
| } |
| 454 |
|
| |
| 455 |
|
| /*The first 5 of names, quoted, and how many more there are, which is enough |
| 456 |
|
| to show how a VCF's samples are named; a mortal's PV.*/ |
| 457 |
5 |
| static const char *vcf_names_list(pTHX_ AV *names) { |
| 458 |
5 |
| SV *some = sv_2mortal(newSVpvs("")); |
| 459 |
5 |
| const SSize_t last = av_len(names); |
| 460 |
20 |
▶ | for (SSize_t i = 0; i <= last && i < 5; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (SSize_t i = 0; i <= last && i < 5; i++) { |
17 |
3 |
20 |
| for (SSize_t i = 0; i <= last && i < 5; i++) { |
15 |
2 |
17 |
|
| 461 |
15 |
| SV **s = av_fetch(names, i, 0); |
| 462 |
30 |
▶ | sv_catpvf(some, "%s\"%s\"", i ? ", " : "", s ? SvPV_nolen(*s) : ""); |
Branch: 2 branches
| Branch | True | False | Total |
| sv_catpvf(some, "%s\"%s\"", i ? ", " : "", s ? SvPV_nolen(*s) : ""); |
15 |
0 |
15 |
| sv_catpvf(some, "%s\"%s\"", i ? ", " : "", s ? SvPV_nolen(*s) : ""); |
10 |
5 |
15 |
|
| 463 |
|
| } |
| 464 |
5 |
▶ | if (last >= 5) sv_catpvf(some, " and %" UVuf " more", (UV)(last - 4)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (last >= 5) sv_catpvf(some, " and %" UVuf " more", (UV)(last - 4)); |
2 |
3 |
5 |
|
| 465 |
5 |
| return SvPV_nolen(some); |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
|
| /*The #CHROM line p..e: each sample after FORMAT gets its column in samples |
| 469 |
|
| and its place in names, and then the reader's on_header is called.*/ |
| 470 |
490 |
| static void vcf_header(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 471 |
490 |
▶ | if (r->header) croak("%s: %s line %" UVuf " is a second #CHROM line", __func__, r->name, r->line); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->header) croak("%s: %s line %" UVuf " is a second #CHROM line", __func__, r->name, r->line); |
1 |
489 |
490 |
|
| 472 |
489 |
| r->header = 1; |
| 473 |
489 |
| unsigned short int col = 0; // columns passed; the samples start after the 9th, FORMAT |
| 474 |
4889 |
▶ | while (p <= e && col < 9) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p <= e && col < 9) { |
4886 |
3 |
4889 |
| while (p <= e && col < 9) { |
4400 |
486 |
4886 |
|
| 475 |
4400 |
| p = vcf_field_end(p, e) + 1; |
| 476 |
4400 |
| col++; |
| 477 |
|
| } |
| 478 |
489 |
▶ | if (p > e) croak("%s: %s has no samples: its #CHROM line (line %" UVuf ") ends at %s", __func__, r->name, r->line, col < 9 ? "INFO or before" : "FORMAT"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (p > e) croak("%s: %s has no samples: its #CHROM line (line %" UVuf ") ends at %s", __func__, r->name, r->line, col < 9 ? "INFO or before" : "FORMAT"); |
3 |
486 |
489 |
| if (p > e) croak("%s: %s has no samples: its #CHROM line (line %" UVuf ") ends at %s", __func__, r->name, r->line, col < 9 ? "INFO or before" : "FORMAT"); |
1 |
2 |
3 |
|
| 479 |
1734 |
▶ | while (p <= e) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p <= e) { |
1250 |
484 |
1734 |
|
| 480 |
1250 |
| const char *f = vcf_field_end(p, e); |
| 481 |
|
| // up to 2026-11-10 a tab at the end of the line made a sample named "" |
| 482 |
1250 |
▶ | if (f == p) croak("%s: %s has an empty sample name in its #CHROM line (line %" UVuf "), column %" UVuf "; a tab at the end of the line, or two together, makes one", __func__, r->name, r->line, (UV)(r->n + 10)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f == p) croak("%s: %s has an empty sample name in its #CHROM line (line %" UVuf "), column %" UVuf "; a tab at the end of the line, or two together, makes one", __func__, r->name, r->line, (UV)(r->n + 10)); |
1 |
1249 |
1250 |
|
| 483 |
1249 |
| SV *name = newSVpvn(p, (STRLEN)(f - p)); |
| 484 |
1249 |
▶ | if (hv_exists_ent(r->samples, name, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(r->samples, name, 0)) { |
1 |
1248 |
1249 |
|
| 485 |
|
| // mortal on a line of its own: SvPV_nolen() is a macro that evaluates its |
| 486 |
|
| // argument twice on perl 5.10.1 to 5.32.1, which then freed the name twice |
| 487 |
|
| // ("Attempt to free unreferenced scalar"); 5.42.3 and 5.44.0 were unaffected |
| 488 |
1 |
| sv_2mortal(name); |
| 489 |
1 |
| const char *dup = SvPV_nolen(name); |
| 490 |
1 |
| croak("%s: %s names sample \"%s\" twice in its #CHROM line", __func__, r->name, dup); |
| 491 |
|
| } |
| 492 |
1248 |
| (void)hv_store_ent(r->samples, name, newSVuv((UV)r->n), 0); |
| 493 |
1248 |
| av_push(r->names, name); |
| 494 |
1248 |
| r->n++; |
| 495 |
1248 |
| p = f + 1; |
| 496 |
|
| } |
| 497 |
484 |
▶ | if (r->on_header) r->on_header(aTHX_ r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->on_header) r->on_header(aTHX_ r); |
484 |
0 |
484 |
|
| 498 |
477 |
| } |
| 499 |
|
| |
| 500 |
|
| /*The line p..e, its '\n' gone.*/ |
| 501 |
354674 |
| static void vcf_line(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 502 |
354674 |
| r->line++; |
| 503 |
354674 |
▶ | if (e > p && e[-1] == '\r') e--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e > p && e[-1] == '\r') e--; |
354673 |
1 |
354674 |
| if (e > p && e[-1] == '\r') e--; |
1419 |
353254 |
354673 |
|
| 504 |
354674 |
▶ | if (p == e) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == e) return; |
1 |
354673 |
354674 |
|
| 505 |
354673 |
▶ | if (*p != '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p != '#') { |
353173 |
1500 |
354673 |
|
| 506 |
353173 |
▶ | if (!r->header) croak("%s: %s line %" UVuf " is a record, but no #CHROM line has come before it", __func__, r->name, r->line); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->header) croak("%s: %s line %" UVuf " is a record, but no #CHROM line has come before it", __func__, r->name, r->line); |
2 |
353171 |
353173 |
|
| 507 |
353171 |
| r->on_record(aTHX_ r, p, e); |
| 508 |
1500 |
▶ | } else if (e - p >= 6 && memcmp(p, "#CHROM", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (e - p >= 6 && memcmp(p, "#CHROM", 6) == 0) { |
1500 |
0 |
1500 |
| } else if (e - p >= 6 && memcmp(p, "#CHROM", 6) == 0) { |
490 |
1010 |
1500 |
|
| 509 |
490 |
| vcf_header(aTHX_ r, p, e); |
| 510 |
|
| } |
| 511 |
|
| } |
| 512 |
|
| |
| 513 |
|
| /*The VCF r->name, which next, a perl sub, returns a block of at a time and |
| 514 |
|
| then undef. r's name, min_depth, on_header and on_record are the caller's to |
| 515 |
|
| set; the rest is set here. |
| 516 |
|
| |
| 517 |
|
| next may grow perl's stack, which moves it, so an XSUB that reaches this must |
| 518 |
|
| PUTBACK before and SPAGAIN after, and push nothing with the SP it had on entry. |
| 519 |
|
| Up to 2026-10-26 none did, and a reader that grew the stack lost the results |
| 520 |
|
| or corrupted the heap ("realloc(): invalid next size"); t/checks.t grows it.*/ |
| 521 |
499 |
| static void vcf_read(pTHX_ SV *next, vcf_reader *r) { |
| 522 |
499 |
| r->line = 0; |
| 523 |
499 |
| r->n = 0; |
| 524 |
499 |
| r->header = FALSE; |
| 525 |
499 |
| r->samples = (HV*)sv_2mortal((SV*)newHV()); |
| 526 |
499 |
| r->names = (AV*)sv_2mortal((SV*)newAV()); |
| 527 |
499 |
| SV *carry = sv_2mortal(newSVpvs("")); // the start of a line that the last block ended inside |
| 528 |
|
| /*The block, copied out of the scope that next's own temporaries are freed |
| 529 |
|
| with, so that whatever on_header saves for freeing (with SAVEFREEPV, say) |
| 530 |
|
| lands in the caller's scope rather than that one; from perl 5.20 the copy |
| 531 |
|
| is copy-on-write, and moves no bytes.*/ |
| 532 |
499 |
| SV *block = sv_newmortal(); |
| 533 |
856 |
| for (;;) { |
| 534 |
1355 |
| dSP; |
| 535 |
1355 |
| ENTER; |
| 536 |
1355 |
| SAVETMPS; |
| 537 |
1355 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1355 |
1355 |
|
| 538 |
1355 |
| PUTBACK; |
| 539 |
1355 |
| I32 got = call_sv(next, G_SCALAR); |
| 540 |
1353 |
| SPAGAIN; |
| 541 |
1353 |
▶ | sv_setsv(block, got ? POPs : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv(block, got ? POPs : &PL_sv_undef); |
1353 |
0 |
1353 |
|
| 542 |
1353 |
| PUTBACK; |
| 543 |
1353 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1353 |
0 |
1353 |
|
| 544 |
1353 |
| LEAVE; |
| 545 |
1353 |
▶ | if (!SvOK(block) || r->stop) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(block) || r->stop) break; |
918 |
435 |
1353 |
| if (!SvOK(block) || r->stop) break; |
915 |
3 |
918 |
|
| 546 |
|
| STRLEN n; |
| 547 |
915 |
| const char *b = SvPV_const(block, n), *end = b + n; // not restrict: perl's buffer |
| 548 |
355529 |
▶ | while (b < end && !r->stop) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (b < end && !r->stop) { |
355154 |
375 |
355529 |
| while (b < end && !r->stop) { |
355094 |
60 |
355154 |
|
| 549 |
355094 |
| const char *nl = (const char*)memchr(b, '\n', (size_t)(end - b)); |
| 550 |
355094 |
▶ | if (!nl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nl) { |
421 |
354673 |
355094 |
|
| 551 |
421 |
| sv_catpvn(carry, b, (STRLEN)(end - b)); |
| 552 |
421 |
| break; |
| 553 |
|
| } |
| 554 |
354673 |
▶ | if (SvCUR(carry)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(carry)) { |
293 |
354380 |
354673 |
|
| 555 |
293 |
| sv_catpvn(carry, b, (STRLEN)(nl - b)); |
| 556 |
293 |
| vcf_line(aTHX_ r, SvPVX_const(carry), SvPVX_const(carry) + SvCUR(carry)); |
| 557 |
293 |
| SvCUR_set(carry, 0); |
| 558 |
|
| } else { |
| 559 |
354380 |
| vcf_line(aTHX_ r, b, nl); |
| 560 |
|
| } |
| 561 |
354614 |
| b = nl + 1; |
| 562 |
|
| } |
| 563 |
|
| } |
| 564 |
438 |
▶ | if (SvCUR(carry) && !r->stop) vcf_line(aTHX_ r, SvPVX_const(carry), SvPVX_const(carry) + SvCUR(carry)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvCUR(carry) && !r->stop) vcf_line(aTHX_ r, SvPVX_const(carry), SvPVX_const(carry) + SvCUR(carry)); |
1 |
437 |
438 |
| if (SvCUR(carry) && !r->stop) vcf_line(aTHX_ r, SvPVX_const(carry), SvPVX_const(carry) + SvCUR(carry)); |
1 |
0 |
1 |
|
| 565 |
438 |
▶ | if (!r->header) croak("%s: %s has no #CHROM line, so it isn't a VCF", __func__, r->name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->header) croak("%s: %s has no #CHROM line, so it isn't a VCF", __func__, r->name); |
6 |
432 |
438 |
|
| 566 |
432 |
| } |
| 567 |
|
| |
| 568 |
|
| /*The genotype counts behind sex_check(). |
| 569 |
|
| |
| 570 |
|
| The method is peddy 0.4.3's sex_check() (peddy/peddy.py): on X outside its |
| 571 |
|
| pseudo-autosomal regions, count each sample's heterozygous and homozygous-ALT |
| 572 |
|
| calls at biallelic sites, leaving out a genotype whose depth is below a |
| 573 |
|
| minimum. Its PAR bounds, X:10000-2781479 and X:154931044-156030895, cover both |
| 574 |
|
| GRCh37's and GRCh38's, so no build need be named. It departs from peddy in |
| 575 |
|
| three ways. Every site is read, where peddy stops at 10,000. Only SNVs are |
| 576 |
|
| counted, where peddy keeps an insertion (its test is len(REF) > 1 alone). And |
| 577 |
|
| peddy's cohort filters (a call rate of at least 0.5, an ALT frequency of at |
| 578 |
|
| least 0.01) are left out, since they mean nothing for a VCF of one genome, |
| 579 |
|
| which is the usual input here. |
| 580 |
|
| |
| 581 |
|
| Non-PAR Y is counted too, for the caller to see, but decides nothing: on |
| 582 |
|
| 2026-10-18 the mother's exome, whose X is plainly female (2,592 het to 2,673 |
| 583 |
|
| hom-ALT), had 530 called SNVs on non-PAR Y, and the father's exome had 659. |
| 584 |
|
| Y's PARs are GRCh37's (Y:10001-2649520, Y:59034050-59363566) and GRCh38's |
| 585 |
|
| (Y:10001-2781479, Y:56887903-57217415), from the GRC's assembly reports. |
| 586 |
|
| |
| 587 |
|
| Nothing is kept but a fixed set of counters for each sample.*/ |
| 588 |
|
| |
| 589 |
|
| /*The counters kept for each sample, in this order in sex_tally's count; their |
| 590 |
|
| names in the hash returned to perl are SEX_NAME's.*/ |
| 591 |
|
| enum { SEX_X_HET, SEX_X_HOM_ALT, SEX_X_HOM_REF, SEX_X_HAPLOID, SEX_X_MISSING, SEX_Y_CALLED, SEX_Y_ALT, SEX_FIELDS }; |
| 592 |
|
| static const char *const SEX_NAME[SEX_FIELDS] = { "x_het", "x_hom_alt", "x_hom_ref", "x_haploid", "x_missing", "y_called", "y_alt" }; |
| 593 |
|
| |
| 594 |
|
| typedef struct { |
| 595 |
|
| vcf_reader r; // first, so that the vcf_reader * that sex_header() and sex_record() are given is this |
| 596 |
|
| UV x_sites; // non-PAR X SNVs read, for perl to say when there were none |
| 597 |
|
| UV *count; // r.n * SEX_FIELDS counters, sample by sample |
| 598 |
|
| } sex_tally; |
| 599 |
|
| |
| 600 |
|
| /*Whether pos on chrom (23 for X, 24 for Y) lies in a pseudo-autosomal |
| 601 |
|
| region; see the comment above SEX_NAME for the bounds.*/ |
| 602 |
1767 |
| static bool sex_par(unsigned short int chrom, UV pos) { |
| 603 |
1767 |
▶ | if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
Branch: 5 branches
| Branch | True | False | Total |
| if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
1614 |
153 |
1767 |
| if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
1614 |
0 |
1614 |
| if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
1578 |
36 |
1614 |
| if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
36 |
1542 |
1578 |
| if (chrom == 23) return (pos >= 10000 && pos <= 2781479) || (pos >= 154931044 && pos <= 156030895); |
18 |
18 |
36 |
|
| 604 |
153 |
▶ | return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
Branch: 6 branches
| Branch | True | False | Total |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
153 |
0 |
153 |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
135 |
18 |
153 |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
36 |
99 |
135 |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
18 |
18 |
36 |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
18 |
99 |
117 |
| return (pos >= 10001 && pos <= 2781479) || (pos >= 56887903 && pos <= 57217415) || (pos >= 59034050 && pos <= 59363566); |
18 |
0 |
18 |
|
| 605 |
|
| } |
| 606 |
|
| |
| 607 |
|
| /*Each sample's counters, once the #CHROM line has said how many samples |
| 608 |
|
| there are.*/ |
| 609 |
44 |
| static void sex_header(pTHX_ vcf_reader *r) { |
| 610 |
44 |
| sex_tally *t = (sex_tally*)r; |
| 611 |
44 |
▶ | Newxz(t->count, r->n * SEX_FIELDS, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t->count, r->n * SEX_FIELDS, UV); |
0 |
44 |
44 |
|
| 612 |
44 |
| SAVEFREEPV(t->count); // freed at the caller's LEAVE, croak or not; see vcf_read()'s block |
| 613 |
44 |
| } |
| 614 |
|
| |
| 615 |
|
| /*Whether the record of chrom (23 for X, 24 for Y), whose columns vcf_columns() |
| 616 |
|
| put in col, is one that sex counts: outside the PARs, a biallelic SNV, and with |
| 617 |
|
| GT, whose place in FORMAT goes in *gt and DP's in *dp. format_end is |
| 618 |
|
| vcf_columns()'s.*/ |
| 619 |
1770 |
| static bool sex_site(pTHX_ const vcf_reader *r, unsigned short int chrom, const char **col, const char *format_end, size_t *gt, size_t *dp) { |
| 620 |
1770 |
| UV pos = vcf_pos(aTHX_ r, col[1], col[2] - 1); |
| 621 |
1767 |
▶ | if (sex_par(chrom, pos) || !vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (sex_par(chrom, pos) || !vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return FALSE; |
1659 |
108 |
1767 |
| if (sex_par(chrom, pos) || !vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return FALSE; |
1623 |
36 |
1659 |
| if (sex_par(chrom, pos) || !vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return FALSE; |
36 |
1587 |
1623 |
|
| 622 |
1587 |
| vcf_format(col[8], format_end, gt, dp); |
| 623 |
1587 |
| return *gt != VCF_ABSENT; |
| 624 |
|
| } |
| 625 |
|
| |
| 626 |
|
| /*One sample's call, the field p..e, at a site of chrom that sex_site() took, |
| 627 |
|
| into c, the sample's SEX_FIELDS counters.*/ |
| 628 |
8005 |
| static void sex_count(const vcf_reader *r, UV *c, unsigned short int chrom, const char *p, const char *e, size_t gt, size_t dp) { |
| 629 |
|
| unsigned short int alleles, alt; |
| 630 |
8005 |
| short int call = vcf_call(r, p, e, gt, dp, &alleles, &alt); |
| 631 |
8053 |
▶ | if (call == VCF_SHALLOW) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (call == VCF_SHALLOW) return; |
63 |
7942 |
8005 |
|
| 632 |
7942 |
▶ | if (chrom == 24) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom == 24) { |
299 |
7643 |
7942 |
|
| 633 |
299 |
▶ | if (call == VCF_NO_CALL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (call == VCF_NO_CALL) return; |
48 |
251 |
299 |
|
| 634 |
251 |
| c[SEX_Y_CALLED]++; |
| 635 |
251 |
▶ | if (alt) c[SEX_Y_ALT]++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alt) c[SEX_Y_ALT]++; |
100 |
151 |
251 |
|
| 636 |
7643 |
▶ | } else if (call == VCF_NO_CALL) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (call == VCF_NO_CALL) { |
427 |
7216 |
7643 |
|
| 637 |
427 |
| c[SEX_X_MISSING]++; |
| 638 |
|
| } else { |
| 639 |
7216 |
▶ | if (alleles == 1) c[SEX_X_HAPLOID]++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alleles == 1) c[SEX_X_HAPLOID]++; |
165 |
7051 |
7216 |
|
| 640 |
|
| // a haploid call is hom-ALT or hom-REF, as peddy's cyvcf2 counts it |
| 641 |
7216 |
▶ | c[alt == 0 ? SEX_X_HOM_REF : alt == alleles ? SEX_X_HOM_ALT : SEX_X_HET]++; |
Branch: 2 branches
| Branch | True | False | Total |
| c[alt == 0 ? SEX_X_HOM_REF : alt == alleles ? SEX_X_HOM_ALT : SEX_X_HET]++; |
5200 |
2016 |
7216 |
| c[alt == 0 ? SEX_X_HOM_REF : alt == alleles ? SEX_X_HOM_ALT : SEX_X_HET]++; |
3604 |
1596 |
5200 |
|
| 642 |
|
| } |
| 643 |
|
| } |
| 644 |
|
| |
| 645 |
|
| //The record p..e.*/ |
| 646 |
9739 |
| static void sex_record(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 647 |
9739 |
| sex_tally *t = (sex_tally*)r; |
| 648 |
9739 |
| const char *f = vcf_field_end(p, e); |
| 649 |
9739 |
| unsigned short int chrom = vcf_chrom(p, f); // only 23 (X) and 24 (Y) are read |
| 650 |
9937 |
▶ | if (chrom != 23 && chrom != 24) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (chrom != 23 && chrom != 24) return; |
9004 |
735 |
9739 |
| if (chrom != 23 && chrom != 24) return; |
8892 |
112 |
9004 |
|
| 651 |
|
| const char *col[9]; |
| 652 |
847 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 653 |
|
| size_t gt, dp; |
| 654 |
846 |
▶ | if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
198 |
645 |
843 |
|
| 655 |
645 |
▶ | if (chrom == 23) t->x_sites++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom == 23) t->x_sites++; |
605 |
40 |
645 |
|
| 656 |
645 |
| p = format_end + 1; |
| 657 |
4416 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
3777 |
639 |
4416 |
|
| 658 |
3777 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 659 |
3771 |
| sex_count(r, t->count + i * SEX_FIELDS, chrom, p, se, gt, dp); |
| 660 |
3771 |
| p = se + 1; |
| 661 |
|
| } |
| 662 |
639 |
| vcf_samples_end(aTHX_ r, p, e); |
| 663 |
|
| } |
| 664 |
|
| |
| 665 |
|
| /*The SEX_FIELDS counts in count of the n samples in names, each stored in out |
| 666 |
|
| as sample => hash of counts, in place of anything out held for it.*/ |
| 667 |
77 |
| static void sex_rows(pTHX_ HV *out, AV *names, const UV *count, size_t n, const char *name) { |
| 668 |
386 |
▶ | for (size_t i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < n; i++) { |
309 |
77 |
386 |
|
| 669 |
309 |
| SV **sample = av_fetch(names, (SSize_t)i, 0); |
| 670 |
309 |
▶ | if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
0 |
309 |
309 |
|
| 671 |
309 |
| HV *row = newHV(); |
| 672 |
2472 |
▶ | for (unsigned short int k = 0; k < SEX_FIELDS; k++) (void)hv_store(row, SEX_NAME[k], (I32)strlen(SEX_NAME[k]), newSVuv(count[i * SEX_FIELDS + k]), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < SEX_FIELDS; k++) (void)hv_store(row, SEX_NAME[k], (I32)strlen(SEX_NAME[k]), newSVuv(count[i * SEX_FIELDS + k]), 0); |
2163 |
309 |
2472 |
|
| 673 |
309 |
| (void)hv_store_ent(out, *sample, newRV_noinc((SV*)row), 0); |
| 674 |
|
| } |
| 675 |
77 |
| } |
| 676 |
|
| |
| 677 |
|
| /*The counts of every sample of the VCF name, which next returns a block of |
| 678 |
|
| at a time. Returns sample => hash of counts, and sets *x_sites and *names, |
| 679 |
|
| the samples in file order, for the report.*/ |
| 680 |
52 |
| static HV *sex_tally_vcf(pTHX_ SV *next, const char *name, UV min_depth, UV *x_sites, AV **names) { |
| 681 |
|
| sex_tally t; |
| 682 |
52 |
| Zero(&t, 1, sex_tally); |
| 683 |
52 |
| t.r.name = name; |
| 684 |
52 |
| t.r.min_depth = min_depth; |
| 685 |
52 |
| t.r.on_header = sex_header; |
| 686 |
52 |
| t.r.on_record = sex_record; |
| 687 |
52 |
| vcf_read(aTHX_ next, &t.r); |
| 688 |
32 |
| HV *out = t.r.samples; |
| 689 |
32 |
| sex_rows(aTHX_ out, t.r.names, t.count, t.r.n, name); // in place of the columns vcf_header() stored |
| 690 |
32 |
| *x_sites = t.x_sites; |
| 691 |
32 |
| *names = t.r.names; |
| 692 |
32 |
| return out; |
| 693 |
|
| } |
| 694 |
|
| |
| 695 |
|
| /*The genotype counts behind parent(): how many autosomal biallelic SNVs the |
| 696 |
|
| child and the parent have with each pair of ALT counts, 0 to 2. perl takes |
| 697 |
|
| IBS0, IBS1, IBS2 and the KING-robust kinship from those nine numbers. |
| 698 |
|
| |
| 699 |
|
| The two samples are either columns of one VCF, which is read a record at a |
| 700 |
|
| time and nothing kept but the nine counts, or each the one sample of its own |
| 701 |
|
| VCF. A VCF of one genome usually lists only the sites where its sample |
| 702 |
|
| carries an ALT allele, so that a site absent from it stands for hom-REF; the |
| 703 |
|
| two are then compared site by site, which takes each file's sites in memory, |
| 704 |
|
| 8 bytes a site, sorted (by C89's qsort(), since the two files need not list |
| 705 |
|
| their chromosomes in the same order). A position is left out when either file |
| 706 |
|
| has more than one SNV there, a call that is not a diploid call of REF and |
| 707 |
|
| ALT, a DP below min_depth, or more than one ALT, or when the files give it |
| 708 |
|
| different REF or ALT alleles. A file that leaves out filtered sites, or |
| 709 |
|
| off-target ones, makes the comparison see hom-REF where the sample was never |
| 710 |
|
| called; perl's report gives the sites each file had alone, for that reason.*/ |
| 711 |
|
| |
| 712 |
|
| #define PARENT_NONE 3 // the code of a sample with no diploid call of REF and ALT, or a DP below min_depth |
| 713 |
|
| |
| 714 |
|
| /*A site of an own VCF is one uint64_t, packed so that sorting the numbers |
| 715 |
|
| sorts the sites by chromosome and then position, and the sites at one |
| 716 |
|
| position share every bit from bit 7 up: |
| 717 |
|
| |
| 718 |
|
| chromosome (1-22) << 39 | POS (32 bits) << 7 | REF << 5 | ALT << 2 | code |
| 719 |
|
| |
| 720 |
|
| where REF and ALT are vcf_base_index()'s 0 to 3, ALT is 4 for a record of |
| 721 |
|
| more than one ALT, and code is the ALT count or PARENT_NONE.*/ |
| 722 |
|
| #define PARENT_POS_SHIFT 7 |
| 723 |
|
| #define PARENT_ALLELES(k) (((k) >> 2) & 0x1f) // REF and ALT together |
| 724 |
|
| #define PARENT_ALT(k) (((k) >> 2) & 7) |
| 725 |
|
| #define PARENT_CODE(k) ((unsigned short int)((k) & 3)) |
| 726 |
|
| |
| 727 |
|
| /*An own VCF's sites, in a holder that the scope's destructor frees, since |
| 728 |
|
| Renew() moves the array.*/ |
| 729 |
|
| typedef struct { |
| 730 |
|
| uint64_t *site; |
| 731 |
|
| size_t n, room; // sites used and allocated |
| 732 |
|
| } parent_sites; |
| 733 |
|
| |
| 734 |
131 |
| static void parent_sites_free(pTHX_ void *p) { |
| 735 |
131 |
| parent_sites *s = (parent_sites*)p; |
| 736 |
131 |
| Safefree(s->site); |
| 737 |
131 |
| Safefree(s); |
| 738 |
131 |
| } |
| 739 |
|
| |
| 740 |
|
| typedef struct { |
| 741 |
|
| vcf_reader r; // first, so that the vcf_reader * that the callbacks are given is this |
| 742 |
|
| SV *want[2]; // one VCF: the child's and the parent's sample names; unused for own VCFs |
| 743 |
|
| size_t col[2]; // one VCF: their columns among the samples |
| 744 |
|
| UV t[9]; // one VCF: t[3 * child + parent], sites by the two codes |
| 745 |
|
| parent_sites *sites; // own VCF: the file's sites |
| 746 |
|
| } parent_reader; |
| 747 |
|
| |
| 748 |
|
| //The code of the call in the sample field p..e: its ALT count, or PARENT_NONE |
| 749 |
661468 |
| static unsigned short int parent_code(const vcf_reader *r, const char *p, const char *e, size_t gt, size_t dp) { |
| 750 |
|
| unsigned short int alleles, alt; |
| 751 |
661468 |
▶ | return vcf_call(r, p, e, gt, dp, &alleles, &alt) == VCF_CALL && alleles == 2 ? alt : PARENT_NONE; |
Branch: 2 branches
| Branch | True | False | Total |
| return vcf_call(r, p, e, gt, dp, &alleles, &alt) == VCF_CALL && alleles == 2 ? alt : PARENT_NONE; |
603819 |
57649 |
661468 |
| return vcf_call(r, p, e, gt, dp, &alleles, &alt) == VCF_CALL && alleles == 2 ? alt : PARENT_NONE; |
603728 |
91 |
603819 |
|
| 752 |
|
| } |
| 753 |
|
| |
| 754 |
|
| //One VCF: where the child's and the parent's columns are. |
| 755 |
60 |
| static void parent_header_one(pTHX_ vcf_reader *r) { |
| 756 |
60 |
| parent_reader *t = (parent_reader*)r; |
| 757 |
179 |
▶ | for (unsigned short int k = 0; k < 2; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 2; k++) { |
120 |
59 |
179 |
|
| 758 |
120 |
| HE *he = hv_fetch_ent(r->samples, t->want[k], 0, 0); |
| 759 |
120 |
▶ | if (he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (he) { |
119 |
1 |
120 |
|
| 760 |
119 |
| t->col[k] = (size_t)SvUV(HeVAL(he)); |
| 761 |
119 |
| continue; |
| 762 |
|
| } |
| 763 |
2 |
| const char *want = SvPV_nolen(t->want[k]), *list = vcf_names_list(aTHX_ r->names); |
| 764 |
1 |
| croak("%s: %s has no sample \"%s\"; its samples are %s", __func__, r->name, want, list); |
| 765 |
|
| } |
| 766 |
59 |
| } |
| 767 |
|
| |
| 768 |
|
| //One VCF: the record p..e. |
| 769 |
111040 |
| static void parent_record_one(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 770 |
111040 |
| parent_reader *t = (parent_reader*)r; |
| 771 |
111040 |
| const char *f = vcf_field_end(p, e); |
| 772 |
111040 |
| unsigned short int chrom = vcf_chrom(p, f); |
| 773 |
111275 |
▶ | if (chrom < 1 || chrom > 22) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (chrom < 1 || chrom > 22) return; |
110899 |
141 |
111040 |
| if (chrom < 1 || chrom > 22) return; |
143 |
110756 |
110899 |
|
| 774 |
|
| const char *col[9]; |
| 775 |
110756 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 776 |
110756 |
▶ | if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
110709 |
47 |
110756 |
| if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
141 |
110568 |
110709 |
|
| 777 |
|
| size_t gt, dp; |
| 778 |
110568 |
| vcf_format(col[8], format_end, >, &dp); |
| 779 |
110568 |
▶ | if (gt == VCF_ABSENT) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gt == VCF_ABSENT) return; |
47 |
110521 |
110568 |
|
| 780 |
110521 |
| unsigned short int code[2] = { PARENT_NONE, PARENT_NONE }; // the child's and the parent's |
| 781 |
110521 |
| p = format_end + 1; |
| 782 |
874148 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
763627 |
110521 |
874148 |
|
| 783 |
763627 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 784 |
2290881 |
▶ | for (unsigned short int k = 0; k < 2; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 2; k++) { |
1527254 |
763627 |
2290881 |
|
| 785 |
1527254 |
▶ | if (i == t->col[k]) code[k] = parent_code(r, p, se, gt, dp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == t->col[k]) code[k] = parent_code(r, p, se, gt, dp); |
221042 |
1306212 |
1527254 |
|
| 786 |
|
| } |
| 787 |
763627 |
| p = se + 1; |
| 788 |
|
| } |
| 789 |
110521 |
| vcf_samples_end(aTHX_ r, p, e); |
| 790 |
110520 |
▶ | if (code[0] != PARENT_NONE && code[1] != PARENT_NONE) t->t[3 * code[0] + code[1]]++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (code[0] != PARENT_NONE && code[1] != PARENT_NONE) t->t[3 * code[0] + code[1]]++; |
100590 |
9930 |
110520 |
| if (code[0] != PARENT_NONE && code[1] != PARENT_NONE) t->t[3 * code[0] + code[1]]++; |
92407 |
8183 |
100590 |
|
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
|
| /*Own VCF: one sample, or it isn't.*/ |
| 794 |
56 |
| static void parent_header_own(pTHX_ vcf_reader *r) { |
| 795 |
56 |
▶ | if (r->n != 1) croak("%s: %s has %" UVuf " samples, and a VCF given as \"child\" or \"parent\" must have one; give a VCF of several as \"vcf\", and the samples by name", __func__, r->name, (UV)r->n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n != 1) croak("%s: %s has %" UVuf " samples, and a VCF given as \"child\" or \"parent\" must have one; give a VCF of several as \"vcf\", and the samples by name", __func__, r->name, (UV)r->n); |
1 |
55 |
56 |
|
| 796 |
55 |
| } |
| 797 |
|
| |
| 798 |
|
| /*Own VCF: the record p..e, kept as a site.*/ |
| 799 |
173492 |
| static void parent_record_own(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 800 |
173492 |
| parent_reader *t = (parent_reader*)r; |
| 801 |
173492 |
| const char *f = vcf_field_end(p, e); |
| 802 |
173492 |
| unsigned short int chrom = vcf_chrom(p, f); |
| 803 |
173612 |
▶ | if (chrom < 1 || chrom > 22) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (chrom < 1 || chrom > 22) return; |
173372 |
120 |
173492 |
| if (chrom < 1 || chrom > 22) return; |
63 |
173309 |
173372 |
|
| 804 |
|
| const char *col[9]; |
| 805 |
173309 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 806 |
173309 |
▶ | if (!vcf_base(col[3], col[4] - 1)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vcf_base(col[3], col[4] - 1)) return; |
40 |
173269 |
173309 |
|
| 807 |
|
| unsigned short int alt; // vcf_base_index()'s, or 4 for more than one ALT |
| 808 |
173269 |
▶ | if (vcf_base(col[4], col[5] - 1)) alt = vcf_base_index(*col[4]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (vcf_base(col[4], col[5] - 1)) alt = vcf_base_index(*col[4]); |
173149 |
120 |
173269 |
|
| 809 |
120 |
▶ | else if (memchr(col[4], ',', (size_t)(col[5] - 1 - col[4]))) alt = 4; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (memchr(col[4], ',', (size_t)(col[5] - 1 - col[4]))) alt = 4; |
40 |
80 |
120 |
|
| 810 |
80 |
| else return; // an indel, or an ALT of "." or "<NON_REF>" |
| 811 |
173189 |
| UV pos = vcf_pos(aTHX_ r, col[1], col[2] - 1); |
| 812 |
173189 |
▶ | if (pos > UINT32_MAX) croak("%s: %s line %" UVuf " has POS %" UVuf ", beyond the 4294967295 that parent can compare", __func__, r->name, r->line, pos); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos > UINT32_MAX) croak("%s: %s line %" UVuf " has POS %" UVuf ", beyond the 4294967295 that parent can compare", __func__, r->name, r->line, pos); |
1 |
173188 |
173189 |
|
| 813 |
|
| size_t gt, dp; |
| 814 |
173188 |
| vcf_format(col[8], format_end, >, &dp); |
| 815 |
173188 |
| p = format_end + 1; |
| 816 |
173188 |
| const char *se = vcf_sample(aTHX_ r, p, e, 0); |
| 817 |
173187 |
| vcf_samples_end(aTHX_ r, se + 1, e); |
| 818 |
173179 |
▶ | unsigned short int code = alt == 4 || gt == VCF_ABSENT ? PARENT_NONE : parent_code(r, p, se, gt, dp); |
Branch: 2 branches
| Branch | True | False | Total |
| unsigned short int code = alt == 4 || gt == VCF_ABSENT ? PARENT_NONE : parent_code(r, p, se, gt, dp); |
173139 |
40 |
173179 |
| unsigned short int code = alt == 4 || gt == VCF_ABSENT ? PARENT_NONE : parent_code(r, p, se, gt, dp); |
173052 |
87 |
173139 |
|
| 819 |
173179 |
| parent_sites *s = t->sites; |
| 820 |
173179 |
▶ | if (s->n == s->room) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->n == s->room) { |
115 |
173064 |
173179 |
|
| 821 |
115 |
▶ | s->room = s->room ? 2 * s->room : 65536; // 512 KiB, a sixth of an exome's 280,000 records |
Branch: 1 branch
| Branch | True | False | Total |
| s->room = s->room ? 2 * s->room : 65536; // 512 KiB, a sixth of an exome's 280,000 records |
0 |
115 |
115 |
|
| 822 |
115 |
▶ | Renew(s->site, s->room, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->site, s->room, uint64_t); |
0 |
115 |
115 |
|
| 823 |
|
| } |
| 824 |
173179 |
| s->site[s->n++] = (uint64_t)chrom << 39 | (uint64_t)pos << PARENT_POS_SHIFT | (uint64_t)vcf_base_index(*col[3]) << 5 | (uint64_t)alt << 2 | code; |
| 825 |
|
| } |
| 826 |
|
| |
| 827 |
949057 |
| static int parent_cmp(const void *a, const void *b) { |
| 828 |
949057 |
| const uint64_t x = *(const uint64_t*)a, y = *(const uint64_t*)b; |
| 829 |
949057 |
| return (x > y) - (x < y); |
| 830 |
|
| } |
| 831 |
|
| |
| 832 |
|
| /*Own VCF: the sites of the VCF that next returns a block of at a time, read |
| 833 |
|
| by t, whose name, min_depth and callbacks the caller has set, sorted.*/ |
| 834 |
131 |
| static parent_sites *parent_sites_read(pTHX_ parent_reader *t, SV *next) { |
| 835 |
131 |
| Newxz(t->sites, 1, parent_sites); |
| 836 |
131 |
| SAVEDESTRUCTOR_X(parent_sites_free, t->sites); // at the caller's LEAVE, croak or not |
| 837 |
131 |
| vcf_read(aTHX_ next, &t->r); |
| 838 |
115 |
▶ | if (t->sites->n) qsort(t->sites->site, t->sites->n, sizeof *t->sites->site, parent_cmp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->sites->n) qsort(t->sites->site, t->sites->n, sizeof *t->sites->site, parent_cmp); |
115 |
0 |
115 |
|
| 839 |
115 |
| return t->sites; |
| 840 |
|
| } |
| 841 |
|
| |
| 842 |
|
| /*Own VCF: the sites of the VCF name, which next returns a block of at a |
| 843 |
|
| time, sorted.*/ |
| 844 |
57 |
| static parent_sites *parent_sites_vcf(pTHX_ SV *next, const char *name, UV min_depth) { |
| 845 |
|
| parent_reader t; |
| 846 |
57 |
| Zero(&t, 1, parent_reader); |
| 847 |
57 |
| t.r.name = name; |
| 848 |
57 |
| t.r.min_depth = min_depth; |
| 849 |
57 |
| t.r.on_header = parent_header_own; |
| 850 |
57 |
| t.r.on_record = parent_record_own; |
| 851 |
57 |
| return parent_sites_read(aTHX_ &t, next); |
| 852 |
|
| } |
| 853 |
|
| |
| 854 |
|
| /*The sites of the child's VCF, a, and of the parent's, b, both sorted, as t |
| 855 |
|
| counts them; in[0], in[1] and in[2] get the sites where either carries the |
| 856 |
|
| ALT that were in both files, in the child's alone, and in the parent's alone. |
| 857 |
|
| The rules are in the comment above PARENT_NONE.*/ |
| 858 |
21 |
| static void parent_merge(const parent_sites *a, const parent_sites *b, UV *t, UV *in) { |
| 859 |
21 |
| size_t i = 0, j = 0; |
| 860 |
36720 |
▶ | while (i < a->n || j < b->n) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < a->n || j < b->n) { |
36277 |
443 |
36720 |
| while (i < a->n || j < b->n) { |
422 |
21 |
443 |
|
| 861 |
36699 |
▶ | const uint64_t pa = i < a->n ? a->site[i] >> PARENT_POS_SHIFT : UINT64_MAX, pb = j < b->n ? b->site[j] >> PARENT_POS_SHIFT : UINT64_MAX; |
Branch: 2 branches
| Branch | True | False | Total |
| const uint64_t pa = i < a->n ? a->site[i] >> PARENT_POS_SHIFT : UINT64_MAX, pb = j < b->n ? b->site[j] >> PARENT_POS_SHIFT : UINT64_MAX; |
36277 |
422 |
36699 |
| const uint64_t pa = i < a->n ? a->site[i] >> PARENT_POS_SHIFT : UINT64_MAX, pb = j < b->n ? b->site[j] >> PARENT_POS_SHIFT : UINT64_MAX; |
36299 |
400 |
36699 |
|
| 862 |
36699 |
| const uint64_t at = pa < pb ? pa : pb; // the position to compare |
| 863 |
36699 |
| size_t na = 0, nb = 0; // its sites in each file |
| 864 |
70221 |
▶ | while (i + na < a->n && a->site[i + na] >> PARENT_POS_SHIFT == at) na++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i + na < a->n && a->site[i + na] >> PARENT_POS_SHIFT == at) na++; |
69778 |
443 |
70221 |
| while (i + na < a->n && a->site[i + na] >> PARENT_POS_SHIFT == at) na++; |
33522 |
36256 |
69778 |
|
| 865 |
69107 |
▶ | while (j + nb < b->n && b->site[j + nb] >> PARENT_POS_SHIFT == at) nb++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j + nb < b->n && b->site[j + nb] >> PARENT_POS_SHIFT == at) nb++; |
68686 |
421 |
69107 |
| while (j + nb < b->n && b->site[j + nb] >> PARENT_POS_SHIFT == at) nb++; |
32408 |
36278 |
68686 |
|
| 866 |
36699 |
▶ | const uint64_t ka = na ? a->site[i] : 0, kb = nb ? b->site[j] : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| const uint64_t ka = na ? a->site[i] : 0, kb = nb ? b->site[j] : 0; |
33510 |
3189 |
36699 |
| const uint64_t ka = na ? a->site[i] : 0, kb = nb ? b->site[j] : 0; |
32406 |
4293 |
36699 |
|
| 867 |
36699 |
| i += na; |
| 868 |
36699 |
| j += nb; |
| 869 |
36699 |
▶ | if (na > 1 || nb > 1) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (na > 1 || nb > 1) continue; |
36687 |
12 |
36699 |
| if (na > 1 || nb > 1) continue; |
2 |
36685 |
36687 |
|
| 870 |
36685 |
▶ | if (na && (PARENT_CODE(ka) == PARENT_NONE || PARENT_ALT(ka) == 4)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (na && (PARENT_CODE(ka) == PARENT_NONE || PARENT_ALT(ka) == 4)) continue; |
33498 |
3187 |
36685 |
| if (na && (PARENT_CODE(ka) == PARENT_NONE || PARENT_ALT(ka) == 4)) continue; |
31814 |
1684 |
33498 |
| if (na && (PARENT_CODE(ka) == PARENT_NONE || PARENT_ALT(ka) == 4)) continue; |
0 |
31814 |
31814 |
|
| 871 |
35001 |
▶ | if (nb && (PARENT_CODE(kb) == PARENT_NONE || PARENT_ALT(kb) == 4)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (nb && (PARENT_CODE(kb) == PARENT_NONE || PARENT_ALT(kb) == 4)) continue; |
31055 |
3946 |
35001 |
| if (nb && (PARENT_CODE(kb) == PARENT_NONE || PARENT_ALT(kb) == 4)) continue; |
29602 |
1453 |
31055 |
| if (nb && (PARENT_CODE(kb) == PARENT_NONE || PARENT_ALT(kb) == 4)) continue; |
0 |
29602 |
29602 |
|
| 872 |
33548 |
▶ | if (na && nb && PARENT_ALLELES(ka) != PARENT_ALLELES(kb)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (na && nb && PARENT_ALLELES(ka) != PARENT_ALLELES(kb)) continue; |
30707 |
2841 |
33548 |
| if (na && nb && PARENT_ALLELES(ka) != PARENT_ALLELES(kb)) continue; |
26761 |
3946 |
30707 |
| if (na && nb && PARENT_ALLELES(ka) != PARENT_ALLELES(kb)) continue; |
24 |
26737 |
26761 |
|
| 873 |
33524 |
▶ | const unsigned short int c = na ? PARENT_CODE(ka) : 0, p = nb ? PARENT_CODE(kb) : 0; // a site absent from a file is hom-REF there |
Branch: 2 branches
| Branch | True | False | Total |
| const unsigned short int c = na ? PARENT_CODE(ka) : 0, p = nb ? PARENT_CODE(kb) : 0; // a site absent from a file is hom-REF there |
30683 |
2841 |
33524 |
| const unsigned short int c = na ? PARENT_CODE(ka) : 0, p = nb ? PARENT_CODE(kb) : 0; // a site absent from a file is hom-REF there |
29578 |
3946 |
33524 |
|
| 874 |
33524 |
| t[3 * c + p]++; |
| 875 |
33524 |
▶ | if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
Branch: 5 branches
| Branch | True | False | Total |
| if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
4953 |
28571 |
33524 |
| if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
4950 |
3 |
4953 |
| if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
30681 |
2840 |
33521 |
| if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
3946 |
26735 |
30681 |
| if (c || p) in[na && nb ? 0 : na ? 1 : 2]++; |
3946 |
2840 |
6786 |
|
| 876 |
|
| } |
| 877 |
21 |
| } |
| 878 |
|
| |
| 879 |
|
| /*The genotype counts behind relatedness(): for every pair of samples, what |
| 880 |
|
| parent() counts for one pair, and for every sample what sex_check() counts, |
| 881 |
|
| from the same reading, so that brothers can be told from a brother and a |
| 882 |
|
| sister. |
| 883 |
|
| |
| 884 |
|
| The samples are either all those of one VCF, or each the one sample of a VCF |
| 885 |
|
| of its own. One VCF is read a record at a time and nothing kept but the |
| 886 |
|
| counts: each sample's call at an autosomal site sets a bit in one of three |
| 887 |
|
| words, for 0, 1 and 2 ALTs, and at the end of a block of 64 sites each pair's |
| 888 |
|
| counts are taken from those words by popcount, 8 per pair, where taking them a |
| 889 |
|
| site at a time would be 64 tests of the pair's two calls. Own VCFs are each |
| 890 |
|
| read into parent_sites, as parent() reads them, and all of them held, and |
| 891 |
|
| then merged in one pass, which applies parent_merge()'s rules to each pair. |
| 892 |
|
| The files are opened by perl one at a time, so that only one is open. |
| 893 |
|
| |
| 894 |
|
| A pair's counts are REL_T UVs, in the order rel_pair() numbers the pairs: t[1] |
| 895 |
|
| to t[8] of parent(), the sites by the first sample's ALT count and then the |
| 896 |
|
| second's, flattened as t[3 * first + second] -- t[0], where both are hom-REF, |
| 897 |
|
| is not a site compared, and is not kept -- and then, for own VCFs, in[0] to |
| 898 |
|
| in[2] of parent_merge(): the sites in both files, in the first's alone and in |
| 899 |
|
| the second's alone. For one VCF those three are left 0 and perl fills them.*/ |
| 900 |
|
| #define REL_T 11 |
| 901 |
|
| |
| 902 |
|
| /*The number of pair (i, j), i < j, of n samples, when they are taken in the |
| 903 |
|
| order (0, 1), (0, 2) .. (0, n - 1), (1, 2) ..*/ |
| 904 |
90107 |
| static size_t rel_pair(size_t n, size_t i, size_t j) { |
| 905 |
90107 |
| return i * (2 * n - i - 1) / 2 + (j - i - 1); |
| 906 |
|
| } |
| 907 |
|
| |
| 908 |
|
| /*The pairs of n samples, which dies when their counters could not be |
| 909 |
|
| addressed.*/ |
| 910 |
110 |
| static size_t rel_pairs(pTHX_ size_t n) { |
| 911 |
110 |
▶ | if (n < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 2) return 0; |
0 |
110 |
110 |
|
| 912 |
110 |
▶ | if (n > ((size_t)-1 / sizeof(UV) / REL_T) / n) croak("%s: %" UVuf " samples make too many pairs to count", __func__, (UV)n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > ((size_t)-1 / sizeof(UV) / REL_T) / n) croak("%s: %" UVuf " samples make too many pairs to count", __func__, (UV)n); |
0 |
110 |
110 |
|
| 913 |
110 |
| return n * (n - 1) / 2; |
| 914 |
|
| } |
| 915 |
|
| |
| 916 |
|
| /*The 1 bits in w, by the SWAR count of Sean Eron Anderson's "Bit Twiddling |
| 917 |
|
| Hacks" (graphics.stanford.edu/~seander/bithacks.html, section |
| 918 |
|
| CountBitsSetParallel), which needs no compiler builtin and no POPCNT |
| 919 |
|
| instruction.*/ |
| 920 |
93976 |
| static unsigned short int rel_popcount(uint64_t w) { |
| 921 |
93976 |
| w -= (w >> 1) & UINT64_C(0x5555555555555555); |
| 922 |
93976 |
| w = (w & UINT64_C(0x3333333333333333)) + ((w >> 2) & UINT64_C(0x3333333333333333)); |
| 923 |
93976 |
| w = (w + (w >> 4)) & UINT64_C(0x0f0f0f0f0f0f0f0f); |
| 924 |
93976 |
| return (unsigned short int)((w * UINT64_C(0x0101010101010101)) >> 56); |
| 925 |
|
| } |
| 926 |
|
| |
| 927 |
|
| typedef struct { |
| 928 |
|
| vcf_reader r; // first, so that the vcf_reader * that the callbacks are given is this |
| 929 |
|
| uint64_t *plane; // 3 words a sample: bit k of plane[3 * s + c] is set when sample s has c ALTs at the block's site k |
| 930 |
|
| unsigned short int k; // the block's sites so far, 0 to 63 |
| 931 |
|
| UV *t; // REL_T counters a pair |
| 932 |
|
| UV *sex; // SEX_FIELDS counters a sample |
| 933 |
|
| } rel_reader; |
| 934 |
|
| |
| 935 |
|
| /*One VCF: its counters, once the #CHROM line has said how many samples there |
| 936 |
|
| are.*/ |
| 937 |
30 |
| static void rel_header_one(pTHX_ vcf_reader *r) { |
| 938 |
30 |
| rel_reader *t = (rel_reader*)r; |
| 939 |
30 |
▶ | if (r->n < 2) croak("%s: %s has 1 sample, and relatedness compares two or more; give each person's VCF, as relatedness($file, $file2)", __func__, r->name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n < 2) croak("%s: %s has 1 sample, and relatedness compares two or more; give each person's VCF, as relatedness($file, $file2)", __func__, r->name); |
1 |
29 |
30 |
|
| 940 |
29 |
| size_t pairs = rel_pairs(aTHX_ r->n); |
| 941 |
29 |
▶ | Newxz(t->plane, 3 * r->n, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t->plane, 3 * r->n, uint64_t); |
0 |
29 |
29 |
|
| 942 |
29 |
| SAVEFREEPV(t->plane); // these three at the caller's LEAVE, croak or not; see vcf_read()'s block |
| 943 |
29 |
▶ | Newxz(t->t, pairs * REL_T, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t->t, pairs * REL_T, UV); |
0 |
29 |
29 |
|
| 944 |
29 |
| SAVEFREEPV(t->t); |
| 945 |
29 |
▶ | Newxz(t->sex, r->n * SEX_FIELDS, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t->sex, r->n * SEX_FIELDS, UV); |
0 |
29 |
29 |
|
| 946 |
29 |
| SAVEFREEPV(t->sex); |
| 947 |
29 |
| } |
| 948 |
|
| |
| 949 |
|
| /*One VCF: the block's sites added to every pair's counts, and the block |
| 950 |
|
| emptied.*/ |
| 951 |
739 |
| static void rel_flush(rel_reader *t) { |
| 952 |
739 |
| const size_t n = t->r.n; |
| 953 |
739 |
| const uint64_t *pl = t->plane; |
| 954 |
739 |
| UV *c = t->t; |
| 955 |
4262 |
▶ | for (size_t i = 0; i + 1 < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i + 1 < n; i++) { |
3523 |
739 |
4262 |
|
| 956 |
3523 |
| const uint64_t a0 = pl[3 * i], a1 = pl[3 * i + 1], a2 = pl[3 * i + 2]; |
| 957 |
15270 |
▶ | for (size_t j = i + 1; j < n; j++, c += REL_T) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t j = i + 1; j < n; j++, c += REL_T) { |
11747 |
3523 |
15270 |
|
| 958 |
11747 |
| const uint64_t *b = pl + 3 * j; |
| 959 |
11747 |
| c[0] += rel_popcount(a0 & b[1]); // c[3 * first + second - 1] |
| 960 |
11747 |
| c[1] += rel_popcount(a0 & b[2]); |
| 961 |
11747 |
| c[2] += rel_popcount(a1 & b[0]); |
| 962 |
11747 |
| c[3] += rel_popcount(a1 & b[1]); |
| 963 |
11747 |
| c[4] += rel_popcount(a1 & b[2]); |
| 964 |
11747 |
| c[5] += rel_popcount(a2 & b[0]); |
| 965 |
11747 |
| c[6] += rel_popcount(a2 & b[1]); |
| 966 |
11747 |
| c[7] += rel_popcount(a2 & b[2]); |
| 967 |
|
| } |
| 968 |
|
| } |
| 969 |
739 |
▶ | Zero(t->plane, 3 * n, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Zero(t->plane, 3 * n, uint64_t); |
0 |
739 |
739 |
|
| 970 |
739 |
| t->k = 0; |
| 971 |
739 |
| } |
| 972 |
|
| |
| 973 |
|
| /*One VCF: the record p..e, an autosomal site for the pairs or an X or Y one |
| 974 |
|
| for each sample's sex.*/ |
| 975 |
47270 |
| static void rel_record_one(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 976 |
47270 |
| rel_reader *t = (rel_reader*)r; |
| 977 |
47270 |
| const char *f = vcf_field_end(p, e); |
| 978 |
47270 |
| unsigned short int chrom = vcf_chrom(p, f); |
| 979 |
48240 |
▶ | if (chrom == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom == 0) return; |
63 |
47207 |
47270 |
|
| 980 |
|
| const char *col[9]; |
| 981 |
47207 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 982 |
|
| size_t gt, dp; |
| 983 |
47207 |
| p = format_end + 1; |
| 984 |
47207 |
▶ | if (chrom > 22) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom > 22) { |
866 |
46341 |
47207 |
|
| 985 |
866 |
▶ | if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
0 |
866 |
866 |
|
| 986 |
5043 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
4178 |
865 |
5043 |
|
| 987 |
4178 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 988 |
4177 |
| sex_count(r, t->sex + i * SEX_FIELDS, chrom, p, se, gt, dp); |
| 989 |
4177 |
| p = se + 1; |
| 990 |
|
| } |
| 991 |
865 |
| vcf_samples_end(aTHX_ r, p, e); |
| 992 |
865 |
| return; |
| 993 |
|
| } |
| 994 |
46341 |
▶ | if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
46320 |
21 |
46341 |
| if (!vcf_base(col[3], col[4] - 1) || !vcf_base(col[4], col[5] - 1)) return; |
63 |
46257 |
46320 |
|
| 995 |
46257 |
| vcf_format(col[8], format_end, >, &dp); |
| 996 |
46257 |
▶ | if (gt == VCF_ABSENT) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gt == VCF_ABSENT) return; |
21 |
46236 |
46257 |
|
| 997 |
46236 |
| const uint64_t bit = (uint64_t)1 << t->k; |
| 998 |
313610 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
267374 |
46236 |
313610 |
|
| 999 |
267374 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 1000 |
267374 |
| unsigned short int code = parent_code(r, p, se, gt, dp); |
| 1001 |
267374 |
▶ | if (code != PARENT_NONE) t->plane[3 * i + code] |= bit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (code != PARENT_NONE) t->plane[3 * i + code] |= bit; |
239102 |
28272 |
267374 |
|
| 1002 |
267374 |
| p = se + 1; |
| 1003 |
|
| } |
| 1004 |
46236 |
| vcf_samples_end(aTHX_ r, p, e); |
| 1005 |
46235 |
▶ | if (++t->k == 64) rel_flush(t); |
Branch: 1 branch
| Branch | True | False | Total |
| if (++t->k == 64) rel_flush(t); |
712 |
45523 |
46235 |
|
| 1006 |
|
| } |
| 1007 |
|
| |
| 1008 |
|
| typedef struct { |
| 1009 |
|
| parent_reader p; // first, so that the vcf_reader * that the callbacks are given is this, and parent_record_own() can read it |
| 1010 |
|
| UV *sex; // the SEX_FIELDS counters of the file's one sample |
| 1011 |
|
| HV *seen; // every file's sample so far => that file's name |
| 1012 |
|
| AV *names; // every file's sample so far, in the files' order |
| 1013 |
|
| } rel_own_reader; |
| 1014 |
|
| |
| 1015 |
|
| /*Own VCF: one sample, or it isn't, and not one that an earlier file named.*/ |
| 1016 |
73 |
| static void rel_header_own(pTHX_ vcf_reader *r) { |
| 1017 |
73 |
| rel_own_reader *t = (rel_own_reader*)r; |
| 1018 |
73 |
▶ | if (r->n != 1) croak("%s: %s has %" UVuf " samples, and each of several VCFs must have one; give a VCF of several samples alone, as relatedness($file)", __func__, r->name, (UV)r->n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n != 1) croak("%s: %s has %" UVuf " samples, and each of several VCFs must have one; give a VCF of several samples alone, as relatedness($file)", __func__, r->name, (UV)r->n); |
1 |
72 |
73 |
|
| 1019 |
72 |
| SV **sample = av_fetch(r->names, 0, 0); |
| 1020 |
72 |
▶ | if (!sample) croak("%s: the sample of %s has lost its name", __func__, r->name); // cannot happen: vcf_header() pushed it |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sample) croak("%s: the sample of %s has lost its name", __func__, r->name); // cannot happen: vcf_header() pushed it |
0 |
72 |
72 |
|
| 1021 |
72 |
| HE *he = hv_fetch_ent(t->seen, *sample, 0, 0); |
| 1022 |
72 |
▶ | if (he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (he) { |
1 |
71 |
72 |
|
| 1023 |
2 |
▶ | const char *first = SvPV_nolen(HeVAL(he)), *name = SvPV_nolen(*sample); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *first = SvPV_nolen(HeVAL(he)), *name = SvPV_nolen(*sample); |
1 |
0 |
1 |
|
| 1024 |
1 |
| croak("%s: %s and %s both name their sample \"%s\"", __func__, first, r->name, name); |
| 1025 |
|
| } |
| 1026 |
71 |
| (void)hv_store_ent(t->seen, *sample, newSVpv(r->name, 0), 0); |
| 1027 |
71 |
| av_push(t->names, newSVsv(*sample)); |
| 1028 |
71 |
| } |
| 1029 |
|
| |
| 1030 |
|
| /*Own VCF: the record p..e, counted for the sample's sex on X and Y and kept |
| 1031 |
|
| as a site, as parent() keeps it, on the autosomes.*/ |
| 1032 |
97751 |
| static void rel_record_own(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 1033 |
97751 |
| const char *f = vcf_field_end(p, e); |
| 1034 |
97751 |
| unsigned short int chrom = vcf_chrom(p, f); |
| 1035 |
97751 |
▶ | if (chrom != 23 && chrom != 24) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (chrom != 23 && chrom != 24) { |
97713 |
38 |
97751 |
| if (chrom != 23 && chrom != 24) { |
97693 |
20 |
97713 |
|
| 1036 |
97693 |
| parent_record_own(aTHX_ r, p, e); |
| 1037 |
97689 |
| return; |
| 1038 |
|
| } |
| 1039 |
|
| const char *col[9]; |
| 1040 |
58 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 1041 |
|
| size_t gt, dp; |
| 1042 |
58 |
▶ | if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sex_site(aTHX_ r, chrom, col, format_end, >, &dp)) return; |
0 |
58 |
58 |
|
| 1043 |
58 |
| p = format_end + 1; |
| 1044 |
58 |
| const char *se = vcf_sample(aTHX_ r, p, e, 0); |
| 1045 |
58 |
| vcf_samples_end(aTHX_ r, se + 1, e); |
| 1046 |
57 |
| sex_count(r, ((rel_own_reader*)r)->sex, chrom, p, se, gt, dp); |
| 1047 |
|
| } |
| 1048 |
|
| |
| 1049 |
|
| //What a file has at the position rel_merge() is comparing |
| 1050 |
|
| enum { |
| 1051 |
|
| REL_ABSENT, // no site: its sample is hom-REF there |
| 1052 |
|
| REL_PRESENT, // one site, whose call counts |
| 1053 |
|
| REL_UNUSABLE // more than one, a call that does not count, or more than one ALT: every pair with its sample leaves the position out |
| 1054 |
|
| }; |
| 1055 |
|
| |
| 1056 |
|
| /*Own VCFs: the sites of the n files in s, each sorted, counted into t, pair by |
| 1057 |
|
| pair, by parent_merge()'s rules. Each position is found by looking at every |
| 1058 |
|
| file's next site, which is n steps, and then counted for each file that |
| 1059 |
|
| carries the ALT there against every other file, so that a pair where both are |
| 1060 |
|
| hom-REF costs nothing.*/ |
| 1061 |
18 |
| static void rel_merge(pTHX_ parent_sites *const *s, size_t n, UV *t) { |
| 1062 |
|
| size_t *at, *carrier; // each file's next site; the files that carry the ALT at this position |
| 1063 |
|
| uint64_t *key; // each file's site at this position, when REL_PRESENT |
| 1064 |
|
| unsigned short int *state; // each file's REL_ABSENT, REL_PRESENT or REL_UNUSABLE at this position |
| 1065 |
18 |
▶ | Newxz(at, n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(at, n, size_t); |
0 |
18 |
18 |
|
| 1066 |
18 |
| SAVEFREEPV(at); // these four at the caller's LEAVE, croak or not |
| 1067 |
18 |
▶ | Newx(carrier, n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(carrier, n, size_t); |
0 |
18 |
18 |
|
| 1068 |
18 |
| SAVEFREEPV(carrier); |
| 1069 |
18 |
▶ | Newx(key, n, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(key, n, uint64_t); |
0 |
18 |
18 |
|
| 1070 |
18 |
| SAVEFREEPV(key); |
| 1071 |
18 |
▶ | Newx(state, n, unsigned short int); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(state, n, unsigned short int); |
0 |
18 |
18 |
|
| 1072 |
18 |
| SAVEFREEPV(state); |
| 1073 |
32216 |
| for (;;) { |
| 1074 |
32234 |
| uint64_t pos = UINT64_MAX; // the position to compare, as the sites' bits from PARENT_POS_SHIFT up |
| 1075 |
128790 |
▶ | for (size_t f = 0; f < n; f++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t f = 0; f < n; f++) { |
96556 |
32234 |
128790 |
|
| 1076 |
96556 |
▶ | if (at[f] < s[f]->n && s[f]->site[at[f]] >> PARENT_POS_SHIFT < pos) pos = s[f]->site[at[f]] >> PARENT_POS_SHIFT; |
Branch: 2 branches
| Branch | True | False | Total |
| if (at[f] < s[f]->n && s[f]->site[at[f]] >> PARENT_POS_SHIFT < pos) pos = s[f]->site[at[f]] >> PARENT_POS_SHIFT; |
95680 |
876 |
96556 |
| if (at[f] < s[f]->n && s[f]->site[at[f]] >> PARENT_POS_SHIFT < pos) pos = s[f]->site[at[f]] >> PARENT_POS_SHIFT; |
36320 |
59360 |
95680 |
|
| 1077 |
|
| } |
| 1078 |
32234 |
▶ | if (pos == UINT64_MAX) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos == UINT64_MAX) break; |
18 |
32216 |
32234 |
|
| 1079 |
32216 |
| size_t m = 0; // carriers |
| 1080 |
128718 |
▶ | for (size_t f = 0; f < n; f++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t f = 0; f < n; f++) { |
96502 |
32216 |
128718 |
|
| 1081 |
96502 |
| size_t k = 0; |
| 1082 |
177473 |
▶ | while (at[f] + k < s[f]->n && s[f]->site[at[f] + k] >> PARENT_POS_SHIFT == pos) k++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (at[f] + k < s[f]->n && s[f]->site[at[f] + k] >> PARENT_POS_SHIFT == pos) k++; |
176597 |
876 |
177473 |
| while (at[f] + k < s[f]->n && s[f]->site[at[f] + k] >> PARENT_POS_SHIFT == pos) k++; |
80971 |
95626 |
176597 |
|
| 1083 |
96502 |
▶ | if (!k) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) { |
15542 |
80960 |
96502 |
|
| 1084 |
15542 |
| state[f] = REL_ABSENT; |
| 1085 |
15542 |
| continue; |
| 1086 |
|
| } |
| 1087 |
80960 |
| key[f] = s[f]->site[at[f]]; |
| 1088 |
80960 |
| at[f] += k; |
| 1089 |
80960 |
▶ | state[f] = k > 1 || PARENT_CODE(key[f]) == PARENT_NONE || PARENT_ALT(key[f]) == 4 ? REL_UNUSABLE : REL_PRESENT; |
Branch: 3 branches
| Branch | True | False | Total |
| state[f] = k > 1 || PARENT_CODE(key[f]) == PARENT_NONE || PARENT_ALT(key[f]) == 4 ? REL_UNUSABLE : REL_PRESENT; |
80949 |
11 |
80960 |
| state[f] = k > 1 || PARENT_CODE(key[f]) == PARENT_NONE || PARENT_ALT(key[f]) == 4 ? REL_UNUSABLE : REL_PRESENT; |
76441 |
4508 |
80949 |
| state[f] = k > 1 || PARENT_CODE(key[f]) == PARENT_NONE || PARENT_ALT(key[f]) == 4 ? REL_UNUSABLE : REL_PRESENT; |
0 |
76441 |
76441 |
|
| 1090 |
80960 |
▶ | if (state[f] == REL_PRESENT && PARENT_CODE(key[f])) carrier[m++] = f; |
Branch: 2 branches
| Branch | True | False | Total |
| if (state[f] == REL_PRESENT && PARENT_CODE(key[f])) carrier[m++] = f; |
76441 |
4519 |
80960 |
| if (state[f] == REL_PRESENT && PARENT_CODE(key[f])) carrier[m++] = f; |
72201 |
4240 |
76441 |
|
| 1091 |
|
| } |
| 1092 |
104417 |
▶ | for (size_t q = 0; q < m; q++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t q = 0; q < m; q++) { |
72201 |
32216 |
104417 |
|
| 1093 |
72201 |
| const size_t i = carrier[q]; |
| 1094 |
72201 |
| const unsigned short int ci = PARENT_CODE(key[i]); |
| 1095 |
305577 |
▶ | for (size_t j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t j = 0; j < n; j++) { |
233376 |
72201 |
305577 |
|
| 1096 |
233376 |
▶ | if (j == i || state[j] == REL_UNUSABLE) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (j == i || state[j] == REL_UNUSABLE) continue; |
161175 |
72201 |
233376 |
| if (j == i || state[j] == REL_UNUSABLE) continue; |
9097 |
152078 |
161175 |
|
| 1097 |
152078 |
| const bool both = state[j] == REL_PRESENT; |
| 1098 |
152078 |
▶ | const unsigned short int cj = both ? PARENT_CODE(key[j]) : 0; // a site absent from a file is hom-REF there |
Branch: 1 branch
| Branch | True | False | Total |
| const unsigned short int cj = both ? PARENT_CODE(key[j]) : 0; // a site absent from a file is hom-REF there |
128138 |
23940 |
152078 |
|
| 1099 |
152078 |
▶ | if (cj && j < i) continue; // counted when j was the carrier |
Branch: 2 branches
| Branch | True | False | Total |
| if (cj && j < i) continue; // counted when j was the carrier |
123898 |
28180 |
152078 |
| if (cj && j < i) continue; // counted when j was the carrier |
61949 |
61949 |
123898 |
|
| 1100 |
90129 |
▶ | if (both && PARENT_ALLELES(key[i]) != PARENT_ALLELES(key[j])) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (both && PARENT_ALLELES(key[i]) != PARENT_ALLELES(key[j])) continue; |
66189 |
23940 |
90129 |
| if (both && PARENT_ALLELES(key[i]) != PARENT_ALLELES(key[j])) continue; |
22 |
66167 |
66189 |
|
| 1101 |
90107 |
▶ | UV *c = t + REL_T * (i < j ? rel_pair(n, i, j) : rel_pair(n, j, i)); |
Branch: 1 branch
| Branch | True | False | Total |
| UV *c = t + REL_T * (i < j ? rel_pair(n, i, j) : rel_pair(n, j, i)); |
76076 |
14031 |
90107 |
|
| 1102 |
90107 |
▶ | c[i < j ? 3 * ci + cj - 1 : 3 * cj + ci - 1]++; |
Branch: 1 branch
| Branch | True | False | Total |
| c[i < j ? 3 * ci + cj - 1 : 3 * cj + ci - 1]++; |
76076 |
14031 |
90107 |
|
| 1103 |
90107 |
▶ | c[8 + (both ? 0 : i < j ? 1 : 2)]++; |
Branch: 2 branches
| Branch | True | False | Total |
| c[8 + (both ? 0 : i < j ? 1 : 2)]++; |
66167 |
23940 |
90107 |
| c[8 + (both ? 0 : i < j ? 1 : 2)]++; |
12021 |
11919 |
23940 |
|
| 1104 |
|
| } |
| 1105 |
|
| } |
| 1106 |
|
| } |
| 1107 |
18 |
| } |
| 1108 |
|
| |
| 1109 |
|
| /*The n samples' pair counters t as one string of UVs, for perl to unpack with |
| 1110 |
|
| "J". For 1,000 samples, an array of a scalar per counter took 226 MB where |
| 1111 |
|
| this takes 44 MB (2026-10-19).*/ |
| 1112 |
45 |
| static SV *rel_counts(pTHX_ const UV *t, size_t n) { |
| 1113 |
45 |
| return newSVpvn((const char*)t, rel_pairs(aTHX_ n) * REL_T * sizeof(UV)); |
| 1114 |
|
| } |
| 1115 |
|
| |
| 1116 |
|
| /*The genotype comparison behind concordance(). |
| 1117 |
|
| |
| 1118 |
|
| The method is ~/Scripts/old/x.low_ram_local_concordance.pl's (its |
| 1119 |
|
| assay_concordance(), concordance() and metrics(), as they stood on |
| 1120 |
|
| 2026-10-19), which compared each sample of a directory of test VCFs with the |
| 1121 |
|
| same sample in a directory of truth VCFs. Every record of the test is compared, |
| 1122 |
|
| sample by sample, with the truth's record of the same site, and a site that |
| 1123 |
|
| the truth lacks is a no-call there; a site that the truth has and the test |
| 1124 |
|
| lacks is not compared at all. Each comparison is one of the 45 kinds of |
| 1125 |
|
| CONC_NAME, and the metrics are taken from their counts as metrics() took them. |
| 1126 |
|
| |
| 1127 |
|
| It departs from that script where the script died or could not match: |
| 1128 |
|
| - A site is CHROM, POS, REF and ALT. The script also took ID, so that "rs123" |
| 1129 |
|
| in one file and "." in the other were two sites, and it dropped a "chr" from |
| 1130 |
|
| the truth's CHROM alone, so that two files that both had one shared no site; |
| 1131 |
|
| here "chr", in any case, is dropped from both. |
| 1132 |
|
| - A call is compared as its alleles, in either order, so that "1/0", "0|1" and |
| 1133 |
|
| "0/1" are one call; the script compared GT strings, and died on the first two. |
| 1134 |
|
| - A call missing in both ("." and "./.") is concordant_no_call, where the |
| 1135 |
|
| script made it discordant_no_call_to_no_call, a kind outside its 45. |
| 1136 |
|
| - A partly missing call ("./1"), one of more than two alleles, and one whose |
| 1137 |
|
| DP is below min_depth are each a no-call, where the script died on the first |
| 1138 |
|
| two and had no min_depth. |
| 1139 |
|
| - The metrics' true positives and true negatives are corrected, and a |
| 1140 |
|
| discordance of two ALT-carrying calls is a false positive and a false |
| 1141 |
|
| negative, as the comment above CONC_TP says. |
| 1142 |
|
| |
| 1143 |
|
| The test is read first, and every site of it kept with its calls for the |
| 1144 |
|
| samples compared, and then the truth is read a record at a time and looked up |
| 1145 |
|
| among them. RAM thus goes with the test's sites, which for the usual input, a |
| 1146 |
|
| panel against a genome, are the fewer, and a truth record that matches no |
| 1147 |
|
| test site costs no more than finding its end.*/ |
| 1148 |
|
| |
| 1149 |
|
| //What one call is. The order is that of the classes' names, which CONC_NAME's kinds are sorted by |
| 1150 |
|
| enum { CONC_HAPLOID_ALT, CONC_HAPLOID_REF, CONC_HET_ALT, CONC_HET_REF, CONC_HOM_ALT, CONC_HOM_REF, CONC_NO_CALL }; |
| 1151 |
|
| |
| 1152 |
|
| /*The kinds of comparison, truth first and then test, in the order and with the |
| 1153 |
|
| names of the script's @concordance_types.*/ |
| 1154 |
|
| #define CONC_KINDS 45 |
| 1155 |
|
| static const char *const CONC_NAME[CONC_KINDS] = { |
| 1156 |
|
| "concordant_diploid_ref_to_haploid_ref", "concordant_haploid_alt", "concordant_haploid_alt_to_hom_alt", |
| 1157 |
|
| "concordant_haploid_ref", "concordant_haploid_ref_to_diploid_ref", "concordant_het_alt", "concordant_het_ref", |
| 1158 |
|
| "concordant_hom_alt", "concordant_hom_alt_to_diploid_alt", "concordant_hom_ref", "concordant_no_call", |
| 1159 |
|
| "discordant_haploid_alt_to_haploid_alt", "discordant_haploid_alt_to_haploid_ref", "discordant_haploid_alt_to_het_alt", |
| 1160 |
|
| "discordant_haploid_alt_to_het_ref", "discordant_haploid_alt_to_hom_alt", "discordant_haploid_alt_to_hom_ref", |
| 1161 |
|
| "discordant_haploid_ref_to_haploid_alt", "discordant_haploid_ref_to_het_alt", "discordant_haploid_ref_to_het_ref", |
| 1162 |
|
| "discordant_haploid_ref_to_hom_alt", |
| 1163 |
|
| "discordant_het_alt_to_haploid_alt", "discordant_het_alt_to_haploid_ref", "discordant_het_alt_to_het_alt", |
| 1164 |
|
| "discordant_het_alt_to_het_ref", "discordant_het_alt_to_hom_alt", "discordant_het_alt_to_hom_ref", |
| 1165 |
|
| "discordant_het_ref_to_haploid_alt", "discordant_het_ref_to_haploid_ref", "discordant_het_ref_to_het_alt", |
| 1166 |
|
| "discordant_het_ref_to_het_ref", "discordant_het_ref_to_hom_alt", "discordant_het_ref_to_hom_ref", |
| 1167 |
|
| "discordant_hom_alt_to_haploid_alt", "discordant_hom_alt_to_haploid_ref", "discordant_hom_alt_to_het_alt", |
| 1168 |
|
| "discordant_hom_alt_to_het_ref", "discordant_hom_alt_to_hom_alt", "discordant_hom_alt_to_hom_ref", |
| 1169 |
|
| "discordant_hom_ref_to_haploid_alt", "discordant_hom_ref_to_het_alt", "discordant_hom_ref_to_het_ref", |
| 1170 |
|
| "discordant_hom_ref_to_hom_alt", |
| 1171 |
|
| "gained_site", "lost_site" |
| 1172 |
|
| }; |
| 1173 |
|
| //The kinds that conc_kind() names, as indexes into CONC_NAME |
| 1174 |
|
| enum { |
| 1175 |
|
| CONC_DIPLOID_REF_TO_HAPLOID_REF = 0, |
| 1176 |
|
| CONC_HAPLOID_ALT_TO_HOM_ALT = 2, |
| 1177 |
|
| CONC_HAPLOID_REF_TO_DIPLOID_REF = 4, |
| 1178 |
|
| CONC_HOM_ALT_TO_DIPLOID_ALT = 8, |
| 1179 |
|
| CONC_DISCORDANT = 11, // the first discordant_ kind; every kind before it is concordant_ |
| 1180 |
|
| CONC_GAINED = 43, // the truth a no-call, the test not |
| 1181 |
|
| CONC_LOST = 44 // the test a no-call, the truth not |
| 1182 |
|
| }; |
| 1183 |
|
| //concordant_<class>, by the class of the two calls, which are the same |
| 1184 |
|
| static const unsigned short int CONC_SAME[7] = { 1, 3, 5, 6, 7, 9, 10 }; |
| 1185 |
|
| /*discordant_<truth's class>_to_<test's class>, by the two classes. -1 marks |
| 1186 |
|
| the four pairs of reference calls, which are concordant whatever their |
| 1187 |
|
| ploidy.*/ |
| 1188 |
|
| static const short int CONC_DIFFERENT[6][6] = { |
| 1189 |
|
| { 11, 12, 13, 14, 15, 16 }, |
| 1190 |
|
| { 17, -1, 18, 19, 20, -1 }, |
| 1191 |
|
| { 21, 22, 23, 24, 25, 26 }, |
| 1192 |
|
| { 27, 28, 29, 30, 31, 32 }, |
| 1193 |
|
| { 33, 34, 35, 36, 37, 38 }, |
| 1194 |
|
| { 39, -1, 40, 41, 42, -1 } |
| 1195 |
|
| }; |
| 1196 |
|
| |
| 1197 |
|
| /*The sets of kinds that metrics() summed, as bits of CONC_NAME's indexes, |
| 1198 |
|
| written out so that the rule cannot drift with the names. Each is what the |
| 1199 |
|
| script's regex picked out of @concordance_types, but for CONC_TP, CONC_TN and |
| 1200 |
|
| CONC_MISMATCH, which correct it (since 2026-10-19): |
| 1201 |
|
| - CONC_TP, the true positives, is every concordant kind whose call carries an |
| 1202 |
|
| ALT. The script's /^conc\w+(?:alt|hom)/ took concordant_hom_ref, by the "hom" |
| 1203 |
|
| in it, so that a hom-REF site counted as a true positive and a true negative |
| 1204 |
|
| both, and it left out concordant_het_ref, the commonest true positive. |
| 1205 |
|
| - CONC_TN, the true negatives, is every concordant kind of reference calls, |
| 1206 |
|
| whatever the ploidy of either. The script's tn_site, |
| 1207 |
|
| /^concordant_(?:hom|haploid)\w+ref$/, left out |
| 1208 |
|
| concordant_diploid_ref_to_haploid_ref, the mirror of a kind it took; and its |
| 1209 |
|
| tn_genotype was concordant_hom_ref alone, though CONC_FP_REF counts a |
| 1210 |
|
| haploid-REF truth called otherwise, so that specificity gave no credit for a |
| 1211 |
|
| haploid-REF truth called right, as on a male's X. Here tn_genotype is |
| 1212 |
|
| tn_site. |
| 1213 |
|
| - CONC_MISMATCH is every discordant kind where both calls carry an ALT (0/1 |
| 1214 |
|
| against 1/1, or 0/1 against 0/2), which the script counted in neither |
| 1215 |
|
| fp_genotype, which was CONC_FP_REF alone, nor fn_genotype, which was |
| 1216 |
|
| CONC_FN_REF alone, so that it changed no precision or recall. It is now in |
| 1217 |
|
| both, as benchmarking tools count it: the test's call is wrong, and the |
| 1218 |
|
| truth's is missed. Specificity still takes CONC_FP_REF alone, since only a |
| 1219 |
|
| reference truth is a negative.*/ |
| 1220 |
|
| #define CONC_BIT(k) ((uint64_t)1 << (k)) |
| 1221 |
|
| #define CONC_TP (CONC_BIT(1) | CONC_BIT(2) | CONC_BIT(5) | CONC_BIT(6) | CONC_BIT(7) | CONC_BIT(8)) |
| 1222 |
|
| #define CONC_TN (CONC_BIT(0) | CONC_BIT(3) | CONC_BIT(4) | CONC_BIT(9)) |
| 1223 |
|
| #define CONC_FP_REF (CONC_BIT(17) | CONC_BIT(18) | CONC_BIT(19) | CONC_BIT(20) | CONC_BIT(39) | CONC_BIT(40) | CONC_BIT(41) | CONC_BIT(42)) // /^discordant_(haploid|hom)_ref_\w+/: a reference truth, the test carrying an ALT |
| 1224 |
|
| #define CONC_FN_REF (CONC_BIT(12) | CONC_BIT(16) | CONC_BIT(22) | CONC_BIT(26) | CONC_BIT(28) | CONC_BIT(32) | CONC_BIT(34) | CONC_BIT(38)) // /^discordant_\w+_(?:hom|haploid)_ref$/: the truth carrying an ALT, a reference test |
| 1225 |
|
| #define CONC_MISMATCH (CONC_BIT(11) | CONC_BIT(13) | CONC_BIT(14) | CONC_BIT(15) | CONC_BIT(21) | CONC_BIT(23) | CONC_BIT(24) | CONC_BIT(25) \ |
| 1226 |
|
| | CONC_BIT(27) | CONC_BIT(29) | CONC_BIT(30) | CONC_BIT(31) | CONC_BIT(33) | CONC_BIT(35) | CONC_BIT(36) | CONC_BIT(37)) |
| 1227 |
|
| #define CONC_AGREE (CONC_BIT(CONC_DISCORDANT - 1) - 1) // /^concordant_\w+(?:alt|ref)$/: every concordant_ kind but concordant_no_call |
| 1228 |
|
| #define CONC_CALLED (((CONC_BIT(CONC_GAINED) - 1) & ~CONC_BIT(CONC_DISCORDANT - 1))) // /(?:alt|ref)$/: every kind but concordant_no_call, gained_site and lost_site |
| 1229 |
|
| |
| 1230 |
|
| /*One sample's call. Alleles are held in 16 bits, which keeps a call to 6 |
| 1231 |
|
| bytes, two of which each test site keeps for each sample compared; conc_gt() |
| 1232 |
|
| dies on an allele above 65535, which no VCF of real data has.*/ |
| 1233 |
|
| typedef struct { |
| 1234 |
|
| uint16_t a, b; // the alleles, in GT's order; b is 0 for a haploid call |
| 1235 |
|
| unsigned char n; // 0 = no-call, 1 = haploid, 2 = diploid |
| 1236 |
|
| bool phased; // GT separated them with '|' |
| 1237 |
|
| } conc_call; |
| 1238 |
|
| |
| 1239 |
|
| /*A site of the test.*/ |
| 1240 |
|
| typedef struct { |
| 1241 |
|
| uint64_t hash; // conc_hash()'s, of CHROM, POS, REF and ALT |
| 1242 |
|
| UV pos; |
| 1243 |
|
| size_t text; // where its CHROM to ALT, as the test has them and ended by '\n', start in conc_store's text |
| 1244 |
|
| bool found; // the truth has it |
| 1245 |
|
| } conc_site; |
| 1246 |
|
| |
| 1247 |
|
| /*Everything the test's reading keeps and the truth's reading looks in, in a |
| 1248 |
|
| holder that the scope's destructor frees, since Renew() moves the arrays.*/ |
| 1249 |
|
| typedef struct { |
| 1250 |
|
| char *text; // every site's CHROM to ALT, each ended by '\n' |
| 1251 |
|
| size_t text_n, text_room; |
| 1252 |
|
| conc_site *site; |
| 1253 |
|
| size_t n, room; // sites used and allocated |
| 1254 |
|
| size_t *slot; // an open-addressed table of the sites by hash: a site's index + 1, or 0 for an empty slot |
| 1255 |
|
| size_t slots; // 0, or a power of 2 at least twice n |
| 1256 |
|
| conc_call *call; // 2 * pairs a site: for each pair, the test's call and then the truth's |
| 1257 |
|
| size_t *hit; // the sites that one truth record matches |
| 1258 |
|
| size_t hit_room; |
| 1259 |
|
| conc_call *truth; // a truth record's calls, one a pair |
| 1260 |
|
| size_t pairs; // samples compared |
| 1261 |
|
| size_t *test_col; // each pair's column among the test's samples |
| 1262 |
|
| size_t *truth_col; // and among the truth's |
| 1263 |
|
| size_t *test_pair; // each test column's pair + 1, or 0 when it is not compared |
| 1264 |
|
| size_t *truth_pair; // each truth column's pair + 1, or 0 |
| 1265 |
|
| size_t truth_n; // the truth's samples, as read before the test |
| 1266 |
|
| UV found; // test sites that the truth has |
| 1267 |
|
| } conc_store; |
| 1268 |
|
| |
| 1269 |
61 |
| static void conc_store_free(pTHX_ void *p) { |
| 1270 |
61 |
| conc_store *s = (conc_store*)p; |
| 1271 |
61 |
| Safefree(s->text); |
| 1272 |
61 |
| Safefree(s->site); |
| 1273 |
61 |
| Safefree(s->slot); |
| 1274 |
61 |
| Safefree(s->call); |
| 1275 |
61 |
| Safefree(s->hit); |
| 1276 |
61 |
| Safefree(s->truth); |
| 1277 |
61 |
| Safefree(s->test_col); |
| 1278 |
61 |
| Safefree(s->truth_col); |
| 1279 |
61 |
| Safefree(s->test_pair); |
| 1280 |
61 |
| Safefree(s->truth_pair); |
| 1281 |
61 |
| Safefree(s); |
| 1282 |
61 |
| } |
| 1283 |
|
| |
| 1284 |
|
| typedef struct { |
| 1285 |
|
| vcf_reader r; // first, so that the vcf_reader * that the callbacks are given is this |
| 1286 |
|
| conc_store *s; |
| 1287 |
|
| AV *truth_names; // the test's reader: the truth's samples, in its order |
| 1288 |
|
| } conc_reader; |
| 1289 |
|
| |
| 1290 |
|
| /*CHROM p..f without a "chr" prefix, in any case, as vcf_chrom() drops it; the |
| 1291 |
|
| start is returned.*/ |
| 1292 |
16580 |
| static const char *conc_chrom(const char *p, const char *f) { |
| 1293 |
16580 |
▶ | if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
Branch: 7 branches
| Branch | True | False | Total |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
5 |
16575 |
16580 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
3 |
5 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
0 |
2 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
3 |
5 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
0 |
2 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
3 |
5 |
| if (f - p > 3 && (p[0] == 'c' || p[0] == 'C') && (p[1] == 'h' || p[1] == 'H') && (p[2] == 'r' || p[2] == 'R')) p += 3; |
2 |
0 |
2 |
|
| 1294 |
16580 |
| return p; |
| 1295 |
|
| } |
| 1296 |
|
| |
| 1297 |
|
| /*FNV-1a, 64-bit (Fowler, Noll and Vo; www.isthe.com/chongo/tech/comp/fnv/, |
| 1298 |
|
| read 2026-10-19), of the bytes p..e, continuing from h.*/ |
| 1299 |
144625 |
| static uint64_t conc_fnv(uint64_t h, const char *p, const char *e) { |
| 1300 |
293234 |
▶ | for (; p < e; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < e; p++) { |
148609 |
144625 |
293234 |
|
| 1301 |
148609 |
| h ^= (unsigned char)*p; |
| 1302 |
148609 |
| h *= UINT64_C(0x100000001b3); |
| 1303 |
|
| } |
| 1304 |
144625 |
| return h; |
| 1305 |
|
| } |
| 1306 |
|
| |
| 1307 |
|
| /*The hash of a site: its CHROM (less "chr") c..cf, POS, REF r..rf and ALT |
| 1308 |
|
| a..af, with a byte that no field holds between the fields, so that moving a |
| 1309 |
|
| byte from one to the next changes it.*/ |
| 1310 |
11125 |
| static uint64_t conc_hash(const char *c, const char *cf, UV pos, const char *r, const char *rf, const char *a, const char *af) { |
| 1311 |
|
| static const char tab = '\t'; |
| 1312 |
11125 |
| uint64_t h = conc_fnv(UINT64_C(0xcbf29ce484222325), c, cf); |
| 1313 |
11125 |
| h = conc_fnv(h, &tab, &tab + 1); |
| 1314 |
100125 |
▶ | for (unsigned short int i = 0; i < 64; i += 8) { // POS's 8 bytes, low first, whatever the width of a UV |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int i = 0; i < 64; i += 8) { // POS's 8 bytes, low first, whatever the width of a UV |
89000 |
11125 |
100125 |
|
| 1315 |
89000 |
| const char byte = (char)(((uint64_t)pos >> i) & 0xff); |
| 1316 |
89000 |
| h = conc_fnv(h, &byte, &byte + 1); |
| 1317 |
|
| } |
| 1318 |
11125 |
| h = conc_fnv(h, r, rf); |
| 1319 |
11125 |
| h = conc_fnv(h, &tab, &tab + 1); |
| 1320 |
11125 |
| return conc_fnv(h, a, af); |
| 1321 |
|
| } |
| 1322 |
|
| |
| 1323 |
|
| /*Where each of the five fields of site k's text starts, in fld, with fld[5] |
| 1324 |
|
| one past its '\n', so that fld[i + 1] - 1 ends field i. The text has its four |
| 1325 |
|
| tabs and its '\n', as conc_record_test() stored it.*/ |
| 1326 |
5767 |
| static void conc_fields(const conc_store *s, size_t k, const char **fld) { |
| 1327 |
5767 |
| const char *e = s->text + s->text_n; |
| 1328 |
5767 |
| fld[0] = s->text + s->site[k].text; |
| 1329 |
28835 |
▶ | for (unsigned short int i = 1; i < 5; i++) fld[i] = (const char*)memchr(fld[i - 1], '\t', (size_t)(e - fld[i - 1])) + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int i = 1; i < 5; i++) fld[i] = (const char*)memchr(fld[i - 1], '\t', (size_t)(e - fld[i - 1])) + 1; |
23068 |
5767 |
28835 |
|
| 1330 |
5767 |
| fld[5] = (const char*)memchr(fld[4], '\n', (size_t)(e - fld[4])) + 1; |
| 1331 |
5767 |
| } |
| 1332 |
|
| |
| 1333 |
|
| /*Whether two byte ranges are equal.*/ |
| 1334 |
16365 |
| static bool conc_eq(const char *p, const char *pe, const char *q, const char *qe) { |
| 1335 |
16365 |
▶ | return pe - p == qe - q && memcmp(p, q, (size_t)(pe - p)) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return pe - p == qe - q && memcmp(p, q, (size_t)(pe - p)) == 0; |
16365 |
0 |
16365 |
| return pe - p == qe - q && memcmp(p, q, (size_t)(pe - p)) == 0; |
16365 |
0 |
16365 |
|
| 1336 |
|
| } |
| 1337 |
|
| |
| 1338 |
|
| /*The call in the sample field p..e, whose FORMAT has GT at gt and DP at dp, |
| 1339 |
|
| into *c. A GT that FORMAT lacks, an empty or missing one, a partly missing one |
| 1340 |
|
| ("./1"), one of more than two alleles, and a DP below min_depth are each a |
| 1341 |
|
| no-call. Dies on a GT that is not one, naming the sample of column i.*/ |
| 1342 |
12036 |
| static void conc_gt(pTHX_ const vcf_reader *r, const char *p, const char *e, size_t gt, size_t dp, size_t i, conc_call *c) { |
| 1343 |
12036 |
| c->a = c->b = 0; |
| 1344 |
12036 |
| c->n = 0; |
| 1345 |
12036 |
| c->phased = FALSE; |
| 1346 |
|
| const char *s, *sf; |
| 1347 |
12036 |
▶ | if (gt == VCF_ABSENT || vcf_shallow(r, p, e, dp) || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return; |
Branch: 4 branches
| Branch | True | False | Total |
| if (gt == VCF_ABSENT || vcf_shallow(r, p, e, dp) || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return; |
12035 |
1 |
12036 |
| if (gt == VCF_ABSENT || vcf_shallow(r, p, e, dp) || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return; |
12033 |
2 |
12035 |
| if (gt == VCF_ABSENT || vcf_shallow(r, p, e, dp) || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return; |
12033 |
0 |
12033 |
| if (gt == VCF_ABSENT || vcf_shallow(r, p, e, dp) || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return; |
0 |
12033 |
12033 |
|
| 1348 |
12033 |
| const char *g = s; // GT, for messages |
| 1349 |
12033 |
| UV allele[2] = { 0, 0 }; |
| 1350 |
12033 |
| size_t n = 0; // alleles |
| 1351 |
12033 |
| bool missing = FALSE, phased = FALSE; |
| 1352 |
11628 |
| for (;;) { |
| 1353 |
23661 |
| UV v = 0; |
| 1354 |
23661 |
▶ | if (s < sf && *s == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s < sf && *s == '.') { |
23661 |
0 |
23661 |
| if (s < sf && *s == '.') { |
152 |
23509 |
23661 |
|
| 1355 |
152 |
| missing = TRUE; |
| 1356 |
152 |
| s++; |
| 1357 |
|
| } else { |
| 1358 |
23509 |
| const char *d = s; |
| 1359 |
47022 |
▶ | for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
Branch: 3 branches
| Branch | True | False | Total |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
35076 |
11946 |
47022 |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
23525 |
11551 |
35076 |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
23515 |
10 |
23525 |
|
| 1360 |
23515 |
| v = v * 10 + (UV)(*s - '0'); |
| 1361 |
23515 |
▶ | if (v > 65535) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > 65535) { |
2 |
23513 |
23515 |
|
| 1362 |
2 |
| SV **name = av_fetch(r->names, (SSize_t)i, 0); |
| 1363 |
4 |
▶ | croak("%s: %s line %" UVuf " has GT \"%.*s\" for sample \"%s\", whose allele is beyond the 65535 that concordance can compare", __func__, r->name, r->line, (int)(sf - g), g, name ? SvPV_nolen(*name) : ""); |
Branch: 1 branch
| Branch | True | False | Total |
| croak("%s: %s line %" UVuf " has GT \"%.*s\" for sample \"%s\", whose allele is beyond the 65535 that concordance can compare", __func__, r->name, r->line, (int)(sf - g), g, name ? SvPV_nolen(*name) : ""); |
2 |
0 |
2 |
|
| 1364 |
|
| } |
| 1365 |
|
| } |
| 1366 |
23507 |
▶ | if (s == d) break; // not an allele: said below |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == d) break; // not an allele: said below |
6 |
23501 |
23507 |
|
| 1367 |
|
| } |
| 1368 |
23653 |
▶ | if (n < 2) allele[n] = v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 2) allele[n] = v; |
23652 |
1 |
23653 |
|
| 1369 |
23653 |
| n++; |
| 1370 |
23653 |
▶ | if (s == sf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == sf) { |
12021 |
11632 |
23653 |
|
| 1371 |
12021 |
▶ | if (missing || n > 2) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (missing || n > 2) return; |
11945 |
76 |
12021 |
| if (missing || n > 2) return; |
1 |
11944 |
11945 |
|
| 1372 |
11944 |
| c->n = (unsigned char)n; |
| 1373 |
11944 |
| c->a = (uint16_t)allele[0]; |
| 1374 |
11944 |
| c->b = (uint16_t)allele[1]; |
| 1375 |
11944 |
| c->phased = phased; |
| 1376 |
11944 |
| return; |
| 1377 |
|
| } |
| 1378 |
11632 |
▶ | if (*s == '|') phased = TRUE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '|') phased = TRUE; |
4 |
11628 |
11632 |
|
| 1379 |
11628 |
▶ | else if (*s != '/') break; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s != '/') break; |
2 |
11626 |
11628 |
|
| 1380 |
11630 |
▶ | if (++s == sf) break; // "0/" |
Branch: 1 branch
| Branch | True | False | Total |
| if (++s == sf) break; // "0/" |
2 |
11628 |
11630 |
|
| 1381 |
|
| } |
| 1382 |
10 |
| SV **name = av_fetch(r->names, (SSize_t)i, 0); |
| 1383 |
20 |
▶ | croak("%s: %s line %" UVuf " has GT \"%.*s\" for sample \"%s\", which isn't a genotype", __func__, r->name, r->line, (int)(sf - g), g, name ? SvPV_nolen(*name) : ""); |
Branch: 1 branch
| Branch | True | False | Total |
| croak("%s: %s line %" UVuf " has GT \"%.*s\" for sample \"%s\", which isn't a genotype", __func__, r->name, r->line, (int)(sf - g), g, name ? SvPV_nolen(*name) : ""); |
10 |
0 |
10 |
|
| 1384 |
|
| } |
| 1385 |
|
| |
| 1386 |
|
| /*The class of call c.*/ |
| 1387 |
11668 |
| static unsigned short int conc_class(const conc_call *c) { |
| 1388 |
11668 |
▶ | if (c->n == 0) return CONC_NO_CALL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->n == 0) return CONC_NO_CALL; |
93 |
11575 |
11668 |
|
| 1389 |
11575 |
▶ | if (c->n == 1) return c->a ? CONC_HAPLOID_ALT : CONC_HAPLOID_REF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->n == 1) return c->a ? CONC_HAPLOID_ALT : CONC_HAPLOID_REF; |
329 |
11246 |
11575 |
|
| 1390 |
11246 |
▶ | if (c->a == c->b) return c->a ? CONC_HOM_ALT : CONC_HOM_REF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->a == c->b) return c->a ? CONC_HOM_ALT : CONC_HOM_REF; |
3510 |
7736 |
11246 |
| if (c->a == c->b) return c->a ? CONC_HOM_ALT : CONC_HOM_REF; |
2887 |
623 |
3510 |
|
| 1391 |
7736 |
▶ | return c->a && c->b ? CONC_HET_ALT : CONC_HET_REF; |
Branch: 2 branches
| Branch | True | False | Total |
| return c->a && c->b ? CONC_HET_ALT : CONC_HET_REF; |
163 |
7573 |
7736 |
| return c->a && c->b ? CONC_HET_ALT : CONC_HET_REF; |
160 |
3 |
163 |
|
| 1392 |
|
| } |
| 1393 |
|
| |
| 1394 |
|
| /*Whether two calls are the same: both no-calls, or of one ploidy with the same |
| 1395 |
|
| alleles, in either order.*/ |
| 1396 |
5834 |
| static bool conc_same(const conc_call *x, const conc_call *y) { |
| 1397 |
5834 |
▶ | if (x->n != y->n) return FALSE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (x->n != y->n) return FALSE; |
274 |
5560 |
5834 |
|
| 1398 |
5560 |
▶ | if (x->n < 2) return x->a == y->a; |
Branch: 1 branch
| Branch | True | False | Total |
| if (x->n < 2) return x->a == y->a; |
74 |
5486 |
5560 |
|
| 1399 |
5486 |
▶ | return (x->a == y->a && x->b == y->b) || (x->a == y->b && x->b == y->a); |
Branch: 4 branches
| Branch | True | False | Total |
| return (x->a == y->a && x->b == y->b) || (x->a == y->b && x->b == y->a); |
3374 |
2112 |
5486 |
| return (x->a == y->a && x->b == y->b) || (x->a == y->b && x->b == y->a); |
63 |
3311 |
3374 |
| return (x->a == y->a && x->b == y->b) || (x->a == y->b && x->b == y->a); |
1382 |
793 |
2175 |
| return (x->a == y->a && x->b == y->b) || (x->a == y->b && x->b == y->a); |
3 |
1379 |
1382 |
|
| 1400 |
|
| } |
| 1401 |
|
| |
| 1402 |
|
| /*The kind of comparison, an index into CONC_NAME, of the truth's call and the |
| 1403 |
|
| test's, by the script's assay_concordance(), in its order.*/ |
| 1404 |
5834 |
| static unsigned short int conc_kind(const conc_call *truth, const conc_call *test) { |
| 1405 |
5834 |
| const unsigned short int ct = conc_class(truth), cs = conc_class(test); |
| 1406 |
5834 |
▶ | if (conc_same(truth, test)) return CONC_SAME[ct]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (conc_same(truth, test)) return CONC_SAME[ct]; |
3360 |
2474 |
5834 |
|
| 1407 |
2474 |
▶ | if (cs == CONC_NO_CALL) return CONC_LOST; // the truth is not, or the two would be the same |
Branch: 1 branch
| Branch | True | False | Total |
| if (cs == CONC_NO_CALL) return CONC_LOST; // the truth is not, or the two would be the same |
13 |
2461 |
2474 |
|
| 1408 |
2461 |
▶ | if (ct == CONC_NO_CALL) return CONC_GAINED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct == CONC_NO_CALL) return CONC_GAINED; |
42 |
2419 |
2461 |
|
| 1409 |
2419 |
▶ | if (ct == CONC_HOM_REF && cs == CONC_HAPLOID_REF) return CONC_DIPLOID_REF_TO_HAPLOID_REF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ct == CONC_HOM_REF && cs == CONC_HAPLOID_REF) return CONC_DIPLOID_REF_TO_HAPLOID_REF; |
56 |
2363 |
2419 |
| if (ct == CONC_HOM_REF && cs == CONC_HAPLOID_REF) return CONC_DIPLOID_REF_TO_HAPLOID_REF; |
10 |
46 |
56 |
|
| 1410 |
2409 |
▶ | if (ct == CONC_HAPLOID_REF && cs == CONC_HOM_REF) return CONC_HAPLOID_REF_TO_DIPLOID_REF; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ct == CONC_HAPLOID_REF && cs == CONC_HOM_REF) return CONC_HAPLOID_REF_TO_DIPLOID_REF; |
62 |
2347 |
2409 |
| if (ct == CONC_HAPLOID_REF && cs == CONC_HOM_REF) return CONC_HAPLOID_REF_TO_DIPLOID_REF; |
17 |
45 |
62 |
|
| 1411 |
2392 |
▶ | if (ct == CONC_HOM_ALT && test->n == 1 && test->a == truth->a) return CONC_HOM_ALT_TO_DIPLOID_ALT; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ct == CONC_HOM_ALT && test->n == 1 && test->a == truth->a) return CONC_HOM_ALT_TO_DIPLOID_ALT; |
1413 |
979 |
2392 |
| if (ct == CONC_HOM_ALT && test->n == 1 && test->a == truth->a) return CONC_HOM_ALT_TO_DIPLOID_ALT; |
35 |
1378 |
1413 |
| if (ct == CONC_HOM_ALT && test->n == 1 && test->a == truth->a) return CONC_HOM_ALT_TO_DIPLOID_ALT; |
9 |
26 |
35 |
|
| 1412 |
2383 |
▶ | if (cs == CONC_HOM_ALT && truth->n == 1 && truth->a == test->a) return CONC_HAPLOID_ALT_TO_HOM_ALT; |
Branch: 3 branches
| Branch | True | False | Total |
| if (cs == CONC_HOM_ALT && truth->n == 1 && truth->a == test->a) return CONC_HAPLOID_ALT_TO_HOM_ALT; |
756 |
1627 |
2383 |
| if (cs == CONC_HOM_ALT && truth->n == 1 && truth->a == test->a) return CONC_HAPLOID_ALT_TO_HOM_ALT; |
27 |
729 |
756 |
| if (cs == CONC_HOM_ALT && truth->n == 1 && truth->a == test->a) return CONC_HAPLOID_ALT_TO_HOM_ALT; |
9 |
18 |
27 |
|
| 1413 |
2374 |
| return (unsigned short int)CONC_DIFFERENT[ct][cs]; // never -1: those four pairs were taken above |
| 1414 |
|
| } |
| 1415 |
|
| |
| 1416 |
|
| /*Call c as GT would write it, a mortal: "." for a no-call.*/ |
| 1417 |
764 |
| static SV *conc_call_sv(pTHX_ const conc_call *c) { |
| 1418 |
764 |
▶ | if (c->n == 0) return sv_2mortal(newSVpvs(".")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->n == 0) return sv_2mortal(newSVpvs(".")); |
50 |
714 |
764 |
|
| 1419 |
714 |
▶ | if (c->n == 1) return sv_2mortal(newSVpvf("%u", (unsigned)c->a)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->n == 1) return sv_2mortal(newSVpvf("%u", (unsigned)c->a)); |
204 |
510 |
714 |
|
| 1420 |
510 |
▶ | return sv_2mortal(newSVpvf("%u%c%u", (unsigned)c->a, c->phased ? '|' : '/', (unsigned)c->b)); |
Branch: 1 branch
| Branch | True | False | Total |
| return sv_2mortal(newSVpvf("%u%c%u", (unsigned)c->a, c->phased ? '|' : '/', (unsigned)c->b)); |
1 |
509 |
510 |
|
| 1421 |
|
| } |
| 1422 |
|
| |
| 1423 |
|
| /*The test's #CHROM line: the pairs of samples to compare. Two VCFs of one |
| 1424 |
|
| sample each are compared whatever their samples are called; otherwise each of |
| 1425 |
|
| the test's samples is compared with the truth's of the same name.*/ |
| 1426 |
60 |
| static void conc_header_test(pTHX_ vcf_reader *r) { |
| 1427 |
60 |
| conc_reader *t = (conc_reader*)r; |
| 1428 |
60 |
| conc_store *s = t->s; |
| 1429 |
60 |
▶ | Newxz(s->test_col, r->n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(s->test_col, r->n, size_t); |
0 |
60 |
60 |
|
| 1430 |
60 |
▶ | Newxz(s->truth_col, r->n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(s->truth_col, r->n, size_t); |
0 |
60 |
60 |
|
| 1431 |
60 |
▶ | Newxz(s->test_pair, r->n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(s->test_pair, r->n, size_t); |
0 |
60 |
60 |
|
| 1432 |
60 |
▶ | Newxz(s->truth_pair, s->truth_n, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(s->truth_pair, s->truth_n, size_t); |
0 |
60 |
60 |
|
| 1433 |
60 |
▶ | if (r->n == 1 && s->truth_n == 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r->n == 1 && s->truth_n == 1) { |
45 |
15 |
60 |
| if (r->n == 1 && s->truth_n == 1) { |
44 |
1 |
45 |
|
| 1434 |
44 |
| s->pairs = 1; |
| 1435 |
44 |
| s->test_pair[0] = s->truth_pair[0] = 1; |
| 1436 |
|
| } else { |
| 1437 |
16 |
| HV *truth = (HV*)sv_2mortal((SV*)newHV()); // the truth's sample => its column |
| 1438 |
56 |
▶ | for (size_t j = 0; j < s->truth_n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t j = 0; j < s->truth_n; j++) { |
40 |
16 |
56 |
|
| 1439 |
40 |
| SV **name = av_fetch(t->truth_names, (SSize_t)j, 0); |
| 1440 |
40 |
▶ | if (name) (void)hv_store_ent(truth, *name, newSVuv((UV)j), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) (void)hv_store_ent(truth, *name, newSVuv((UV)j), 0); |
40 |
0 |
40 |
|
| 1441 |
|
| } |
| 1442 |
63 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
47 |
16 |
63 |
|
| 1443 |
47 |
| SV **name = av_fetch(r->names, (SSize_t)i, 0); |
| 1444 |
47 |
▶ | HE *he = name ? hv_fetch_ent(truth, *name, 0, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HE *he = name ? hv_fetch_ent(truth, *name, 0, 0) : NULL; |
47 |
0 |
47 |
|
| 1445 |
47 |
▶ | if (!he) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) continue; |
19 |
28 |
47 |
|
| 1446 |
28 |
| const size_t j = (size_t)SvUV(HeVAL(he)); |
| 1447 |
28 |
| s->test_col[s->pairs] = i; |
| 1448 |
28 |
| s->truth_col[s->pairs] = j; |
| 1449 |
28 |
| s->test_pair[i] = s->truth_pair[j] = ++s->pairs; |
| 1450 |
|
| } |
| 1451 |
|
| } |
| 1452 |
60 |
▶ | if (!s->pairs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->pairs) { |
2 |
58 |
60 |
|
| 1453 |
2 |
| const char *test = vcf_names_list(aTHX_ r->names), *truth = vcf_names_list(aTHX_ t->truth_names); |
| 1454 |
2 |
| croak("%s: %s and the truth share no sample: the test's are %s, and the truth's %s", __func__, r->name, test, truth); |
| 1455 |
|
| } |
| 1456 |
58 |
▶ | Newx(s->truth, s->pairs, conc_call); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(s->truth, s->pairs, conc_call); |
0 |
58 |
58 |
|
| 1457 |
58 |
| } |
| 1458 |
|
| |
| 1459 |
|
| /*Room for one more site of the test r, its calls, and a slot in the table, |
| 1460 |
|
| which is rebuilt at twice the size when it would be more than half full. Dies, |
| 1461 |
|
| naming r, when the calls' size would not fit in a size_t: on a 32-bit perl, |
| 1462 |
|
| 349,526 samples or more do at the first 1,024 sites. Renew() checks only the |
| 1463 |
|
| multiplication by the type's size, and only on a perl built with |
| 1464 |
|
| usemallocwrap (the default), where it died with "panic: memory wrap", naming |
| 1465 |
|
| nothing; on one built without, the calls were allocated too small.*/ |
| 1466 |
5658 |
| static void conc_grow(pTHX_ const vcf_reader *r, conc_store *s) { |
| 1467 |
5658 |
▶ | if (s->n == s->room) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->n == s->room) { |
60 |
5598 |
5658 |
|
| 1468 |
60 |
▶ | const size_t room = s->room ? 2 * s->room : 1024; // small, since its calls are 12 bytes a pair each: 1,000 samples start at 12 MB |
Branch: 1 branch
| Branch | True | False | Total |
| const size_t room = s->room ? 2 * s->room : 1024; // small, since its calls are 12 bytes a pair each: 1,000 samples start at 12 MB |
3 |
57 |
60 |
|
| 1469 |
60 |
▶ | if (room < s->room || room > ((size_t)-1 / sizeof(conc_call) / 2) / s->pairs) |
Branch: 2 branches
| Branch | True | False | Total |
| if (room < s->room || room > ((size_t)-1 / sizeof(conc_call) / 2) / s->pairs) |
60 |
0 |
60 |
| if (room < s->room || room > ((size_t)-1 / sizeof(conc_call) / 2) / s->pairs) |
0 |
60 |
60 |
|
| 1470 |
0 |
| croak("%s: %s has more sites of its %" UVuf " samples compared than this perl can hold (%" UVuf " already)", __func__, r->name, (UV)s->pairs, (UV)s->n); |
| 1471 |
60 |
| s->room = room; |
| 1472 |
60 |
▶ | Renew(s->site, s->room, conc_site); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->site, s->room, conc_site); |
0 |
60 |
60 |
|
| 1473 |
60 |
▶ | Renew(s->call, s->room * 2 * s->pairs, conc_call); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->call, s->room * 2 * s->pairs, conc_call); |
0 |
60 |
60 |
|
| 1474 |
|
| } |
| 1475 |
5658 |
▶ | if (2 * (s->n + 1) <= s->slots) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (2 * (s->n + 1) <= s->slots) return; |
5598 |
60 |
5658 |
|
| 1476 |
60 |
▶ | s->slots = s->slots ? 2 * s->slots : 2048; |
Branch: 1 branch
| Branch | True | False | Total |
| s->slots = s->slots ? 2 * s->slots : 2048; |
3 |
57 |
60 |
|
| 1477 |
60 |
| Safefree(s->slot); |
| 1478 |
60 |
▶ | Newxz(s->slot, s->slots, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(s->slot, s->slots, size_t); |
0 |
60 |
60 |
|
| 1479 |
7228 |
▶ | for (size_t k = 0; k < s->n; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < s->n; k++) { |
7168 |
60 |
7228 |
|
| 1480 |
7168 |
| size_t at = (size_t)s->site[k].hash & (s->slots - 1); |
| 1481 |
11118 |
▶ | while (s->slot[at]) at = (at + 1) & (s->slots - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->slot[at]) at = (at + 1) & (s->slots - 1); |
3950 |
7168 |
11118 |
|
| 1482 |
7168 |
| s->slot[at] = k + 1; |
| 1483 |
|
| } |
| 1484 |
|
| } |
| 1485 |
|
| |
| 1486 |
|
| /*The test's record p..e, kept as a site with its calls for each pair. A site |
| 1487 |
|
| the test lists twice is kept twice, and each compared, as the script compared |
| 1488 |
|
| each line.*/ |
| 1489 |
5658 |
| static void conc_record_test(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 1490 |
5658 |
| conc_store *s = ((conc_reader*)r)->s; |
| 1491 |
5658 |
| const char *f = vcf_field_end(p, e); |
| 1492 |
|
| const char *col[9]; |
| 1493 |
5658 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 1494 |
5658 |
| conc_grow(aTHX_ r, s); |
| 1495 |
5658 |
| conc_site *site = s->site + s->n; |
| 1496 |
5658 |
| site->pos = vcf_pos(aTHX_ r, col[1], col[2] - 1); |
| 1497 |
5657 |
| const char *chrom = conc_chrom(col[0], col[1] - 1); |
| 1498 |
5657 |
| site->hash = conc_hash(chrom, col[1] - 1, site->pos, col[3], col[4] - 1, col[4], col[5] - 1); |
| 1499 |
5657 |
| site->found = FALSE; |
| 1500 |
5657 |
| const size_t len = (size_t)(col[5] - p); // CHROM to ALT, and the tab after ALT, which becomes the '\n' |
| 1501 |
5657 |
▶ | if (s->text_n + len > s->text_room) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->text_n + len > s->text_room) { |
56 |
5601 |
5657 |
|
| 1502 |
112 |
▶ | while (s->text_n + len > s->text_room) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->text_n + len > s->text_room) { |
56 |
56 |
112 |
|
| 1503 |
56 |
▶ | if (s->text_room > (size_t)-1 / 2) croak("%s: %s has more sites than this perl can hold (%" UVuf " already)", __func__, r->name, (UV)s->n); // doubling would wrap to 0, and loop for ever |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->text_room > (size_t)-1 / 2) croak("%s: %s has more sites than this perl can hold (%" UVuf " already)", __func__, r->name, (UV)s->n); // doubling would wrap to 0, and loop for ever |
0 |
56 |
56 |
|
| 1504 |
56 |
▶ | s->text_room = s->text_room ? 2 * s->text_room : 1 << 20; |
Branch: 1 branch
| Branch | True | False | Total |
| s->text_room = s->text_room ? 2 * s->text_room : 1 << 20; |
0 |
56 |
56 |
|
| 1505 |
|
| } |
| 1506 |
56 |
| Renew(s->text, s->text_room, char); |
| 1507 |
|
| } |
| 1508 |
5657 |
| site->text = s->text_n; |
| 1509 |
5657 |
| Copy(p, s->text + s->text_n, len, char); |
| 1510 |
5657 |
| s->text_n += len; |
| 1511 |
5657 |
| s->text[s->text_n - 1] = '\n'; |
| 1512 |
5657 |
| size_t at = (size_t)site->hash & (s->slots - 1); |
| 1513 |
11348 |
▶ | while (s->slot[at]) at = (at + 1) & (s->slots - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->slot[at]) at = (at + 1) & (s->slots - 1); |
5691 |
5657 |
11348 |
|
| 1514 |
5657 |
| s->slot[at] = ++s->n; |
| 1515 |
5657 |
| conc_call *call = s->call + (s->n - 1) * 2 * s->pairs; |
| 1516 |
5657 |
▶ | Zero(call, 2 * s->pairs, conc_call); // the truth's calls stay no-calls unless it has the site |
Branch: 1 branch
| Branch | True | False | Total |
| Zero(call, 2 * s->pairs, conc_call); // the truth's calls stay no-calls unless it has the site |
0 |
5657 |
5657 |
|
| 1517 |
|
| size_t gt, dp; |
| 1518 |
5657 |
| vcf_format(col[8], format_end, >, &dp); |
| 1519 |
5657 |
| p = format_end + 1; |
| 1520 |
12419 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
6767 |
5652 |
12419 |
|
| 1521 |
6767 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 1522 |
6766 |
▶ | if (s->test_pair[i]) conc_gt(aTHX_ r, p, se, gt, dp, i, call + 2 * (s->test_pair[i] - 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->test_pair[i]) conc_gt(aTHX_ r, p, se, gt, dp, i, call + 2 * (s->test_pair[i] - 1)); |
6214 |
552 |
6766 |
|
| 1523 |
6762 |
| p = se + 1; |
| 1524 |
|
| } |
| 1525 |
5652 |
| vcf_samples_end(aTHX_ r, p, e); |
| 1526 |
5651 |
| } |
| 1527 |
|
| |
| 1528 |
|
| /*The truth's #CHROM line, which must be the one read before the test was.*/ |
| 1529 |
51 |
| static void conc_header_truth(pTHX_ vcf_reader *r) { |
| 1530 |
51 |
| conc_store *s = ((conc_reader*)r)->s; |
| 1531 |
51 |
▶ | if (r->n != s->truth_n) croak("%s: %s had %" UVuf " samples when it was first read, and has %" UVuf " now", __func__, r->name, (UV)s->truth_n, (UV)r->n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n != s->truth_n) croak("%s: %s had %" UVuf " samples when it was first read, and has %" UVuf " now", __func__, r->name, (UV)s->truth_n, (UV)r->n); |
0 |
51 |
51 |
|
| 1532 |
51 |
| } |
| 1533 |
|
| |
| 1534 |
|
| /*The truth's record p..e: its calls for each pair, given to every test site |
| 1535 |
|
| that it is. A site the truth lists twice gives the test its last calls, as the |
| 1536 |
|
| script's hash of the truth's sites did.*/ |
| 1537 |
5470 |
| static void conc_record_truth(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 1538 |
5470 |
| conc_store *s = ((conc_reader*)r)->s; |
| 1539 |
5485 |
▶ | if (!s->slots) return; // the test has no sites |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->slots) return; // the test has no sites |
1 |
5469 |
5470 |
|
| 1540 |
5469 |
| const char *f = vcf_field_end(p, e); |
| 1541 |
|
| const char *col[9]; |
| 1542 |
5469 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 1543 |
5469 |
| const UV pos = vcf_pos(aTHX_ r, col[1], col[2] - 1); |
| 1544 |
5468 |
| const char *chrom = conc_chrom(col[0], col[1] - 1); |
| 1545 |
5468 |
| const uint64_t hash = conc_hash(chrom, col[1] - 1, pos, col[3], col[4] - 1, col[4], col[5] - 1); |
| 1546 |
5468 |
| size_t hits = 0; |
| 1547 |
19083 |
▶ | for (size_t at = (size_t)hash & (s->slots - 1); s->slot[at]; at = (at + 1) & (s->slots - 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t at = (size_t)hash & (s->slots - 1); s->slot[at]; at = (at + 1) & (s->slots - 1)) { |
13615 |
5468 |
19083 |
|
| 1548 |
13615 |
| const size_t k = s->slot[at] - 1; |
| 1549 |
13615 |
| const conc_site *site = s->site + k; |
| 1550 |
13615 |
▶ | if (site->hash != hash || site->pos != pos) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (site->hash != hash || site->pos != pos) continue; |
5455 |
8160 |
13615 |
| if (site->hash != hash || site->pos != pos) continue; |
0 |
5455 |
5455 |
|
| 1551 |
|
| const char *fld[6]; |
| 1552 |
5455 |
| conc_fields(s, k, fld); |
| 1553 |
5455 |
▶ | if (!conc_eq(conc_chrom(fld[0], fld[1] - 1), fld[1] - 1, chrom, col[1] - 1) || !conc_eq(fld[3], fld[4] - 1, col[3], col[4] - 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!conc_eq(conc_chrom(fld[0], fld[1] - 1), fld[1] - 1, chrom, col[1] - 1) || !conc_eq(fld[3], fld[4] - 1, col[3], col[4] - 1) |
5455 |
0 |
5455 |
| if (!conc_eq(conc_chrom(fld[0], fld[1] - 1), fld[1] - 1, chrom, col[1] - 1) || !conc_eq(fld[3], fld[4] - 1, col[3], col[4] - 1) |
5455 |
0 |
5455 |
|
| 1554 |
5455 |
▶ | || !conc_eq(fld[4], fld[5] - 1, col[4], col[5] - 1)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| || !conc_eq(fld[4], fld[5] - 1, col[4], col[5] - 1)) continue; |
0 |
5455 |
5455 |
|
| 1555 |
5455 |
▶ | if (hits == s->hit_room) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hits == s->hit_room) { |
43 |
5412 |
5455 |
|
| 1556 |
43 |
▶ | s->hit_room = s->hit_room ? 2 * s->hit_room : 8; // a site the test lists more than once, which is rare |
Branch: 1 branch
| Branch | True | False | Total |
| s->hit_room = s->hit_room ? 2 * s->hit_room : 8; // a site the test lists more than once, which is rare |
0 |
43 |
43 |
|
| 1557 |
43 |
▶ | Renew(s->hit, s->hit_room, size_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(s->hit, s->hit_room, size_t); |
0 |
43 |
43 |
|
| 1558 |
|
| } |
| 1559 |
5455 |
| s->hit[hits++] = k; |
| 1560 |
|
| } |
| 1561 |
5468 |
▶ | if (!hits) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hits) return; |
15 |
5453 |
5468 |
|
| 1562 |
|
| size_t gt, dp; |
| 1563 |
5453 |
| vcf_format(col[8], format_end, >, &dp); |
| 1564 |
5453 |
| p = format_end + 1; |
| 1565 |
11627 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
6183 |
5444 |
11627 |
|
| 1566 |
6183 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 1567 |
6182 |
▶ | if (s->truth_pair[i]) conc_gt(aTHX_ r, p, se, gt, dp, i, s->truth + (s->truth_pair[i] - 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->truth_pair[i]) conc_gt(aTHX_ r, p, se, gt, dp, i, s->truth + (s->truth_pair[i] - 1)); |
5822 |
360 |
6182 |
|
| 1568 |
6174 |
| p = se + 1; |
| 1569 |
|
| } |
| 1570 |
5444 |
| vcf_samples_end(aTHX_ r, p, e); |
| 1571 |
10888 |
▶ | for (size_t h = 0; h < hits; h++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t h = 0; h < hits; h++) { |
5445 |
5443 |
10888 |
|
| 1572 |
5445 |
| conc_site *site = s->site + s->hit[h]; |
| 1573 |
5445 |
▶ | if (!site->found) s->found++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!site->found) s->found++; |
5443 |
2 |
5445 |
|
| 1574 |
5445 |
| site->found = TRUE; |
| 1575 |
5445 |
| conc_call *call = s->call + s->hit[h] * 2 * s->pairs; |
| 1576 |
11256 |
▶ | for (size_t k = 0; k < s->pairs; k++) call[2 * k + 1] = s->truth[k]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < s->pairs; k++) call[2 * k + 1] = s->truth[k]; |
5811 |
5445 |
11256 |
|
| 1577 |
|
| } |
| 1578 |
|
| } |
| 1579 |
|
| |
| 1580 |
|
| /*The sum of the counts c of the kinds in set, CONC_BIT()s.*/ |
| 1581 |
350 |
| static UV conc_sum(const UV *c, uint64_t set) { |
| 1582 |
350 |
| UV sum = 0; |
| 1583 |
16100 |
▶ | for (unsigned short int k = 0; k < CONC_KINDS; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < CONC_KINDS; k++) { |
15750 |
350 |
16100 |
|
| 1584 |
15750 |
▶ | if (set & CONC_BIT(k)) sum += c[k]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (set & CONC_BIT(k)) sum += c[k]; |
4700 |
11050 |
15750 |
|
| 1585 |
|
| } |
| 1586 |
350 |
| return sum; |
| 1587 |
|
| } |
| 1588 |
|
| |
| 1589 |
|
| /*x / y as a mortal, or undef when y is 0, where the script gave the string |
| 1590 |
|
| "undefined" (and, for genotype_concordance, died).*/ |
| 1591 |
300 |
| static SV *conc_ratio(pTHX_ UV x, UV y) { |
| 1592 |
300 |
▶ | return y ? sv_2mortal(newSVnv((NV)x / (NV)y)) : sv_newmortal(); |
Branch: 1 branch
| Branch | True | False | Total |
| return y ? sv_2mortal(newSVnv((NV)x / (NV)y)) : sv_newmortal(); |
217 |
83 |
300 |
|
| 1593 |
|
| } |
| 1594 |
|
| |
| 1595 |
3956 |
| static void conc_store_sv(pTHX_ HV *h, const char *key, SV *v) { |
| 1596 |
3956 |
| (void)hv_store(h, key, (I32)strlen(key), SvREFCNT_inc_simple_NN(v), 0); |
| 1597 |
3956 |
| } |
| 1598 |
|
| |
| 1599 |
|
| /*The metrics of counts c, by the script's metrics() called as its main |
| 1600 |
|
| program called it, with no-calls left out ("no"), and with the corrections |
| 1601 |
|
| given above CONC_TP.*/ |
| 1602 |
50 |
| static HV *conc_metrics(pTHX_ const UV *c) { |
| 1603 |
50 |
| HV *m = newHV(); |
| 1604 |
50 |
| const UV tp = conc_sum(c, CONC_TP), fp_site = c[CONC_GAINED], fn_site = c[CONC_LOST]; |
| 1605 |
50 |
| const UV fp_ref = conc_sum(c, CONC_FP_REF), mismatch = conc_sum(c, CONC_MISMATCH); |
| 1606 |
50 |
| const UV fp_genotype = fp_ref + mismatch, fn_genotype = conc_sum(c, CONC_FN_REF) + mismatch; |
| 1607 |
50 |
| const UV tn = conc_sum(c, CONC_TN); |
| 1608 |
50 |
| conc_store_sv(aTHX_ m, "tp_site", sv_2mortal(newSVuv(tp))); |
| 1609 |
50 |
| conc_store_sv(aTHX_ m, "fp_site", sv_2mortal(newSVuv(fp_site))); |
| 1610 |
50 |
| conc_store_sv(aTHX_ m, "fn_site", sv_2mortal(newSVuv(fn_site))); |
| 1611 |
50 |
| conc_store_sv(aTHX_ m, "tn_site", sv_2mortal(newSVuv(tn))); |
| 1612 |
50 |
| conc_store_sv(aTHX_ m, "tp_genotype", sv_2mortal(newSVuv(tp))); |
| 1613 |
50 |
| conc_store_sv(aTHX_ m, "fp_genotype", sv_2mortal(newSVuv(fp_genotype))); |
| 1614 |
50 |
| conc_store_sv(aTHX_ m, "fn_genotype", sv_2mortal(newSVuv(fn_genotype))); |
| 1615 |
50 |
| conc_store_sv(aTHX_ m, "tn_genotype", sv_2mortal(newSVuv(tn))); |
| 1616 |
50 |
| conc_store_sv(aTHX_ m, "genotype_concordance", conc_ratio(aTHX_ conc_sum(c, CONC_AGREE), conc_sum(c, CONC_CALLED))); |
| 1617 |
50 |
| conc_store_sv(aTHX_ m, "precision_site", conc_ratio(aTHX_ tp, tp + fp_site)); |
| 1618 |
50 |
| conc_store_sv(aTHX_ m, "recall_site", conc_ratio(aTHX_ tp, tp + fn_site)); |
| 1619 |
50 |
| conc_store_sv(aTHX_ m, "precision_genotype", conc_ratio(aTHX_ tp, tp + fp_genotype)); |
| 1620 |
50 |
| conc_store_sv(aTHX_ m, "recall_genotype", conc_ratio(aTHX_ tp, tp + fn_genotype)); |
| 1621 |
50 |
| conc_store_sv(aTHX_ m, "specificity_genotype", conc_ratio(aTHX_ tn, tn + fp_ref)); |
| 1622 |
50 |
| return m; |
| 1623 |
|
| } |
| 1624 |
|
| |
| 1625 |
|
| /*Each pair's results, keyed by the test's name for its sample, from the test |
| 1626 |
|
| and truth names, the sites s holds, and whether to list the discordant, |
| 1627 |
|
| gained and lost ones.*/ |
| 1628 |
40 |
| static HV *conc_results(pTHX_ const conc_store *s, AV *test_names, AV *truth_names, bool discordances) { |
| 1629 |
|
| UV *count; // CONC_KINDS a pair |
| 1630 |
40 |
▶ | Newxz(count, s->pairs * CONC_KINDS, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(count, s->pairs * CONC_KINDS, UV); |
0 |
40 |
40 |
|
| 1631 |
40 |
| SAVEFREEPV(count); |
| 1632 |
|
| AV **list; // each pair's discordances, or NULL |
| 1633 |
40 |
▶ | Newxz(list, s->pairs, AV*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(list, s->pairs, AV*); |
0 |
40 |
40 |
|
| 1634 |
40 |
| SAVEFREEPV(list); |
| 1635 |
87 |
▶ | for (size_t k = 0; k < s->pairs && discordances; k++) list[k] = (AV*)sv_2mortal((SV*)newAV()); |
Branch: 2 branches
| Branch | True | False | Total |
| for (size_t k = 0; k < s->pairs && discordances; k++) list[k] = (AV*)sv_2mortal((SV*)newAV()); |
49 |
38 |
87 |
| for (size_t k = 0; k < s->pairs && discordances; k++) list[k] = (AV*)sv_2mortal((SV*)newAV()); |
47 |
2 |
49 |
|
| 1636 |
5500 |
▶ | for (size_t i = 0; i < s->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < s->n; i++) { |
5460 |
40 |
5500 |
|
| 1637 |
5460 |
| const conc_call *call = s->call + i * 2 * s->pairs; |
| 1638 |
|
| const char *fld[6]; |
| 1639 |
5460 |
| bool split = FALSE; // fld has been set |
| 1640 |
11294 |
▶ | for (size_t k = 0; k < s->pairs; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < s->pairs; k++) { |
5834 |
5460 |
11294 |
|
| 1641 |
5834 |
| const unsigned short int kind = conc_kind(call + 2 * k + 1, call + 2 * k); |
| 1642 |
5834 |
| count[k * CONC_KINDS + kind]++; |
| 1643 |
5834 |
▶ | if (!list[k] || kind < CONC_DISCORDANT) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!list[k] || kind < CONC_DISCORDANT) continue; |
742 |
5092 |
5834 |
| if (!list[k] || kind < CONC_DISCORDANT) continue; |
360 |
382 |
742 |
|
| 1644 |
382 |
▶ | if (!split) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!split) { |
312 |
70 |
382 |
|
| 1645 |
312 |
| conc_fields(s, i, fld); |
| 1646 |
312 |
| split = TRUE; |
| 1647 |
|
| } |
| 1648 |
382 |
| HV *row = newHV(); |
| 1649 |
382 |
| conc_store_sv(aTHX_ row, "chrom", sv_2mortal(newSVpvn(fld[0], (STRLEN)(fld[1] - 1 - fld[0])))); |
| 1650 |
382 |
| conc_store_sv(aTHX_ row, "pos", sv_2mortal(newSVuv(s->site[i].pos))); |
| 1651 |
382 |
| conc_store_sv(aTHX_ row, "id", sv_2mortal(newSVpvn(fld[2], (STRLEN)(fld[3] - 1 - fld[2])))); |
| 1652 |
382 |
| conc_store_sv(aTHX_ row, "ref", sv_2mortal(newSVpvn(fld[3], (STRLEN)(fld[4] - 1 - fld[3])))); |
| 1653 |
382 |
| conc_store_sv(aTHX_ row, "alt", sv_2mortal(newSVpvn(fld[4], (STRLEN)(fld[5] - 1 - fld[4])))); |
| 1654 |
382 |
| conc_store_sv(aTHX_ row, "type", sv_2mortal(newSVpvn(CONC_NAME[kind], strlen(CONC_NAME[kind])))); |
| 1655 |
382 |
| conc_store_sv(aTHX_ row, "test", conc_call_sv(aTHX_ call + 2 * k)); |
| 1656 |
382 |
| conc_store_sv(aTHX_ row, "truth", conc_call_sv(aTHX_ call + 2 * k + 1)); |
| 1657 |
382 |
| av_push(list[k], newRV_noinc((SV*)row)); |
| 1658 |
|
| } |
| 1659 |
|
| } |
| 1660 |
40 |
| HV *out = (HV*)sv_2mortal((SV*)newHV()); |
| 1661 |
90 |
▶ | for (size_t k = 0; k < s->pairs; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < s->pairs; k++) { |
50 |
40 |
90 |
|
| 1662 |
50 |
| SV **test = av_fetch(test_names, (SSize_t)s->test_col[k], 0), **truth = av_fetch(truth_names, (SSize_t)s->truth_col[k], 0); |
| 1663 |
50 |
▶ | if (!test || !truth) croak("%s: pair %" UVuf " has lost a sample's name", __func__, (UV)k); // cannot happen: both headers pushed every name |
Branch: 2 branches
| Branch | True | False | Total |
| if (!test || !truth) croak("%s: pair %" UVuf " has lost a sample's name", __func__, (UV)k); // cannot happen: both headers pushed every name |
50 |
0 |
50 |
| if (!test || !truth) croak("%s: pair %" UVuf " has lost a sample's name", __func__, (UV)k); // cannot happen: both headers pushed every name |
0 |
50 |
50 |
|
| 1664 |
50 |
| const UV *c = count + k * CONC_KINDS; |
| 1665 |
50 |
| HV *pair = newHV(); |
| 1666 |
50 |
| HV *counts = newHV(); |
| 1667 |
2300 |
▶ | for (unsigned short int j = 0; j < CONC_KINDS; j++) (void)hv_store(counts, CONC_NAME[j], (I32)strlen(CONC_NAME[j]), newSVuv(c[j]), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int j = 0; j < CONC_KINDS; j++) (void)hv_store(counts, CONC_NAME[j], (I32)strlen(CONC_NAME[j]), newSVuv(c[j]), 0); |
2250 |
50 |
2300 |
|
| 1668 |
50 |
| conc_store_sv(aTHX_ pair, "test_sample", sv_2mortal(newSVsv(*test))); |
| 1669 |
50 |
| conc_store_sv(aTHX_ pair, "truth_sample", sv_2mortal(newSVsv(*truth))); |
| 1670 |
50 |
| conc_store_sv(aTHX_ pair, "sites", sv_2mortal(newSVuv((UV)s->n))); |
| 1671 |
50 |
| conc_store_sv(aTHX_ pair, "in_truth", sv_2mortal(newSVuv(s->found))); |
| 1672 |
50 |
| (void)hv_store(pair, "counts", 6, newRV_noinc((SV*)counts), 0); |
| 1673 |
50 |
| (void)hv_store(pair, "metrics", 7, newRV_noinc((SV*)conc_metrics(aTHX_ c)), 0); |
| 1674 |
50 |
▶ | (void)hv_store(pair, "discordances", 12, list[k] ? newRV_inc((SV*)list[k]) : newSV(0), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_store(pair, "discordances", 12, list[k] ? newRV_inc((SV*)list[k]) : newSV(0), 0); |
47 |
3 |
50 |
|
| 1675 |
50 |
| (void)hv_store_ent(out, *test, newRV_noinc((SV*)pair), 0); |
| 1676 |
|
| } |
| 1677 |
40 |
| return out; |
| 1678 |
|
| } |
| 1679 |
|
| |
| 1680 |
|
| /*An on_header that reads no further, and an on_record that is never called |
| 1681 |
|
| after it, for _vcf_samples(), which reads only the #CHROM line.*/ |
| 1682 |
61 |
| static void conc_stop(pTHX_ vcf_reader *r) { |
| 1683 |
61 |
| r->stop = TRUE; |
| 1684 |
61 |
| } |
| 1685 |
|
| |
| 1686 |
0 |
| static void conc_skip(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 1687 |
|
| PERL_UNUSED_ARG(r); |
| 1688 |
|
| PERL_UNUSED_ARG(p); |
| 1689 |
|
| PERL_UNUSED_ARG(e); |
| 1690 |
0 |
| } |
| 1691 |
|
| |
| 1692 |
|
| /*The genotypes of a marker panel, for hirisplex_genotypes(). |
| 1693 |
|
| |
| 1694 |
|
| The panel is 41 markers and a VCF of a whole genome is tens of millions of |
| 1695 |
|
| records, so each record is tested against the panel rather than the panel |
| 1696 |
|
| searched for in the file. Matching by position tests the chromosome against a |
| 1697 |
|
| bitmap first, which turns away a record on one test, and reads POS and |
| 1698 |
|
| searches only when that passes; matching by ID compares the ID column against |
| 1699 |
|
| the markers' rs numbers. Either way a record that matches nothing is read no |
| 1700 |
|
| further than its first three fields. |
| 1701 |
|
| |
| 1702 |
|
| Which allele is counted is perl's to say, from share/hirisplex_s_panel.tsv: |
| 1703 |
|
| the panel names the allele on the forward strand, so nothing here complements |
| 1704 |
|
| anything. The allele is looked for among REF and the ALTs, and its place |
| 1705 |
|
| there is what the genotypes are counted against, so that a site written with |
| 1706 |
|
| several ALTs, or with the counted allele as its REF, is counted the same way. |
| 1707 |
|
| A record whose REF is longer than one base is passed over for every marker but |
| 1708 |
|
| the one that is an indel, since it is some other variant that shares the |
| 1709 |
|
| position. That one, an insertion, is counted from the ALT that is REF with its |
| 1710 |
|
| base put in once, which is the same test however the VCF anchors the indel. |
| 1711 |
|
| Bases are compared regardless of case, as CHROM is. |
| 1712 |
|
| |
| 1713 |
|
| What is kept is one byte for each sample at each marker, so a file costs one |
| 1714 |
|
| byte times its samples times the panel, however many records it holds.*/ |
| 1715 |
|
| |
| 1716 |
|
| #define HPS_ID 16 // an rs number and its NUL; the longest the panel holds is "rs796296176", 11 characters |
| 1717 |
|
| |
| 1718 |
|
| //What hps_dose() gives for a sample at a marker; 0, 1 and 2 are counts of the counted allele |
| 1719 |
|
| enum { |
| 1720 |
|
| HPS_ABSENT = 3, // no record of the marker was in the file; set for every sample before reading |
| 1721 |
|
| HPS_NO_CALL = 4 // the marker was in the file, and this sample had no diploid call of it |
| 1722 |
|
| }; |
| 1723 |
|
| |
| 1724 |
|
| typedef struct { |
| 1725 |
|
| unsigned short int chrom; // vcf_chrom()'s numbering, 1 to 24; 0 when the panel gives no position |
| 1726 |
|
| UV pos; |
| 1727 |
|
| char allele; // the base counted, or the base inserted when insertion is TRUE |
| 1728 |
|
| bool insertion; // TRUE = count the ALT that is REF with allele put in once, for the one marker of the panel that is an indel |
| 1729 |
|
| size_t k; // the marker's place in the panel perl gave, which its genotypes are stored under |
| 1730 |
|
| char id[HPS_ID]; // the marker's rs number, for matching by ID |
| 1731 |
|
| } hps_site; |
| 1732 |
|
| |
| 1733 |
|
| typedef struct { |
| 1734 |
|
| vcf_reader r; // first, so that the vcf_reader * hps_header() and hps_record() are given is this |
| 1735 |
|
| hps_site *site; // what is searched, sorted by chromosome and position, or by rs number when by_id |
| 1736 |
|
| size_t entries; // how many site's holds, which is more than sites when a marker is searched for under more than one rs number |
| 1737 |
|
| size_t sites; // markers in the panel, which is what each sample's genotypes are counted into |
| 1738 |
|
| bool by_id; // TRUE = match the ID column, FALSE = CHROM and POS |
| 1739 |
|
| uint32_t chroms; // bit c set for each chromosome c the panel uses, tested before POS is read |
| 1740 |
|
| unsigned char *dose; // r.n * sites, sample-major: a count, or one of the codes above |
| 1741 |
|
| bool *seen; // one per marker: the file held a record for it |
| 1742 |
|
| bool *carried; // one per marker: a record of it held the counted allele among REF and the ALTs |
| 1743 |
|
| } hps_reader; |
| 1744 |
|
| |
| 1745 |
|
| /*Orders the panel by chromosome and position, for hps_find().*/ |
| 1746 |
3030 |
| static int hps_cmp_pos(const void *a, const void *b) { |
| 1747 |
3030 |
| const hps_site *x = (const hps_site*)a, *y = (const hps_site*)b; |
| 1748 |
3030 |
▶ | if (x->chrom != y->chrom) return x->chrom < y->chrom ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->chrom != y->chrom) return x->chrom < y->chrom ? -1 : 1; |
1342 |
1688 |
3030 |
| if (x->chrom != y->chrom) return x->chrom < y->chrom ? -1 : 1; |
330 |
1012 |
1342 |
|
| 1749 |
1688 |
▶ | if (x->pos != y->pos) return x->pos < y->pos ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->pos != y->pos) return x->pos < y->pos ? -1 : 1; |
1688 |
0 |
1688 |
| if (x->pos != y->pos) return x->pos < y->pos ? -1 : 1; |
1294 |
394 |
1688 |
|
| 1750 |
0 |
| return 0; |
| 1751 |
|
| } |
| 1752 |
|
| |
| 1753 |
|
| /*Orders the panel by rs number, for hps_find_id().*/ |
| 1754 |
522 |
| static int hps_cmp_id(const void *a, const void *b) { |
| 1755 |
522 |
| return strcmp(((const hps_site*)a)->id, ((const hps_site*)b)->id); |
| 1756 |
|
| } |
| 1757 |
|
| |
| 1758 |
|
| /*The marker at chrom and pos, or NULL.*/ |
| 1759 |
332 |
| static const hps_site *hps_find(const hps_reader *t, unsigned short int chrom, UV pos) { |
| 1760 |
332 |
| size_t lo = 0, hi = t->entries; |
| 1761 |
1349 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
1324 |
25 |
1349 |
|
| 1762 |
1324 |
| size_t mid = lo + (hi - lo) / 2; |
| 1763 |
1324 |
| const hps_site *s = t->site + mid; |
| 1764 |
1324 |
▶ | if (s->chrom < chrom || (s->chrom == chrom && s->pos < pos)) lo = mid + 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->chrom < chrom || (s->chrom == chrom && s->pos < pos)) lo = mid + 1; |
1102 |
222 |
1324 |
| if (s->chrom < chrom || (s->chrom == chrom && s->pos < pos)) lo = mid + 1; |
964 |
138 |
1102 |
| if (s->chrom < chrom || (s->chrom == chrom && s->pos < pos)) lo = mid + 1; |
194 |
770 |
964 |
|
| 1765 |
908 |
▶ | else if (s->chrom == chrom && s->pos == pos) return s; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (s->chrom == chrom && s->pos == pos) return s; |
770 |
138 |
908 |
| else if (s->chrom == chrom && s->pos == pos) return s; |
307 |
463 |
770 |
|
| 1766 |
601 |
| else hi = mid; |
| 1767 |
|
| } |
| 1768 |
25 |
| return NULL; |
| 1769 |
|
| } |
| 1770 |
|
| |
| 1771 |
|
| /*The marker whose rs number is the one name p..f, or NULL.*/ |
| 1772 |
151 |
| static const hps_site *hps_find_name(const hps_reader *t, const char *p, const char *f) { |
| 1773 |
151 |
| size_t n = (size_t)(f - p); |
| 1774 |
151 |
▶ | if (n == 0 || n >= HPS_ID) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 0 || n >= HPS_ID) return NULL; |
151 |
0 |
151 |
| if (n == 0 || n >= HPS_ID) return NULL; |
0 |
151 |
151 |
|
| 1775 |
151 |
| size_t lo = 0, hi = t->entries; |
| 1776 |
827 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
772 |
55 |
827 |
|
| 1777 |
772 |
| size_t mid = lo + (hi - lo) / 2; |
| 1778 |
772 |
| const hps_site *s = t->site + mid; |
| 1779 |
772 |
| int c = strncmp(s->id, p, n); |
| 1780 |
772 |
▶ | if (c == 0 && s->id[n] != '\0') c = 1; // s->id runs on where p..f has ended, so it sorts after |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == 0 && s->id[n] != '\0') c = 1; // s->id runs on where p..f has ended, so it sorts after |
102 |
670 |
772 |
| if (c == 0 && s->id[n] != '\0') c = 1; // s->id runs on where p..f has ended, so it sorts after |
6 |
96 |
102 |
|
| 1781 |
772 |
▶ | if (c < 0) lo = mid + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0) lo = mid + 1; |
168 |
604 |
772 |
|
| 1782 |
604 |
▶ | else if (c == 0) return s; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == 0) return s; |
96 |
508 |
604 |
|
| 1783 |
508 |
| else hi = mid; |
| 1784 |
|
| } |
| 1785 |
55 |
| return NULL; |
| 1786 |
|
| } |
| 1787 |
|
| |
| 1788 |
|
| /*The marker whose rs number is in the ID field p..f, or NULL. An ID of several |
| 1789 |
|
| names, which the VCF specification separates with semicolons ("Semicolon- |
| 1790 |
|
| separated list of unique identifiers where available", VCFv4.2 section 1.4.1, |
| 1791 |
|
| field 3), is matched on the first of them that is a marker's. Up to 2026-11-10 |
| 1792 |
|
| only its first name was looked at, so that "foo;rs12913832" matched nothing.*/ |
| 1793 |
103 |
| static const hps_site *hps_find_id(const hps_reader *t, const char *p, const char *f) { |
| 1794 |
48 |
| for (;;) { |
| 1795 |
151 |
| const char *semi = (const char*)memchr(p, ';', (size_t)(f - p)); |
| 1796 |
151 |
▶ | const hps_site *s = hps_find_name(t, p, semi ? semi : f); |
Branch: 1 branch
| Branch | True | False | Total |
| const hps_site *s = hps_find_name(t, p, semi ? semi : f); |
96 |
55 |
151 |
|
| 1797 |
151 |
▶ | if (s || !semi) return s; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s || !semi) return s; |
55 |
96 |
151 |
| if (s || !semi) return s; |
7 |
48 |
55 |
|
| 1798 |
48 |
| p = semi + 1; |
| 1799 |
|
| } |
| 1800 |
|
| } |
| 1801 |
|
| |
| 1802 |
|
| /*Whether the ALT a..ae is the REF r..re with the one base put in, in either |
| 1803 |
|
| case. Past the bases the two share at the start, the first base of ALT that |
| 1804 |
|
| differs is the one put in, and the rest of ALT must be the rest of REF; in a |
| 1805 |
|
| run of that base it could have gone anywhere in the run, and gives the same |
| 1806 |
|
| ALT. An MNV or a symbolic ALT such as <INS> fails it.*/ |
| 1807 |
28 |
| static bool hps_inserts(char base, const char *r, const char *re, const char *a, const char *ae) { |
| 1808 |
28 |
| const size_t rl = (size_t)(re - r); |
| 1809 |
28 |
▶ | if ((size_t)(ae - a) != rl + 1) return FALSE; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)(ae - a) != rl + 1) return FALSE; |
2 |
26 |
28 |
|
| 1810 |
26 |
| size_t i = 0; // the first place ALT differs from REF, read after the loop |
| 1811 |
52 |
▶ | while (i < rl && toUPPER(r[i]) == toUPPER(a[i])) i++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (i < rl && toUPPER(r[i]) == toUPPER(a[i])) i++; |
27 |
25 |
52 |
| while (i < rl && toUPPER(r[i]) == toUPPER(a[i])) i++; |
1 |
26 |
27 |
| while (i < rl && toUPPER(r[i]) == toUPPER(a[i])) i++; |
1 |
26 |
27 |
| while (i < rl && toUPPER(r[i]) == toUPPER(a[i])) i++; |
26 |
1 |
27 |
|
| 1812 |
26 |
▶ | if (toUPPER(a[i]) != toUPPER(base)) return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (toUPPER(a[i]) != toUPPER(base)) return FALSE; |
1 |
25 |
26 |
| if (toUPPER(a[i]) != toUPPER(base)) return FALSE; |
0 |
26 |
26 |
| if (toUPPER(a[i]) != toUPPER(base)) return FALSE; |
2 |
24 |
26 |
|
| 1813 |
24 |
▶ | for (size_t j = i; j < rl; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t j = i; j < rl; j++) { |
0 |
24 |
24 |
|
| 1814 |
0 |
▶ | if (toUPPER(r[j]) != toUPPER(a[j + 1])) return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (toUPPER(r[j]) != toUPPER(a[j + 1])) return FALSE; |
0 |
0 |
0 |
| if (toUPPER(r[j]) != toUPPER(a[j + 1])) return FALSE; |
0 |
0 |
0 |
| if (toUPPER(r[j]) != toUPPER(a[j + 1])) return FALSE; |
0 |
0 |
0 |
|
| 1815 |
|
| } |
| 1816 |
24 |
| return TRUE; |
| 1817 |
|
| } |
| 1818 |
|
| |
| 1819 |
|
| /*Where the marker's counted allele is among REF (0) and the ALTs (1, 2, ...), |
| 1820 |
|
| or -1 when the record does not carry it at all, which leaves every call of the |
| 1821 |
|
| record 0 copies of it. REF is ref..ref_end and ALT is alt..alt_end.*/ |
| 1822 |
400 |
| static short int hps_allele(const hps_site *s, const char *ref, const char *ref_end, const char *alt, const char *alt_end) { |
| 1823 |
400 |
| size_t ref_len = (size_t)(ref_end - ref); |
| 1824 |
400 |
▶ | if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
Branch: 5 branches
| Branch | True | False | Total |
| if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
373 |
27 |
400 |
| if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
373 |
0 |
373 |
| if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
1 |
372 |
373 |
| if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
0 |
373 |
373 |
| if (!s->insertion && ref_len == 1 && toUPPER(*ref) == toUPPER(s->allele)) return 0; |
81 |
292 |
373 |
|
| 1825 |
319 |
| short int i = 1; |
| 1826 |
390 |
▶ | for (const char *q = alt; q <= alt_end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *q = alt; q <= alt_end; i++) { |
347 |
43 |
390 |
|
| 1827 |
347 |
| const char *c = (const char*)memchr(q, ',', (size_t)(alt_end - q)); |
| 1828 |
347 |
▶ | const char *ce = c ? c : alt_end; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *ce = c ? c : alt_end; |
28 |
319 |
347 |
|
| 1829 |
347 |
▶ | if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
Branch: 6 branches
| Branch | True | False | Total |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
28 |
319 |
347 |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
319 |
0 |
319 |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
0 |
319 |
319 |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
0 |
319 |
319 |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
252 |
67 |
319 |
| if (s->insertion ? hps_inserts(s->allele, ref, ref_end, q, ce) : (ce - q == 1 && toUPPER(*q) == toUPPER(s->allele))) return i; |
276 |
71 |
347 |
|
| 1830 |
71 |
▶ | if (i == 127) break; // a short int holds no more, and no record of these markers has so many ALTs |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == 127) break; // a short int holds no more, and no record of these markers has so many ALTs |
0 |
71 |
71 |
|
| 1831 |
71 |
| q = ce + 1; |
| 1832 |
|
| } |
| 1833 |
43 |
| return -1; |
| 1834 |
|
| } |
| 1835 |
|
| |
| 1836 |
|
| /*The sample field p..e at a marker whose counted allele is at idx, whose |
| 1837 |
|
| FORMAT has GT at gt and DP at dp: how many of the two alleles are idx, or |
| 1838 |
|
| HPS_NO_CALL for a genotype that is missing or partly missing, is not diploid, |
| 1839 |
|
| or has a DP below min_depth. The panel is autosomal, so a haploid call is not |
| 1840 |
|
| a dosage and is not counted as one.*/ |
| 1841 |
2948 |
| static unsigned char hps_dose(const vcf_reader *r, const char *p, const char *e, size_t gt, size_t dp, short int idx) { |
| 1842 |
|
| const char *s, *sf; |
| 1843 |
2948 |
▶ | if (vcf_shallow(r, p, e, dp)) return HPS_NO_CALL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (vcf_shallow(r, p, e, dp)) return HPS_NO_CALL; |
116 |
2832 |
2948 |
|
| 1844 |
2832 |
▶ | if (gt == VCF_ABSENT || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return HPS_NO_CALL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (gt == VCF_ABSENT || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return HPS_NO_CALL; |
2832 |
0 |
2832 |
| if (gt == VCF_ABSENT || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return HPS_NO_CALL; |
2832 |
0 |
2832 |
| if (gt == VCF_ABSENT || !vcf_subfield(p, e, gt, &s, &sf) || s == sf) return HPS_NO_CALL; |
0 |
2832 |
2832 |
|
| 1845 |
2832 |
| unsigned short int alleles = 0, carried = 0; |
| 1846 |
8139 |
▶ | while (s < sf) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < sf) { |
5462 |
2677 |
8139 |
|
| 1847 |
5462 |
▶ | if (*s < '0' || *s > '9') return HPS_NO_CALL; // missing ("."), or a separator where an allele belongs |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s < '0' || *s > '9') return HPS_NO_CALL; // missing ("."), or a separator where an allele belongs |
5307 |
155 |
5462 |
| if (*s < '0' || *s > '9') return HPS_NO_CALL; // missing ("."), or a separator where an allele belongs |
0 |
5307 |
5307 |
|
| 1848 |
5307 |
| UV a = 0; |
| 1849 |
10614 |
▶ | for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
Branch: 3 branches
| Branch | True | False | Total |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
7937 |
2677 |
10614 |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
5471 |
2466 |
7937 |
| for (; s < sf && *s >= '0' && *s <= '9'; s++) { |
5307 |
164 |
5471 |
|
| 1850 |
5307 |
▶ | if (a > (UV_MAX - 9) / 10) return HPS_NO_CALL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a > (UV_MAX - 9) / 10) return HPS_NO_CALL; |
0 |
5307 |
5307 |
|
| 1851 |
5307 |
| a = a * 10 + (UV)(*s - '0'); |
| 1852 |
|
| } |
| 1853 |
5307 |
▶ | if (++alleles > 2) return HPS_NO_CALL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++alleles > 2) return HPS_NO_CALL; |
0 |
5307 |
5307 |
|
| 1854 |
5307 |
▶ | if (idx >= 0 && a == (UV)idx) carried++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx >= 0 && a == (UV)idx) carried++; |
5141 |
166 |
5307 |
| if (idx >= 0 && a == (UV)idx) carried++; |
2611 |
2530 |
5141 |
|
| 1855 |
5307 |
▶ | if (s < sf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s < sf) { |
2630 |
2677 |
5307 |
|
| 1856 |
2630 |
▶ | if (*s != '/' && *s != '|') return HPS_NO_CALL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s != '/' && *s != '|') return HPS_NO_CALL; |
164 |
2466 |
2630 |
| if (*s != '/' && *s != '|') return HPS_NO_CALL; |
0 |
164 |
164 |
|
| 1857 |
2630 |
▶ | if (++s == sf) return HPS_NO_CALL; // a separator with no allele after it |
Branch: 1 branch
| Branch | True | False | Total |
| if (++s == sf) return HPS_NO_CALL; // a separator with no allele after it |
0 |
2630 |
2630 |
|
| 1858 |
|
| } |
| 1859 |
|
| } |
| 1860 |
2677 |
▶ | if (alleles != 2) return HPS_NO_CALL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alleles != 2) return HPS_NO_CALL; |
47 |
2630 |
2677 |
|
| 1861 |
2630 |
| return (unsigned char)carried; |
| 1862 |
|
| } |
| 1863 |
|
| |
| 1864 |
49 |
| static void hps_header(pTHX_ vcf_reader *r) { |
| 1865 |
49 |
| hps_reader *t = (hps_reader*)r; |
| 1866 |
49 |
| Newx(t->dose, r->n * t->sites, unsigned char); |
| 1867 |
49 |
| SAVEFREEPV(t->dose); // freed at the caller's LEAVE, croak or not; see vcf_read()'s block |
| 1868 |
49 |
| memset(t->dose, HPS_ABSENT, r->n * t->sites); |
| 1869 |
49 |
| Newxz(t->seen, t->sites, bool); |
| 1870 |
49 |
| SAVEFREEPV(t->seen); |
| 1871 |
49 |
| Newxz(t->carried, t->sites, bool); |
| 1872 |
49 |
| SAVEFREEPV(t->carried); |
| 1873 |
49 |
| } |
| 1874 |
|
| |
| 1875 |
440 |
| static void hps_record(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 1876 |
440 |
| hps_reader *t = (hps_reader*)r; |
| 1877 |
440 |
| const char *f = vcf_field_end(p, e); |
| 1878 |
|
| const hps_site *s; |
| 1879 |
440 |
▶ | if (t->by_id) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->by_id) { |
103 |
337 |
440 |
|
| 1880 |
163 |
▶ | if (f >= e) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (f >= e) return; |
0 |
103 |
103 |
|
| 1881 |
103 |
| const char *pos = f + 1; |
| 1882 |
103 |
| const char *pos_end = vcf_field_end(pos, e); |
| 1883 |
103 |
▶ | if (pos_end >= e) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos_end >= e) return; |
0 |
103 |
103 |
|
| 1884 |
103 |
| const char *id = pos_end + 1; |
| 1885 |
103 |
| s = hps_find_id(t, id, vcf_field_end(id, e)); |
| 1886 |
|
| } else { |
| 1887 |
337 |
| unsigned short int chrom = vcf_chrom(p, f); |
| 1888 |
337 |
▶ | if (chrom == 0 || chrom > 31 || !(t->chroms & ((uint32_t)1 << chrom))) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (chrom == 0 || chrom > 31 || !(t->chroms & ((uint32_t)1 << chrom))) return; |
337 |
0 |
337 |
| if (chrom == 0 || chrom > 31 || !(t->chroms & ((uint32_t)1 << chrom))) return; |
337 |
0 |
337 |
| if (chrom == 0 || chrom > 31 || !(t->chroms & ((uint32_t)1 << chrom))) return; |
5 |
332 |
337 |
|
| 1889 |
332 |
▶ | if (f >= e) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (f >= e) return; |
0 |
332 |
332 |
|
| 1890 |
332 |
| const char *pos = f + 1; |
| 1891 |
332 |
| s = hps_find(t, chrom, vcf_pos(aTHX_ r, pos, vcf_field_end(pos, e))); |
| 1892 |
|
| } |
| 1893 |
435 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
32 |
403 |
435 |
|
| 1894 |
|
| const char *col[9]; |
| 1895 |
403 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 1896 |
403 |
| const char *ref = col[3], *ref_end = col[4] - 1; |
| 1897 |
403 |
▶ | if (!s->insertion && ref_end - ref != 1) return; // some other variant sharing the marker's position |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s->insertion && ref_end - ref != 1) return; // some other variant sharing the marker's position |
376 |
27 |
403 |
| if (!s->insertion && ref_end - ref != 1) return; // some other variant sharing the marker's position |
3 |
373 |
376 |
|
| 1898 |
400 |
| short int idx = hps_allele(s, ref, ref_end, col[4], col[5] - 1); |
| 1899 |
|
| // A site split into one record per ALT reaches here once for each, and only |
| 1900 |
|
| // the record that carries the counted allele says anything about it; the |
| 1901 |
|
| // others would count every call 0. So a marker already counted from a record |
| 1902 |
|
| // that carried it is left alone, and one counted from a record that did not |
| 1903 |
|
| // is counted again when a later record does carry it. |
| 1904 |
400 |
▶ | if (t->seen[s->k] && (t->carried[s->k] || idx < 0)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (t->seen[s->k] && (t->carried[s->k] || idx < 0)) return; |
40 |
360 |
400 |
| if (t->seen[s->k] && (t->carried[s->k] || idx < 0)) return; |
20 |
20 |
40 |
| if (t->seen[s->k] && (t->carried[s->k] || idx < 0)) return; |
0 |
20 |
20 |
|
| 1905 |
|
| size_t gt, dp; |
| 1906 |
380 |
| vcf_format(col[8], format_end, >, &dp); |
| 1907 |
380 |
| t->seen[s->k] = TRUE; |
| 1908 |
380 |
▶ | if (idx >= 0) t->carried[s->k] = TRUE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx >= 0) t->carried[s->k] = TRUE; |
357 |
23 |
380 |
|
| 1909 |
380 |
| p = format_end + 1; |
| 1910 |
3318 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
2938 |
380 |
3318 |
|
| 1911 |
2938 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 1912 |
2938 |
| t->dose[i * t->sites + s->k] = hps_dose(r, p, se, gt, dp, idx); |
| 1913 |
2938 |
| p = se + 1; |
| 1914 |
|
| } |
| 1915 |
380 |
| vcf_samples_end(aTHX_ r, p, e); |
| 1916 |
|
| } |
| 1917 |
|
| |
| 1918 |
|
| /*What perl gave to search for, as hps_site's. Each element of panel is |
| 1919 |
|
| [chromosome, position, allele, insertion, rs number, marker], the chromosome |
| 1920 |
|
| PLINK's number or 0 where no position is given, and marker the 0-based place |
| 1921 |
|
| in the panel of markers that the entry's genotypes belong to. There is |
| 1922 |
|
| normally one entry for each marker, and more than one where a marker is |
| 1923 |
|
| searched for under an rs number that has since been merged into another, which |
| 1924 |
|
| the file may use either of; markers says how many markers there are in all.*/ |
| 1925 |
52 |
| static void hps_panel(pTHX_ hps_reader *t, AV *panel, size_t markers) { |
| 1926 |
52 |
| t->entries = (size_t)(av_len(panel) + 1); |
| 1927 |
52 |
| t->sites = markers; |
| 1928 |
52 |
▶ | if (!t->entries || !markers) croak("%s: the panel is empty", __func__); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!t->entries || !markers) croak("%s: the panel is empty", __func__); |
52 |
0 |
52 |
| if (!t->entries || !markers) croak("%s: the panel is empty", __func__); |
0 |
52 |
52 |
|
| 1929 |
52 |
▶ | Newxz(t->site, t->entries, hps_site); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t->site, t->entries, hps_site); |
0 |
52 |
52 |
|
| 1930 |
52 |
| SAVEFREEPV(t->site); |
| 1931 |
1174 |
▶ | for (SSize_t i = 0; i <= av_len(panel); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (SSize_t i = 0; i <= av_len(panel); i++) { |
1123 |
51 |
1174 |
|
| 1932 |
1123 |
| SV **row = av_fetch(panel, i, 0); |
| 1933 |
1123 |
▶ | if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) croak("%s: entry %" IVdf " of the panel isn't an array ref", __func__, (IV)i); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) croak("%s: entry %" IVdf " of the panel isn't an array ref", __func__, (IV)i); |
1123 |
0 |
1123 |
| if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) croak("%s: entry %" IVdf " of the panel isn't an array ref", __func__, (IV)i); |
1123 |
0 |
1123 |
| if (!row || !SvROK(*row) || SvTYPE(SvRV(*row)) != SVt_PVAV) croak("%s: entry %" IVdf " of the panel isn't an array ref", __func__, (IV)i); |
0 |
1123 |
1123 |
|
| 1934 |
1123 |
| AV *m = (AV*)SvRV(*row); |
| 1935 |
1123 |
▶ | if (av_len(m) != 5) croak("%s: entry %" IVdf " of the panel has %" IVdf " fields, and needs 6", __func__, (IV)i, (IV)(av_len(m) + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(m) != 5) croak("%s: entry %" IVdf " of the panel has %" IVdf " fields, and needs 6", __func__, (IV)i, (IV)(av_len(m) + 1)); |
0 |
1123 |
1123 |
|
| 1936 |
|
| SV **fld[6]; |
| 1937 |
7861 |
▶ | for (unsigned short int k = 0; k < 6; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 6; k++) { |
6738 |
1123 |
7861 |
|
| 1938 |
6738 |
| fld[k] = av_fetch(m, (SSize_t)k, 0); |
| 1939 |
6738 |
▶ | if (!fld[k]) croak("%s: field %u of entry %" IVdf " of the panel is missing", __func__, (unsigned)k, (IV)i); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fld[k]) croak("%s: field %u of entry %" IVdf " of the panel is missing", __func__, (unsigned)k, (IV)i); |
0 |
6738 |
6738 |
|
| 1940 |
|
| } |
| 1941 |
1123 |
| hps_site *s = t->site + i; |
| 1942 |
1123 |
| s->k = (size_t)SvUV(*fld[5]); |
| 1943 |
1123 |
▶ | if (s->k >= markers) croak("%s: entry %" IVdf " of the panel is marker %" UVuf ", and there are %" UVuf, __func__, (IV)i, (UV)s->k, (UV)markers); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->k >= markers) croak("%s: entry %" IVdf " of the panel is marker %" UVuf ", and there are %" UVuf, __func__, (IV)i, (UV)s->k, (UV)markers); |
0 |
1123 |
1123 |
|
| 1944 |
1123 |
| s->chrom = (unsigned short int)SvUV(*fld[0]); |
| 1945 |
1123 |
| s->pos = SvUV(*fld[1]); |
| 1946 |
1123 |
| s->insertion = SvTRUE(*fld[3]) ? TRUE : FALSE; |
| 1947 |
|
| STRLEN len; |
| 1948 |
1123 |
| const char *allele = SvPV(*fld[2], len); |
| 1949 |
1123 |
▶ | if (len != 1) croak("%s: entry %" IVdf " of the panel counts \"%.*s\", which isn't one base", __func__, (IV)i, (int)len, allele); |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != 1) croak("%s: entry %" IVdf " of the panel counts \"%.*s\", which isn't one base", __func__, (IV)i, (int)len, allele); |
1 |
1122 |
1123 |
|
| 1950 |
1122 |
| s->allele = allele[0]; |
| 1951 |
1122 |
| const char *id = SvPV(*fld[4], len); |
| 1952 |
1122 |
▶ | if (!len || len >= HPS_ID) croak("%s: entry %" IVdf " of the panel is named \"%.*s\", which is %s", __func__, (IV)i, (int)len, id, |
Branch: 3 branches
| Branch | True | False | Total |
| if (!len || len >= HPS_ID) croak("%s: entry %" IVdf " of the panel is named \"%.*s\", which is %s", __func__, (IV)i, (int)len, id, |
1122 |
0 |
1122 |
| if (!len || len >= HPS_ID) croak("%s: entry %" IVdf " of the panel is named \"%.*s\", which is %s", __func__, (IV)i, (int)len, id, |
0 |
1122 |
1122 |
| if (!len || len >= HPS_ID) croak("%s: entry %" IVdf " of the panel is named \"%.*s\", which is %s", __func__, (IV)i, (int)len, id, |
0 |
0 |
0 |
|
| 1953 |
|
| len ? "longer than an rs number" : "empty"); |
| 1954 |
1122 |
| memcpy(s->id, id, len); |
| 1955 |
1122 |
| s->id[len] = '\0'; |
| 1956 |
1122 |
▶ | if (s->chrom && s->chrom < 32) t->chroms |= (uint32_t)1 << s->chrom; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->chrom && s->chrom < 32) t->chroms |= (uint32_t)1 << s->chrom; |
996 |
126 |
1122 |
| if (s->chrom && s->chrom < 32) t->chroms |= (uint32_t)1 << s->chrom; |
996 |
0 |
996 |
|
| 1957 |
|
| } |
| 1958 |
51 |
▶ | qsort(t->site, t->entries, sizeof(hps_site), t->by_id ? hps_cmp_id : hps_cmp_pos); |
Branch: 1 branch
| Branch | True | False | Total |
| qsort(t->site, t->entries, sizeof(hps_site), t->by_id ? hps_cmp_id : hps_cmp_pos); |
3 |
48 |
51 |
|
| 1959 |
51 |
| } |
| 1960 |
|
| |
| 1961 |
|
| /*The panel's genotypes for every sample of the VCF name, which next returns a |
| 1962 |
|
| block of at a time. Returns sample => an array of one value for each marker of |
| 1963 |
|
| the panel, in the panel's order: 0, 1 or 2 copies of the counted allele, or |
| 1964 |
|
| undef where the sample had no call. *names is the samples in file order and |
| 1965 |
|
| *seen one 0 or 1 for each marker, saying whether the file held a record of it |
| 1966 |
|
| at all, which tells a marker nobody was called at from one nobody carries.*/ |
| 1967 |
17 |
| static HV *hps_dosage_vcf(pTHX_ SV *next, const char *name, UV min_depth, AV *panel, size_t markers, bool by_id, AV **names, AV **seen) { |
| 1968 |
|
| hps_reader t; |
| 1969 |
17 |
| Zero(&t, 1, hps_reader); |
| 1970 |
17 |
| t.r.name = name; |
| 1971 |
17 |
| t.r.min_depth = min_depth; |
| 1972 |
17 |
| t.r.on_header = hps_header; |
| 1973 |
17 |
| t.r.on_record = hps_record; |
| 1974 |
17 |
| t.by_id = by_id; |
| 1975 |
17 |
| hps_panel(aTHX_ &t, panel, markers); |
| 1976 |
16 |
| vcf_read(aTHX_ next, &t.r); |
| 1977 |
15 |
| HV *out = t.r.samples; |
| 1978 |
47 |
▶ | for (size_t i = 0; i < t.r.n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < t.r.n; i++) { |
32 |
15 |
47 |
|
| 1979 |
32 |
| SV **sample = av_fetch(t.r.names, (SSize_t)i, 0); |
| 1980 |
32 |
▶ | if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
0 |
32 |
32 |
|
| 1981 |
32 |
| AV *row = newAV(); |
| 1982 |
32 |
| av_extend(row, (SSize_t)t.sites - 1); |
| 1983 |
1188 |
▶ | for (size_t k = 0; k < t.sites; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < t.sites; k++) { |
1156 |
32 |
1188 |
|
| 1984 |
1156 |
| unsigned char d = t.dose[i * t.sites + k]; |
| 1985 |
1156 |
▶ | av_push(row, d <= 2 ? newSVuv(d) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(row, d <= 2 ? newSVuv(d) : newSV(0)); |
705 |
451 |
1156 |
|
| 1986 |
|
| } |
| 1987 |
32 |
| (void)hv_store_ent(out, *sample, newRV_noinc((SV*)row), 0); // in place of the column vcf_header() stored |
| 1988 |
|
| } |
| 1989 |
15 |
| AV *was = newAV(); |
| 1990 |
15 |
| av_extend(was, (SSize_t)t.sites - 1); |
| 1991 |
552 |
▶ | for (size_t k = 0; k < t.sites; k++) av_push(was, newSVuv(t.seen[k] ? 1 : 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < t.sites; k++) av_push(was, newSVuv(t.seen[k] ? 1 : 0)); |
537 |
15 |
552 |
|
| 1992 |
15 |
| *names = t.r.names; |
| 1993 |
15 |
| *seen = was; |
| 1994 |
15 |
| return out; |
| 1995 |
|
| } |
| 1996 |
|
| |
| 1997 |
|
| /*The genotypes behind blood_type(). |
| 1998 |
|
| |
| 1999 |
|
| Its markers are a panel like hirisplex_genotypes()'s -- three of ABO and the |
| 2000 |
|
| sites of RHD at which somebody who has the gene almost always differs from |
| 2001 |
|
| the reference -- so hps_record() reads them, and this adds the two things a |
| 2002 |
|
| panel of markers cannot say. |
| 2003 |
|
| |
| 2004 |
|
| One is a record of a deletion that covers the whole RHD gene, which is how |
| 2005 |
|
| 1000 Genomes' phase 3 writes the usual cause of RhD negative (1:25592642, |
| 2006 |
|
| ALT <CN0>, END=25661222): its genotype is the answer, where the SNVs can only |
| 2007 |
|
| say whether a sample has the gene at all. A deletion is an ALT of <CN0>, |
| 2008 |
|
| <DEL> or <DEL:...>, the VCF specification's symbolic alleles, and how far it |
| 2009 |
|
| runs is INFO's END, or else POS plus SVLEN (VCFv4.2, "INFO keys used for |
| 2010 |
|
| structural variants"). |
| 2011 |
|
| |
| 2012 |
|
| The other is what the file could have shown. A VCF of one genome usually |
| 2013 |
|
| lists only the sites where its sample carries an ALT allele, so perl takes a |
| 2014 |
|
| marker with no record as homozygous REF; that is wrong for a file that could |
| 2015 |
|
| not have held the record, so each chromosome a marker is on is noted when any |
| 2016 |
|
| record of it is read, and again when a record of it has an allele longer than |
| 2017 |
|
| one base, without which a file cannot have held ABO's insertion. |
| 2018 |
|
| |
| 2019 |
|
| What is kept is hps_reader's byte a sample a marker and one more byte a |
| 2020 |
|
| sample for the deletion.*/ |
| 2021 |
|
| |
| 2022 |
|
| typedef struct { |
| 2023 |
|
| hps_reader h; // first, so that the vcf_reader * bt_header() and bt_record() are given is this, and is what hps_record() takes |
| 2024 |
|
| unsigned short int chrom; // the gene's chromosome, in vcf_chrom()'s numbering |
| 2025 |
|
| UV start, end; // the gene, 1-based and inclusive, which a deletion has to cover |
| 2026 |
|
| uint32_t chroms; // bit c set for each chromosome c that a marker or the gene is on; a record of any other is read no further than CHROM |
| 2027 |
|
| uint32_t seen; // bit c set once a record of chromosome c has been read |
| 2028 |
|
| uint32_t longer; // bit c set once a record of chromosome c had a REF or an ALT longer than one base |
| 2029 |
|
| unsigned char *deleted; // one per sample: how many copies of a deletion covering the gene it has, 0 to 2, or HPS_ABSENT or HPS_NO_CALL |
| 2030 |
|
| } bt_reader; |
| 2031 |
|
| |
| 2032 |
34 |
| static void bt_header(pTHX_ vcf_reader *r) { |
| 2033 |
34 |
| bt_reader *t = (bt_reader*)r; |
| 2034 |
34 |
| hps_header(aTHX_ r); |
| 2035 |
34 |
| Newx(t->deleted, r->n, unsigned char); |
| 2036 |
34 |
| SAVEFREEPV(t->deleted); // freed at the caller's LEAVE, croak or not; see vcf_read()'s block |
| 2037 |
34 |
| memset(t->deleted, HPS_ABSENT, r->n); |
| 2038 |
34 |
| } |
| 2039 |
|
| |
| 2040 |
|
| /*Whether any allele of the ALT field p..e is longer than one base. A symbolic |
| 2041 |
|
| allele ("<DEL>", "<NON_REF>") is a name, not bases, and is not one.*/ |
| 2042 |
169 |
| static bool bt_longer(const char *p, const char *e) { |
| 2043 |
177 |
▶ | for (const char *q = p; q < e;) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *q = p; q < e;) { |
177 |
0 |
177 |
|
| 2044 |
177 |
| const char *c = (const char*)memchr(q, ',', (size_t)(e - q)); |
| 2045 |
177 |
▶ | const char *ce = c ? c : e; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *ce = c ? c : e; |
8 |
169 |
177 |
|
| 2046 |
177 |
▶ | if (ce - q > 1 && *q != '<') return TRUE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ce - q > 1 && *q != '<') return TRUE; |
28 |
149 |
177 |
| if (ce - q > 1 && *q != '<') return TRUE; |
16 |
12 |
28 |
|
| 2047 |
161 |
▶ | if (!c) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) break; |
153 |
8 |
161 |
|
| 2048 |
8 |
| q = ce + 1; |
| 2049 |
|
| } |
| 2050 |
153 |
| return FALSE; |
| 2051 |
|
| } |
| 2052 |
|
| |
| 2053 |
|
| /*Where a deletion is among the alleles of the ALT field p..e: 1 for the first |
| 2054 |
|
| ALT, 2 for the second and so on, or -1 when none of them is one.*/ |
| 2055 |
132 |
| static short int bt_deletion(const char *p, const char *e) { |
| 2056 |
132 |
| short int i = 1; |
| 2057 |
132 |
▶ | for (const char *q = p; q < e; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *q = p; q < e; i++) { |
132 |
0 |
132 |
|
| 2058 |
132 |
| const char *c = (const char*)memchr(q, ',', (size_t)(e - q)); |
| 2059 |
132 |
▶ | const char *ce = c ? c : e; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *ce = c ? c : e; |
1 |
131 |
132 |
|
| 2060 |
132 |
| const size_t n = (size_t)(ce - q); |
| 2061 |
132 |
▶ | if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
Branch: 5 branches
| Branch | True | False | Total |
| if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
10 |
122 |
132 |
| if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
8 |
2 |
10 |
| if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
1 |
7 |
8 |
| if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
1 |
122 |
123 |
| if ((n == 5 && (memcmp(q, "<CN0>", 5) == 0 || memcmp(q, "<DEL>", 5) == 0)) || (n > 5 && memcmp(q, "<DEL:", 5) == 0)) return i; |
1 |
0 |
1 |
|
| 2062 |
122 |
▶ | if (!c || i == 127) break; // a short int holds no more, and no record has so many ALTs |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c || i == 127) break; // a short int holds no more, and no record has so many ALTs |
0 |
122 |
122 |
| if (!c || i == 127) break; // a short int holds no more, and no record has so many ALTs |
0 |
0 |
0 |
|
| 2063 |
0 |
| q = ce + 1; |
| 2064 |
|
| } |
| 2065 |
122 |
| return -1; |
| 2066 |
|
| } |
| 2067 |
|
| |
| 2068 |
|
| /*The whole number that key, which ends in its "=", has in the INFO field |
| 2069 |
|
| p..e, in *v, or FALSE when INFO lacks the key or its value is not one. A "-" |
| 2070 |
|
| before the number is passed over, since SVLEN is negative for a deletion, and |
| 2071 |
|
| the number ends at a comma, since SVLEN has a value for each ALT.*/ |
| 2072 |
13 |
| static bool bt_info(const char *p, const char *e, const char *key, UV *v) { |
| 2073 |
13 |
| const size_t kl = strlen(key); |
| 2074 |
16 |
▶ | for (const char *q = p; q < e;) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (const char *q = p; q < e;) { |
16 |
0 |
16 |
|
| 2075 |
16 |
| const char *semi = (const char*)memchr(q, ';', (size_t)(e - q)); |
| 2076 |
16 |
▶ | const char *qe = semi ? semi : e; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *qe = semi ? semi : e; |
4 |
12 |
16 |
|
| 2077 |
16 |
▶ | if ((size_t)(qe - q) > kl && memcmp(q, key, kl) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((size_t)(qe - q) > kl && memcmp(q, key, kl) == 0) { |
16 |
0 |
16 |
| if ((size_t)(qe - q) > kl && memcmp(q, key, kl) == 0) { |
9 |
7 |
16 |
|
| 2078 |
9 |
| const char *s = q + kl; |
| 2079 |
9 |
▶ | if (s < qe && *s == '-') s++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s < qe && *s == '-') s++; |
9 |
0 |
9 |
| if (s < qe && *s == '-') s++; |
2 |
7 |
9 |
|
| 2080 |
9 |
▶ | if (s == qe || *s < '0' || *s > '9') return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (s == qe || *s < '0' || *s > '9') return FALSE; |
9 |
0 |
9 |
| if (s == qe || *s < '0' || *s > '9') return FALSE; |
9 |
0 |
9 |
| if (s == qe || *s < '0' || *s > '9') return FALSE; |
0 |
9 |
9 |
|
| 2081 |
9 |
| UV n = 0; |
| 2082 |
74 |
▶ | for (; s < qe && *s != ','; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (; s < qe && *s != ','; s++) { |
65 |
9 |
74 |
| for (; s < qe && *s != ','; s++) { |
65 |
0 |
65 |
|
| 2083 |
65 |
▶ | if (*s < '0' || *s > '9' || n > (UV_MAX - 9) / 10) return FALSE; |
Branch: 3 branches
| Branch | True | False | Total |
| if (*s < '0' || *s > '9' || n > (UV_MAX - 9) / 10) return FALSE; |
65 |
0 |
65 |
| if (*s < '0' || *s > '9' || n > (UV_MAX - 9) / 10) return FALSE; |
65 |
0 |
65 |
| if (*s < '0' || *s > '9' || n > (UV_MAX - 9) / 10) return FALSE; |
0 |
65 |
65 |
|
| 2084 |
65 |
| n = n * 10 + (UV)(*s - '0'); |
| 2085 |
|
| } |
| 2086 |
9 |
| *v = n; |
| 2087 |
9 |
| return TRUE; |
| 2088 |
|
| } |
| 2089 |
7 |
▶ | if (!semi) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!semi) break; |
4 |
3 |
7 |
|
| 2090 |
3 |
| q = qe + 1; |
| 2091 |
|
| } |
| 2092 |
4 |
| return FALSE; |
| 2093 |
|
| } |
| 2094 |
|
| |
| 2095 |
174 |
| static void bt_record(pTHX_ vcf_reader *r, const char *p, const char *e) { |
| 2096 |
174 |
| bt_reader *t = (bt_reader*)r; |
| 2097 |
174 |
| const char *f = vcf_field_end(p, e); |
| 2098 |
174 |
| const unsigned short int chrom = vcf_chrom(p, f); |
| 2099 |
341 |
▶ | if (chrom == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom == 0) return; |
0 |
174 |
174 |
|
| 2100 |
174 |
| const uint32_t bit = (uint32_t)1 << chrom; // chrom is 24 at most |
| 2101 |
174 |
▶ | if (!(t->chroms & bit)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(t->chroms & bit)) return; |
3 |
171 |
174 |
|
| 2102 |
171 |
| t->seen |= bit; |
| 2103 |
|
| const char *col[9]; |
| 2104 |
171 |
| const char *format_end = vcf_columns(aTHX_ r, p, f, e, col); |
| 2105 |
170 |
| const char *alt = col[4], *alt_end = col[5] - 1; |
| 2106 |
170 |
▶ | if (col[4] - 1 - col[3] > 1 || bt_longer(alt, alt_end)) t->longer |= bit; |
Branch: 2 branches
| Branch | True | False | Total |
| if (col[4] - 1 - col[3] > 1 || bt_longer(alt, alt_end)) t->longer |= bit; |
169 |
1 |
170 |
| if (col[4] - 1 - col[3] > 1 || bt_longer(alt, alt_end)) t->longer |= bit; |
16 |
153 |
169 |
|
| 2107 |
170 |
▶ | if (t->h.chroms & bit) hps_record(aTHX_ r, p, e); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->h.chroms & bit) hps_record(aTHX_ r, p, e); |
170 |
0 |
170 |
|
| 2108 |
170 |
▶ | if (chrom != t->chrom) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (chrom != t->chrom) return; |
38 |
132 |
170 |
|
| 2109 |
132 |
| const short int idx = bt_deletion(alt, alt_end); |
| 2110 |
132 |
▶ | if (idx < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx < 0) return; |
122 |
10 |
132 |
|
| 2111 |
10 |
| const UV pos = vcf_pos(aTHX_ r, col[1], col[2] - 1); |
| 2112 |
10 |
| const char *info = col[7], *info_end = col[8] - 1; |
| 2113 |
10 |
| UV end = 0, len = 0; |
| 2114 |
10 |
▶ | if (!bt_info(info, info_end, "END=", &end) && bt_info(info, info_end, "SVLEN=", &len)) end = pos + len; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!bt_info(info, info_end, "END=", &end) && bt_info(info, info_end, "SVLEN=", &len)) end = pos + len; |
3 |
7 |
10 |
| if (!bt_info(info, info_end, "END=", &end) && bt_info(info, info_end, "SVLEN=", &len)) end = pos + len; |
2 |
1 |
3 |
|
| 2115 |
10 |
▶ | if (pos > t->start || end < t->end) return; // some other deletion, which leaves part of the gene |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos > t->start || end < t->end) return; // some other deletion, which leaves part of the gene |
9 |
1 |
10 |
| if (pos > t->start || end < t->end) return; // some other deletion, which leaves part of the gene |
3 |
6 |
9 |
|
| 2116 |
|
| size_t gt, dp; |
| 2117 |
6 |
| vcf_format(col[8], format_end, >, &dp); |
| 2118 |
6 |
| p = format_end + 1; |
| 2119 |
16 |
▶ | for (size_t i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < r->n; i++) { |
10 |
6 |
16 |
|
| 2120 |
10 |
| const char *se = vcf_sample(aTHX_ r, p, e, i); |
| 2121 |
|
| // the first record that calls a sample is kept, should a file hold two |
| 2122 |
10 |
▶ | if (t->deleted[i] > 2) t->deleted[i] = hps_dose(r, p, se, gt, dp, idx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->deleted[i] > 2) t->deleted[i] = hps_dose(r, p, se, gt, dp, idx); |
10 |
0 |
10 |
|
| 2123 |
10 |
| p = se + 1; |
| 2124 |
|
| } |
| 2125 |
6 |
| vcf_samples_end(aTHX_ r, p, e); |
| 2126 |
|
| } |
| 2127 |
|
| |
| 2128 |
|
| /*blood_type()'s panel for every sample of the VCF name, which next returns a |
| 2129 |
|
| block of at a time. panel and markers are hps_panel()'s; chrom, start and end |
| 2130 |
|
| are the gene a deletion has to cover. Returns, as hps_dosage_vcf() does, |
| 2131 |
|
| sample => an array of one value for each marker, and sets *names and *seen as |
| 2132 |
|
| it does. *deleted is one value for each sample, in file order: how many copies |
| 2133 |
|
| of a deletion covering the gene it has, or undef where the file has no such |
| 2134 |
|
| record or the sample no call of it. flags[0] has bit c set for each |
| 2135 |
|
| chromosome c of the panel that the file has a record of, and flags[1] for each |
| 2136 |
|
| that it has an allele longer than one base on.*/ |
| 2137 |
37 |
| static HV *bt_tally_vcf(pTHX_ SV *next, const char *name, UV min_depth, AV *panel, size_t markers, UV chrom, UV start, UV end, AV **names, AV **seen, AV **deleted, UV *flags) { |
| 2138 |
37 |
▶ | if (chrom < 1 || chrom > 24 || start < 1 || start > end) |
Branch: 4 branches
| Branch | True | False | Total |
| if (chrom < 1 || chrom > 24 || start < 1 || start > end) |
37 |
0 |
37 |
| if (chrom < 1 || chrom > 24 || start < 1 || start > end) |
36 |
1 |
37 |
| if (chrom < 1 || chrom > 24 || start < 1 || start > end) |
36 |
0 |
36 |
| if (chrom < 1 || chrom > 24 || start < 1 || start > end) |
1 |
35 |
36 |
|
| 2139 |
2 |
| croak("%s: the gene is chromosome %" UVuf ", %" UVuf " to %" UVuf ", which isn't a stretch of a chromosome", __func__, chrom, start, end); |
| 2140 |
|
| bt_reader t; |
| 2141 |
35 |
| Zero(&t, 1, bt_reader); |
| 2142 |
35 |
| t.h.r.name = name; |
| 2143 |
35 |
| t.h.r.min_depth = min_depth; |
| 2144 |
35 |
| t.h.r.on_header = bt_header; |
| 2145 |
35 |
| t.h.r.on_record = bt_record; |
| 2146 |
35 |
| t.chrom = (unsigned short int)chrom; |
| 2147 |
35 |
| t.start = start; |
| 2148 |
35 |
| t.end = end; |
| 2149 |
35 |
| hps_panel(aTHX_ &t.h, panel, markers); |
| 2150 |
35 |
| t.chroms = t.h.chroms | ((uint32_t)1 << t.chrom); |
| 2151 |
35 |
| vcf_read(aTHX_ next, &t.h.r); |
| 2152 |
33 |
| HV *out = t.h.r.samples; |
| 2153 |
33 |
| AV *lost = newAV(); |
| 2154 |
33 |
| av_extend(lost, (SSize_t)t.h.r.n - 1); |
| 2155 |
177 |
▶ | for (size_t i = 0; i < t.h.r.n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < t.h.r.n; i++) { |
144 |
33 |
177 |
|
| 2156 |
144 |
| SV **sample = av_fetch(t.h.r.names, (SSize_t)i, 0); |
| 2157 |
144 |
▶ | if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sample) croak("%s: sample %" UVuf " of %s has lost its name", __func__, (UV)i, name); // cannot happen: every sample read was pushed |
0 |
144 |
144 |
|
| 2158 |
144 |
| AV *row = newAV(); |
| 2159 |
144 |
| av_extend(row, (SSize_t)t.h.sites - 1); |
| 2160 |
2672 |
▶ | for (size_t k = 0; k < t.h.sites; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < t.h.sites; k++) { |
2528 |
144 |
2672 |
|
| 2161 |
2528 |
| const unsigned char d = t.h.dose[i * t.h.sites + k]; |
| 2162 |
2528 |
▶ | av_push(row, d <= 2 ? newSVuv(d) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(row, d <= 2 ? newSVuv(d) : newSV(0)); |
1836 |
692 |
2528 |
|
| 2163 |
|
| } |
| 2164 |
144 |
| (void)hv_store_ent(out, *sample, newRV_noinc((SV*)row), 0); // in place of the column vcf_header() stored |
| 2165 |
144 |
▶ | av_push(lost, t.deleted[i] <= 2 ? newSVuv(t.deleted[i]) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(lost, t.deleted[i] <= 2 ? newSVuv(t.deleted[i]) : newSV(0)); |
9 |
135 |
144 |
|
| 2166 |
|
| } |
| 2167 |
33 |
| AV *was = newAV(); |
| 2168 |
33 |
| av_extend(was, (SSize_t)t.h.sites - 1); |
| 2169 |
595 |
▶ | for (size_t k = 0; k < t.h.sites; k++) av_push(was, newSVuv(t.h.seen[k] ? 1 : 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t k = 0; k < t.h.sites; k++) av_push(was, newSVuv(t.h.seen[k] ? 1 : 0)); |
562 |
33 |
595 |
|
| 2170 |
33 |
| *names = t.h.r.names; |
| 2171 |
33 |
| *seen = was; |
| 2172 |
33 |
| *deleted = lost; |
| 2173 |
33 |
| flags[0] = (UV)t.seen; |
| 2174 |
33 |
| flags[1] = (UV)t.longer; |
| 2175 |
33 |
| return out; |
| 2176 |
|
| } |
| 2177 |
|
| |
| 2178 |
|
| /*---- translation and k-mers: translate(), translate_six_frames(), find_orfs() |
| 2179 |
|
| and kmer_count(). |
| 2180 |
|
| |
| 2181 |
|
| A genetic code arrives as the 64 letters of one line of |
| 2182 |
|
| share/genetic_codes.tsv, which is NCBI's ncbieaa: the amino acid of each codon |
| 2183 |
|
| in the order TTT, TTC, TTA, TTG, TCT .. GGG, with '*' for a stop. The rules for |
| 2184 |
|
| a codon with an ambiguous base are Biopython 1.87's (Bio/Seq.py, |
| 2185 |
|
| _translate_str(), and AmbiguousForwardTable in Bio/Data/CodonTable.py), which |
| 2186 |
|
| t/seq.t checks against that library's own tests.*/ |
| 2187 |
|
| |
| 2188 |
|
| /*The bases that the IUPAC nucleotide letter c stands for, as a set of bits in |
| 2189 |
|
| the codes' own base order: 1 = T or U, 2 = C, 4 = A, 8 = G; 0 when c is not a |
| 2190 |
|
| nucleotide letter. The letters are those of NC-IUB's "Nomenclature for |
| 2191 |
|
| incompletely specified bases in nucleic acid sequences" (1984), as Biopython |
| 2192 |
|
| 1.87 lists them in ambiguous_dna_values of Bio/Data/IUPACData.py. That table |
| 2193 |
|
| also has X, kept there "for historical reasons although it is not an IUPAC |
| 2194 |
|
| nucleotide"; it is left out here, so a codon with an X is refused.*/ |
| 2195 |
445437 |
| static unsigned char nt_set(char c) { |
| 2196 |
445437 |
| switch (c) { |
| 2197 |
119682 |
| case 'T': case 't': case 'U': case 'u': return 1; |
| 2198 |
102829 |
| case 'C': case 'c': return 2; |
| 2199 |
119750 |
| case 'A': case 'a': return 4; |
| 2200 |
103079 |
| case 'G': case 'g': return 8; |
| 2201 |
3 |
| case 'Y': case 'y': return 1 | 2; // C or T |
| 2202 |
5 |
| case 'W': case 'w': return 1 | 4; // A or T |
| 2203 |
3 |
| case 'K': case 'k': return 1 | 8; // G or T |
| 2204 |
8 |
| case 'M': case 'm': return 2 | 4; // A or C |
| 2205 |
3 |
| case 'S': case 's': return 2 | 8; // C or G |
| 2206 |
19 |
| case 'R': case 'r': return 4 | 8; // A or G |
| 2207 |
2 |
| case 'H': case 'h': return 1 | 2 | 4; // not G |
| 2208 |
0 |
| case 'B': case 'b': return 1 | 2 | 8; // not A |
| 2209 |
0 |
| case 'D': case 'd': return 1 | 4 | 8; // not C |
| 2210 |
0 |
| case 'V': case 'v': return 2 | 4 | 8; // not T |
| 2211 |
20 |
| case 'N': case 'n': return 1 | 2 | 4 | 8; |
| 2212 |
34 |
| default: return 0; |
| 2213 |
|
| } |
| 2214 |
|
| } |
| 2215 |
|
| |
| 2216 |
|
| /*Whether the base set s holds exactly one base.*/ |
| 2217 |
|
| #define NT_ONE(s) (((s) & ((s) - 1)) == 0) |
| 2218 |
|
| /*The place in T, C, A, G order of the one base in the set s: 1, 2, 4, 8 give |
| 2219 |
|
| 0, 1, 2, 3.*/ |
| 2220 |
|
| #define NT_INDEX(s) (((s) >> 1) - ((s) >> 3)) |
| 2221 |
|
| /*The bit that stands for the letter c of a code in codon_letter()'s set: one |
| 2222 |
|
| for each letter from 'A' on, and bit 63 for a stop. A 64-bit set rather than a |
| 2223 |
|
| 32-bit one, so that it also holds on EBCDIC, where 'Z' - 'A' is 40.*/ |
| 2224 |
|
| #define AA_BIT(c) (UINT64_C(1) << ((c) == '*' ? 63 : (c) - 'A')) |
| 2225 |
|
| |
| 2226 |
|
| /*The letter that a codon is translated to under code, where s1, s2 and s3 are |
| 2227 |
|
| nt_set() of its three letters, none of them 0: '*' for a stop. |
| 2228 |
|
| |
| 2229 |
|
| A codon of three plain bases is looked up. One with an ambiguous base stands |
| 2230 |
|
| for several codons, and is given what they agree on: their amino acid when they |
| 2231 |
|
| have one between them, '*' when every one is a stop, B for D and N together, Z |
| 2232 |
|
| for E and Q, J for I and L, and X for anything else, a mix of stops and amino |
| 2233 |
|
| acids included. Those are Biopython's rules: B, Z and J are the only letters of |
| 2234 |
|
| its extended_protein_values that stand for two amino acids, and X is its |
| 2235 |
|
| pos_stop.*/ |
| 2236 |
148461 |
| static char codon_letter(const char *code, unsigned char s1, unsigned char s2, unsigned char s3) { |
| 2237 |
148461 |
▶ | if (NT_ONE(s1) && NT_ONE(s2) && NT_ONE(s3)) return code[16 * NT_INDEX(s1) + 4 * NT_INDEX(s2) + NT_INDEX(s3)]; |
Branch: 3 branches
| Branch | True | False | Total |
| if (NT_ONE(s1) && NT_ONE(s2) && NT_ONE(s3)) return code[16 * NT_INDEX(s1) + 4 * NT_INDEX(s2) + NT_INDEX(s3)]; |
148438 |
23 |
148461 |
| if (NT_ONE(s1) && NT_ONE(s2) && NT_ONE(s3)) return code[16 * NT_INDEX(s1) + 4 * NT_INDEX(s2) + NT_INDEX(s3)]; |
148432 |
6 |
148438 |
| if (NT_ONE(s1) && NT_ONE(s2) && NT_ONE(s3)) return code[16 * NT_INDEX(s1) + 4 * NT_INDEX(s2) + NT_INDEX(s3)]; |
148416 |
16 |
148432 |
|
| 2238 |
45 |
| uint64_t seen = 0; // AA_BIT() of every letter the codon could be |
| 2239 |
45 |
| char last = 'X'; // one of those letters, which is the answer when there is only one |
| 2240 |
225 |
▶ | for (unsigned short int i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int i = 0; i < 4; i++) { |
180 |
45 |
225 |
|
| 2241 |
180 |
▶ | if (!(s1 >> i & 1)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(s1 >> i & 1)) continue; |
101 |
79 |
180 |
|
| 2242 |
395 |
▶ | for (unsigned short int j = 0; j < 4; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int j = 0; j < 4; j++) { |
316 |
79 |
395 |
|
| 2243 |
316 |
▶ | if (!(s2 >> j & 1)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(s2 >> j & 1)) continue; |
171 |
145 |
316 |
|
| 2244 |
725 |
▶ | for (unsigned short int k = 0; k < 4; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 4; k++) { |
580 |
145 |
725 |
|
| 2245 |
580 |
▶ | if (!(s3 >> k & 1)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(s3 >> k & 1)) continue; |
159 |
421 |
580 |
|
| 2246 |
421 |
| last = code[16 * i + 4 * j + k]; |
| 2247 |
421 |
▶ | seen |= AA_BIT(last); |
Branch: 1 branch
| Branch | True | False | Total |
| seen |= AA_BIT(last); |
390 |
31 |
421 |
|
| 2248 |
|
| } |
| 2249 |
|
| } |
| 2250 |
|
| } |
| 2251 |
45 |
▶ | if (NT_ONE(seen)) return last; |
Branch: 1 branch
| Branch | True | False | Total |
| if (NT_ONE(seen)) return last; |
14 |
31 |
45 |
|
| 2252 |
31 |
▶ | if (seen == (AA_BIT('D') | AA_BIT('N'))) return 'B'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen == (AA_BIT('D') | AA_BIT('N'))) return 'B'; |
5 |
26 |
31 |
|
| 2253 |
26 |
▶ | if (seen == (AA_BIT('E') | AA_BIT('Q'))) return 'Z'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen == (AA_BIT('E') | AA_BIT('Q'))) return 'Z'; |
3 |
23 |
26 |
|
| 2254 |
23 |
▶ | if (seen == (AA_BIT('I') | AA_BIT('L'))) return 'J'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen == (AA_BIT('I') | AA_BIT('L'))) return 'J'; |
9 |
14 |
23 |
|
| 2255 |
14 |
| return 'X'; |
| 2256 |
|
| } |
| 2257 |
|
| |
| 2258 |
|
| /*seq translated under code, as a new mortal SV: one letter for each three of |
| 2259 |
|
| seq's, a stop written as stop_symbol, and whatever is left after the last whole |
| 2260 |
|
| codon dropped. With to_stop, the translation ends before the first stop. With |
| 2261 |
|
| gap other than NULL, a codon of three *gap is written as *gap. |
| 2262 |
|
| |
| 2263 |
|
| A codon with any other letter that is not a nucleotide's dies, and names the |
| 2264 |
|
| codon. The SV is mortal so that dying then does not leak it.*/ |
| 2265 |
260 |
| static SV *translate_codons(pTHX_ SV *seq, SV *code_sv, char stop_symbol, bool to_stop, const char *gap) { |
| 2266 |
|
| STRLEN code_len, len; |
| 2267 |
260 |
| const char *code = SvPV_const(code_sv, code_len); // not restrict: perl's buffer |
| 2268 |
260 |
▶ | if (code_len != 64) croak("%s: a genetic code is 64 letters, not %" UVuf, __func__, (UV)code_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (code_len != 64) croak("%s: a genetic code is 64 letters, not %" UVuf, __func__, (UV)code_len); |
0 |
260 |
260 |
|
| 2269 |
16900 |
▶ | for (unsigned short int i = 0; i < 64; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int i = 0; i < 64; i++) { |
16640 |
260 |
16900 |
|
| 2270 |
16640 |
▶ | if (code[i] != '*' && !isUPPER(code[i])) croak("%s: letter %" UVuf " of the genetic code \"%s\" is neither a capital letter nor *", __func__, (UV)i + 1, code); |
Branch: 2 branches
| Branch | True | False | Total |
| if (code[i] != '*' && !isUPPER(code[i])) croak("%s: letter %" UVuf " of the genetic code \"%s\" is neither a capital letter nor *", __func__, (UV)i + 1, code); |
15969 |
671 |
16640 |
| if (code[i] != '*' && !isUPPER(code[i])) croak("%s: letter %" UVuf " of the genetic code \"%s\" is neither a capital letter nor *", __func__, (UV)i + 1, code); |
0 |
15969 |
15969 |
|
| 2271 |
|
| } |
| 2272 |
260 |
| const char *p = SvPV_const(seq, len); // not restrict: perl's buffer |
| 2273 |
260 |
| const size_t codons = len / 3; |
| 2274 |
260 |
▶ | SV *out = sv_2mortal(newSV(codons ? codons : 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *out = sv_2mortal(newSV(codons ? codons : 1)); |
249 |
11 |
260 |
|
| 2275 |
260 |
| SvPOK_only(out); |
| 2276 |
260 |
| char *o = SvPVX(out); |
| 2277 |
260 |
| size_t n = 0; // letters written, which is read after the loop |
| 2278 |
148718 |
▶ | for (size_t i = 0; i < codons; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < codons; i++) { |
148479 |
239 |
148718 |
|
| 2279 |
148479 |
| const char *c = p + 3 * i; |
| 2280 |
148479 |
| const unsigned char s1 = nt_set(c[0]), s2 = nt_set(c[1]), s3 = nt_set(c[2]); |
| 2281 |
148479 |
▶ | if (!s1 || !s2 || !s3) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!s1 || !s2 || !s3) { |
148471 |
8 |
148479 |
| if (!s1 || !s2 || !s3) { |
148470 |
1 |
148471 |
| if (!s1 || !s2 || !s3) { |
9 |
148461 |
148470 |
|
| 2282 |
18 |
▶ | if (gap && c[0] == *gap && c[1] == *gap && c[2] == *gap) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (gap && c[0] == *gap && c[1] == *gap && c[2] == *gap) { |
7 |
11 |
18 |
| if (gap && c[0] == *gap && c[1] == *gap && c[2] == *gap) { |
5 |
2 |
7 |
| if (gap && c[0] == *gap && c[1] == *gap && c[2] == *gap) { |
5 |
0 |
5 |
| if (gap && c[0] == *gap && c[1] == *gap && c[2] == *gap) { |
5 |
0 |
5 |
|
| 2283 |
5 |
| o[n++] = *gap; |
| 2284 |
5 |
| continue; |
| 2285 |
|
| } |
| 2286 |
13 |
| croak("%s: codon %" UVuf ", \"%.3s\", has a letter that isn't a nucleotide's", __func__, (UV)(i + 1), c); |
| 2287 |
|
| } |
| 2288 |
148461 |
| const char aa = codon_letter(code, s1, s2, s3); |
| 2289 |
148461 |
▶ | if (aa != '*') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (aa != '*') { |
143787 |
4674 |
148461 |
|
| 2290 |
143787 |
| o[n++] = aa; |
| 2291 |
4674 |
▶ | } else if (to_stop) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (to_stop) { |
8 |
4666 |
4674 |
|
| 2292 |
8 |
| break; |
| 2293 |
|
| } else { |
| 2294 |
4666 |
| o[n++] = stop_symbol; |
| 2295 |
|
| } |
| 2296 |
|
| } |
| 2297 |
247 |
| o[n] = '\0'; |
| 2298 |
247 |
| SvCUR_set(out, n); |
| 2299 |
247 |
| return out; |
| 2300 |
|
| } |
| 2301 |
|
| |
| 2302 |
|
| /*How many times each run of k bytes occurs in seq, as a new hash of run => |
| 2303 |
|
| count: the run at every step-th byte, so step 1 counts every k-mer and step k |
| 2304 |
|
| those that do not overlap. A k of 0 or longer than seq gives an empty hash. |
| 2305 |
|
| |
| 2306 |
|
| The runs are bytes, so lib/Cn/Bioinf.pm counts a string that holds a character |
| 2307 |
|
| above 255 itself. A hash key's length is an I32, and a negative one means a |
| 2308 |
|
| UTF-8 key, so a k beyond I32_MAX dies; perl refuses it first.*/ |
| 2309 |
24 |
| static HV *kmer_tally(pTHX_ SV *seq, UV k, UV step) { |
| 2310 |
|
| STRLEN len; |
| 2311 |
24 |
| const char *p = SvPV_const(seq, len); // not restrict: perl's buffer |
| 2312 |
24 |
| HV *h = (HV*)sv_2mortal((SV*)newHV()); |
| 2313 |
24 |
▶ | if (k > (UV)I32_MAX) croak("%s: k is %" UVuf ", and a hash key can be no longer than %" IVdf, __func__, k, (IV)I32_MAX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (k > (UV)I32_MAX) croak("%s: k is %" UVuf ", and a hash key can be no longer than %" IVdf, __func__, k, (IV)I32_MAX); |
0 |
24 |
24 |
|
| 2314 |
24 |
▶ | if (step == 0) croak("%s: a step of 0 would count the first k-mer for ever", __func__); // cannot happen: perl passes 1 or k |
Branch: 1 branch
| Branch | True | False | Total |
| if (step == 0) croak("%s: a step of 0 would count the first k-mer for ever", __func__); // cannot happen: perl passes 1 or k |
0 |
24 |
24 |
|
| 2315 |
24 |
▶ | if (k == 0 || k > len) return h; |
Branch: 2 branches
| Branch | True | False | Total |
| if (k == 0 || k > len) return h; |
24 |
0 |
24 |
| if (k == 0 || k > len) return h; |
8 |
16 |
24 |
|
| 2316 |
16 |
| const size_t last = len - (size_t)k; // where the last k-mer starts |
| 2317 |
9685 |
▶ | for (size_t i = 0; i <= last; i += (size_t)step) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i <= last; i += (size_t)step) { |
9685 |
0 |
9685 |
|
| 2318 |
9685 |
| SV **count = hv_fetch(h, p + i, (I32)k, 1); |
| 2319 |
9685 |
▶ | if (!count) croak("%s: couldn't count the k-mer at %" UVuf, __func__, (UV)i); // cannot happen: an lvalue fetch from a plain hash |
Branch: 1 branch
| Branch | True | False | Total |
| if (!count) croak("%s: couldn't count the k-mer at %" UVuf, __func__, (UV)i); // cannot happen: an lvalue fetch from a plain hash |
0 |
9685 |
9685 |
|
| 2320 |
9685 |
▶ | sv_setuv(*count, SvOK(*count) ? SvUV(*count) + 1 : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setuv(*count, SvOK(*count) ? SvUV(*count) + 1 : 1); |
9571 |
114 |
9685 |
|
| 2321 |
9685 |
▶ | if (step > last - i) break; // i + step would pass the end, and could wrap |
Branch: 1 branch
| Branch | True | False | Total |
| if (step > last - i) break; // i + step would pass the end, and could wrap |
16 |
9669 |
9685 |
|
| 2322 |
|
| } |
| 2323 |
16 |
| return h; |
| 2324 |
|
| } |
| 2325 |
|
| |
| 2326 |
|
| MODULE = Cn::Bioinf PACKAGE = Cn::Bioinf |
| 2327 |
|
| PROTOTYPES: DISABLE |
| 2328 |
|
| |
| 2329 |
|
| SV * |
| 2330 |
|
| _read_fasta(fh, key, name, order = NULL) |
| 2331 |
|
| PerlIO *fh |
| 2332 |
|
| SV *key |
| 2333 |
|
| const char *name |
| 2334 |
|
| AV *order |
| 2335 |
|
| CODE: |
| 2336 |
61 |
▶ | RETVAL = read_fasta(aTHX_ fh, SvOK(key) ? key : NULL, name, order); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = read_fasta(aTHX_ fh, SvOK(key) ? key : NULL, name, order); |
16 |
45 |
61 |
|
| 2337 |
|
| OUTPUT: |
| 2338 |
|
| RETVAL |
| 2339 |
|
| |
| 2340 |
|
| void |
| 2341 |
|
| _write_fasta(fh, h, order, width) |
| 2342 |
|
| PerlIO *fh |
| 2343 |
|
| HV *h |
| 2344 |
|
| AV *order |
| 2345 |
|
| UV width |
| 2346 |
|
| CODE: |
| 2347 |
23 |
| write_fasta(aTHX_ fh, h, order, (STRLEN)width); |
| 2348 |
|
| |
| 2349 |
|
| void |
| 2350 |
|
| _sex_tally(next, name, min_depth) |
| 2351 |
|
| SV *next |
| 2352 |
|
| const char *name |
| 2353 |
|
| UV min_depth |
| 2354 |
|
| PPCODE: |
| 2355 |
|
| UV x_sites; |
| 2356 |
|
| AV *names; |
| 2357 |
52 |
| PUTBACK; |
| 2358 |
52 |
| HV *out = sex_tally_vcf(aTHX_ next, name, min_depth, &x_sites, &names); |
| 2359 |
32 |
| SPAGAIN; |
| 2360 |
32 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
32 |
32 |
|
| 2361 |
32 |
| PUSHs(sv_2mortal(newRV_inc((SV*)out))); |
| 2362 |
32 |
| PUSHs(sv_2mortal(newSVuv(x_sites))); |
| 2363 |
32 |
| PUSHs(sv_2mortal(newRV_inc((SV*)names))); |
| 2364 |
|
| |
| 2365 |
|
| void |
| 2366 |
|
| _parent_one(next, name, min_depth, child, parent) |
| 2367 |
|
| SV *next |
| 2368 |
|
| const char *name |
| 2369 |
|
| UV min_depth |
| 2370 |
|
| SV *child |
| 2371 |
|
| SV *parent |
| 2372 |
|
| PPCODE: |
| 2373 |
|
| parent_reader t; |
| 2374 |
60 |
| Zero(&t, 1, parent_reader); |
| 2375 |
60 |
| t.r.name = name; |
| 2376 |
60 |
| t.r.min_depth = min_depth; |
| 2377 |
60 |
| t.r.on_header = parent_header_one; |
| 2378 |
60 |
| t.r.on_record = parent_record_one; |
| 2379 |
60 |
| t.want[0] = child; |
| 2380 |
60 |
| t.want[1] = parent; |
| 2381 |
60 |
| PUTBACK; |
| 2382 |
60 |
| vcf_read(aTHX_ next, &t.r); |
| 2383 |
58 |
| SPAGAIN; |
| 2384 |
58 |
| UV in = 0; // the sites where either carries the ALT, every one of them in both, since there is one file |
| 2385 |
522 |
▶ | for (unsigned short int k = 1; k < 9; k++) in += t.t[k]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 1; k < 9; k++) in += t.t[k]; |
464 |
58 |
522 |
|
| 2386 |
58 |
▶ | EXTEND(SP, 12); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 12); |
0 |
58 |
58 |
|
| 2387 |
580 |
▶ | for (unsigned short int k = 0; k < 9; k++) PUSHs(sv_2mortal(newSVuv(t.t[k]))); |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 9; k++) PUSHs(sv_2mortal(newSVuv(t.t[k]))); |
522 |
58 |
580 |
|
| 2388 |
58 |
| PUSHs(sv_2mortal(newSVuv(in))); |
| 2389 |
58 |
| PUSHs(sv_2mortal(newSVuv(0))); |
| 2390 |
58 |
| PUSHs(sv_2mortal(newSVuv(0))); |
| 2391 |
|
| |
| 2392 |
|
| void |
| 2393 |
|
| _parent_own(child_next, child_name, parent_next, parent_name, min_depth) |
| 2394 |
|
| SV *child_next |
| 2395 |
|
| const char *child_name |
| 2396 |
|
| SV *parent_next |
| 2397 |
|
| const char *parent_name |
| 2398 |
|
| UV min_depth |
| 2399 |
|
| PPCODE: |
| 2400 |
29 |
| PUTBACK; |
| 2401 |
29 |
| const parent_sites *a = parent_sites_vcf(aTHX_ child_next, child_name, min_depth); |
| 2402 |
28 |
| const parent_sites *b = parent_sites_vcf(aTHX_ parent_next, parent_name, min_depth); |
| 2403 |
21 |
| SPAGAIN; |
| 2404 |
21 |
| UV t[9] = { 0 }, in[3] = { 0 }; // as parent_merge() fills them |
| 2405 |
21 |
| parent_merge(a, b, t, in); |
| 2406 |
21 |
▶ | EXTEND(SP, 12); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 12); |
0 |
21 |
21 |
|
| 2407 |
210 |
▶ | for (unsigned short int k = 0; k < 9; k++) PUSHs(sv_2mortal(newSVuv(t[k]))); |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 9; k++) PUSHs(sv_2mortal(newSVuv(t[k]))); |
189 |
21 |
210 |
|
| 2408 |
84 |
▶ | for (unsigned short int k = 0; k < 3; k++) PUSHs(sv_2mortal(newSVuv(in[k]))); |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned short int k = 0; k < 3; k++) PUSHs(sv_2mortal(newSVuv(in[k]))); |
63 |
21 |
84 |
|
| 2409 |
|
| |
| 2410 |
|
| void |
| 2411 |
|
| _relatedness_one(next, name, min_depth) |
| 2412 |
|
| SV *next |
| 2413 |
|
| const char *name |
| 2414 |
|
| UV min_depth |
| 2415 |
|
| PPCODE: |
| 2416 |
|
| rel_reader t; |
| 2417 |
30 |
| Zero(&t, 1, rel_reader); |
| 2418 |
30 |
| t.r.name = name; |
| 2419 |
30 |
| t.r.min_depth = min_depth; |
| 2420 |
30 |
| t.r.on_header = rel_header_one; |
| 2421 |
30 |
| t.r.on_record = rel_record_one; |
| 2422 |
30 |
| PUTBACK; |
| 2423 |
30 |
| vcf_read(aTHX_ next, &t.r); |
| 2424 |
27 |
▶ | if (t.k) rel_flush(&t); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t.k) rel_flush(&t); |
27 |
0 |
27 |
|
| 2425 |
27 |
| SPAGAIN; |
| 2426 |
27 |
| HV *sex = (HV*)sv_2mortal((SV*)newHV()); |
| 2427 |
27 |
| sex_rows(aTHX_ sex, t.r.names, t.sex, t.r.n, name); |
| 2428 |
27 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
27 |
27 |
|
| 2429 |
27 |
| PUSHs(sv_2mortal(newRV_inc((SV*)t.r.names))); |
| 2430 |
27 |
| PUSHs(sv_2mortal(rel_counts(aTHX_ t.t, t.r.n))); |
| 2431 |
27 |
| PUSHs(sv_2mortal(newRV_inc((SV*)sex))); |
| 2432 |
|
| |
| 2433 |
|
| void |
| 2434 |
|
| _relatedness_own(nexts, files, min_depth) |
| 2435 |
|
| AV *nexts |
| 2436 |
|
| AV *files |
| 2437 |
|
| UV min_depth |
| 2438 |
|
| PPCODE: |
| 2439 |
28 |
| const size_t n = (size_t)(av_len(nexts) + 1); |
| 2440 |
28 |
▶ | if ((SSize_t)n != av_len(files) + 1) croak("_relatedness_own: %" UVuf " readers for %" UVuf " files", (UV)n, (UV)(av_len(files) + 1)); // cannot happen: perl makes one for each; named literally, since __func__ in an XSUB is XS_Cn__Bioinf__relatedness_own |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SSize_t)n != av_len(files) + 1) croak("_relatedness_own: %" UVuf " readers for %" UVuf " files", (UV)n, (UV)(av_len(files) + 1)); // cannot happen: perl makes one for each; named literally, since __func__ in an XSUB is XS_Cn__Bioinf__relatedness_own |
1 |
27 |
28 |
|
| 2441 |
|
| parent_sites **s; // each file's sites, freed by parent_sites_read()'s destructor |
| 2442 |
27 |
▶ | Newx(s, n, parent_sites*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(s, n, parent_sites*); |
0 |
27 |
27 |
|
| 2443 |
27 |
| SAVEFREEPV(s); |
| 2444 |
|
| UV *count; // SEX_FIELDS counters a file |
| 2445 |
27 |
▶ | Newxz(count, n * SEX_FIELDS, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(count, n * SEX_FIELDS, UV); |
0 |
27 |
27 |
|
| 2446 |
27 |
| SAVEFREEPV(count); |
| 2447 |
27 |
| HV *seen = (HV*)sv_2mortal((SV*)newHV()); |
| 2448 |
27 |
| AV *names = (AV*)sv_2mortal((SV*)newAV()); |
| 2449 |
27 |
| PUTBACK; |
| 2450 |
93 |
▶ | for (size_t f = 0; f < n; f++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t f = 0; f < n; f++) { |
75 |
18 |
93 |
|
| 2451 |
75 |
| SV **next = av_fetch(nexts, (SSize_t)f, 0), **file = av_fetch(files, (SSize_t)f, 0); |
| 2452 |
75 |
▶ | if (!next || !file) croak("_relatedness_own: file %" UVuf " has no reader", (UV)f); // cannot happen: perl fills both arrays |
Branch: 2 branches
| Branch | True | False | Total |
| if (!next || !file) croak("_relatedness_own: file %" UVuf " has no reader", (UV)f); // cannot happen: perl fills both arrays |
75 |
0 |
75 |
| if (!next || !file) croak("_relatedness_own: file %" UVuf " has no reader", (UV)f); // cannot happen: perl fills both arrays |
1 |
74 |
75 |
|
| 2453 |
|
| rel_own_reader t; |
| 2454 |
74 |
| Zero(&t, 1, rel_own_reader); |
| 2455 |
74 |
| t.p.r.name = SvPV_nolen(*file); |
| 2456 |
74 |
| t.p.r.min_depth = min_depth; |
| 2457 |
74 |
| t.p.r.on_header = rel_header_own; |
| 2458 |
74 |
| t.p.r.on_record = rel_record_own; |
| 2459 |
74 |
| t.sex = count + f * SEX_FIELDS; |
| 2460 |
74 |
| t.seen = seen; |
| 2461 |
74 |
| t.names = names; |
| 2462 |
74 |
| s[f] = parent_sites_read(aTHX_ &t.p, *next); |
| 2463 |
|
| } |
| 2464 |
18 |
| SPAGAIN; |
| 2465 |
|
| UV *t; // REL_T counters a pair |
| 2466 |
18 |
▶ | Newxz(t, rel_pairs(aTHX_ n) * REL_T, UV); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(t, rel_pairs(aTHX_ n) * REL_T, UV); |
0 |
18 |
18 |
|
| 2467 |
18 |
| SAVEFREEPV(t); |
| 2468 |
18 |
| rel_merge(aTHX_ (parent_sites *const *)s, n, t); |
| 2469 |
18 |
| HV *sex = (HV*)sv_2mortal((SV*)newHV()); |
| 2470 |
18 |
| sex_rows(aTHX_ sex, names, count, n, "the VCFs"); |
| 2471 |
18 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
18 |
18 |
|
| 2472 |
18 |
| PUSHs(sv_2mortal(newRV_inc((SV*)names))); |
| 2473 |
18 |
| PUSHs(sv_2mortal(rel_counts(aTHX_ t, n))); |
| 2474 |
18 |
| PUSHs(sv_2mortal(newRV_inc((SV*)sex))); |
| 2475 |
|
| |
| 2476 |
|
| void |
| 2477 |
|
| _vcf_samples(next, name) |
| 2478 |
|
| SV *next |
| 2479 |
|
| const char *name |
| 2480 |
|
| PPCODE: |
| 2481 |
|
| vcf_reader r; |
| 2482 |
63 |
| Zero(&r, 1, vcf_reader); |
| 2483 |
63 |
| r.name = name; |
| 2484 |
63 |
| r.on_header = conc_stop; |
| 2485 |
63 |
| r.on_record = conc_skip; |
| 2486 |
63 |
| PUTBACK; |
| 2487 |
63 |
| vcf_read(aTHX_ next, &r); |
| 2488 |
61 |
| SPAGAIN; |
| 2489 |
61 |
▶ | XPUSHs(sv_2mortal(newRV_inc((SV*)r.names))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newRV_inc((SV*)r.names))); |
0 |
61 |
61 |
|
| 2490 |
|
| |
| 2491 |
|
| void |
| 2492 |
|
| _concordance(test_next, test_name, truth_next, truth_name, truth_names, min_depth, discordances) |
| 2493 |
|
| SV *test_next |
| 2494 |
|
| const char *test_name |
| 2495 |
|
| SV *truth_next |
| 2496 |
|
| const char *truth_name |
| 2497 |
|
| AV *truth_names |
| 2498 |
|
| UV min_depth |
| 2499 |
|
| bool discordances |
| 2500 |
|
| PPCODE: |
| 2501 |
|
| conc_store *s; |
| 2502 |
61 |
| Newxz(s, 1, conc_store); |
| 2503 |
61 |
| SAVEDESTRUCTOR_X(conc_store_free, s); // at this call's LEAVE, croak or not |
| 2504 |
61 |
| s->truth_n = (size_t)(av_len(truth_names) + 1); |
| 2505 |
|
| conc_reader test; |
| 2506 |
61 |
| Zero(&test, 1, conc_reader); |
| 2507 |
61 |
| test.r.name = test_name; |
| 2508 |
61 |
| test.r.min_depth = min_depth; |
| 2509 |
61 |
| test.r.on_header = conc_header_test; |
| 2510 |
61 |
| test.r.on_record = conc_record_test; |
| 2511 |
61 |
| test.s = s; |
| 2512 |
61 |
| test.truth_names = truth_names; |
| 2513 |
61 |
| PUTBACK; |
| 2514 |
61 |
| vcf_read(aTHX_ test_next, &test.r); |
| 2515 |
|
| conc_reader truth; |
| 2516 |
51 |
| Zero(&truth, 1, conc_reader); |
| 2517 |
51 |
| truth.r.name = truth_name; |
| 2518 |
51 |
| truth.r.min_depth = min_depth; |
| 2519 |
51 |
| truth.r.on_header = conc_header_truth; |
| 2520 |
51 |
| truth.r.on_record = conc_record_truth; |
| 2521 |
51 |
| truth.s = s; |
| 2522 |
51 |
| vcf_read(aTHX_ truth_next, &truth.r); |
| 2523 |
40 |
| SPAGAIN; |
| 2524 |
40 |
| HV *out = conc_results(aTHX_ s, test.r.names, truth.r.names, discordances); |
| 2525 |
40 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
40 |
40 |
|
| 2526 |
40 |
| PUSHs(sv_2mortal(newRV_inc((SV*)out))); |
| 2527 |
40 |
| PUSHs(sv_2mortal(newRV_inc((SV*)test.r.names))); |
| 2528 |
|
| |
| 2529 |
|
| void |
| 2530 |
|
| _hirisplex_dosage(next, name, min_depth, panel, markers, by_id) |
| 2531 |
|
| SV *next |
| 2532 |
|
| const char *name |
| 2533 |
|
| UV min_depth |
| 2534 |
|
| AV *panel |
| 2535 |
|
| UV markers |
| 2536 |
|
| UV by_id |
| 2537 |
|
| PPCODE: |
| 2538 |
|
| AV *names, *seen; |
| 2539 |
17 |
| PUTBACK; |
| 2540 |
17 |
| HV *out = hps_dosage_vcf(aTHX_ next, name, min_depth, panel, (size_t)markers, by_id ? TRUE : FALSE, &names, &seen); |
| 2541 |
15 |
| SPAGAIN; |
| 2542 |
15 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
15 |
15 |
|
| 2543 |
15 |
| PUSHs(sv_2mortal(newRV_inc((SV*)out))); |
| 2544 |
15 |
| PUSHs(sv_2mortal(newRV_inc((SV*)names))); |
| 2545 |
15 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)seen))); |
| 2546 |
|
| |
| 2547 |
|
| void |
| 2548 |
|
| _blood_type_tally(next, name, min_depth, panel, markers, chrom, start, end) |
| 2549 |
|
| SV *next |
| 2550 |
|
| const char *name |
| 2551 |
|
| UV min_depth |
| 2552 |
|
| AV *panel |
| 2553 |
|
| UV markers |
| 2554 |
|
| UV chrom |
| 2555 |
|
| UV start |
| 2556 |
|
| UV end |
| 2557 |
|
| PPCODE: |
| 2558 |
|
| AV *names, *seen, *deleted; |
| 2559 |
|
| UV flags[2]; // bt_tally_vcf()'s: the chromosomes read, and those with an allele longer than a base |
| 2560 |
37 |
| PUTBACK; |
| 2561 |
37 |
| HV *out = bt_tally_vcf(aTHX_ next, name, min_depth, panel, (size_t)markers, chrom, start, end, &names, &seen, &deleted, flags); |
| 2562 |
33 |
| SPAGAIN; |
| 2563 |
33 |
▶ | EXTEND(SP, 6); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 6); |
0 |
33 |
33 |
|
| 2564 |
33 |
| PUSHs(sv_2mortal(newRV_inc((SV*)out))); |
| 2565 |
33 |
| PUSHs(sv_2mortal(newRV_inc((SV*)names))); |
| 2566 |
33 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)seen))); |
| 2567 |
33 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)deleted))); |
| 2568 |
33 |
| PUSHs(sv_2mortal(newSVuv(flags[0]))); |
| 2569 |
33 |
| PUSHs(sv_2mortal(newSVuv(flags[1]))); |
| 2570 |
|
| |
| 2571 |
|
| SV * |
| 2572 |
|
| _translate(seq, code, stop_symbol, to_stop, gap) |
| 2573 |
|
| SV *seq |
| 2574 |
|
| SV *code |
| 2575 |
|
| char stop_symbol |
| 2576 |
|
| UV to_stop |
| 2577 |
|
| SV *gap |
| 2578 |
|
| CODE: |
| 2579 |
|
| // On a line of its own: SvREFCNT_inc_simple_NN() is a macro that evaluates |
| 2580 |
|
| // its argument twice, and would translate twice. The mortal is kept by |
| 2581 |
|
| // taking a second reference to it. |
| 2582 |
267 |
▶ | SV *protein = translate_codons(aTHX_ seq, code, stop_symbol, to_stop ? TRUE : FALSE, SvOK(gap) ? SvPV_nolen(gap) : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *protein = translate_codons(aTHX_ seq, code, stop_symbol, to_stop ? TRUE : FALSE, SvOK(gap) ? SvPV_nolen(gap) : NULL); |
7 |
253 |
260 |
|
| 2583 |
247 |
| RETVAL = SvREFCNT_inc_simple_NN(protein); |
| 2584 |
|
| OUTPUT: |
| 2585 |
|
| RETVAL |
| 2586 |
|
| |
| 2587 |
|
| SV * |
| 2588 |
|
| _kmer_count(seq, k, step) |
| 2589 |
|
| SV *seq |
| 2590 |
|
| UV k |
| 2591 |
|
| UV step |
| 2592 |
|
| CODE: |
| 2593 |
24 |
| RETVAL = newRV_inc((SV*)kmer_tally(aTHX_ seq, k, step)); // inc, since the hash is mortal |
| 2594 |
|
| OUTPUT: |
| 2595 |
|
| RETVAL |