Advertisement
Guest User

TSVReader.clj

a guest
Dec 2nd, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns net.myexperiments.viciouscycle.util.TSVReader
  2.   (:require [clojure.string :as st])
  3.   (:import (java.util.regex Pattern) (java.util HashMap) (java.io File))
  4.   (:gen-class
  5.     :name    "net.myexperiments.viciouscycle.util.TSVReader"
  6.     :prefix "-"
  7.     :load-impl-ns false
  8.     :methods [^{:static true} [loadTSV [String] java.util.Map]
  9.               ^{:static true} [readMap [String] java.util.HashMap]
  10.               ^{:static true} [convertTSV [String String] String]]))
  11.  
  12. (defn load-tsv-clj
  13.   "Reads a TSV file in and returns a sorted map with the first column's data
  14.  as keys, and for values, hashmaps of the header row's data as keys to the
  15.  other rows' data as values.  A file with a first row of 'Name\tID\tSalary'
  16.  and a second row of 'John\t1\t100' will return a sorted map of {\"John\"
  17.  {\"Name\" \"John\", \"ID\" 1, \"Salary\" 100}}"
  18.   [filename]
  19.   (let [tbl-rows (st/split-lines  (slurp (File. filename)))
  20.         pat-t (. Pattern compile "\t")
  21.         header (st/split (first tbl-rows) pat-t)
  22.          read-unknown (fn [s] (if (integer? (read-string s)) (read-string s) s))
  23.          make-data (fn [rw] (zipmap header (map read-unknown (st/split rw pat-t))))]
  24.   (apply sorted-map (interleave
  25.                                      (map (fn [rw] (first (st/split rw pat-t))) (rest tbl-rows))
  26.                                      (map make-data (rest tbl-rows))))))
  27. (defn -loadTSV
  28.   "Reads a TSV file in and returns a hash-map with the first column's data
  29.  as keys, and for values, hash-maps of the header row's data as keys to the
  30.  other rows' data as values.  A file with a first row of 'Name\tID\tSalary'
  31.  and a second row of 'John\t1\t100' will return a hash-map of {\"John\"
  32.  {\"Name\" \"John\", \"ID\" \"1\", \"Salary\" \"100\"}}"
  33.   [filename]
  34.   (let [tbl-rows (st/split-lines  (slurp (File. filename)))
  35.         pat-t (. Pattern compile "\t")
  36.         header (st/split (first tbl-rows) pat-t)
  37.         make-data (fn [rw] (zipmap header (st/split rw pat-t)))]
  38.   (apply hash-map (interleave
  39.                                      (map (fn [rw] (first (st/split rw pat-t))) (rest tbl-rows))
  40.                                      (map make-data (rest tbl-rows))))))
  41. (defn read-obj
  42.   "Reads a Clojure object from a file, ignoring exceptions."
  43.   [filename]
  44.   (try
  45.      (read-string (slurp  (File. filename)))
  46.   (catch Exception e nil)))
  47. (defn -readMap
  48.   "Reads a Clojure map from a file, returning a Java HashMap
  49.   equivalent to the data or an empty HashMap on failure."
  50.   [filename]
  51.   (try
  52.     (let [f (read-string (slurp (File. filename)))]
  53.      (if (map? f) (HashMap. f) (HashMap.)))
  54.   (catch Exception e (HashMap. ))))
  55.  
  56. (defn write-obj
  57.   "Writes a Clojure object to a file, to be loaded with read-obj ."
  58.   [filename obj]
  59.   (binding [*out* (java.io.FileWriter. filename)]
  60.     (prn obj)))
  61.  
  62. (defn -convertTSV
  63.   "Reads a TSV file (with the name specified by the String infile)
  64.   in with loadTSV, using the same format, and writes it to a
  65.   Clojure file (with the name specified by the String outfile)."
  66.   [infile outfile]
  67.   (binding [*out* (java.io.FileWriter. outfile)]
  68.     (prn (load-tsv-clj infile))))
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement