Skip to content

Commit

Permalink
Use charbuffer to serialize FASTQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
alumi committed Nov 13, 2017
1 parent cf09ce1 commit 516a250
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/cljam/io/fastq.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[clojure.string :as string]
[cljam.io.protocols :as protocols]
[cljam.util :as util])
(:import [java.io Closeable]))
(:import [java.io Closeable]
[java.nio CharBuffer]))

(declare read-sequences write-sequences)

Expand Down Expand Up @@ -87,7 +88,7 @@

(defn- ^String serialize-fastq
"Serialize a FASTQRead to FASTQ format string."
[^FASTQRead {:keys [name sequence quality]}
[^FASTQRead {:keys [^String name ^String sequence quality]}
{:keys [encode-quality] :or {encode-quality :phred33}}]
{:pre [(not-empty name)
(not-empty sequence)
Expand All @@ -97,16 +98,24 @@
:phred33 (<= 0 % 93)
:phred64 (<= 0 % 62)
true) quality)]}
(-> [(str "@" name)
sequence
"+"
(apply str (map #(case encode-quality
:phred33 (char (+ % 33))
:phred64 (char (+ % 64))
%)
quality))]
(interleave (repeat \newline))
(as-> x (apply str x))))
(let [cb (CharBuffer/allocate (+ 6 (.length name) (.length sequence) (.length sequence)))]
(.put cb \@)
(.put cb name)
(.put cb \newline)
(.put cb sequence)
(.put cb \newline)
(.put cb \+)
(.put cb \newline)
(if (string? quality)
(.put cb ^String quality)
(doseq [q quality]
(.put cb (char (case encode-quality
:phred33 (+ q 33)
:phred64 (+ q 64)
q)))))
(.put cb \newline)
(.flip cb)
(.toString cb)))

(defn write-sequences
"Write given sequence of reads to a FASTQ file."
Expand Down

0 comments on commit 516a250

Please sign in to comment.