我有一个函数,我在其中使用:strs在键上破坏地图。我可以很容易地破坏字符串(每个下面),然后单独解析字符串到整数(未显示),但我正在寻找一种更简洁的方式 - 最好在使用:strs
的赋值中任何建议都会受到赞赏 - 也可以随意评论其他功能,我很乐意学习。
(def t1 ["chr1" "85742012" "." "G" "C" "2383.75" "PASS" "AF=0.993671;AO=157;DP=158;FAO=157;FDP=158;FR=.;FRO=1;FSAF=111;FSAR=46;FSRF=1;FSRR=0;FWDB=-0.0106874;FXX=0;HRUN=1;LEN=1;MLLD=115.26;QD=60.3481;RBI=0.0173573;REFB=-0.0963825;REVB=0.0136769;RO=1;SAF=111;SAR=46;SRF=1;SRR=0;SSEN=0;SSEP=0;SSSB=-0.00426731;STB=0.502242;STBP=0.593;TYPE=snp;VARB=0.000132517;OID=.;OPOS=85742012;OREF=G;OALT=C;OMAPALT=C" "GT:GQ:DP:FDP:RO:FRO:AO:FAO:AF:SAR:SAF:SRF:SRR:FSAR:FSAF:FSRF:FSRR" "1/1:9:158:158:1:1:157:157:0.993671:46:111:1:0:46:111:1:0"])
(defn extract-vcf-single
[vcf-row]
(let [[chrom posn id ref- alt- qual filt info dt-form-str dt-val-str] vcf-row
dt-form (str/split dt-form-str #":")
dt-val (str/split dt-val-str #":")
dt (zipmap dt-form dt-val)
{:strs [GT RO AO DP]} dt
;; R0, A0 and DP are all integers. Is there a neat way to parse them in the destruct? so I can do this?
;; LAF (if (> RO AO) (/ RO DP) (/ AO DP))
] {(str chrom "_" posn)[chrom posn GT DP]}))
我能做的最好的事情是;
(defn extract-vcf-single
[vcf-row]
(let [[chrom posn id ref- alt- qual filt info dt-form-str dt-val-str] vcf-row
dt-form (str/split dt-form-str #":")
dt-val (str/split dt-val-str #":")
dt (zipmap dt-form dt-val)
{:strs [GT RO AO DP]} dt
[ROi AOi DPi] (map #(Integer. %) [RO AO DP])
LAF (if (> ROi AOi) (/ ROi DPi) (/ AOi DPi))
] {(str chrom "_" posn)[chrom posn GT DPi LAF]}))
答案 0 :(得分:1)
我会考虑创建一个函数,在地图中的特定值上应用函数,如下所示:
(defn multi-update
[m ks f]
(reduce (fn [acc k]
(update-in acc [k] f))
m
ks))
之后,您可以先将特定值解析为整数,然后进行实际解构:
(let [{:strs [GT RO]} (multi-update {"GT" "2"
"FOOBAR" "BAZ"
"RO" "6"}
["GT" "RO"]
#(Integer/parseInt %))]
[GT RO])
;=> [2 6]