diff mutation_analysis.py @ 4:069419cccba4 draft

Uploaded
author davidvanzessen
date Mon, 22 Sep 2014 10:19:36 -0400
parents 74d2bc479bee
children c9f9623f1f76
line wrap: on
line diff
--- a/mutation_analysis.py	Wed Sep 17 07:25:17 2014 -0400
+++ b/mutation_analysis.py	Mon Sep 22 10:19:36 2014 -0400
@@ -2,79 +2,125 @@
 import argparse
 
 parser = argparse.ArgumentParser()
-parser.add_argument("--mutationfile", help="The '7_V-REGION-mutation-and-AA-change-table' file from the IMGT output")
-parser.add_argument("--hotspotfile", help="The '10_V-REGION-mutation-hotspots' file from the IMGT output")
+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")
+parser.add_argument("--genes", help="The genes available in the 'best_match' column")
 parser.add_argument("--output", help="Output file")
 
 args = parser.parse_args()
 
-mutationfile = args.mutationfile #"test_VH-Ca_Cg_25nt/7_V-REGION-mutation-and-AA-change-table_test_VH-Ca_Cg_25nt_241013.txt"
-hotspotsfile = args.hotspotfile #"test_VH-Ca_Cg_25nt/10_V-REGION-mutation-hotspots_test_VH-Ca_Cg_25nt_241013.txt"
-outfile = args.output #"out.txt"
+infile = args.input
+genes = str(args.genes).split(",")
+outfile = args.output 
+
+genedic = dict()
 
 mutationdic = dict()
 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
 linecount = 0
 
-with open(mutationfile, 'r') as i:
-  for line in i.readlines()[1:]:
+IDIndex = 0
+best_matchIndex = 0
+fr1Index = 0
+cdr1Index = 0
+fr2Index = 0
+cdr2Index = 0
+fr3Index = 0
+first=True
+with open(infile, 'r') as i:
+  for line in i:
+		if first:
+			linesplt = line.split("\t")
+			IDIndex = linesplt.index("Sequence.ID")
+			best_matchIndex = linesplt.index("best_match")
+			fr1Index = linesplt.index("FR1.IMGT")
+			cdr1Index = linesplt.index("CDR1.IMGT")
+			fr2Index = linesplt.index("FR2.IMGT")
+			cdr2Index = linesplt.index("CDR2.IMGT")
+			fr3Index = linesplt.index("FR3.IMGT")
+			first = False
+			continue
 		linecount += 1
 		linesplt = line.split("\t")
-		if linesplt[2] != "productive":
-			continue
-		ID = linesplt[1]
-		mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[5].split("|") if x]
-		mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[6].split("|") if x]
-		mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[7].split("|") if x]
-		mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[8].split("|") if x]
+		ID = linesplt[IDIndex]
+		genedic[ID] = linesplt[best_matchIndex]
+		mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x]
+		mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
+		mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
+		mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
 		mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
-		mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[9].split("|") if x]
+		mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
     
 if linecount == 0:
 	print "No data, exiting"
 	with open(outfile, 'w') as o:
-		o.write("RGYW (%),0,0,NA\n")
-		o.write("WRCY (%),0,0,NA\n")
-		o.write("WA (%),0,0,NA\n")
-		o.write("TW (%),0,0,NA\n")
+		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()
 
 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
-RGYWCount = 0
-WRCYCount = 0
-WACount = 0
-TWCount = 0
+RGYWCount = {g: 0 for g in genes}
+WRCYCount = {g: 0 for g in genes}
+WACount = {g: 0 for g in genes}
+TWCount = {g: 0 for g in genes}
 
-with open(hotspotsfile, 'r') as i:
-	for line in i.readlines()[1:]:
-		linesplt = line.split("\t")
-		if linesplt[2] != "productive":
+IDIndex = 0
+ataIndex = 0
+tatIndex = 0
+aggctatIndex = 0
+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
-		ID = linesplt[1]
-		RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[6].split("|") if x]]
-		WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[7].split("|") if x]]
-		WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[4].split("|") if x]]
-		TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[5].split("|") if x]]
-		RGYWCount += sum([1 for (x,y,z) in RGYW if z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
-		WRCYCount += sum([1 for (x,y,z) in WRCY if z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
-		WACount += sum([1 for (x,y,z) in WA if z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
-		TWCount += sum([1 for (x,y,z) in TW if z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
-
-path = outfile[:outfile.rfind("/") + 1] + "mutations.txt"
-value = 0;
-with open(path, 'r') as f:
-	value = f.readlines()[0].split(",")[1]
-with open(outfile, 'w') as o:
-	if value != "0":
-		o.write("RGYW (%)," + str(RGYWCount) + "," + value + "," + str(round(RGYWCount / float(value) * 100, 1)) + "\n")
-		o.write("WRCY (%)," + str(WRCYCount) + "," + value + "," + str(round(WRCYCount / float(value) * 100, 1)) + "\n")
-		o.write("WA (%)," + str(WACount) + "," + value + "," + str(round(WACount / float(value) * 100, 1)) + "\n")
-		o.write("TW (%)," + str(TWCount) + "," + value + "," + str(round(TWCount / float(value) * 100, 1)) + "\n")
-	else:
-		o.write("RGYW (%)," + str(RGYWCount) + ",0,NA\n")
-		o.write("WRCY (%)," + str(WRCYCount) + ",0,NA\n")
-		o.write("WA (%)," + str(WACount) + ",0,NA\n")
-		o.write("TW (%)," + str(TWCount) + ",0,NA\n")
+		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] = sum([1 for (x,y,z) in RGYW if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
+		WRCYCount[ID] = sum([1 for (x,y,z) in WRCY if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
+		WACount[ID] = sum([1 for (x,y,z) in WA if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
+		TWCount[ID] = sum([1 for (x,y,z) in TW if z and z != "CDR3" and any([(x <= int(where) <= y) for (frm, where, to, a,b,c,d) in mutationdic[ID + "_" + z]])])
 
 
+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 + "total_value.txt", 'r') as v:
+	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 = 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 = 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")