<commit_before>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()
    samples = dict()
    for i in f:
        i = i.strip("\n").split("\t")
        if len(i) > 1:
            samples[i[0]] = list()
            for j in i[1:]:
                samples[i[0]].append(j.split("/")[-1])
    f.close()
    out = open(path + "/outputs/stats_trim.txt", 'w')
    print >> out, "sample_id\t" + "\t".join(fnames2)
    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 = ""
        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
            else:
                g1 = g1 + "\t" + r1
        if g2 != '':
            print >> out, sample + ' (1)' + g1
            print >> out, sample + ' (2)' + g2
        else:
            print >> out, sample + g1
    out.close()


def read_star_log(datafile, sample):
    H = []<commit_msg>star-fusion<commit_after>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()
    samples = dict()
    for i in f:
        i = i.strip("\n").split("\t")
        if len(i) > 1:
            samples[i[0]] = list()
            for j in i[1:]:
                samples[i[0]].append(j.split("/")[-1])
    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
                if '%' in r2:
                    plt1 = plt2 + "\t" + r2.split(' ')[1].replace('(', '').replace('%)', '')
                    plt2 = plt2 + "\t" + r2.split(' ')[1].replace('(', '').replace('%)', '')
            else:
                if '%' in r1:
                    plt1 = plt1 + "\t" + r1.split(' ')[1].replace('(', '').replace('%)', '')
                g1 = g1 + "\t" + r1
        if g2 != '':
            print >> out, sample + ' (1)' + g1
            print >> out, sample + ' (2)' + g2
            print >> out2, sample + ' (1)' + plt1
            print >> out2, sample + ' (2)' + plt2
        else:
            print >> out, sample + g1
            print >> out2, sample + plt1
    out.close()


def read_star_log(datafile, sample):
    H = []
{'fraction_non_alphanumeric': 0.17951657590522596, 'fraction_numerical': 0.01643259768797172, 'mean_word_length': 1.72533194480604, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4641610', 'n_tokens_mistral': 3564, 'n_tokens_neox': 3415, 'n_words': 818}