SELECT COUNT(DISTINCT cgi.sample_idSince)Impala不允许SET操作或select语句中的子查询,我很难弄清楚如何将列值除以返回的总行数。我的最终目标是计算每个chr:起始位置的次要等位基因频率。
我的数据结构如下:
| chr | start | stop | ref | allele1seq | allele2seq | sample_id |
| 6 | 66720709 | 66720710 | A | A | T | 101-46-3 |
| 7 | 66720809 | 66720810 | GG | GA | GG | 101-46-3 |
我想做类似于以下查询的事情:
WITH vars as
(SELECT cgi.chr, cgi.start, concat(cgi.chr, ':', CAST(cgi.start AS STRING)) as pos, cgi.ref, cgi.allele1seq, cgi.allele2seq,
CASE
WHEN (cgi.allele1seq = cgi.ref AND cgi.allele2seq <> cgi.ref) THEN '1'
WHEN (cgi.allele1seq <> cgi.ref AND cgi.allele2seq = cgi.ref) THEN '1'
WHEN (cgi.allele1seq = cgi.ref AND cgi.allele2seq = cgi.ref) THEN '2'
ELSE '0' END as ma_count
FROM comgen_variants as cgi)
SELECT vars.*, (CAST(vars.ma_count as INT)/
((SELECT COUNT(DISTINCT cgi.sample_id) from comgen_variants as cgi) * 2)) as maf
FROM vars
我想要的输出结果如下:
| chr | start | ref | allele1seq | allele2seq | ma_count | maf |
| 6 | 66720709 | A | A | T | 1 | .05 |
| 7 | 66720809 | GG | GG | GG | 0 | 0 |
除了找出按行计数的方法之外,我还需要按chr和pos对结果进行分组,然后计算每个备用等位基因(其中allele1seq和allele2seq不等于ref)的出现次数如上所述,按行计算;但由于计算问题,我还没有那么远。
提前感谢您的帮助。
答案 0 :(得分:1)
看起来您可以提前计算total number of distinct sample_ids*2
,然后将其用于后续查询,因为该值不会每行更改。如果值 取决于行,您可能需要查看analytic/window functions available to Impala。
但是,由于它看起来不像你需要,你可以做类似以下的事情:
WITH total AS
(SELECT COUNT(DISTINCT sample_id) * 2 AS total FROM comgen_variants)
SELECT cgi.*,
(CASE
WHEN (cgi.allele1seq = cgi.ref AND cgi.allele2seq <> cgi.ref) THEN 1
WHEN (cgi.allele1seq <> cgi.ref AND cgi.allele2seq = cgi.ref) THEN 1
WHEN (cgi.allele1seq = cgi.ref AND cgi.allele2seq = cgi.ref) THEN 2
ELSE 0 END) / total.total AS maf
FROM comgen_variants AS cgi, total;
我不确定这是次要等位基因的频率是多少;您似乎想为每个基因座选择第二个最常见的等位基因频率?