<p>If you are familiar with plink and python, you can use <a href="https://pandas-plink.readthedocs.io/en/latest/api/pandas_plink.read_plink.html" rel="nofollow noreferrer">pandas_plink</a>.</p>
<pre><code>import pandas_plink
snp_info,sample_info,genotypes  = pandas_plink.read_plink('genotypes/chr.1')
genotype_mat = genotypes.compute()
</code></pre>
<p><code>genotype_mat</code> is a matrix of genotypes (in 0,1,2) and <code>sample_info</code> has the sample names and <code>snp_info</code> has the SNP positions, ref, alt etc. You need to convert vcf to plink format first. Please make sure ref and alt alleles are extracted as expected.</p>

CC BY-SA 4.0
2020-07-27T18:40:13.010Z
This looks very promising, thank you very much. Follow up question, how to deal with Nan values?
snowflake
CC BY-SA 4.0
2020-07-28T04:01:00.090Z
That should be a part of QC and depends on downstream goal. For example, if its for an eQTL analysis or any other association test, if there are not many missing values, instead of removing variant, a mean value is used to replace NaNs. But post-hoc this should be carefully checked.
geek_y
CC BY-SA 4.0
2020-07-27T11:46:20.487Z
2020-07-27T12:17:09.873Z
2020-07-27T12:17:09.873Z
geek_y
<p>How can I convert a VCF file into a genotype table (SNP matrix)?</p>
<p>I have this format:</p>
<pre><code>##fileformat=VCFv4.0
##fileDate=20200722
##source=test
##reference=test
##FILTER=&lt;ID=PASS,Description=&quot;All filters passed&quot;&gt;
##FORMAT=&lt;ID=GT,Number=1,Type=String,Description=&quot;Genotype&quot;&gt;
##FORMAT=&lt;ID=DP,Number=1,Type=Integer,Description=&quot;Read Depth&quot;&gt;
##FORMAT=&lt;ID=DV,Number=1,Type=Integer,Description=&quot;Number of high-quality non-reference bases&quot;&gt;
##FORMAT=&lt;ID=GQ,Number=1,Type=Integer,Description=&quot;Genotype Quality&quot;&gt;
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  HOR_997 HOR_1020    HOR_1251    HOR_2970    HOR_12367   HOR_473 HOR_8148    HOR_17169   HOR_21258   HOR_4606    HOR_15922   HOR_20226   HOR_9724    HOR_10742   HOR_1071    HOR_10702   HOR_18382   HOR_8819    HOR_9558    HOR_7428    HOR_3023    HOR_18300   HOR_13924   HOR_1080    HOR_4030    HOR_6962    HOR_3460    HOR_8117    HOR_337 HOR_16112   HOR_1626    HOR_8659    HOR_16569   HOR_11083   HOR_18087   HOR_4273    HOR_2237    HOR_8440    HOR_7056    HOR_869 HOR_13987   HOR_12184   HOR_13800   HOR_9808    HOR_10192   HOR_4264    HOR_12830   HOR_10764   HOR_12171   HOR_10784   HOR_610 HOR_11875   HOR_2637    HOR_4136    HOR_282 HOR_10725   HOR_17649   HOR_1384    HOR_2598    HOR_15841   HOR_14342   HOR_18917   HOR_3125    HOR_11662   HOR_6220    HOR_18201   HOR_12412   HOR_14876   HOR_19952   HOR_16358   HOR_6892    HOR_3155    HOR_19184   HOR_9927    HOR_14787   HOR_8370    HOR_4258    HOR_14411   HOR_21548   HOR_12070   HOR_7251    HOR_8904    HOR_4653    HOR_2749    HOR_2957    HOR_2028    HOR_1816    HOR_7236    HOR_495 HOR_2403    HOR_15860   HOR_14439   HOR_11409   HOR_9043    HOR_5876    HOR_13836   HOR_10886   HOR_13938   HOR_20269   HOR_11448   HOR_13979   HOR_14557   HOR_12311   HOR_10004   HOR_15908   HOR_1531    HOR_13821   HOR_18681   HOR_6936    HOR_1990    HOR_10990   HOR_3187    HOR_3728    HOR_15857   HOR_20909   HOR_1581    HOR_13462   HOR_14914   HOR_16149   HOR_12168   HOR_6952    HOR_1048    HOR_18824   HOR_1361    HOR_16071   HOR_3233    HOR_21599   HOR_15871   HOR_11101   HOR_13724   HOR_1780    HOR_8711    HOR_3369    HOR_3909    HOR_21461   HOR_3093    HOR_11013   HOR_18795   HOR_15254   HOR_5486    HOR_17591   HOR_19568   HOR_7129    HOR_6934    HOR_8760    HOR_18924   HOR_21687   HOR_15895   HOR_15898   HOR_15905   HOR_21641   HOR_3686    HOR_7657    HOR_18385   HOR_8710    HOR_17077   HOR_2293    HOR_3081    HOR_7172    HOR_4201    HOR_2383    HOR_19379   HOR_7124    HOR_3912    HOR_15401   HOR_18381   HOR_4034    HOR_16078   HOR_12072   HOR_13716   HOR_4403    HOR_4829    HOR_11431   HOR_4138    HOR_2290    HOR_15788   HOR_7552    HOR_10775   HOR_3153    HOR_14689   HOR_11126   HOR_2180    HOR_2369    HOR_1044    HOR_21428   HOR_18220   HOR_3359    HOR_15778   HOR_15840   HOR_18364   HOR_9250    HOR_11922   HOR_3926    HOR_7474    HOR_12830   HOR_5020    HOR_11450   HOR_9492    HOR_5373    HOR_18913   HOR_12791
1H  42240   .   C   A   999 .   .   GT:DP:DV:GQ 0/0:15:0:100    0/1:9:3:100 0/0:3:0:100 0/0:20:0:100    0/0:4:0:100 1/1:3:3:100 ./.:.:.:.   0/0:24:0:100    0/0:9:0:100 0/0:12:0:100    0/0:14:0:100    ./.:.:.:.   0/0:9:0:100 ./.:.:.:.   0/0:29:0:100    0/0:10:0:100    0/0:5:0:100 0/0:14:0:100    0/0:3:0:100 0/0:18:0:100    0/0:18:0:100    0/0:7:0:100 0/0:9:0:100 0/0:9:0:100 1/1:5:5:100 0/0:13:0:100    ./.:.:.:.   0/0:22:0:100    ./.:.:.:.   ./.:.:.:.   0/0:4:0:100 0/0:18:0:100    ./.:.:.:.   0/0:7:0:100 0/0:11:0:100    0/0:32:0:100    0/0:13:0:100    ./.:.:.:.   0/0:15:0:100    0/0:12:0:100    0/0:8:0:100 ./.:.:.:.   0/0:20:0:100    0/0:11:0:100    0/0:6:0:100 0/0:14:0:100    0/0:20:0:100    0/0:5:0:100 ./.:.:.:.   0/0:3:0:100 0/0:5:0:100 0/0:16:0:100    1/1:3:3:100 0/0:32:0:100    0/0:15:0:100    0/0:17:0:100    0/0:16:0:100    0/0:4:0:100 0/0:17:0:100    0/0:2:0:100 0/0:4:0:100 0/0:7:0:100 0/0:20:0:100    ./.:.:.:.   0/0:20:0:100    0/0:8:0:100 0/0:20:0:100    0/0:3:0:100 0/0:21:0:100    0/0:5:0:100 ./.:.:.:.   0/0:12:0:100    0/0:16:0:100    0/0:87:0:100    0/0:21:0:100    0/0:5:0:100 0/0:20:0:100    1/1:6:6:100 0/0:6:0:100 0/0:10:0:100    ./.:.:.:.   0/0:6:0:100 0/0:4:0:100 0/0:8:0:100 0/0:19:0:100    0/0:17:0:100    0/0:4:0:100 0/0:3:0:100 1/1:7:7:100 0/0:18:0:100    0/0:12:0:100    0/0:11:0:100    0/0:13:0:100    ./.:.:.:.   0/0:18:0:100    0/0:18:0:100    0/0:2:0:100 0/0:12:0:100    0/0:10:0:100    0/0:3:0:100 ./.:.:.:.   0/0:12:0:100    0/0:2:0:100 0/0:8:0:100 0/0:15:0:100    0/0:7:0:100 0/0:22:0:100    0/0:12:0:100    0/0:14:0:100    0/0:8:0:100 0/0:10:0:100    0/0:22:0:100    0/0:10:0:100    0/0:11:0:100    0/0:3:0:100 0/0:6:0:100 0/0:6:0:100 0/0:8:0:100 0/0:18:0:100    ./.:.:.:.   0/0:13:0:100    0/0:20:0:100    0/0:17:0:100    0/0:8:0:100 0/0:4:0:100 0/0:11:0:100    0/0:12:0:100    0/0:8:0:100 0/0:12:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:10:0:100    0/0:6:0:100 0/0:10:0:100    0/0:15:0:100    0/0:15:0:100    0/0:17:0:100    0/0:4:0:100 0/0:9:0:100 ./.:.:.:.   0/0:14:0:100    0/0:13:0:100    0/0:9:0:100 0/0:3:0:100 0/0:12:0:100    0/0:12:0:100    ./.:.:.:.   ./.:.:.:.   0/1:5:2:100 0/0:21:0:100    0/0:9:0:100 0/0:14:0:100    0/0:18:0:100    0/0:11:0:100    1/1:3:3:100 0/0:19:0:100    0/0:25:0:100    ./.:.:.:.   ./.:.:.:.   0/0:19:0:100    0/0:12:0:100    0/0:7:0:100 0/0:10:0:100    0/0:13:0:100    0/0:11:0:100    0/0:13:0:100    0/0:13:0:100    0/0:6:0:100 ./.:.:.:.   0/0:4:0:100 0/0:10:0:100    0/0:9:0:100 0/0:14:2:100    1/1:2:2:100 0/0:16:0:100    ./.:.:.:.   0/0:27:0:100    0/0:18:0:100    0/0:6:0:100 0/0:10:0:100    0/0:14:0:100    0/0:7:0:100 1/1:3:3:100 0/0:15:0:100    0/0:4:0:100 0/0:15:0:100    ./.:.:.:.   ./.:.:.:.   0/0:8:0:100 0/0:16:0:100    0/0:4:0:100 ./.:.:.:.   0/0:12:0:100    0/0:12:0:100    0/0:10:0:100    0/0:7:0:100 0/0:29:0:100    0/0:7:0:100 0/0:8:0:100 0/0:32:0:100
1H  42247   .   C   A   999 .   .   GT:DP:DV:GQ 0/0:15:0:100    0/0:11:0:100    1/1:2:2:100 0/0:19:0:100    0/0:5:0:100 ./.:.:.:.   ./.:.:.:.   0/0:23:0:100    0/0:12:0:100    1/1:13:13:100   0/0:14:0:100    ./.:.:.:.   0/0:9:0:100 ./.:.:.:.   1/1:28:27:100   0/0:12:0:100    1/1:6:6:100 0/0:15:0:100    1/1:3:3:100 0/0:17:0:100    0/0:19:0:100    0/0:7:0:100 0/0:9:0:100 0/0:10:0:100    0/0:4:0:100 1/1:12:12:100   ./.:.:.:.   0/0:22:0:100    ./.:.:.:.   ./.:.:.:.   0/0:6:0:100 0/0:17:0:100    ./.:.:.:.   0/0:8:0:100 0/0:11:0:100    0/0:33:0:100    0/0:14:0:100    ./.:.:.:.   0/0:15:0:100    0/0:11:0:100    0/0:8:0:100 ./.:.:.:.   0/0:21:0:100    0/0:11:0:100    0/0:6:0:100 1/1:15:15:100   0/0:20:0:100    0/0:5:0:100 ./.:.:.:.   0/0:3:0:100 0/0:6:0:100 0/0:17:0:100    0/0:4:0:100 0/0:32:0:100    1/1:15:15:100   0/0:16:0:100    0/0:18:0:100    0/0:5:0:100 0/0:17:0:100    0/0:2:0:100 0/0:5:0:100 0/0:7:0:100 0/0:20:0:100    ./.:.:.:.   0/0:21:0:100    0/0:8:0:100 0/0:20:0:100    0/0:3:0:100 1/1:21:21:100   0/0:6:0:100 0/0:2:0:100 0/0:11:0:100    0/0:15:0:100    0/0:88:0:100    0/0:21:0:100    1/1:5:5:100 0/0:22:0:100    0/0:8:0:100 0/0:6:0:100 0/0:10:0:100    ./.:.:.:.   0/0:5:0:100 1/1:4:4:100 0/0:8:0:100 0/0:19:0:100    0/0:17:0:100    1/1:4:4:100 1/1:3:3:100 0/0:5:0:100 0/0:18:0:100    0/0:11:0:100    0/0:11:0:100    0/0:15:0:100    ./.:.:.:.   0/0:18:0:100    0/0:18:0:100    0/0:2:0:100 0/0:15:0:100    0/0:11:0:100    1/1:3:3:100 ./.:.:.:.   0/0:14:0:100    ./.:.:.:.   1/1:7:7:100 0/0:16:0:100    0/0:7:0:100 0/0:23:0:100    0/0:14:0:100    0/0:14:0:100    0/0:8:0:100 0/0:10:0:100    0/0:24:0:100    0/0:10:0:100    0/0:11:0:100    0/0:3:0:100 0/0:5:0:100 0/0:7:0:100 1/1:8:8:100 0/0:17:0:100    ./.:.:.:.   0/0:14:0:100    0/0:20:0:100    0/0:17:0:100    1/1:8:8:100 0/0:4:0:100 0/0:11:0:100    0/0:13:0:100    0/0:8:0:100 0/0:15:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:11:0:100    0/0:7:0:100 0/0:10:0:100    0/0:17:0:100    0/0:18:0:100    0/0:16:0:100    0/0:4:0:100 0/0:9:0:100 ./.:.:.:.   0/0:14:0:100    0/0:13:1:100    0/0:9:0:100 0/0:3:0:100 0/0:12:0:100    0/0:12:0:100    ./.:.:.:.   0/0:3:0:100 0/0:4:0:100 0/0:21:0:100    0/0:9:0:100 0/0:16:0:100    0/0:16:0:100    0/0:12:0:100    0/0:4:0:100 0/0:20:0:100    0/0:24:0:100    ./.:.:.:.   ./.:.:.:.   0/0:18:0:100    0/0:12:0:100    0/0:7:0:100 0/0:10:0:100    0/0:12:0:100    0/0:12:0:100    0/0:14:0:100    0/0:13:0:100    1/1:6:6:100 ./.:.:.:.   0/0:5:0:100 0/0:13:0:100    0/0:8:0:100 0/0:13:0:100    0/0:4:0:100 0/0:15:0:100    ./.:.:.:.   0/0:29:0:100    0/0:19:0:100    0/0:6:0:100 0/0:9:0:100 0/0:15:0:100    1/1:7:7:100 0/0:4:0:100 1/1:14:14:100   1/1:4:4:100 0/0:16:0:100    ./.:.:.:.   ./.:.:.:.   0/0:8:0:100 0/0:15:0:100    1/1:4:4:100 ./.:.:.:.   1/1:13:13:100   0/0:12:0:100    0/0:10:0:100    1/1:6:6:100 0/0:30:0:100    0/0:8:0:100 0/0:7:0:100 0/0:31:0:100
1H  42264   .   G   T   999 .   .   GT:DP:DV:GQ 0/0:23:0:100    0/0:17:0:100    0/0:3:0:100 0/0:21:0:100    0/0:6:0:100 0/0:8:0:100 ./.:.:.:.   0/0:29:0:100    0/0:20:0:100    0/0:9:0:100 0/0:20:0:100    ./.:.:.:.   0/0:11:0:100    ./.:.:.:.   0/0:26:0:100    0/0:13:0:100    0/0:8:0:100 0/0:19:0:100    0/0:3:0:100 0/0:19:0:100    0/0:18:0:100    0/0:7:0:100 0/0:9:0:100 0/0:18:0:100    0/0:9:0:100 0/0:13:0:100    ./.:.:.:.   0/0:22:0:100    0/0:6:0:100 ./.:.:.:.   0/0:7:0:100 0/0:22:0:100    ./.:.:.:.   0/0:10:0:100    0/0:14:0:100    0/0:39:0:100    0/0:16:0:100    ./.:.:.:.   0/0:21:0:100    0/0:19:0:100    0/0:8:0:100 ./.:.:.:.   0/0:25:0:100    0/0:13:0:100    0/0:6:0:100 0/0:19:0:100    0/0:19:0:100    0/0:4:0:100 ./.:.:.:.   0/0:2:0:100 0/0:11:0:100    0/0:28:0:100    0/0:11:0:100    0/0:37:0:100    0/0:12:0:100    0/0:25:0:100    0/0:19:0:100    0/0:9:0:100 0/0:17:0:100    ./.:.:.:.   0/0:6:0:100 0/0:9:0:100 0/0:28:0:100    0/0:3:0:100 0/0:24:0:100    0/0:11:0:100    0/0:23:0:100    0/0:4:0:100 0/0:16:0:100    0/0:7:0:100 0/0:7:0:100 0/0:19:0:100    0/0:17:0:100    0/0:105:0:100   0/0:23:0:100    0/0:6:0:100 0/0:28:0:100    0/0:18:0:100    0/0:9:0:100 0/0:15:0:100    ./.:.:.:.   0/0:7:0:100 0/0:6:0:100 0/0:11:0:100    0/0:23:0:100    0/0:19:0:100    0/0:7:0:100 0/0:7:0:100 0/0:32:0:100    0/0:20:0:100    0/0:13:0:100    0/0:14:0:100    0/0:18:0:100    0/0:11:0:100    0/0:23:0:100    0/0:19:0:100    0/0:2:0:100 0/0:20:0:100    0/0:15:0:100    0/0:4:0:100 ./.:.:.:.   0/0:17:0:100    0/0:2:0:100 0/0:10:0:100    0/0:17:0:100    0/0:8:0:100 0/0:23:0:100    0/0:20:0:100    0/0:17:0:100    0/0:8:0:100 0/0:14:0:100    0/0:30:0:100    0/0:12:0:100    0/0:15:0:100    0/0:3:0:100 0/0:6:0:100 0/0:9:0:100 0/0:13:0:100    0/0:18:0:100    ./.:.:.:.   0/0:18:0:100    0/0:21:0:100    0/0:23:0:100    0/0:9:0:100 0/0:6:0:100 0/0:14:0:100    0/0:14:0:100    0/0:9:0:100 0/0:18:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:18:0:100    0/0:8:0:100 0/0:15:0:100    0/0:17:0:100    0/0:23:0:100    0/0:26:0:100    0/0:10:0:100    0/0:12:0:100    ./.:.:.:.   0/0:17:0:100    0/0:15:0:100    0/0:11:0:100    0/0:4:0:100 0/0:16:0:100    0/0:15:0:100    ./.:.:.:.   0/0:10:0:100    0/0:7:0:100 0/0:22:0:100    0/0:10:0:100    0/0:21:0:100    0/0:24:0:100    0/0:13:0:100    0/0:19:0:100    0/0:24:0:100    0/0:32:0:100    ./.:.:.:.   0/0:6:0:100 0/0:23:0:100    0/0:17:0:100    0/0:15:0:100    0/0:15:0:100    0/0:15:0:100    0/0:14:0:100    0/0:16:0:100    0/0:17:0:100    0/0:7:0:100 ./.:.:.:.   0/0:8:0:100 0/0:14:0:100    0/0:12:0:100    0/0:30:0:100    0/0:19:0:100    0/0:24:0:100    ./.:.:.:.   0/0:36:0:100    0/0:26:0:100    0/0:7:0:100 0/0:10:0:100    0/0:21:0:100    0/0:8:0:100 0/0:13:0:100    0/0:17:0:100    0/0:4:0:100 0/0:18:0:100    0/0:2:0:100 ./.:.:.:.   0/0:9:0:100 0/0:18:0:100    0/0:5:0:100 ./.:.:.:.   0/0:9:0:100 0/0:14:0:100    0/0:15:0:100    0/0:11:0:100    0/0:32:0:100    0/0:9:0:100 0/0:12:0:100    0/0:34:0:100
</code></pre>
<p>And I need a SNP matrix in this format:</p>
<pre><code> , pos1, pos2, pos3
ID1   0,    1,    0
ID2   1,    1,    0
</code></pre>

<python><vcf><snp><gwas>
Convert VCF to genotype table
# Convert VCF to genotype table

(Asked by: username_0 on 2020-07-27)

How can I convert a VCF file into a genotype table (SNP matrix)?
I have this format:
##fileformat=VCFv4.0
##fileDate=20200722
##source=test
##reference=test
##FILTER=
##FORMAT=
##FORMAT=
##FORMAT=
##FORMAT=
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  HOR_997 HOR_1020    HOR_1251    HOR_2970    HOR_12367   HOR_473 HOR_8148    HOR_17169   HOR_21258   HOR_4606    HOR_15922   HOR_20226   HOR_9724    HOR_10742   HOR_1071    HOR_10702   HOR_18382   HOR_8819    HOR_9558    HOR_7428    HOR_3023    HOR_18300   HOR_13924   HOR_1080    HOR_4030    HOR_6962    HOR_3460    HOR_8117    HOR_337 HOR_16112   HOR_1626    HOR_8659    HOR_16569   HOR_11083   HOR_18087   HOR_4273    HOR_2237    HOR_8440    HOR_7056    HOR_869 HOR_13987   HOR_12184   HOR_13800   HOR_9808    HOR_10192   HOR_4264    HOR_12830   HOR_10764   HOR_12171   HOR_10784   HOR_610 HOR_11875   HOR_2637    HOR_4136    HOR_282 HOR_10725   HOR_17649   HOR_1384    HOR_2598    HOR_15841   HOR_14342   HOR_18917   HOR_3125    HOR_11662   HOR_6220    HOR_18201   HOR_12412   HOR_14876   HOR_19952   HOR_16358   HOR_6892    HOR_3155    HOR_19184   HOR_9927    HOR_14787   HOR_8370    HOR_4258    HOR_14411   HOR_21548   HOR_12070   HOR_7251    HOR_8904    HOR_4653    HOR_2749    HOR_2957    HOR_2028    HOR_1816    HOR_7236    HOR_495 HOR_2403    HOR_15860   HOR_14439   HOR_11409   HOR_9043    HOR_5876    HOR_13836   HOR_10886   HOR_13938   HOR_20269   HOR_11448   HOR_13979   HOR_14557   HOR_12311   HOR_10004   HOR_15908   HOR_1531    HOR_13821   HOR_18681   HOR_6936    HOR_1990    HOR_10990   HOR_3187    HOR_3728    HOR_15857   HOR_20909   HOR_1581    HOR_13462   HOR_14914   HOR_16149   HOR_12168   HOR_6952    HOR_1048    HOR_18824   HOR_1361    HOR_16071   HOR_3233    HOR_21599   HOR_15871   HOR_11101   HOR_13724   HOR_1780    HOR_8711    HOR_3369    HOR_3909    HOR_21461   HOR_3093    HOR_11013   HOR_18795   HOR_15254   HOR_5486    HOR_17591   HOR_19568   HOR_7129    HOR_6934    HOR_8760    HOR_18924   HOR_21687   HOR_15895   HOR_15898   HOR_15905   HOR_21641   HOR_3686    HOR_7657    HOR_18385   HOR_8710    HOR_17077   HOR_2293    HOR_3081    HOR_7172    HOR_4201    HOR_2383    HOR_19379   HOR_7124    HOR_3912    HOR_15401   HOR_18381   HOR_4034    HOR_16078   HOR_12072   HOR_13716   HOR_4403    HOR_4829    HOR_11431   HOR_4138    HOR_2290    HOR_15788   HOR_7552    HOR_10775   HOR_3153    HOR_14689   HOR_11126   HOR_2180    HOR_2369    HOR_1044    HOR_21428   HOR_18220   HOR_3359    HOR_15778   HOR_15840   HOR_18364   HOR_9250    HOR_11922   HOR_3926    HOR_7474    HOR_12830   HOR_5020    HOR_11450   HOR_9492    HOR_5373    HOR_18913   HOR_12791
1H  42240   .   C   A   999 .   .   GT:DP:DV:GQ 0/0:15:0:100    0/1:9:3:100 0/0:3:0:100 0/0:20:0:100    0/0:4:0:100 1/1:3:3:100 ./.:.:.:.   0/0:24:0:100    0/0:9:0:100 0/0:12:0:100    0/0:14:0:100    ./.:.:.:.   0/0:9:0:100 ./.:.:.:.   0/0:29:0:100    0/0:10:0:100    0/0:5:0:100 0/0:14:0:100    0/0:3:0:100 0/0:18:0:100    0/0:18:0:100    0/0:7:0:100 0/0:9:0:100 0/0:9:0:100 1/1:5:5:100 0/0:13:0:100    ./.:.:.:.   0/0:22:0:100    ./.:.:.:.   ./.:.:.:.   0/0:4:0:100 0/0:18:0:100    ./.:.:.:.   0/0:7:0:100 0/0:11:0:100    0/0:32:0:100    0/0:13:0:100    ./.:.:.:.   0/0:15:0:100    0/0:12:0:100    0/0:8:0:100 ./.:.:.:.   0/0:20:0:100    0/0:11:0:100    0/0:6:0:100 0/0:14:0:100    0/0:20:0:100    0/0:5:0:100 ./.:.:.:.   0/0:3:0:100 0/0:5:0:100 0/0:16:0:100    1/1:3:3:100 0/0:32:0:100    0/0:15:0:100    0/0:17:0:100    0/0:16:0:100    0/0:4:0:100 0/0:17:0:100    0/0:2:0:100 0/0:4:0:100 0/0:7:0:100 0/0:20:0:100    ./.:.:.:.   0/0:20:0:100    0/0:8:0:100 0/0:20:0:100    0/0:3:0:100 0/0:21:0:100    0/0:5:0:100 ./.:.:.:.   0/0:12:0:100    0/0:16:0:100    0/0:87:0:100    0/0:21:0:100    0/0:5:0:100 0/0:20:0:100    1/1:6:6:100 0/0:6:0:100 0/0:10:0:100    ./.:.:.:.   0/0:6:0:100 0/0:4:0:100 0/0:8:0:100 0/0:19:0:100    0/0:17:0:100    0/0:4:0:100 0/0:3:0:100 1/1:7:7:100 0/0:18:0:100    0/0:12:0:100    0/0:11:0:100    0/0:13:0:100    ./.:.:.:.   0/0:18:0:100    0/0:18:0:100    0/0:2:0:100 0/0:12:0:100    0/0:10:0:100    0/0:3:0:100 ./.:.:.:.   0/0:12:0:100    0/0:2:0:100 0/0:8:0:100 0/0:15:0:100    0/0:7:0:100 0/0:22:0:100    0/0:12:0:100    0/0:14:0:100    0/0:8:0:100 0/0:10:0:100    0/0:22:0:100    0/0:10:0:100    0/0:11:0:100    0/0:3:0:100 0/0:6:0:100 0/0:6:0:100 0/0:8:0:100 0/0:18:0:100    ./.:.:.:.   0/0:13:0:100    0/0:20:0:100    0/0:17:0:100    0/0:8:0:100 0/0:4:0:100 0/0:11:0:100    0/0:12:0:100    0/0:8:0:100 0/0:12:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:10:0:100    0/0:6:0:100 0/0:10:0:100    0/0:15:0:100    0/0:15:0:100    0/0:17:0:100    0/0:4:0:100 0/0:9:0:100 ./.:.:.:.   0/0:14:0:100    0/0:13:0:100    0/0:9:0:100 0/0:3:0:100 0/0:12:0:100    0/0:12:0:100    ./.:.:.:.   ./.:.:.:.   0/1:5:2:100 0/0:21:0:100    0/0:9:0:100 0/0:14:0:100    0/0:18:0:100    0/0:11:0:100    1/1:3:3:100 0/0:19:0:100    0/0:25:0:100    ./.:.:.:.   ./.:.:.:.   0/0:19:0:100    0/0:12:0:100    0/0:7:0:100 0/0:10:0:100    0/0:13:0:100    0/0:11:0:100    0/0:13:0:100    0/0:13:0:100    0/0:6:0:100 ./.:.:.:.   0/0:4:0:100 0/0:10:0:100    0/0:9:0:100 0/0:14:2:100    1/1:2:2:100 0/0:16:0:100    ./.:.:.:.   0/0:27:0:100    0/0:18:0:100    0/0:6:0:100 0/0:10:0:100    0/0:14:0:100    0/0:7:0:100 1/1:3:3:100 0/0:15:0:100    0/0:4:0:100 0/0:15:0:100    ./.:.:.:.   ./.:.:.:.   0/0:8:0:100 0/0:16:0:100    0/0:4:0:100 ./.:.:.:.   0/0:12:0:100    0/0:12:0:100    0/0:10:0:100    0/0:7:0:100 0/0:29:0:100    0/0:7:0:100 0/0:8:0:100 0/0:32:0:100
1H  42247   .   C   A   999 .   .   GT:DP:DV:GQ 0/0:15:0:100    0/0:11:0:100    1/1:2:2:100 0/0:19:0:100    0/0:5:0:100 ./.:.:.:.   ./.:.:.:.   0/0:23:0:100    0/0:12:0:100    1/1:13:13:100   0/0:14:0:100    ./.:.:.:.   0/0:9:0:100 ./.:.:.:.   1/1:28:27:100   0/0:12:0:100    1/1:6:6:100 0/0:15:0:100    1/1:3:3:100 0/0:17:0:100    0/0:19:0:100    0/0:7:0:100 0/0:9:0:100 0/0:10:0:100    0/0:4:0:100 1/1:12:12:100   ./.:.:.:.   0/0:22:0:100    ./.:.:.:.   ./.:.:.:.   0/0:6:0:100 0/0:17:0:100    ./.:.:.:.   0/0:8:0:100 0/0:11:0:100    0/0:33:0:100    0/0:14:0:100    ./.:.:.:.   0/0:15:0:100    0/0:11:0:100    0/0:8:0:100 ./.:.:.:.   0/0:21:0:100    0/0:11:0:100    0/0:6:0:100 1/1:15:15:100   0/0:20:0:100    0/0:5:0:100 ./.:.:.:.   0/0:3:0:100 0/0:6:0:100 0/0:17:0:100    0/0:4:0:100 0/0:32:0:100    1/1:15:15:100   0/0:16:0:100    0/0:18:0:100    0/0:5:0:100 0/0:17:0:100    0/0:2:0:100 0/0:5:0:100 0/0:7:0:100 0/0:20:0:100    ./.:.:.:.   0/0:21:0:100    0/0:8:0:100 0/0:20:0:100    0/0:3:0:100 1/1:21:21:100   0/0:6:0:100 0/0:2:0:100 0/0:11:0:100    0/0:15:0:100    0/0:88:0:100    0/0:21:0:100    1/1:5:5:100 0/0:22:0:100    0/0:8:0:100 0/0:6:0:100 0/0:10:0:100    ./.:.:.:.   0/0:5:0:100 1/1:4:4:100 0/0:8:0:100 0/0:19:0:100    0/0:17:0:100    1/1:4:4:100 1/1:3:3:100 0/0:5:0:100 0/0:18:0:100    0/0:11:0:100    0/0:11:0:100    0/0:15:0:100    ./.:.:.:.   0/0:18:0:100    0/0:18:0:100    0/0:2:0:100 0/0:15:0:100    0/0:11:0:100    1/1:3:3:100 ./.:.:.:.   0/0:14:0:100    ./.:.:.:.   1/1:7:7:100 0/0:16:0:100    0/0:7:0:100 0/0:23:0:100    0/0:14:0:100    0/0:14:0:100    0/0:8:0:100 0/0:10:0:100    0/0:24:0:100    0/0:10:0:100    0/0:11:0:100    0/0:3:0:100 0/0:5:0:100 0/0:7:0:100 1/1:8:8:100 0/0:17:0:100    ./.:.:.:.   0/0:14:0:100    0/0:20:0:100    0/0:17:0:100    1/1:8:8:100 0/0:4:0:100 0/0:11:0:100    0/0:13:0:100    0/0:8:0:100 0/0:15:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:11:0:100    0/0:7:0:100 0/0:10:0:100    0/0:17:0:100    0/0:18:0:100    0/0:16:0:100    0/0:4:0:100 0/0:9:0:100 ./.:.:.:.   0/0:14:0:100    0/0:13:1:100    0/0:9:0:100 0/0:3:0:100 0/0:12:0:100    0/0:12:0:100    ./.:.:.:.   0/0:3:0:100 0/0:4:0:100 0/0:21:0:100    0/0:9:0:100 0/0:16:0:100    0/0:16:0:100    0/0:12:0:100    0/0:4:0:100 0/0:20:0:100    0/0:24:0:100    ./.:.:.:.   ./.:.:.:.   0/0:18:0:100    0/0:12:0:100    0/0:7:0:100 0/0:10:0:100    0/0:12:0:100    0/0:12:0:100    0/0:14:0:100    0/0:13:0:100    1/1:6:6:100 ./.:.:.:.   0/0:5:0:100 0/0:13:0:100    0/0:8:0:100 0/0:13:0:100    0/0:4:0:100 0/0:15:0:100    ./.:.:.:.   0/0:29:0:100    0/0:19:0:100    0/0:6:0:100 0/0:9:0:100 0/0:15:0:100    1/1:7:7:100 0/0:4:0:100 1/1:14:14:100   1/1:4:4:100 0/0:16:0:100    ./.:.:.:.   ./.:.:.:.   0/0:8:0:100 0/0:15:0:100    1/1:4:4:100 ./.:.:.:.   1/1:13:13:100   0/0:12:0:100    0/0:10:0:100    1/1:6:6:100 0/0:30:0:100    0/0:8:0:100 0/0:7:0:100 0/0:31:0:100
1H  42264   .   G   T   999 .   .   GT:DP:DV:GQ 0/0:23:0:100    0/0:17:0:100    0/0:3:0:100 0/0:21:0:100    0/0:6:0:100 0/0:8:0:100 ./.:.:.:.   0/0:29:0:100    0/0:20:0:100    0/0:9:0:100 0/0:20:0:100    ./.:.:.:.   0/0:11:0:100    ./.:.:.:.   0/0:26:0:100    0/0:13:0:100    0/0:8:0:100 0/0:19:0:100    0/0:3:0:100 0/0:19:0:100    0/0:18:0:100    0/0:7:0:100 0/0:9:0:100 0/0:18:0:100    0/0:9:0:100 0/0:13:0:100    ./.:.:.:.   0/0:22:0:100    0/0:6:0:100 ./.:.:.:.   0/0:7:0:100 0/0:22:0:100    ./.:.:.:.   0/0:10:0:100    0/0:14:0:100    0/0:39:0:100    0/0:16:0:100    ./.:.:.:.   0/0:21:0:100    0/0:19:0:100    0/0:8:0:100 ./.:.:.:.   0/0:25:0:100    0/0:13:0:100    0/0:6:0:100 0/0:19:0:100    0/0:19:0:100    0/0:4:0:100 ./.:.:.:.   0/0:2:0:100 0/0:11:0:100    0/0:28:0:100    0/0:11:0:100    0/0:37:0:100    0/0:12:0:100    0/0:25:0:100    0/0:19:0:100    0/0:9:0:100 0/0:17:0:100    ./.:.:.:.   0/0:6:0:100 0/0:9:0:100 0/0:28:0:100    0/0:3:0:100 0/0:24:0:100    0/0:11:0:100    0/0:23:0:100    0/0:4:0:100 0/0:16:0:100    0/0:7:0:100 0/0:7:0:100 0/0:19:0:100    0/0:17:0:100    0/0:105:0:100   0/0:23:0:100    0/0:6:0:100 0/0:28:0:100    0/0:18:0:100    0/0:9:0:100 0/0:15:0:100    ./.:.:.:.   0/0:7:0:100 0/0:6:0:100 0/0:11:0:100    0/0:23:0:100    0/0:19:0:100    0/0:7:0:100 0/0:7:0:100 0/0:32:0:100    0/0:20:0:100    0/0:13:0:100    0/0:14:0:100    0/0:18:0:100    0/0:11:0:100    0/0:23:0:100    0/0:19:0:100    0/0:2:0:100 0/0:20:0:100    0/0:15:0:100    0/0:4:0:100 ./.:.:.:.   0/0:17:0:100    0/0:2:0:100 0/0:10:0:100    0/0:17:0:100    0/0:8:0:100 0/0:23:0:100    0/0:20:0:100    0/0:17:0:100    0/0:8:0:100 0/0:14:0:100    0/0:30:0:100    0/0:12:0:100    0/0:15:0:100    0/0:3:0:100 0/0:6:0:100 0/0:9:0:100 0/0:13:0:100    0/0:18:0:100    ./.:.:.:.   0/0:18:0:100    0/0:21:0:100    0/0:23:0:100    0/0:9:0:100 0/0:6:0:100 0/0:14:0:100    0/0:14:0:100    0/0:9:0:100 0/0:18:0:100    ./.:.:.:.   0/0:6:0:100 ./.:.:.:.   0/0:18:0:100    0/0:8:0:100 0/0:15:0:100    0/0:17:0:100    0/0:23:0:100    0/0:26:0:100    0/0:10:0:100    0/0:12:0:100    ./.:.:.:.   0/0:17:0:100    0/0:15:0:100    0/0:11:0:100    0/0:4:0:100 0/0:16:0:100    0/0:15:0:100    ./.:.:.:.   0/0:10:0:100    0/0:7:0:100 0/0:22:0:100    0/0:10:0:100    0/0:21:0:100    0/0:24:0:100    0/0:13:0:100    0/0:19:0:100    0/0:24:0:100    0/0:32:0:100    ./.:.:.:.   0/0:6:0:100 0/0:23:0:100    0/0:17:0:100    0/0:15:0:100    0/0:15:0:100    0/0:15:0:100    0/0:14:0:100    0/0:16:0:100    0/0:17:0:100    0/0:7:0:100 ./.:.:.:.   0/0:8:0:100 0/0:14:0:100    0/0:12:0:100    0/0:30:0:100    0/0:19:0:100    0/0:24:0:100    ./.:.:.:.   0/0:36:0:100    0/0:26:0:100    0/0:7:0:100 0/0:10:0:100    0/0:21:0:100    0/0:8:0:100 0/0:13:0:100    0/0:17:0:100    0/0:4:0:100 0/0:18:0:100    0/0:2:0:100 ./.:.:.:.   0/0:9:0:100 0/0:18:0:100    0/0:5:0:100 ./.:.:.:.   0/0:9:0:100 0/0:14:0:100    0/0:15:0:100    0/0:11:0:100    0/0:32:0:100    0/0:9:0:100 0/0:12:0:100    0/0:34:0:100

And I need a SNP matrix in this format:
 , pos1, pos2, pos3
ID1   0,    1,    0
ID2   1,    1,    0

**Tags:** python, vcf, snp, gwas

---

## 1 answers

### Answer by username_1 on 2020-07-27. Score: 1 | Accepted answer

If you are familiar with plink and python, you can use pandas_plink.
import pandas_plink
snp_info,sample_info,genotypes  = pandas_plink.read_plink('genotypes/chr.1')
genotype_mat = genotypes.compute()

genotype_mat is a matrix of genotypes (in 0,1,2) and sample_info has the sample names and snp_info has the SNP positions, ref, alt etc. You need to convert vcf to plink format first. Please make sure ref and alt alleles are extracted as expected.

> **Comments:**
> **username_0**: This looks very promising, thank you very much. Follow up question, how to deal with Nan values?  
> **username_1**: That should be a part of QC and depends on downstream goal. For example, if its for an eQTL analysis or any other association test, if there are not many missing values, instead of removing variant, a mean value is used to replace NaNs. But post-hoc this should be carefully checked.  

---

