0
|
1 import pandas as pd
|
3
|
2 try:
|
|
3 pd.options.mode.chained_assignment = None # default='warn'
|
|
4 except:
|
|
5 pass
|
0
|
6 import re
|
|
7 import argparse
|
|
8 import os
|
|
9
|
|
10 def stop_err( msg, ret=1 ):
|
|
11 sys.stderr.write( msg )
|
|
12 sys.exit( ret )
|
|
13
|
|
14 #docs.python.org/dev/library/argparse.html
|
|
15 parser = argparse.ArgumentParser()
|
|
16 parser.add_argument("--input", help="Input folder with files")
|
|
17 parser.add_argument("--output", help="Output file")
|
|
18
|
|
19 args = parser.parse_args()
|
|
20
|
|
21 old_summary_columns = [u'Sequence ID', u'JUNCTION frame', u'V-GENE and allele', u'D-GENE and allele', u'J-GENE and allele', u'CDR1-IMGT length', u'CDR2-IMGT length', u'CDR3-IMGT length', u'Orientation']
|
|
22 old_sequence_columns = [u'CDR1-IMGT', u'CDR2-IMGT', u'CDR3-IMGT']
|
|
23 old_junction_columns = [u'JUNCTION']
|
|
24
|
|
25 added_summary_columns = [u'Functionality', u'V-REGION identity %', u'V-REGION identity nt', u'D-REGION reading frame', u'AA JUNCTION', u'Functionality comment', u'Sequence']
|
|
26 added_sequence_columns = [u'FR1-IMGT', u'FR2-IMGT', u'FR3-IMGT', u'CDR3-IMGT', u'JUNCTION', u'J-REGION', u'FR4-IMGT']
|
|
27 added_junction_columns = [u"P3'V-nt nb", u'N1-REGION-nt nb', u"P5'D-nt nb", u"P3'D-nt nb", u'N2-REGION-nt nb', u"P5'J-nt nb", u"3'V-REGION trimmed-nt nb", u"5'D-REGION trimmed-nt nb", u"3'D-REGION trimmed-nt nb", u"5'J-REGION trimmed-nt nb"]
|
|
28
|
|
29 inputFolder = args.input
|
|
30
|
|
31 dirContents = os.listdir(inputFolder)
|
|
32 if len(dirContents) == 1:
|
|
33 inputFolder = os.path.join(inputFolder, dirContents[0])
|
|
34 if os.path.isdir(inputFolder):
|
|
35 dirContents = os.listdir(inputFolder)
|
4
|
36 files = sorted([os.path.join(inputFolder, f) for f in dirContents if os.path.isfile(os.path.join(inputFolder, f))])
|
0
|
37
|
|
38 if len(files) % 3 is not 0:
|
|
39 stop_err("Files in zip not a multiple of 3, it should contain the all the 1_, 5_ and 6_ files for a sample")
|
|
40 import sys
|
|
41 sys.exit()
|
|
42
|
|
43 triplets = []
|
|
44 step = len(files) / 3
|
|
45 for i in range(0, step):
|
|
46 triplets.append((files[i], files[i + step], files[i + step + step]))
|
|
47
|
|
48 outFile = args.output
|
|
49
|
|
50 fSummary = pd.read_csv(triplets[0][0], sep="\t")
|
|
51 fSequence = pd.read_csv(triplets[0][1], sep="\t")
|
|
52 fJunction = pd.read_csv(triplets[0][2], sep="\t")
|
|
53 tmp = fSummary[["Sequence ID", "JUNCTION frame", "V-GENE and allele", "D-GENE and allele", "J-GENE and allele"]]
|
|
54
|
|
55 tmp["CDR1 Seq"] = fSequence["CDR1-IMGT"]
|
|
56 tmp["CDR1 Length"] = fSummary["CDR1-IMGT length"]
|
|
57
|
|
58 tmp["CDR2 Seq"] = fSequence["CDR2-IMGT"]
|
|
59 tmp["CDR2 Length"] = fSummary["CDR2-IMGT length"]
|
|
60
|
|
61 tmp["CDR3 Seq"] = fSequence["CDR3-IMGT"]
|
|
62 tmp["CDR3 Length"] = fSummary["CDR3-IMGT length"]
|
|
63
|
|
64 tmp["CDR3 Seq DNA"] = fJunction["JUNCTION"]
|
|
65 tmp["CDR3 Length DNA"] = '1'
|
|
66 tmp["Strand"] = fSummary["Orientation"]
|
|
67 tmp["CDR3 Found How"] = 'a'
|
|
68
|
|
69 for col in added_summary_columns:
|
|
70 tmp[col] = fSummary[col]
|
|
71
|
|
72 for col in added_sequence_columns:
|
|
73 tmp[col] = fSequence[col]
|
|
74
|
|
75 for col in added_junction_columns:
|
|
76 tmp[col] = fJunction[col]
|
|
77
|
|
78 outFrame = tmp
|
|
79
|
4
|
80
|
|
81
|
0
|
82 for triple in triplets[1:]:
|
|
83 fSummary = pd.read_csv(triple[0], sep="\t")
|
|
84 fSequence = pd.read_csv(triple[1], sep="\t")
|
|
85 fJunction = pd.read_csv(triple[2], sep="\t")
|
|
86
|
|
87 tmp = fSummary[["Sequence ID", "JUNCTION frame", "V-GENE and allele", "D-GENE and allele", "J-GENE and allele"]]
|
|
88
|
|
89 tmp["CDR1 Seq"] = fSequence["CDR1-IMGT"]
|
|
90 tmp["CDR1 Length"] = fSummary["CDR1-IMGT length"]
|
|
91
|
|
92 tmp["CDR2 Seq"] = fSequence["CDR2-IMGT"]
|
|
93 tmp["CDR2 Length"] = fSummary["CDR2-IMGT length"]
|
|
94
|
|
95 tmp["CDR3 Seq"] = fSequence["CDR3-IMGT"]
|
|
96 tmp["CDR3 Length"] = fSummary["CDR3-IMGT length"]
|
|
97
|
|
98 tmp["CDR3 Seq DNA"] = fJunction["JUNCTION"]
|
|
99 tmp["CDR3 Length DNA"] = '1'
|
|
100 tmp["Strand"] = fSummary["Orientation"]
|
|
101 tmp["CDR3 Found How"] = 'a'
|
|
102
|
|
103 for col in added_summary_columns:
|
|
104 tmp[col] = fSummary[col]
|
|
105
|
|
106 for col in added_sequence_columns:
|
|
107 tmp[col] = fSequence[col]
|
|
108
|
|
109 for col in added_junction_columns:
|
|
110 tmp[col] = fJunction[col]
|
|
111
|
|
112 outFrame = outFrame.append(tmp)
|
|
113
|
4
|
114
|
0
|
115 outFrame.columns = [u'ID', u'VDJ Frame', u'Top V Gene', u'Top D Gene', u'Top J Gene', u'CDR1 Seq', u'CDR1 Length', u'CDR2 Seq', u'CDR2 Length', u'CDR3 Seq', u'CDR3 Length', u'CDR3 Seq DNA', u'CDR3 Length DNA', u'Strand', u'CDR3 Found How', u'Functionality', 'V-REGION identity %', 'V-REGION identity nt', 'D-REGION reading frame', 'AA JUNCTION', 'Functionality comment', 'Sequence', 'FR1-IMGT', 'FR2-IMGT', 'FR3-IMGT', 'CDR3-IMGT', 'JUNCTION', 'J-REGION', 'FR4-IMGT', 'P3V-nt nb', 'N1-REGION-nt nb', 'P5D-nt nb', 'P3D-nt nb', 'N2-REGION-nt nb', 'P5J-nt nb', '3V-REGION trimmed-nt nb', '5D-REGION trimmed-nt nb', '3D-REGION trimmed-nt nb', '5J-REGION trimmed-nt nb']
|
|
116
|
4
|
117 """
|
|
118 IGHV[0-9]-[0-9ab]+-?[0-9]?D?
|
|
119 TRBV[0-9]{1,2}-?[0-9]?-?[123]?
|
|
120 IGKV[0-3]D?-[0-9]{1,2}
|
|
121 IGLV[0-9]-[0-9]{1,2}
|
|
122 TRAV[0-9]{1,2}(-[1-46])?(/DV[45678])?
|
|
123 TRGV[234589]
|
|
124 TRDV[1-3]
|
|
125
|
|
126 IGHD[0-9]-[0-9ab]+
|
|
127 TRBD[12]
|
|
128 TRDD[1-3]
|
|
129
|
|
130 IGHJ[1-6]
|
|
131 TRBJ[12]-[1-7]
|
|
132 IGKJ[1-5]
|
|
133 IGLJ[12367]
|
|
134 TRAJ[0-9]{1,2}
|
|
135 TRGJP?[12]
|
|
136 TRDJ[1-4]
|
|
137 """
|
1
|
138
|
4
|
139 vPattern = [r"(IGHV[0-9]-[0-9ab]+-?[0-9]?D?)",
|
|
140 r"(TRBV[0-9]{1,2}-?[0-9]?-?[123]?)",
|
|
141 r"(IGKV[0-3]D?-[0-9]{1,2})",
|
|
142 r"(IGLV[0-9]-[0-9]{1,2})",
|
|
143 r"(TRAV[0-9]{1,2}(-[1-46])?(/DV[45678])?)",
|
|
144 r"(TRGV[234589])",
|
|
145 r"(TRDV[1-3])"]
|
1
|
146
|
4
|
147 dPattern = [r"(IGHD[0-9]-[0-9ab]+)",
|
|
148 r"(TRBD[12])",
|
|
149 r"(TRDD[1-3])"]
|
|
150
|
|
151 jPattern = [r"(IGHJ[1-6])",
|
|
152 r"(TRBJ[12]-[1-7])",
|
|
153 r"(IGKJ[1-5])",
|
|
154 r"(IGLJ[12367])",
|
|
155 r"(TRAJ[0-9]{1,2})",
|
|
156 r"(TRGJP?[12])",
|
|
157 r"(TRDJ[1-4])"]
|
1
|
158
|
4
|
159 vPattern = re.compile(r"|".join(vPattern))
|
|
160
|
|
161 dPattern = re.compile(r"|".join(dPattern))
|
|
162
|
|
163 jPattern = re.compile(r"|".join(jPattern))
|
0
|
164
|
3
|
165
|
0
|
166 def filterGenes(s, pattern):
|
|
167 if type(s) is not str:
|
|
168 return "NA"
|
|
169 res = pattern.search(s)
|
|
170 if res:
|
|
171 return res.group(0)
|
|
172 return "NA"
|
|
173
|
|
174
|
4
|
175
|
0
|
176 outFrame["Top V Gene"] = outFrame["Top V Gene"].apply(lambda x: filterGenes(x, vPattern))
|
|
177 outFrame["Top D Gene"] = outFrame["Top D Gene"].apply(lambda x: filterGenes(x, dPattern))
|
|
178 outFrame["Top J Gene"] = outFrame["Top J Gene"].apply(lambda x: filterGenes(x, jPattern))
|
|
179
|
4
|
180 print outFrame
|
0
|
181
|
|
182 tmp = outFrame["VDJ Frame"]
|
|
183 tmp = tmp.replace("in-frame", "In-frame")
|
|
184 tmp = tmp.replace("null", "Out-of-frame")
|
|
185 tmp = tmp.replace("out-of-frame", "Out-of-frame")
|
|
186 outFrame["VDJ Frame"] = tmp
|
|
187 outFrame["CDR3 Length DNA"] = outFrame["CDR3 Seq DNA"].map(str).map(len)
|
|
188 safeLength = lambda x: len(x) if type(x) == str else 0
|
|
189 outFrame = outFrame[(outFrame["CDR3 Seq DNA"].map(safeLength) > 0) & (outFrame["Top V Gene"] != "NA") & (outFrame["Top D Gene"] != "NA") & (outFrame["Top J Gene"] != "NA")] #filter out weird rows?
|
3
|
190 outFrame.to_csv(outFile, sep="\t", index=False, index_label="index")
|