|
| 1 | +(ns bcbio.variation.vcfsample |
| 2 | + "Sort VCF sample columns to have a consistent order between multiple inputs. |
| 3 | + Variant callers order called outputs differently and this ensures they are |
| 4 | + consistent to feed into ensemble calling." |
| 5 | + (:import [htsjdk.samtools.util BlockCompressedInputStream BlockCompressedOutputStream]) |
| 6 | + (:require [bcbio.run.fsp :as fsp] |
| 7 | + [bcbio.run.itx :as itx] |
| 8 | + [bcbio.variation.variantcontext :as vc] |
| 9 | + [clojure.core.strint :refer [<<]] |
| 10 | + [clojure.java.io :as io] |
| 11 | + [clojure.string :as string] |
| 12 | + [me.raynes.fs :as fs])) |
| 13 | + |
| 14 | +;; ## bgzipped and indexed files |
| 15 | + |
| 16 | +(defn- tabix-index-vcf |
| 17 | + "Tabix index input VCF inside a transactional directory." |
| 18 | + [bgzip-file] |
| 19 | + (let [tabix-file (str bgzip-file ".tbi")] |
| 20 | + (when (or (itx/needs-run? tabix-file) (not (itx/up-to-date? tabix-file bgzip-file))) |
| 21 | + (itx/with-tx-file [tx-tabix-file tabix-file] |
| 22 | + (let [tx-bgzip-file (fsp/file-root tx-tabix-file) |
| 23 | + full-bgzip-file (str (fs/file bgzip-file)) |
| 24 | + tmp-dir (str (fs/parent tx-bgzip-file))] |
| 25 | + (itx/check-run (<< "ln -s ~{full-bgzip-file} ~{tx-bgzip-file}")) |
| 26 | + (itx/check-run (<< "bcftools tabix -p vcf ~{tx-bgzip-file}"))))) |
| 27 | + tabix-file)) |
| 28 | + |
| 29 | +(defn bgzip-index-vcf |
| 30 | + "Prepare a VCF file for positional query with bgzip and tabix indexing." |
| 31 | + [vcf-file & {:keys [remove-orig? remove-nopass? dir]}] |
| 32 | + (let [out-orig (str (fsp/file-root vcf-file) |
| 33 | + (if remove-nopass? "-passonly" "") |
| 34 | + ".vcf.gz") |
| 35 | + out-file (if dir (str (io/file dir (fs/base-name out-orig))) out-orig)] |
| 36 | + (if remove-nopass? |
| 37 | + (itx/run-cmd out-file "bcftools view -f 'PASS,.' ~{vcf-file} | bgzip -c > ~{out-file}") |
| 38 | + (itx/run-cmd out-file "bgzip -c ~{vcf-file} > ~{out-file}")) |
| 39 | + (when (and (not (.endsWith vcf-file ".gz")) remove-orig?) |
| 40 | + (fsp/remove-path vcf-file)) |
| 41 | + (tabix-index-vcf out-file) |
| 42 | + out-file)) |
| 43 | + |
| 44 | +;; ## Unique file names |
| 45 | + |
| 46 | +(defn unique-work-file |
| 47 | + "Create a work file with unique name in case of shared base names." |
| 48 | + [orig-file ext all-files work-dir] |
| 49 | + (let [cmp-files (remove #(= % orig-file) all-files) |
| 50 | + parts (reverse (string/split orig-file #"/")) |
| 51 | + unique-file (loop [i 1] |
| 52 | + (let [cur-name (string/join "-" (reverse (take i parts)))] |
| 53 | + (if (not-any? #(.endsWith % cur-name) cmp-files) |
| 54 | + cur-name |
| 55 | + (recur (inc i)))))] |
| 56 | + (fsp/add-file-part unique-file ext work-dir))) |
| 57 | + |
| 58 | +;; ## Consistent order sorting |
| 59 | + |
| 60 | +(defn- sort-sample-line |
| 61 | + "Sort samples in a VCF line using reordered indexes from calculate-reorder." |
| 62 | + [line reorder] |
| 63 | + (let [[keep samples] (split-at 9 (string/split line #"\t"))] |
| 64 | + (string/join "\t" |
| 65 | + (concat keep |
| 66 | + (->> samples |
| 67 | + (map-indexed vector) |
| 68 | + (sort-by (fn [[i x]] (get reorder i))) |
| 69 | + (map second)))))) |
| 70 | + |
| 71 | +(defn- calculate-reorder |
| 72 | + "Create a dictionary of sample indexes in the original VCF to those desired in the output." |
| 73 | + [orig-order want-order] |
| 74 | + (let [want-indexes (reduce (fn [coll [i x]] |
| 75 | + (assoc coll x i)) |
| 76 | + {} (map-indexed vector want-order))] |
| 77 | + (reduce (fn [coll [i x]] |
| 78 | + (assoc coll i (get want-indexes x))) |
| 79 | + {} (map-indexed vector orig-order)))) |
| 80 | + |
| 81 | +(defn- sort-samples |
| 82 | + "Sort samples in a VCF file, moving from orig-order to want-order." |
| 83 | + [vcf-file orig-order want-order all-vcfs work-dir] |
| 84 | + (let [out-file (unique-work-file vcf-file "ssort" all-vcfs work-dir) |
| 85 | + sample-reorder (calculate-reorder orig-order want-order)] |
| 86 | + (with-open [rdr (io/reader (BlockCompressedInputStream. (io/file vcf-file))) |
| 87 | + wtr (io/writer (BlockCompressedOutputStream. (io/file out-file)))] |
| 88 | + (doseq [line (line-seq rdr)] |
| 89 | + (.write wtr (str (if (.startsWith line "##") |
| 90 | + line |
| 91 | + (sort-sample-line line sample-reorder)) |
| 92 | + "\n")))) |
| 93 | + (bgzip-index-vcf out-file) |
| 94 | + out-file)) |
| 95 | + |
| 96 | +(defn- maybe-sort-names |
| 97 | + "Extract sample names for the current file and do sorting if needed." |
| 98 | + [vcf-file sorder all-vcfs work-dir] |
| 99 | + (let [cur-sorder (vc/get-samples vcf-file)] |
| 100 | + (if (not= cur-sorder sorder) |
| 101 | + (sort-samples vcf-file cur-sorder sorder all-vcfs work-dir) |
| 102 | + vcf-file))) |
| 103 | + |
| 104 | +(defn consistent-order |
| 105 | + "Ensure the set of VCF files have a consistent sample order relative to the first." |
| 106 | + [vcf-files work-dir] |
| 107 | + (fsp/safe-mkdir work-dir) |
| 108 | + (let [sorder (vc/get-samples (first vcf-files))] |
| 109 | + (cons (first vcf-files) |
| 110 | + (map #(maybe-sort-names % sorder vcf-files work-dir) (rest vcf-files))))) |
0 commit comments