diff mutation_analysis.py @ 49:5c6b9e99d576 draft

Uploaded
author davidvanzessen
date Wed, 18 Nov 2015 05:55:04 -0500
parents 4b1bab1a9ad2
children 7290a88ea202
line wrap: on
line diff
--- a/mutation_analysis.py	Thu Nov 12 09:46:37 2015 -0500
+++ b/mutation_analysis.py	Wed Nov 18 05:55:04 2015 -0500
@@ -1,9 +1,10 @@
+from collections import defaultdict
 import re
 import argparse
 
 parser = argparse.ArgumentParser()
 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")
+					help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation")
 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
 parser.add_argument("--includefr1", help="The genes available in the 'best_match' column")
 parser.add_argument("--output", help="Output file")
@@ -33,6 +34,8 @@
 IDlist = []
 mutationList = []
 mutationListByID = {}
+cdr1LengthDic = {}
+cdr2LengthDic = {}
 
 with open(infile, 'r') as i:
 	for line in i:
@@ -45,6 +48,8 @@
 			fr2Index = linesplt.index("FR2.IMGT")
 			cdr2Index = linesplt.index("CDR2.IMGT")
 			fr3Index = linesplt.index("FR3.IMGT")
+			cdr1LengthIndex = linesplt.index("CDR1.IMGT.Nb.of.nucleotides")
+			cdr2LengthIndex = linesplt.index("CDR2.IMGT.Nb.of.nucleotides")
 			first = False
 			continue
 		linecount += 1
@@ -62,9 +67,17 @@
 		mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
 		mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
 
+		cdr1Length = linesplt[cdr1LengthIndex]
+		cdr2Length = linesplt[cdr2LengthIndex]
+
+		cdr1LengthDic[ID] = int(cdr1Length) / 3
+		cdr2LengthDic[ID] = int(cdr2Length) / 3
+
 		IDlist += [ID]
 
-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
+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
+
+AA_mutation = [0] * AALength
 AA_mutation_empty = AA_mutation[:]
 
 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt"
@@ -75,28 +88,74 @@
 			if mutation[4]:
 				AA_mutation[int(mutation[4])] += 1
 				AA_mutation_for_ID[int(mutation[4])] += 1
-		o.write(ID + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID]) + "\n")
+		o.write(ID + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n")
 
 
 
-#for mutation in mutationList:
-#    if mutation[4]:  # if non silent mutation
-#        AA_mutation[int(mutation[4])] += 1
+#absent AA stuff
+absentAACDR1Dic = defaultdict(list)
+absentAACDR1Dic[5] = range(29,36)
+absentAACDR1Dic[6] = range(29,35)
+absentAACDR1Dic[7] = range(30,35)
+absentAACDR1Dic[8] = range(30,34)
+absentAACDR1Dic[9] = range(31,34)
+absentAACDR1Dic[10] = range(31,33)
+absentAACDR1Dic[11] = [32]
+
+absentAACDR2Dic = defaultdict(list)
+absentAACDR2Dic[0] = range(55,65)
+absentAACDR2Dic[1] = range(56,65)
+absentAACDR2Dic[2] = range(56,64)
+absentAACDR2Dic[3] = range(57,64)
+absentAACDR2Dic[4] = range(57,63)
+absentAACDR2Dic[5] = range(58,63)
+absentAACDR2Dic[6] = range(58,62)
+absentAACDR2Dic[7] = range(59,62)
+absentAACDR2Dic[8] = range(59,61)
+absentAACDR2Dic[9] = [60]
+
+absentAA = [len(IDlist)] * (AALength-1)
+for k, cdr1Length in cdr1LengthDic.iteritems():
+	for c in absentAACDR1Dic[cdr1Length]:
+		absentAA[c] -= 1
+
+for k, cdr2Length in cdr2LengthDic.iteritems():
+	for c in absentAACDR2Dic[cdr2Length]:
+		absentAA[c] -= 1
+
+
+aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt"
+with open(aa_mutations_by_id_file, 'w') as o:
+	o.write("ID\tcdr1length\tcdr2length\t" + "\t".join([str(x) for x in range(1,AALength-1)]) + "\n")
+	for ID in IDlist:
+		absentAAbyID = [1] * (AALength-1)
+		cdr1Length = cdr1LengthDic[ID]
+		for c in absentAACDR1Dic[cdr1Length]:
+			absentAAbyID[c] -= 1
+
+		cdr2Length = cdr2LengthDic[ID]
+		for c in absentAACDR2Dic[cdr2Length]:
+			absentAAbyID[c] -= 1
+		o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n")
+
+
 
 aa_mutations_file = outfile[:outfile.rindex("/")] + "/aa_mutations.txt"
 with open(aa_mutations_file, 'w') as o:
-    o.write(",".join([str(x) for x in AA_mutation]) + "\n")
+	o.write("row.name\t" + "\t".join([str(x) for x in range(1, AALength-1)]) + "\n")
+	o.write("mutations.at.position\t" + "\t".join([str(x) for x in AA_mutation[1:]]) + "\n")
+	o.write("AA.at.position\t" + "\t".join([str(x) for x in absentAA]) + "\n")
 
 if linecount == 0:
-    print "No data, exiting"
-    with open(outfile, 'w') as o:
-        o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
-        o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
-        o.write("WA (%)," + ("0,0,0\n" * len(genes)))
-        o.write("TW (%)," + ("0,0,0\n" * len(genes)))
-    import sys
+	print "No data, exiting"
+	with open(outfile, 'w') as o:
+		o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
+		o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
+		o.write("WA (%)," + ("0,0,0\n" * len(genes)))
+		o.write("TW (%)," + ("0,0,0\n" * len(genes)))
+	import sys
 
-    sys.exit()
+	sys.exit()
 
 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
 RGYWCount = {g: 0 for g in genes}
@@ -111,80 +170,80 @@
 atagcctIndex = 0
 first = True
 with open(infile, 'r') as i:
-    for line in i:
-        if first:
-            linesplt = line.split("\t")
-            ataIndex = linesplt.index("X.a.t.a")
-            tatIndex = linesplt.index("t.a.t.")
-            aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
-            atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
-            first = False
-            continue
-        linesplt = line.split("\t")
-        gene = linesplt[best_matchIndex]
-        ID = linesplt[IDIndex]
-        RGYW = [(int(x), int(y), z) for (x, y, z) in
-                [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
-        WRCY = [(int(x), int(y), z) for (x, y, z) in
-                [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
-        WA = [(int(x), int(y), z) for (x, y, z) in
-              [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
-        TW = [(int(x), int(y), z) for (x, y, z) in
-              [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
-        RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
+	for line in i:
+		if first:
+			linesplt = line.split("\t")
+			ataIndex = linesplt.index("X.a.t.a")
+			tatIndex = linesplt.index("t.a.t.")
+			aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
+			atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
+			first = False
+			continue
+		linesplt = line.split("\t")
+		gene = linesplt[best_matchIndex]
+		ID = linesplt[IDIndex]
+		RGYW = [(int(x), int(y), z) for (x, y, z) in
+				[hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
+		WRCY = [(int(x), int(y), z) for (x, y, z) in
+				[hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
+		WA = [(int(x), int(y), z) for (x, y, z) in
+			  [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
+		TW = [(int(x), int(y), z) for (x, y, z) in
+			  [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
+		RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
 
-        mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[
-            ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
-        for mutation in mutationList:
-            frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
-            mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW])
-            mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY])
-            mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA])
-            mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW])
+		mutationList = (mutationdic[ID + "_FR1"] if include_fr1 else []) + mutationdic[ID + "_CDR1"] + mutationdic[
+			ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
+		for mutation in mutationList:
+			frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
+			mutation_in_RGYW = any([(start <= int(where) <= end) for (start, end, region) in RGYW])
+			mutation_in_WRCY = any([(start <= int(where) <= end) for (start, end, region) in WRCY])
+			mutation_in_WA = any([(start <= int(where) <= end) for (start, end, region) in WA])
+			mutation_in_TW = any([(start <= int(where) <= end) for (start, end, region) in TW])
 
-            in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
+			in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
 
-            if in_how_many_motifs > 0:
-                RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
-                WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
-                WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
-                TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
+			if in_how_many_motifs > 0:
+				RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
+				WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
+				WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
+				TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
 
 directory = outfile[:outfile.rfind("/") + 1]
 value = 0
 valuedic = dict()
 for gene in genes:
-    with open(directory + gene + "_value.txt", 'r') as v:
-        valuedic[gene] = int(v.readlines()[0].rstrip())
+	with open(directory + gene + "_value.txt", 'r') as v:
+		valuedic[gene] = int(v.readlines()[0].rstrip())
 with open(directory + "total_value.txt", 'r') as v:
-    valuedic["total"] = int(v.readlines()[0].rstrip())
+	valuedic["total"] = int(v.readlines()[0].rstrip())
 
 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
 arr = ["RGYW", "WRCY", "WA", "TW"]
 with open(outfile, 'w') as o:
-    for typ in arr:
-        o.write(typ + " (%)")
-        curr = dic[typ]
-        for gene in genes:
-            geneMatcher = re.compile(".*" + gene + ".*")
-            if valuedic[gene] is 0:
-                o.write(",0,0,0")
-            else:
-                x = int(round(sum([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]])))
-                y = valuedic[gene]
-                z = str(round(x / float(valuedic[gene]) * 100, 1))
-                o.write("," + str(x) + "," + str(y) + "," + z)
-        # for total
-        x = int(round(sum([y for x, y in curr.iteritems()])))
-        y = valuedic["total"]
-        z = str(round(x / float(valuedic["total"]) * 100, 1))
-        o.write("," + str(x) + "," + str(y) + "," + z + "\n")
+	for typ in arr:
+		o.write(typ + " (%)")
+		curr = dic[typ]
+		for gene in genes:
+			geneMatcher = re.compile(".*" + gene + ".*")
+			if valuedic[gene] is 0:
+				o.write(",0,0,0")
+			else:
+				x = int(round(sum([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]])))
+				y = valuedic[gene]
+				z = str(round(x / float(valuedic[gene]) * 100, 1))
+				o.write("," + str(x) + "," + str(y) + "," + z)
+		# for total
+		x = int(round(sum([y for x, y in curr.iteritems()])))
+		y = valuedic["total"]
+		z = str(round(x / float(valuedic["total"]) * 100, 1))
+		o.write("," + str(x) + "," + str(y) + "," + z + "\n")
 
 
 # for testing
 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
 with open(seq_motif_file, 'w') as o:
-    o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
-    for ID in IDlist:
-        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")
+	o.write("ID\tRGYWC\tWRCY\tWA\tTW\n")
+	for ID in IDlist:
+		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")