comparison mutation_analysis.py @ 0:74d2bc479bee draft

Uploaded
author davidvanzessen
date Mon, 18 Aug 2014 04:04:37 -0400
parents
children 069419cccba4
comparison
equal deleted inserted replaced
-1:000000000000 0:74d2bc479bee
1 import re
2 import argparse
3
4 parser = argparse.ArgumentParser()
5 parser.add_argument("--mutationfile", help="The '7_V-REGION-mutation-and-AA-change-table' file from the IMGT output")
6 parser.add_argument("--hotspotfile", help="The '10_V-REGION-mutation-hotspots' file from the IMGT output")
7 parser.add_argument("--output", help="Output file")
8
9 args = parser.parse_args()
10
11 mutationfile = args.mutationfile #"test_VH-Ca_Cg_25nt/7_V-REGION-mutation-and-AA-change-table_test_VH-Ca_Cg_25nt_241013.txt"
12 hotspotsfile = args.hotspotfile #"test_VH-Ca_Cg_25nt/10_V-REGION-mutation-hotspots_test_VH-Ca_Cg_25nt_241013.txt"
13 outfile = args.output #"out.txt"
14
15 mutationdic = dict()
16 mutationMatcher = re.compile("^(.)(\d+).(.),?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
17 linecount = 0
18
19 with open(mutationfile, 'r') as i:
20 for line in i.readlines()[1:]:
21 linecount += 1
22 linesplt = line.split("\t")
23 if linesplt[2] != "productive":
24 continue
25 ID = linesplt[1]
26 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[5].split("|") if x]
27 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[6].split("|") if x]
28 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[7].split("|") if x]
29 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[8].split("|") if x]
30 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
31 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[9].split("|") if x]
32
33 if linecount == 0:
34 print "No data, exiting"
35 with open(outfile, 'w') as o:
36 o.write("RGYW (%),0,0,NA\n")
37 o.write("WRCY (%),0,0,NA\n")
38 o.write("WA (%),0,0,NA\n")
39 o.write("TW (%),0,0,NA\n")
40 import sys
41 sys.exit()
42
43 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
44 RGYWCount = 0
45 WRCYCount = 0
46 WACount = 0
47 TWCount = 0
48
49 with open(hotspotsfile, 'r') as i:
50 for line in i.readlines()[1:]:
51 linesplt = line.split("\t")
52 if linesplt[2] != "productive":
53 continue
54 ID = linesplt[1]
55 RGYW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[6].split("|") if x]]
56 WRCY = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[7].split("|") if x]]
57 WA = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[4].split("|") if x]]
58 TW = [(int(x),int(y),z) for (x,y,z) in [hotspotMatcher.match(x).groups() for x in linesplt[5].split("|") if x]]
59 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]])])
60 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]])])
61 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]])])
62 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]])])
63
64 path = outfile[:outfile.rfind("/") + 1] + "mutations.txt"
65 value = 0;
66 with open(path, 'r') as f:
67 value = f.readlines()[0].split(",")[1]
68 with open(outfile, 'w') as o:
69 if value != "0":
70 o.write("RGYW (%)," + str(RGYWCount) + "," + value + "," + str(round(RGYWCount / float(value) * 100, 1)) + "\n")
71 o.write("WRCY (%)," + str(WRCYCount) + "," + value + "," + str(round(WRCYCount / float(value) * 100, 1)) + "\n")
72 o.write("WA (%)," + str(WACount) + "," + value + "," + str(round(WACount / float(value) * 100, 1)) + "\n")
73 o.write("TW (%)," + str(TWCount) + "," + value + "," + str(round(TWCount / float(value) * 100, 1)) + "\n")
74 else:
75 o.write("RGYW (%)," + str(RGYWCount) + ",0,NA\n")
76 o.write("WRCY (%)," + str(WRCYCount) + ",0,NA\n")
77 o.write("WA (%)," + str(WACount) + ",0,NA\n")
78 o.write("TW (%)," + str(TWCount) + ",0,NA\n")
79
80