Mercurial > repos > davidvanzessen > mutation_analysis
comparison mutation_analysis.py @ 49:5c6b9e99d576 draft
Uploaded
| author | davidvanzessen | 
|---|---|
| date | Wed, 18 Nov 2015 05:55:04 -0500 | 
| parents | 4b1bab1a9ad2 | 
| children | 7290a88ea202 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 48:d09b1bdfd388 | 49:5c6b9e99d576 | 
|---|---|
| 1 from collections import defaultdict | |
| 1 import re | 2 import re | 
| 2 import argparse | 3 import argparse | 
| 3 | 4 | 
| 4 parser = argparse.ArgumentParser() | 5 parser = argparse.ArgumentParser() | 
| 5 parser.add_argument("--input", | 6 parser.add_argument("--input", | 
| 6 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation") | 7 help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation") | 
| 7 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | 8 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | 
| 8 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column") | 9 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column") | 
| 9 parser.add_argument("--output", help="Output file") | 10 parser.add_argument("--output", help="Output file") | 
| 10 | 11 | 
| 11 args = parser.parse_args() | 12 args = parser.parse_args() | 
| 31 fr3Index = 0 | 32 fr3Index = 0 | 
| 32 first = True | 33 first = True | 
| 33 IDlist = [] | 34 IDlist = [] | 
| 34 mutationList = [] | 35 mutationList = [] | 
| 35 mutationListByID = {} | 36 mutationListByID = {} | 
| 37 cdr1LengthDic = {} | |
| 38 cdr2LengthDic = {} | |
| 36 | 39 | 
| 37 with open(infile, 'r') as i: | 40 with open(infile, 'r') as i: | 
| 38 for line in i: | 41 for line in i: | 
| 39 if first: | 42 if first: | 
| 40 linesplt = line.split("\t") | 43 linesplt = line.split("\t") | 
| 43 fr1Index = linesplt.index("FR1.IMGT") | 46 fr1Index = linesplt.index("FR1.IMGT") | 
| 44 cdr1Index = linesplt.index("CDR1.IMGT") | 47 cdr1Index = linesplt.index("CDR1.IMGT") | 
| 45 fr2Index = linesplt.index("FR2.IMGT") | 48 fr2Index = linesplt.index("FR2.IMGT") | 
| 46 cdr2Index = linesplt.index("CDR2.IMGT") | 49 cdr2Index = linesplt.index("CDR2.IMGT") | 
| 47 fr3Index = linesplt.index("FR3.IMGT") | 50 fr3Index = linesplt.index("FR3.IMGT") | 
| 51 cdr1LengthIndex = linesplt.index("CDR1.IMGT.Nb.of.nucleotides") | |
| 52 cdr2LengthIndex = linesplt.index("CDR2.IMGT.Nb.of.nucleotides") | |
| 48 first = False | 53 first = False | 
| 49 continue | 54 continue | 
| 50 linecount += 1 | 55 linecount += 1 | 
| 51 linesplt = line.split("\t") | 56 linesplt = line.split("\t") | 
| 52 ID = linesplt[IDIndex] | 57 ID = linesplt[IDIndex] | 
| 60 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] | 65 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x] | 
| 61 | 66 | 
| 62 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 67 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 
| 63 mutationListByID[ID] = 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"] | 
| 64 | 69 | 
| 70 cdr1Length = linesplt[cdr1LengthIndex] | |
| 71 cdr2Length = linesplt[cdr2LengthIndex] | |
| 72 | |
| 73 cdr1LengthDic[ID] = int(cdr1Length) / 3 | |
| 74 cdr2LengthDic[ID] = int(cdr2Length) / 3 | |
| 75 | |
| 65 IDlist += [ID] | 76 IDlist += [ID] | 
| 66 | 77 | 
| 67 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 | 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 | |
| 68 AA_mutation_empty = AA_mutation[:] | 81 AA_mutation_empty = AA_mutation[:] | 
| 69 | 82 | 
| 70 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" | 83 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" | 
| 71 with open(aa_mutations_by_id_file, 'w') as o: | 84 with open(aa_mutations_by_id_file, 'w') as o: | 
| 72 for ID in mutationListByID.keys(): | 85 for ID in mutationListByID.keys(): | 
| 73 AA_mutation_for_ID = AA_mutation_empty[:] | 86 AA_mutation_for_ID = AA_mutation_empty[:] | 
| 74 for mutation in mutationListByID[ID]: | 87 for mutation in mutationListByID[ID]: | 
| 75 if mutation[4]: | 88 if mutation[4]: | 
| 76 AA_mutation[int(mutation[4])] += 1 | 89 AA_mutation[int(mutation[4])] += 1 | 
| 77 AA_mutation_for_ID[int(mutation[4])] += 1 | 90 AA_mutation_for_ID[int(mutation[4])] += 1 | 
| 78 o.write(ID + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID]) + "\n") | 91 o.write(ID + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n") | 
| 79 | 92 | 
| 80 | 93 | 
| 81 | 94 | 
| 82 #for mutation in mutationList: | 95 #absent AA stuff | 
| 83 # if mutation[4]: # if non silent mutation | 96 absentAACDR1Dic = defaultdict(list) | 
| 84 # AA_mutation[int(mutation[4])] += 1 | 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 | |
| 85 | 142 | 
| 86 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt" | 143 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt" | 
| 87 with open(aa_mutations_file, 'w') as o: | 144 with open(aa_mutations_file, 'w') as o: | 
| 88 o.write(",".join([str(x) for x in AA_mutation]) + "\n") | 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") | |
| 89 | 148 | 
| 90 if linecount == 0: | 149 if linecount == 0: | 
| 91 print "No data, exiting" | 150 print "No data, exiting" | 
| 92 with open(outfile, 'w') as o: | 151 with open(outfile, 'w') as o: | 
| 93 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | 152 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | 
| 94 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | 153 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | 
| 95 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | 154 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | 
| 96 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | 155 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | 
| 97 import sys | 156 import sys | 
| 98 | 157 | 
| 99 sys.exit() | 158 sys.exit() | 
| 100 | 159 | 
| 101 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | 160 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)") | 
| 102 RGYWCount = {g: 0 for g in genes} | 161 RGYWCount = {g: 0 for g in genes} | 
| 103 WRCYCount = {g: 0 for g in genes} | 162 WRCYCount = {g: 0 for g in genes} | 
| 104 WACount = {g: 0 for g in genes} | 163 WACount = {g: 0 for g in genes} | 
| 109 tatIndex = 0 | 168 tatIndex = 0 | 
| 110 aggctatIndex = 0 | 169 aggctatIndex = 0 | 
| 111 atagcctIndex = 0 | 170 atagcctIndex = 0 | 
| 112 first = True | 171 first = True | 
| 113 with open(infile, 'r') as i: | 172 with open(infile, 'r') as i: | 
| 114 for line in i: | 173 for line in i: | 
| 115 if first: | 174 if first: | 
| 116 linesplt = line.split("\t") | 175 linesplt = line.split("\t") | 
| 117 ataIndex = linesplt.index("X.a.t.a") | 176 ataIndex = linesplt.index("X.a.t.a") | 
| 118 tatIndex = linesplt.index("t.a.t.") | 177 tatIndex = linesplt.index("t.a.t.") | 
| 119 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | 178 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | 
| 120 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | 179 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | 
| 121 first = False | 180 first = False | 
| 122 continue | 181 continue | 
| 123 linesplt = line.split("\t") | 182 linesplt = line.split("\t") | 
| 124 gene = linesplt[best_matchIndex] | 183 gene = linesplt[best_matchIndex] | 
| 125 ID = linesplt[IDIndex] | 184 ID = linesplt[IDIndex] | 
| 126 RGYW = [(int(x), int(y), z) for (x, y, z) in | 185 RGYW = [(int(x), int(y), z) for (x, y, z) in | 
| 127 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | 186 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]] | 
| 128 WRCY = [(int(x), int(y), z) for (x, y, z) in | 187 WRCY = [(int(x), int(y), z) for (x, y, z) in | 
| 129 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | 188 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]] | 
| 130 WA = [(int(x), int(y), z) for (x, y, z) in | 189 WA = [(int(x), int(y), z) for (x, y, z) in | 
| 131 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | 190 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]] | 
| 132 TW = [(int(x), int(y), z) for (x, y, z) in | 191 TW = [(int(x), int(y), z) for (x, y, z) in | 
| 133 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | 192 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]] | 
| 134 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 | 193 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 | 
| 135 | 194 | 
| 136 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ | 195 mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[ | 
| 137 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 196 ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | 
| 138 for mutation in mutationList: | 197 for mutation in mutationList: | 
| 139 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | 198 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | 
| 140 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW]) | 199 mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW]) | 
| 141 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY]) | 200 mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY]) | 
| 142 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA]) | 201 mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA]) | 
| 143 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW]) | 202 mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW]) | 
| 144 | 203 | 
| 145 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | 204 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | 
| 146 | 205 | 
| 147 if in_how_many_motifs > 0: | 206 if in_how_many_motifs > 0: | 
| 148 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | 207 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | 
| 149 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | 208 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | 
| 150 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | 209 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | 
| 151 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | 210 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | 
| 152 | 211 | 
| 153 directory = outfile[:outfile.rfind("/") + 1] | 212 directory = outfile[:outfile.rfind("/") + 1] | 
| 154 value = 0 | 213 value = 0 | 
| 155 valuedic = dict() | 214 valuedic = dict() | 
| 156 for gene in genes: | 215 for gene in genes: | 
| 157 with open(directory + gene + "_value.txt", 'r') as v: | 216 with open(directory + gene + "_value.txt", 'r') as v: | 
| 158 valuedic[gene] = int(v.readlines()[0].rstrip()) | 217 valuedic[gene] = int(v.readlines()[0].rstrip()) | 
| 159 with open(directory + "total_value.txt", 'r') as v: | 218 with open(directory + "total_value.txt", 'r') as v: | 
| 160 valuedic["total"] = int(v.readlines()[0].rstrip()) | 219 valuedic["total"] = int(v.readlines()[0].rstrip()) | 
| 161 | 220 | 
| 162 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | 221 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | 
| 163 arr = ["RGYW", "WRCY", "WA", "TW"] | 222 arr = ["RGYW", "WRCY", "WA", "TW"] | 
| 164 with open(outfile, 'w') as o: | 223 with open(outfile, 'w') as o: | 
| 165 for typ in arr: | 224 for typ in arr: | 
| 166 o.write(typ + " (%)") | 225 o.write(typ + " (%)") | 
| 167 curr = dic[typ] | 226 curr = dic[typ] | 
| 168 for gene in genes: | 227 for gene in genes: | 
| 169 geneMatcher = re.compile(".*" + gene + ".*") | 228 geneMatcher = re.compile(".*" + gene + ".*") | 
| 170 if valuedic[gene] is 0: | 229 if valuedic[gene] is 0: | 
| 171 o.write(",0,0,0") | 230 o.write(",0,0,0") | 
| 172 else: | 231 else: | 
| 173 x = int(round(sum([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]]))) | 232 x = int(round(sum([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]]))) | 
| 174 y = valuedic[gene] | 233 y = valuedic[gene] | 
| 175 z = str(round(x / float(valuedic[gene]) * 100, 1)) | 234 z = str(round(x / float(valuedic[gene]) * 100, 1)) | 
| 176 o.write("," + str(x) + "," + str(y) + "," + z) | 235 o.write("," + str(x) + "," + str(y) + "," + z) | 
| 177 # for total | 236 # for total | 
| 178 x = int(round(sum([y for x, y in curr.iteritems()]))) | 237 x = int(round(sum([y for x, y in curr.iteritems()]))) | 
| 179 y = valuedic["total"] | 238 y = valuedic["total"] | 
| 180 z = str(round(x / float(valuedic["total"]) * 100, 1)) | 239 z = str(round(x / float(valuedic["total"]) * 100, 1)) | 
| 181 o.write("," + str(x) + "," + str(y) + "," + z + "\n") | 240 o.write("," + str(x) + "," + str(y) + "," + z + "\n") | 
| 182 | 241 | 
| 183 | 242 | 
| 184 # for testing | 243 # for testing | 
| 185 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | 244 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | 
| 186 with open(seq_motif_file, 'w') as o: | 245 with open(seq_motif_file, 'w') as o: | 
| 187 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n") | 246 o.write("ID\tRGYWC\tWRCY\tWA\tTW\n") | 
| 188 for ID in IDlist: | 247 for ID in IDlist: | 
| 189 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str( | 248 o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str( | 
| 190 round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n") | 249 round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n") | 
