| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
set -exo pipefail |
| 4 |
|
| 5 |
# 1kg_phase3_chr21_start.vcf.gz is the header (253 lines) + first 600 |
| 6 |
# biallelic lines from the official data release (grep -v "MULTI_ALLELIC"). |
| 7 |
# This previously included many more lines, but the test was too slow. |
| 8 |
# Ideally, we want to test the full chr21 file, or even better, chrX/Y/MT, |
| 9 |
# before significant releases. |
| 10 |
# todo: support multiallelic variants |
| 11 |
plink --vcf 1kg_phase3_chr21_start.vcf.gz --out plink1_data |
| 12 |
$1/plink2 $2 $3 --vcf 1kg_phase3_chr21_start.vcf.gz --double-id --out plink2_data |
| 13 |
# Verify BCF round-trip. |
| 14 |
$1/plink2 $2 $3 --pfile plink2_data --export bcf --out plink2_roundtrip |
| 15 |
$1/plink2 $2 $3 --bcf plink2_roundtrip.bcf --out plink2_roundtrip |
| 16 |
diff -q plink2_data.pgen plink2_roundtrip.pgen |
| 17 |
# Verify haps and hapslegend roundtrip. |
| 18 |
$1/plink2 $2 $3 --pfile plink2_data --export haps sample-v2 --out plink2_roundtrip |
| 19 |
$1/plink2 $2 $3 --haps plink2_roundtrip.haps --sample plink2_roundtrip.sample --real-ref-alleles --make-pgen --out plink2_roundtrip |
| 20 |
diff -q plink2_data.pgen plink2_roundtrip.pgen |
| 21 |
$1/plink2 $2 $3 --pfile plink2_data --export hapslegend sample-v2 --out plink2_roundtrip |
| 22 |
$1/plink2 $2 $3 --haps plink2_roundtrip.haps --legend plink2_roundtrip.legend 21 --sample plink2_roundtrip.sample --real-ref-alleles --make-pgen --out plink2_roundtrip |
| 23 |
diff -q plink2_data.pgen plink2_roundtrip.pgen |
| 24 |
|
| 25 |
$1/plink2 $2 $3 --pfile plink2_data --export vcf --out plink2_data |
| 26 |
|
| 27 |
gunzip -c 1kg_phase3_chr21_start.vcf.gz > 1kg_phase3_chr21_start.vcf |
| 28 |
|
| 29 |
cat 1kg_phase3_chr21_start.vcf | sed '/^#/ d' > 1kg_noheader.txt |
| 30 |
|
| 31 |
# INFO column is now preserved. |
| 32 |
cat plink2_data.vcf | sed '/^#/ d' > plink2_noheader.txt |
| 33 |
diff -q 1kg_noheader.txt plink2_noheader.txt |
| 34 |
|
| 35 |
# 10th column is the first genotype column, so this removes samples 492-569 and |
| 36 |
# sample 667 |
| 37 |
cat 1kg_noheader.txt | cut -f 1-500,579-675,677- > 1kg_noheader_pruned.txt |
| 38 |
|
| 39 |
cat plink1_data.fam | head -n 569 | tail -n 78 | cut -f 1-2 > remove.txt |
| 40 |
cat plink1_data.fam | head -n 667 | tail -n 1 | cut -f 1-2 >> remove.txt |
| 41 |
$1/plink2 $2 $3 --pfile plink2_data --remove remove.txt --export vcf --out plink2_pruned |
| 42 |
|
| 43 |
# there's a tiny chance that fileDate will change between this and the next VCF |
| 44 |
# export command. it's the second header line, so remove the top two header |
| 45 |
# lines before comparing. |
| 46 |
cat plink2_pruned.vcf | tail -n +3 > plink2_pruned_nodate.txt |
| 47 |
|
| 48 |
cat plink2_pruned.vcf | sed '/^#/ d' > plink2_noheader_pruned.txt |
| 49 |
diff -q 1kg_noheader_pruned.txt plink2_noheader_pruned.txt |
| 50 |
$1/plink2 $2 $3 --pfile plink2_data --remove remove.txt --make-pgen --out plink2_pruned |
| 51 |
|
| 52 |
# this exercises different subsetting code paths |
| 53 |
$1/plink2 $2 $3 --pfile plink2_pruned --export vcf --out plink2_pruned2 |
| 54 |
|
| 55 |
cat plink2_pruned2.vcf | tail -n +3 > plink2_pruned2_nodate.txt |
| 56 |
diff -q plink2_pruned_nodate.txt plink2_pruned2_nodate.txt |
| 57 |
|
| 58 |
# N.B. to reproduce a failure, it may be necessary to reuse the |
| 59 |
# sparse.bim/sparse.fam generated here (or reuse the random seed in |
| 60 |
# sparse.log). |
| 61 |
plink --bfile plink1_data --thin 0.1 --thin-indiv 0.1 --make-bed --out sparse |
| 62 |
|
| 63 |
cat sparse.bim | cut -f 2 > extract.txt |
| 64 |
$1/plink2 $2 $3 --pfile plink2_data --keep sparse.fam --extract extract.txt --export vcf --out plink2_sparse |
| 65 |
|
| 66 |
# test phased genotype sorting code path |
| 67 |
$1/plink2 $2 $3 --pfile plink2_data --keep sparse.fam --indiv-sort f sparse.fam --extract extract.txt --make-pgen --out plink2_sparse2 |
| 68 |
$1/plink2 $2 $3 --vcf plink2_sparse.vcf --id-delim --make-pgen --out plink2_sparse |
| 69 |
diff -q plink2_sparse.pgen plink2_sparse2.pgen |
| 70 |
diff -q plink2_sparse.pvar plink2_sparse2.pvar |
| 71 |
diff -q plink2_sparse.psam plink2_sparse2.psam |
| 72 |
|
| 73 |
# test GRM/PCA, with tolerance for floating point errors and sign differences |
| 74 |
# reduce to 3 PCs since that's the most that --pca approx can handle with 29 |
| 75 |
# variants |
| 76 |
plink --bfile plink1_data --maf 0.02 --pca 3 header tabs var-wts --out plink1_pca |
| 77 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pca 3 biallelic-var-wts --out plink2_pca |
| 78 |
python3 pca_compare.py -1 plink1_pca -2 plink2_pca -t 0.000002 |
| 79 |
|
| 80 |
# while we're at it, test all --read-freq input formats. |
| 81 |
# (todo: add multiallelic --read-freq test when --score gets better |
| 82 |
# multiallelic-variant support.) |
| 83 |
plink --bfile plink1_data --freq |
| 84 |
$1/plink2 $2 $3 --bfile plink1_data --read-freq plink.frq --maf 0.02 --pca 3 biallelic-var-wts --out plink2_pca_rfreq |
| 85 |
# .frq only has 4-digit precision |
| 86 |
python3 pca_compare.py -1 plink2_pca -2 plink2_pca_rfreq -t 0.0002 |
| 87 |
plink --bfile plink1_data --freqx |
| 88 |
$1/plink2 $2 $3 --bfile plink1_data --read-freq plink.frqx --maf 0.02 --pca 3 biallelic-var-wts --out plink2_pca_rfreq |
| 89 |
diff -q plink2_pca.eigenvec plink2_pca_rfreq.eigenvec |
| 90 |
$1/plink2 --bfile plink1_data --freq |
| 91 |
$1/plink2 $2 $3 --bfile plink1_data --read-freq plink2.afreq --maf 0.02 --pca 3 biallelic-var-wts --out plink2_pca_rfreq |
| 92 |
python3 pca_compare.py -1 plink2_pca -2 plink2_pca_rfreq -t 0.000002 |
| 93 |
$1/plink2 --bfile plink1_data --geno-counts |
| 94 |
$1/plink2 $2 $3 --bfile plink1_data --read-freq plink2.gcount --maf 0.02 --pca 3 biallelic-var-wts --out plink2_pca_rfreq |
| 95 |
diff -q plink2_pca.eigenvec plink2_pca_rfreq.eigenvec |
| 96 |
|
| 97 |
# note that this run depends on the random seed. |
| 98 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pca 3 approx biallelic-var-wts --out plink2_pca_approx |
| 99 |
python3 pca_compare.py -1 plink1_pca -2 plink2_pca_approx -t 0.009 |
| 100 |
|
| 101 |
# Test --glm and --clump. |
| 102 |
# Generate random binary and quantitative phenotypes for 1kg_phase3_chr21 |
| 103 |
# samples, and verify regression results are consistent with plink 1.9 within a |
| 104 |
# tolerance (tbd: see if we can generate new phenotypes each time, or if it's |
| 105 |
# better to just hardcode one pair) |
| 106 |
# |
| 107 |
# conditions: |
| 108 |
# - no covariates |
| 109 |
# - no covariates, 'genotypic' |
| 110 |
# - top PCs as covariates |
| 111 |
# - top PCs, 'genotypic' |
| 112 |
# - top PCs, --tests |
| 113 |
# |
| 114 |
# TODO: Also test --condition/--condition-list, local-covar. |
| 115 |
# TODO: TEST_MULTIALLELIC_VCF fixture, etc. |
| 116 |
|
| 117 |
$1/plink2 --dummy 2504 1 --seed 1 --out pheno_cc |
| 118 |
$1/plink2 --dummy 2504 1 0.01 0.01 scalar-pheno --seed 1 --out pheno_qt |
| 119 |
cat pheno_cc.psam | tail -n +2 | cut -f 3 > pheno_cc_col.txt |
| 120 |
cat pheno_qt.psam | tail -n +2 | cut -f 3 > pheno_qt_col.txt |
| 121 |
cat plink1_data.fam | cut -f 1-2 > sample_ids.txt |
| 122 |
paste sample_ids.txt pheno_cc_col.txt > pheno_cc.txt |
| 123 |
paste sample_ids.txt pheno_qt_col.txt > pheno_qt.txt |
| 124 |
|
| 125 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --logistic --allow-no-sex --out plink1_glm |
| 126 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm allow-no-covars no-firth single-prec-cc --out plink2_glm |
| 127 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm.PHENO1.glm.logistic -t 0.1 |
| 128 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm allow-no-covars no-firth --out plink2_glm_dbl |
| 129 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm_dbl.PHENO1.glm.logistic -t 0.3 |
| 130 |
plink --bfile plink1_data --clump plink2_glm_dbl.PHENO1.glm.logistic --clump-snp-field ID --clump-p1 0.1 --clump-p2 0.2 --out plink1_test |
| 131 |
# Extract SNP, TOTAL, NSIG, S05, S01, S001, S0001, and SP2 columns. |
| 132 |
# ($12 != "") check needed because plink 1.x puts two blank lines at the end of |
| 133 |
# the .clumped file. |
| 134 |
cat plink1_test.clumped | tail -n +2 | awk '{if ($12 == "NONE") $12 = "."; if ($12 != "") print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink1_test.clump_compare |
| 135 |
$1/plink2 --bfile plink1_data --clump cols=+f plink2_glm_dbl.PHENO1.glm.logistic --clump-p1 0.1 --clump-p2 0.2 --out plink2_test |
| 136 |
cat plink2_test.clumps | tail -n +2 | awk '{print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink2_test.clump_compare |
| 137 |
diff -q plink1_test.clump_compare plink2_test.clump_compare |
| 138 |
|
| 139 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --linear --allow-no-sex --out plink1_glm |
| 140 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm allow-no-covars --out plink2_glm |
| 141 |
python3 glm_compare.py -1 plink1_glm.assoc.linear -2 plink2_glm.PHENO1.glm.linear -t 0.1 |
| 142 |
plink --bfile plink1_data --clump plink2_glm.PHENO1.glm.linear --clump-snp-field ID --clump-p1 0.1 --clump-p2 0.2 --out plink1_test |
| 143 |
cat plink1_test.clumped | tail -n +2 | awk '{if ($12 == "NONE") $12 = "."; if ($12 != "") print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink1_test.clump_compare |
| 144 |
$1/plink2 --bfile plink1_data --clump cols=+f plink2_glm.PHENO1.glm.linear --clump-p1 0.1 --clump-p2 0.2 --out plink2_test |
| 145 |
cat plink2_test.clumps | tail -n +2 | awk '{print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink2_test.clump_compare |
| 146 |
diff -q plink1_test.clump_compare plink2_test.clump_compare |
| 147 |
|
| 148 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --logistic genotypic --allow-no-sex --out plink1_glm |
| 149 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm allow-no-covars no-firth single-prec-cc genotypic --out plink2_glm |
| 150 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm.PHENO1.glm.logistic -t 0.1 |
| 151 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm allow-no-covars no-firth genotypic --out plink2_glm_dbl |
| 152 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm_dbl.PHENO1.glm.logistic -t 0.3 |
| 153 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --linear genotypic --allow-no-sex --out plink1_glm |
| 154 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm allow-no-covars genotypic --out plink2_glm |
| 155 |
python3 glm_compare.py -1 plink1_glm.assoc.linear -2 plink2_glm.PHENO1.glm.linear -t 0.1 |
| 156 |
# don't test --clump here since plink 1.x ignores TEST column |
| 157 |
|
| 158 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --logistic --covar plink1_pca.eigenvec --allow-no-sex --out plink1_glm |
| 159 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth single-prec-cc --covar plink2_pca.eigenvec --out plink2_glm |
| 160 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm.PHENO1.glm.logistic -t 0.1 |
| 161 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth --covar plink2_pca.eigenvec --out plink2_glm_dbl |
| 162 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm_dbl.PHENO1.glm.logistic -t 0.3 |
| 163 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --linear --covar plink1_pca.eigenvec --allow-no-sex --out plink1_glm |
| 164 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm --covar plink2_pca.eigenvec --out plink2_glm |
| 165 |
python3 glm_compare.py -1 plink1_glm.assoc.linear -2 plink2_glm.PHENO1.glm.linear -t 0.1 |
| 166 |
# hide-covar allows --clump test here |
| 167 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm hide-covar --covar plink2_pca.eigenvec --out plink2_glm |
| 168 |
plink --bfile plink1_data --clump plink2_glm.PHENO1.glm.linear --clump-snp-field ID --clump-p1 0.1 --clump-p2 0.2 --out plink1_test |
| 169 |
cat plink1_test.clumped | tail -n +2 | awk '{if ($12 == "NONE") $12 = "."; if ($12 != "") print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink1_test.clump_compare |
| 170 |
$1/plink2 --bfile plink1_data --clump cols=+f plink2_glm.PHENO1.glm.linear --clump-p1 0.1 --clump-p2 0.2 --out plink2_test |
| 171 |
cat plink2_test.clumps | tail -n +2 | awk '{print $3"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12}' > plink2_test.clump_compare |
| 172 |
diff -q plink1_test.clump_compare plink2_test.clump_compare |
| 173 |
|
| 174 |
# Exclude rs368311657 for now since possible ARM/x86 mismatch seems to be |
| 175 |
# spurious (driven by floating-point rounding), and it's in plink 1.9 (which |
| 176 |
# doesn't try to set FP rounding mode) rather than 2.0. |
| 177 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --logistic genotypic --covar plink1_pca.eigenvec --allow-no-sex --out plink1_glm --exclude-snp rs368311657 |
| 178 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth single-prec-cc genotypic --covar plink2_pca.eigenvec --out plink2_glm --exclude-snp rs368311657 |
| 179 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm.PHENO1.glm.logistic -t 0.1 |
| 180 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth genotypic --covar plink2_pca.eigenvec --out plink2_glm_dbl --exclude-snp rs368311657 |
| 181 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm_dbl.PHENO1.glm.logistic -t 0.3 |
| 182 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --linear genotypic --covar plink1_pca.eigenvec --allow-no-sex --out plink1_glm |
| 183 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm genotypic --covar plink2_pca.eigenvec --out plink2_glm |
| 184 |
python3 glm_compare.py -1 plink1_glm.assoc.linear -2 plink2_glm.PHENO1.glm.linear -t 0.1 |
| 185 |
|
| 186 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --logistic --covar plink1_pca.eigenvec --tests 1,3,4 --allow-no-sex --out plink1_glm |
| 187 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth single-prec-cc --covar plink2_pca.eigenvec --tests 1,3,4 --out plink2_glm |
| 188 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm.PHENO1.glm.logistic -t 0.1 |
| 189 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_cc.txt --glm no-firth --covar plink2_pca.eigenvec --tests 1,3,4 --out plink2_glm_dbl |
| 190 |
python3 glm_compare.py -1 plink1_glm.assoc.logistic -2 plink2_glm_dbl.PHENO1.glm.logistic -t 0.3 |
| 191 |
# Note that 5 is "out of range" here. This was handled incorrectly by ALL |
| 192 |
# versions of plink before this test!! |
| 193 |
plink --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --linear --covar plink1_pca.eigenvec --tests 1,3,5 --allow-no-sex --out plink1_glm |
| 194 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt.txt --glm --covar plink2_pca.eigenvec --tests 1,3,5 --out plink2_glm |
| 195 |
python3 glm_compare.py -1 plink1_glm.assoc.linear -2 plink2_glm.PHENO1.glm.linear -t 0.1 |
| 196 |
|
| 197 |
# Test --pheno/--covar support for comma-separated files. |
| 198 |
echo "#FID,IID,PHENO1" > pheno_qt_commas.txt |
| 199 |
cat pheno_qt.txt | tr '\t' ',' >> pheno_qt_commas.txt |
| 200 |
cat plink2_pca.eigenvec | tr '\t' ',' > plink2_pca_commas.eigenvec |
| 201 |
$1/plink2 $2 $3 --bfile plink1_data --maf 0.02 --pheno pheno_qt_commas.txt --glm --covar plink2_pca_commas.eigenvec --tests 1,3,5 --out plink2_glm_commas |
| 202 |
diff -q plink2_glm.PHENO1.glm.linear plink2_glm_commas.PHENO1.glm.linear |
| 203 |
|
| 204 |
# Multiallelic-variant / mixed-phase round trip test. |
| 205 |
# (It may be necessary to update mt_mixed_phase.pgen when the encoder is tweaked.) |
| 206 |
$1/plink2 $2 $3 --pfile mt_mixed_phase vzs --export vcf bgz --out plink2_mt |
| 207 |
$1/plink2 $2 $3 --vcf plink2_mt.vcf.gz --psam mt_mixed_phase.psam --make-pgen --out plink2_mt2 |
| 208 |
diff -q mt_mixed_phase.pgen plink2_mt2.pgen |