 
 
 (defn- insert-scores-out [^java.io.BufferedWriter out seqfn slist]
-  (let [sid (seqfn)]
-    (.write out (str sid "\t" (:hex slist) "\n"))
-    sid))
-
-(defn- insert-joins-out [^java.io.BufferedWriter out pid i sid]
-  (.write out (str pid "\t" i "\t" sid "\n")))
+  (let [score-id (seqfn)]
+    (.write out (str score-id "\t" (:hex slist) "\n"))
+    score-id))
+
+(defn- insert-field-score-out [^java.io.BufferedWriter out field-id i score-id]
+  (.write out (str field-id "\t" i "\t" score-id "\n")))
 
 (defn- sequence-for-column [table column]
   (-> (exec-raw

  (assoc md :text (json/write-str md :escape-slash false)))

(defn- load-probe-meta [feature-list]
  (doseq [[pid feature] feature-list]
    (let [fmeta (normalize-meta features-meta feature)
          fmeta (merge fmeta {:probes_id pid})
          fid (insert features (values fmeta))
          order (:order feature)]
      (doseq [code (:state feature)]
        (insert codes (values {:features_id fid
                               :ordering (order code)
                               :value code}))))))

;
;
;

(defn- probes-in-exp [exp]
  (subselect probes (fields :id) (where {:eid exp})))

(defn- scores-with-probes [probes]
  (subselect joins (fields :sid) (where {:pid [in probes]})))

(defn- clear-by-exp [exp]
  (let [p (probes-in-exp exp)]
    (delete exp_samples (where {:experiments_id exp}))
    (delete scores (where {:id [in (scores-with-probes p)]}))
    (delete joins (where {:pid [in p]}))
    (delete probes (where {:id [in p]}))))

; Merge cohort, returning id
(defn- merge-cohort [cohort]
  (exec-raw ["MERGE INTO cohorts(name) KEY(name) VALUES (?)" [cohort]])
  (let [[{cid :ID}] (select cohorts (where {:name cohort}))]
    cid))

; Update meta entity record.
(defn- merge-m-ent [m-ent ename metadata]

;

; Insert probe & return id
(defn- insert-field [exp name]
  (let [id (insert field (values {:dataset_id exp :name name}))]
    id))

(defn- insert-scores [slist]
  (let [score-id (insert scores (values {:scores slist}))]
    score-id))

(defn- insert-field-score [field-id i score-id]
  (insert field_score (values {:field_id field-id :i i :score_id score-id})))

(defn- table-writer-default [dir f]
  (f {:insert-field insert-field
         :insert-score insert-scores
         :insert-field-score insert-field-score
         :encode score-encode
         :key ahashable}))

