Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRAM writer (alpha) #313

Merged
merged 10 commits into from
Jul 22, 2024
33 changes: 31 additions & 2 deletions src/cljam/io/cram.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(ns cljam.io.cram
"Alpha - subject to change. Provides functions for reading from a CRAM file."
"Alpha - subject to change. Provides functions for reading and writing a CRAM file."
(:refer-clojure :exclude [indexed?])
(:require [cljam.io.cram.core :as cram]
[cljam.io.protocols :as protocols]
[cljam.io.util :as io-util])
(:import [cljam.io.cram.reader CRAMReader]))
(:import [cljam.io.cram.reader CRAMReader]
[cljam.io.cram.writer CRAMWriter]))

(defn reader
"Creates a CRAM reader depending on the argument f: If f is a file or a string
Expand Down Expand Up @@ -47,3 +48,31 @@
function immediately realizes a delayed index."
[rdr]
(protocols/indexed? rdr))

(defn writer
"Creates a new CRAM writer that writes to a CRAM file f.

The function also takes an optional argument `option`, which is a map that
consists of:
- reference: A string representing the path to the reference file, or
a sequence reader that reads sequences from the reference file.
This may be omitted only when the CRAM file to be read does not
require a reference file."
(^CRAMWriter [f] (writer f {}))
(^CRAMWriter [f option] (cram/writer f option)))

(defn write-header
"Writes header to the CRAM file."
[wtr header]
(protocols/write-header wtr header))

(defn write-refs
"Does nothing. This exists only for the sake of compatibility with other
alignment writers."
[wtr refs]
(protocols/write-refs wtr refs))

(defn write-alignments
"Writes alignments to the CRAM file."
[wtr alns header]
(protocols/write-alignments wtr alns header))
18 changes: 18 additions & 0 deletions src/cljam/io/cram/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
(:require [cljam.io.crai :as crai]
[cljam.io.cram.reader :as reader]
[cljam.io.cram.seq-resolver :as resolver]
[cljam.io.cram.writer :as writer]
[cljam.io.sam.util.refs :as util.refs]
[cljam.io.util.byte-buffer :as bb]
[cljam.util :as util]
[clojure.java.io :as cio]
[clojure.string :as str])
(:import [cljam.io.cram.reader CRAMReader]
[cljam.io.cram.writer CRAMWriter]
[java.io FileNotFoundException]
[java.net URL]
[java.nio.channels FileChannel]
Expand Down Expand Up @@ -71,3 +73,19 @@
(reader/read-file-definition rdr')
(reader/skip-container rdr')
rdr'))

(defn writer
"Creates a new CRAM writer that writes to a CRAM file f.

Takes an option map as the second argument. An option map consists of:
- reference: a string representing the path to a reference file"
^CRAMWriter [f {:keys [reference]}]
(let [file (cio/file f)
url (cio/as-url file)
url' (str url)
file-id (subs url' 0 (min 20 (count url')))
out (cio/output-stream file)
seq-resolver (some-> reference resolver/seq-resolver resolver/cached-resolver)
wtr (writer/->CRAMWriter url out seq-resolver)]
(writer/write-file-definition wtr file-id)
wtr))
Loading