from collections import defaultdict
import pandas as pd
from sys import argv

_, filename = argv

primers = pd.read_csv("truseq_primers.csv")
primers["exp"] = primers["Sample"].str.replace("Day", "d").str.replace(" T=", "t").str.split(" ").str.get(1)
primers["group"] = primers["Sample"].str.split(" ").str.get(0)
primers = primers[["group", "exp", "Indexing BC"]]

group_exp_barcode_map = primers[~pd.isnull(primers["group"])].set_index("Indexing BC").to_dict()
exp, group = group_exp_barcode_map["exp"], group_exp_barcode_map["group"]
data = {key:defaultdict(int) for key in group}

with open(filename) as fastq_file, open(filename+".log", "w") as logfile:
    lines = enumerate(fastq_file)
    for idx, line in lines:
        header = line
        _, seq = next(lines, (None, None))
        next(lines, (None, None))
        _, qual = next(lines, (None, None))
        index = header[-7:].strip()
        barcode = seq[:18]

        if index in group:
            print(group[index], file=logfile)
            data[index][barcode] += 1

df = pd.DataFrame([exp, group, data]).T
df.columns = ["exp", "group", "data"]
df2 = df["data"].apply(lambda x: pd.Series(x))
df2.columns.name = "barcodes"
df2.index.name = "index"
df.drop("data", axis=1, inplace=True)
results = df.reset_index().merge(df2.stack("barcodes").reset_index(), on="index").set_index(["group", "index", "exp", "barcodes"])
results.columns = ["counts"]
results.reset_index().drop("index", axis=1)
print(results)
results.to_hdf(filename+".h5", key="grouped_data", mode="w", complevel=9)


@@ -49,16 +49,35 @@
 s as pd%0A
+import numpy as np%0A
 from sys
@@ -1430,16 +1430,174 @@
 ounts%22%5D%0A
+results.reset_index(level=%22exp%22, inplace=True)%0Aresults%5B%5B%22days%22, %22timepoints%22%5D%5D = results%5B%22exp%22%5D.str.replace(r%22t%22, %22_t%22).str.split(%22_%22, expand=True)%0Aresults = 
 results.
@@ -1615,16 +1615,17 @@
 ().drop(
+%5B
 %22index%22,
@@ -1625,22 +1625,180 @@
 index%22, 
-axis=1
+%22exp%22%5D, axis=1)%0Astr_cols = %5B%22group%22, %22days%22, %22timepoints%22, %22barcodes%22%5D%0Aresults%5Bstr_cols%5D = results%5Bstr_cols%5D.astype(np.str)%0Aresults.set_index(str_cols, inplace=True
 )%0Aprint(
@@ -1846,15 +1846,19 @@
 ey=%22
-grouped
+raw_barcode
 _dat

split timepoints prior to save, to match new loading procedure