Mercurial > repos > davidvanzessen > argalaxy_tools
comparison mutation_analysis.py @ 11:0510cf1f7cbc draft
Uploaded
| author | davidvanzessen |
|---|---|
| date | Tue, 04 Aug 2015 09:59:26 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 10:edbf4fba5fc7 | 11:0510cf1f7cbc |
|---|---|
| 1 import re | |
| 2 import argparse | |
| 3 | |
| 4 parser = argparse.ArgumentParser() | |
| 5 parser.add_argument("--input", help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation") | |
| 6 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | |
| 7 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column") | |
| 8 parser.add_argument("--output", help="Output file") | |
| 9 | |
| 10 args = parser.parse_args() | |
| 11 | |
| 12 infile = args.input | |
| 13 genes = str(args.genes).split(",") | |
| 14 print "includefr1 =", args.includefr1 | |
| 15 include_fr1 = True if args.includefr1 == "yes" else False | |
| 16 outfile = args.output | |
| 17 | |
| 18 genedic = dict() | |
| 19 | |
| 20 mutationdic = dict() | |
| 21 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") | |
| 22 linecount = 0 | |
| 23 | |
| 24 IDIndex = 0 | |
| 25 best_matchIndex = 0 | |
| 26 fr1Index = 0 | |
| 27 cdr1Index = 0 | |
| 28 fr2Index = 0 | |
| 29 cdr2Index = 0 | |
| 30 fr3Index = 0 | |
| 31 first=True | |
| 32 IDlist = [] | |
| 33 mutationList = [] | |
| 34 | |
| 35 with open(infile, 'r') as i: | |
| 36 for line in i: | |
| 37 if first: | |
| 38 linesplt = line.split("\t") | |
| 39 IDIndex = linesplt.index("Sequence.ID") | |
| 40 best_matchIndex = linesplt.index("best_match") | |
| 41 fr1Index = linesplt.index("FR1.IMGT") | |
| 42 cdr1Index = linesplt.index("CDR1.IMGT") | |
| 43 fr2Index = linesplt.index("FR2.IMGT") | |
| 44 cdr2Index = linesplt.index("CDR2.IMGT") | |
| 45 fr3Index = linesplt.index("FR3.IMGT") | |
| 46 first = False | |
| 47 continue | |
| 48 linecount += 1 | |
| 49 linesplt = line.split("\t") | |
| 50 ID = linesplt[IDIndex] | |
| 51 genedic[ID] = linesplt[best_matchIndex] | |
| 52 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else [] | |
| 53 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] | |
| 54 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] | |
| 55 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] | |
| 56 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] | |
| 57 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] | |
| 58 | |
| 59 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
| 60 | |
| 61 IDlist += [ID] | |
| 62 | |
| 63 | |
| 64 AA_mutation = [0] * (int(max(mutationList, key=lambda i:int(i[4]) if i[4] else 0)[4]) + 1) #[4] is the position of the AA mutation, None if silent | |
| 65 | |
| 66 for mutation in mutationList: | |
| 67 if mutation[4]: #if non silent mutation | |
| 68 AA_mutation[int(mutation[4])] += 1 | |
| 69 | |
| 70 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt" | |
| 71 with open(aa_mutations_file, 'w') as o: | |
| 72 o.write(",".join([str(x) for x in AA_mutation]) + "\n") | |
| 73 | |
| 74 if linecount == 0: | |
| 75 print "No data, exiting" | |
| 76 with open(outfile, 'w') as o: | |
| 77 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | |
| 78 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | |
| 79 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | |
| 80 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | |
| 81 import sys | |
| 82 sys.exit() | |
| 83 | |
| 84 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | |
| 85 RGYWCount = {g: 0 for g in genes} | |
| 86 WRCYCount = {g: 0 for g in genes} | |
| 87 WACount = {g: 0 for g in genes} | |
| 88 TWCount = {g: 0 for g in genes} | |
| 89 | |
| 90 IDIndex = 0 | |
| 91 ataIndex = 0 | |
| 92 tatIndex = 0 | |
| 93 aggctatIndex = 0 | |
| 94 atagcctIndex = 0 | |
| 95 first = True | |
| 96 with open(infile, 'r') as i: | |
| 97 for line in i: | |
| 98 if first: | |
| 99 linesplt = line.split("\t") | |
| 100 ataIndex = linesplt.index("X.a.t.a") | |
| 101 tatIndex = linesplt.index("t.a.t.") | |
| 102 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | |
| 103 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | |
| 104 first = False | |
| 105 continue | |
| 106 linesplt = line.split("\t") | |
| 107 gene = linesplt[best_matchIndex] | |
| 108 ID = linesplt[IDIndex] | |
| 109 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | |
| 110 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | |
| 111 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | |
| 112 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | |
| 113 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0,0,0,0 | |
| 114 | |
| 115 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
| 116 for mutation in mutationList: | |
| 117 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
| 118 mutation_in_RGYW = any([(start <= int(where) <= end) for (start,end,region) in RGYW]) | |
| 119 mutation_in_WRCY = any([(start <= int(where) <= end) for (start,end,region) in WRCY]) | |
| 120 mutation_in_WA = any([(start <= int(where) <= end) for (start,end,region) in WA]) | |
| 121 mutation_in_TW = any([(start <= int(where) <= end) for (start,end,region) in TW]) | |
| 122 | |
| 123 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | |
| 124 | |
| 125 if in_how_many_motifs > 0: | |
| 126 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | |
| 127 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | |
| 128 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | |
| 129 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | |
| 130 | |
| 131 directory = outfile[:outfile.rfind("/") + 1] | |
| 132 value = 0 | |
| 133 valuedic = dict() | |
| 134 for gene in genes: | |
| 135 with open(directory + gene + "_value.txt", 'r') as v: | |
| 136 valuedic[gene] = int(v.readlines()[0].rstrip()) | |
| 137 with open(directory + "total_value.txt", 'r') as v: | |
| 138 valuedic["total"] = int(v.readlines()[0].rstrip()) | |
| 139 | |
| 140 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | |
| 141 arr = ["RGYW", "WRCY", "WA", "TW"] | |
| 142 with open(outfile, 'w') as o: | |
| 143 for typ in arr: | |
| 144 o.write(typ + " (%)") | |
| 145 curr = dic[typ] | |
| 146 for gene in genes: | |
| 147 geneMatcher = re.compile(".*" + gene + ".*") | |
| 148 if valuedic[gene] is 0: | |
| 149 o.write(",0,0,0") | |
| 150 else: | |
| 151 x = int(round(sum([curr[x] for x in [y for y,z in genedic.iteritems() if geneMatcher.match(z)]]))) | |
| 152 y = valuedic[gene] | |
| 153 z = str(round(x / float(valuedic[gene]) * 100, 1)) | |
| 154 o.write("," + str(x) + "," + str(y) + "," + z) | |
| 155 #for total | |
| 156 x = int(round(sum([y for x,y in curr.iteritems()]))) | |
| 157 y = valuedic["total"] | |
| 158 z = str(round(x / float(valuedic["total"]) * 100, 1)) | |
| 159 o.write("," + str(x) + "," + str(y) + "," + z + "\n") | |
| 160 | |
| 161 | |
| 162 #for testing | |
| 163 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | |
| 164 first = True | |
| 165 with open(seq_motif_file, 'w') as o: | |
| 166 for ID in IDlist: | |
| 167 if first: | |
| 168 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n") | |
| 169 first = False | |
| 170 continue | |
| 171 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n") |
