import os
import config

head_star_log = ["Sample", "Links","Started job","Started mapping","Finished","Mapping speed [Mr/h]","Input reads [n]","Input read length (mean)","Uniquely mapped [n]","Uniquely mapped [%]","Mapped length","Splices [n]","Splices annotated [n]","Splices GT/AG [n]","Splices: GC/AG [n]","Splices: AT/AC [n]","Splices: Non-canonical [n]","Mismatch rate per base [%]","Deletion rate per base [%]","Deletion average length","Insertion rate per base [%]","Insertion average length","Multimapping reads [n]","Multimapping reads [%]","Multimapping reads (+) [n]","Multimapping reads (+) [%]","Unmapped reads: too many mismatches [%]","Unmapped reads: too short [%]","Unmapped reads: other [%]"]


def stats_varcall(path, mod):
    f = open(path + "/samples.list", 'r')
    h = f.readline()
    samples = dict()
    for i in f:
        i = i.strip("\n").split("\t")
        samples[i[0]] = path + "/results_" + mod + "/" + i[0] + ".vcf"
    f.close()
    out = open(path + "/outputs/stats_" + mod + ".txt", 'w')
    print >> out, "sample_id\t0/1\t1/1"
    for sample, filename in samples.iteritems():
        if os.path.exists(filename):
            f = open(filename, 'r')
            i = f.readline()
            n = [0,0,0]
            for i in f:
                if not i.startswith("#"):
                    i = i.strip("\n").split("\t")
                    if len(i) > 1:
                        gt = i[9].split(":")[0]
                        if gt == "1/1":
                            n[1] += 1
                        elif gt == "0/1":
                            n[0] += 1
                        elif gt == "1/2":
                            n[2] += 1
                        else:
                            print gt
            f.close()
            print >> out, sample + "\t" + str(n[0]) + "\t" + str(n[1]) + "\t" + str(n[2])
        else:
            print >> out, sample + "\t-1\t-1\t-1"
    out.close()

def stats_fusion(path):
    f = open(path + "/samples.list", 'r')
    h = f.readline()
    samples = dict()
    for i in f:
        i = i.strip("\n").split("\t")
        samples[i[0]] = path + "/results_star-fusion/" + i[0] + "/star-fusion.fusion_candidates.final.abridged"
    f.close()
    out = open(path + "/outputs/starfusion_aggregate.txt", 'w')
    print >> out, "sample_id\tFusionName\tJunctionReadCount\tSpanningFragCount\tSpliceType\tLeftGene\tLeftBreakpoint\tRightGene\tRightBreakpoint\tLargeAnchorSupport\tLeftBreakDinuc\tLeftBreakEntropy\tRightBreakDinucRightBreakEntropy"
    for sample, filename in samples.iteritems():
        if os.path.exists(filename):
            f = open(filename, 'r')
            i = f.readline()
            for i in f:
                i = i.strip("\n").split("\t")
                if len(i) > 1:
                    print >> out, sample + "\t" + "\t".join(i)
    out.close()


def stats_trimgalore(path):
    fields2 = ["Total reads processed:","Reads with adapters:","Reads written (passing filters):","Total basepairs processed:","Quality-trimmed:","Total written (filtered):"]
    fnames2 = ['Processed reads', 'Reads with adapters', 'Reads passing filters', 'Processed basepairs', 'Quality-trimmed basepairs', 'Basepairs passing filters']
    f = open(path + "/samples.list", 'r')
    h = f.readline().strip('\n').split('\t')
    idx = []
    for ix, i in enumerate(h):
        if i.startswith('FASTQ'):
            idx.append(ix)
    samples = dict()
    for i in f:
        i = i.strip("\n").split("\t")
        if len(i) > 1:
            samples[i[0]] = [i[j].split("/")[-1] for j in idx]
    f.close()
    out = open(path + "/outputs/stats_trim.txt", 'w')
    print >> out, "sample_id\t" + "\t".join(fnames2)
    out2 = open(path + "/outputs/stats_trim_plot.txt", 'w')
    print >> out2, "sample_id\tReads with adapters\tReads passing filters\tQuality-trimmed basepairs\tBasepairs passing filters"
    for sample, files in samples.iteritems():
        k = 0
        data = [{},{}]
        for filname in files:
            fpath = path + "/results_trimgalore/" + filname + "_trimming_report.txt"
            if os.path.exists(fpath):
                f = open(fpath, 'r')
                for i in f:
                    i = i.strip("\n").replace(' bp', '').replace(',', '')
                    for fi in fields2:
                        j = i.split(fi)
                        if len(j) > 1:
                            data[k][fi] = ' '.join(j[1:])
                f.close()
            k += 1
        g1 = ""
        g2 = ""
        plt1 = ''
        plt2 = ''
        for i in fields2:
            r1 = "NA"
            r2 = "NA"
            if data[0].has_key(i):
                r1 = data[0][i]
            if len(files) == 2:
                if data[1].has_key(i):
                    r2 = data[1][i]
                g1 = g1 + "\t" + r1
                g2 = g2 + "\t" + r2