#! /usr/bin/env python
import glob
import os
import shutil

bw_dir = "/beevol/home/riemondy/Projects/hesselberth-clp1/data/rzhoseq/2023-10-26/bw-tag-filtered"
bws = glob.glob(os.path.join(bw_dir, "*.bw"))
dst = "/beevol/home/riemondy/Projects/hesselberth-clp1/hub/hg38/ribozyme-5OH-pilot-2"

for bw in bws:
  shutil.copy(bw, dst)

sample_info = {
 'JH326_RZ_7B': {'rz_status': 'WT', 'treatment': 'None'},
 'JH326_RZ_7C': {'rz_status': 'MUT', 'treatment': 'None'},
 'JH326_RZ_WT_7A': {'rz_status': 'WT', 'treatment': 'None'},
 'JH326_RZ_mut_7D': {'rz_status': 'MUT', 'treatment': 'None'},
 'JH326_RZ_WT_1': {'rz_status': 'WT', 'treatment': 'None'},
 'JH326_RZ_mut__2': {'rz_status': 'MUT', 'treatment': 'None'},
 'JH326_RZ_WT_3A': {'rz_status': 'WT', 'treatment': 'polyIC'},
 'JH326_RZ_WT_3B': {'rz_status': 'WT', 'treatment': 'polyIC'},
 'JH326_RZ_WT_3C': {'rz_status': 'WT', 'treatment': 'polyIC'},
 'JH326_RZ_mut__4A': {'rz_status': 'MUT', 'treatment': 'polyIC'},
 'JH326_RZ_mut__4B': {'rz_status': 'MUT', 'treatment': 'polyIC'},
 'JH326_RZ_mut__4C': {'rz_status': 'MUT', 'treatment': 'polyIC'},
 'JH326_RZ_WT_5A': {'rz_status': 'WT', 'treatment': 'H2O2'},
 'JH326_RZ_WT_5B': {'rz_status': 'WT', 'treatment': 'H2O2'},
 'JH326_RZ_WT_5C': {'rz_status': 'WT', 'treatment': 'H2O2'},
 'JH326_RZ_mut__6A': {'rz_status': 'MUT', 'treatment': 'H2O2'},
 'JH326_RZ_mut__6B': {'rz_status': 'MUT', 'treatment': 'H2O2'},
 'JH326_RZ_mut__6C': {'rz_status': 'MUT', 'treatment': 'H2O2'}
}

bws = glob.glob(os.path.join(dst, "*.bw"))

track_preamble = """
track ribozyme-5OH-seq_bigwigs_2023-10-26
compositeTrack on show
shortLabel ribozyme-5OH-seq_bigwigs_2023-10-26
longLabel ribozyme-5OH-seq read coverage bigwigs 2023-10-26
type bigWig
autoScale group
windowingFunction mean
visibility show 
maxHeightPixels 100:30:8
subGroup1 rz Ribozme  WT=WT MUT=Mutant 
subGroup2 trt Treatment None=None polyIC=polyIC H2O2=H2O2
subGroup3 strand Strand fwd=Positive rev=Negative
subGroup4 dtype Type fullcoverage=Full-coverage 5pcoverage=5p-end
dimensions dimX=rz dimY=trt dimA=strand dimB=dtype
dimensionAchecked fullcoverage
"""

print(track_preamble)

color = "213,94,0"
dtypes = ["5pcoverage", "fullcoverage"]
samples = []
for b in bws:
    bn = os.path.basename(b)
    bvals = bn.split("_")
    bvals = [y for y in bvals if y != ""]

    sample = bn.split("_dedup_")[0]
    #sample = bid.split("_")[-1]
    
    if sample not in samples:
      samples.append(sample)

    rz = sample_info[sample]["rz_status"]
    trt = sample_info[sample]["treatment"]

    dtype = ""
    for bval in bvals:
        if bval in dtypes:
            dtype = bval
    
    if "pos" in b:
        strand = "fwd"
    else:
        strand = "rev"
    
    trackname = f"{sample}_{strand}_{dtype}"
    
    track_template = f"track {trackname}\n"
    track_template += f"parent ribozyme-5OH-seq_bigwigs_2023-10-26\n" 
    track_template += f"shortLabel {trackname}\n"
    track_template += f"bigDataUrl ribozyme-5OH-pilot-2/{bn}\n"
    track_template += f"longLabel {trackname}\n"  
    track_template += f"subGroups rz={rz} trt={trt} strand={strand} dtype={dtype}\n"
    track_template += f"type bigWig\n"
    track_template += f"color {color}\n"
    print(track_template)

