Mercurial > repos > davidvanzessen > argalaxy_tools
comparison mutation_analysis.py @ 57:16c7fc1c4bf8 draft
Uploaded
| author | davidvanzessen |
|---|---|
| date | Fri, 18 Mar 2016 07:50:34 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 56:2eb94c08e550 | 57:16c7fc1c4bf8 |
|---|---|
| 1 from __future__ import division | |
| 2 from collections import defaultdict | |
| 3 import re | |
| 4 import argparse | |
| 5 | |
| 6 parser = argparse.ArgumentParser() | |
| 7 parser.add_argument("--input", | |
| 8 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation") | |
| 9 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | |
| 10 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column") | |
| 11 parser.add_argument("--output", help="Output file") | |
| 12 | |
| 13 args = parser.parse_args() | |
| 14 | |
| 15 infile = args.input | |
| 16 genes = str(args.genes).split(",") | |
| 17 print "includefr1 =", args.includefr1 | |
| 18 include_fr1 = True if args.includefr1 == "yes" else False | |
| 19 outfile = args.output | |
| 20 | |
| 21 genedic = dict() | |
| 22 | |
| 23 mutationdic = dict() | |
| 24 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?") | |
| 25 linecount = 0 | |
| 26 | |
| 27 IDIndex = 0 | |
| 28 best_matchIndex = 0 | |
| 29 fr1Index = 0 | |
| 30 cdr1Index = 0 | |
| 31 fr2Index = 0 | |
| 32 cdr2Index = 0 | |
| 33 fr3Index = 0 | |
| 34 first = True | |
| 35 IDlist = [] | |
| 36 mutationList = [] | |
| 37 mutationListByID = {} | |
| 38 cdr1LengthDic = {} | |
| 39 cdr2LengthDic = {} | |
| 40 | |
| 41 with open(infile, 'r') as i: | |
| 42 for line in i: | |
| 43 if first: | |
| 44 linesplt = line.split("\t") | |
| 45 IDIndex = linesplt.index("Sequence.ID") | |
| 46 best_matchIndex = linesplt.index("best_match") | |
| 47 fr1Index = linesplt.index("FR1.IMGT") | |
| 48 cdr1Index = linesplt.index("CDR1.IMGT") | |
| 49 fr2Index = linesplt.index("FR2.IMGT") | |
| 50 cdr2Index = linesplt.index("CDR2.IMGT") | |
| 51 fr3Index = linesplt.index("FR3.IMGT") | |
| 52 cdr1LengthIndex = linesplt.index("CDR1.IMGT.Nb.of.nucleotides") | |
| 53 cdr2LengthIndex = linesplt.index("CDR2.IMGT.Nb.of.nucleotides") | |
| 54 first = False | |
| 55 continue | |
| 56 linecount += 1 | |
| 57 linesplt = line.split("\t") | |
| 58 ID = linesplt[IDIndex] | |
| 59 genedic[ID] = linesplt[best_matchIndex] | |
| 60 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x] if include_fr1 else [] | |
| 61 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x] | |
| 62 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x] | |
| 63 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x] | |
| 64 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] | |
| 65 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] | |
| 66 | |
| 67 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
| 68 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
| 69 | |
| 70 cdr1Length = linesplt[cdr1LengthIndex] | |
| 71 cdr2Length = linesplt[cdr2LengthIndex] | |
| 72 | |
| 73 cdr1LengthDic[ID] = int(cdr1Length) / 3 | |
| 74 cdr2LengthDic[ID] = int(cdr2Length) / 3 | |
| 75 | |
| 76 IDlist += [ID] | |
| 77 | |
| 78 AALength = (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 | |
| 79 | |
| 80 AA_mutation = [0] * AALength | |
| 81 AA_mutation_empty = AA_mutation[:] | |
| 82 | |
| 83 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" | |
| 84 with open(aa_mutations_by_id_file, 'w') as o: | |
| 85 for ID in mutationListByID.keys(): | |
| 86 AA_mutation_for_ID = AA_mutation_empty[:] | |
| 87 for mutation in mutationListByID[ID]: | |
| 88 if mutation[4]: | |
| 89 AA_mutation[int(mutation[4])] += 1 | |
| 90 AA_mutation_for_ID[int(mutation[4])] += 1 | |
| 91 o.write(ID + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n") | |
| 92 | |
| 93 | |
| 94 | |
| 95 #absent AA stuff | |
| 96 absentAACDR1Dic = defaultdict(list) | |
| 97 absentAACDR1Dic[5] = range(29,36) | |
| 98 absentAACDR1Dic[6] = range(29,35) | |
| 99 absentAACDR1Dic[7] = range(30,35) | |
| 100 absentAACDR1Dic[8] = range(30,34) | |
| 101 absentAACDR1Dic[9] = range(31,34) | |
| 102 absentAACDR1Dic[10] = range(31,33) | |
| 103 absentAACDR1Dic[11] = [32] | |
| 104 | |
| 105 absentAACDR2Dic = defaultdict(list) | |
| 106 absentAACDR2Dic[0] = range(55,65) | |
| 107 absentAACDR2Dic[1] = range(56,65) | |
| 108 absentAACDR2Dic[2] = range(56,64) | |
| 109 absentAACDR2Dic[3] = range(57,64) | |
| 110 absentAACDR2Dic[4] = range(57,63) | |
| 111 absentAACDR2Dic[5] = range(58,63) | |
| 112 absentAACDR2Dic[6] = range(58,62) | |
| 113 absentAACDR2Dic[7] = range(59,62) | |
| 114 absentAACDR2Dic[8] = range(59,61) | |
| 115 absentAACDR2Dic[9] = [60] | |
| 116 | |
| 117 absentAA = [len(IDlist)] * (AALength-1) | |
| 118 for k, cdr1Length in cdr1LengthDic.iteritems(): | |
| 119 for c in absentAACDR1Dic[cdr1Length]: | |
| 120 absentAA[c] -= 1 | |
| 121 | |
| 122 for k, cdr2Length in cdr2LengthDic.iteritems(): | |
| 123 for c in absentAACDR2Dic[cdr2Length]: | |
| 124 absentAA[c] -= 1 | |
| 125 | |
| 126 | |
| 127 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt" | |
| 128 with open(aa_mutations_by_id_file, 'w') as o: | |
| 129 o.write("ID\tcdr1length\tcdr2length\t" + "\t".join([str(x) for x in range(1,AALength-1)]) + "\n") | |
| 130 for ID in IDlist: | |
| 131 absentAAbyID = [1] * (AALength-1) | |
| 132 cdr1Length = cdr1LengthDic[ID] | |
| 133 for c in absentAACDR1Dic[cdr1Length]: | |
| 134 absentAAbyID[c] -= 1 | |
| 135 | |
| 136 cdr2Length = cdr2LengthDic[ID] | |
| 137 for c in absentAACDR2Dic[cdr2Length]: | |
| 138 absentAAbyID[c] -= 1 | |
| 139 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n") | |
| 140 | |
| 141 | |
| 142 | |
| 143 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt" | |
| 144 with open(aa_mutations_file, 'w') as o: | |
| 145 o.write("row.name\t" + "\t".join([str(x) for x in range(1, AALength-1)]) + "\n") | |
| 146 o.write("mutations.at.position\t" + "\t".join([str(x) for x in AA_mutation[1:]]) + "\n") | |
| 147 o.write("AA.at.position\t" + "\t".join([str(x) for x in absentAA]) + "\n") | |
| 148 | |
| 149 if linecount == 0: | |
| 150 print "No data, exiting" | |
| 151 with open(outfile, 'w') as o: | |
| 152 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | |
| 153 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | |
| 154 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | |
| 155 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | |
| 156 import sys | |
| 157 | |
| 158 sys.exit() | |
| 159 | |
| 160 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | |
| 161 RGYWCount = {g: 0 for g in genes} | |
| 162 WRCYCount = {g: 0 for g in genes} | |
| 163 WACount = {g: 0 for g in genes} | |
| 164 TWCount = {g: 0 for g in genes} | |
| 165 | |
| 166 IDIndex = 0 | |
| 167 ataIndex = 0 | |
| 168 tatIndex = 0 | |
| 169 aggctatIndex = 0 | |
| 170 atagcctIndex = 0 | |
| 171 first = True | |
| 172 with open(infile, 'r') as i: | |
| 173 for line in i: | |
| 174 if first: | |
| 175 linesplt = line.split("\t") | |
| 176 ataIndex = linesplt.index("X.a.t.a") | |
| 177 tatIndex = linesplt.index("t.a.t.") | |
| 178 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | |
| 179 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | |
| 180 first = False | |
| 181 continue | |
| 182 linesplt = line.split("\t") | |
| 183 gene = linesplt[best_matchIndex] | |
| 184 ID = linesplt[IDIndex] | |
| 185 RGYW = [(int(x), int(y), z) for (x, y, z) in | |
| 186 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | |
| 187 WRCY = [(int(x), int(y), z) for (x, y, z) in | |
| 188 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | |
| 189 WA = [(int(x), int(y), z) for (x, y, z) in | |
| 190 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | |
| 191 TW = [(int(x), int(y), z) for (x, y, z) in | |
| 192 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | |
| 193 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 | |
| 194 | |
| 195 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ | |
| 196 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
| 197 for mutation in mutationList: | |
| 198 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
| 199 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW]) | |
| 200 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY]) | |
| 201 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA]) | |
| 202 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW]) | |
| 203 | |
| 204 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | |
| 205 | |
| 206 if in_how_many_motifs > 0: | |
| 207 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | |
| 208 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | |
| 209 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | |
| 210 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | |
| 211 | |
| 212 | |
| 213 def mean(lst): | |
| 214 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0 | |
| 215 | |
| 216 | |
| 217 def median(lst): | |
| 218 lst = sorted(lst) | |
| 219 l = len(lst) | |
| 220 if l == 0: | |
| 221 return 0 | |
| 222 if l == 1: | |
| 223 return lst[0] | |
| 224 | |
| 225 l = int(l / 2) | |
| 226 | |
| 227 if len(lst) % 2 == 0: | |
| 228 print "list length is", l | |
| 229 return float(lst[l] + lst[(l - 1)]) / 2.0 | |
| 230 else: | |
| 231 return lst[l] | |
| 232 | |
| 233 funcs = {"mean": mean, "median": median, "sum": sum} | |
| 234 | |
| 235 directory = outfile[:outfile.rfind("/") + 1] | |
| 236 value = 0 | |
| 237 valuedic = dict() | |
| 238 | |
| 239 for fname in funcs.keys(): | |
| 240 for gene in genes: | |
| 241 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v: | |
| 242 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip()) | |
| 243 with open(directory + "all_" + fname + "_value.txt", 'r') as v: | |
| 244 valuedic["total_" + fname] = float(v.readlines()[0].rstrip()) | |
| 245 | |
| 246 print valuedic | |
| 247 | |
| 248 def get_xyz(lst, gene, f, fname): | |
| 249 x = int(round(f(lst))) | |
| 250 y = valuedic[gene + "_" + fname] | |
| 251 z = str(round(x / float(valuedic[gene + "_" + fname]) * 100, 1)) if valuedic[gene + "_" + fname] != 0 else "0" | |
| 252 return (str(x), str(y), z) | |
| 253 | |
| 254 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | |
| 255 arr = ["RGYW", "WRCY", "WA", "TW"] | |
| 256 | |
| 257 for fname in funcs.keys(): | |
| 258 func = funcs[fname] | |
| 259 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt" | |
| 260 with open(foutfile, 'w') as o: | |
| 261 for typ in arr: | |
| 262 o.write(typ + " (%)") | |
| 263 curr = dic[typ] | |
| 264 for gene in genes: | |
| 265 geneMatcher = re.compile(".*" + gene + ".*") | |
| 266 if valuedic[gene + "_" + fname] is 0: | |
| 267 o.write(",0,0,0") | |
| 268 else: | |
| 269 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname) | |
| 270 o.write("," + x + "," + y + "," + z) | |
| 271 # for total | |
| 272 x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname) | |
| 273 o.write("," + x + "," + y + "," + z + "\n") | |
| 274 | |
| 275 | |
| 276 # for testing | |
| 277 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | |
| 278 with open(seq_motif_file, 'w') as o: | |
| 279 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n") | |
| 280 for ID in IDlist: | |
| 281 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") |
