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 print "is dir"
|
|
36 dirContents = os.listdir(inputFolder)
|
|
37 files = sorted([os.path.join(inputFolder, f) for f in dirContents])
|
|
38
|
|
39 if len(files) % 3 is not 0:
|
|
40 stop_err("Files in zip not a multiple of 3, it should contain the all the 1_, 5_ and 6_ files for a sample")
|
|
41 import sys
|
|
42 sys.exit()
|
|
43
|
|
44 triplets = []
|
|
45 step = len(files) / 3
|
|
46 for i in range(0, step):
|
|
47 triplets.append((files[i], files[i + step], files[i + step + step]))
|
|
48
|
|
49 outFile = args.output
|
|
50
|
|
51 fSummary = pd.read_csv(triplets[0][0], sep="\t")
|
|
52 fSequence = pd.read_csv(triplets[0][1], sep="\t")
|
|
53 fJunction = pd.read_csv(triplets[0][2], sep="\t")
|
|
54 tmp = fSummary[["Sequence ID", "JUNCTION frame", "V-GENE and allele", "D-GENE and allele", "J-GENE and allele"]]
|
|
55
|
|
56 tmp["CDR1 Seq"] = fSequence["CDR1-IMGT"]
|
|
57 tmp["CDR1 Length"] = fSummary["CDR1-IMGT length"]
|
|
58
|
|
59 tmp["CDR2 Seq"] = fSequence["CDR2-IMGT"]
|
|
60 tmp["CDR2 Length"] = fSummary["CDR2-IMGT length"]
|
|
61
|
|
62 tmp["CDR3 Seq"] = fSequence["CDR3-IMGT"]
|
|
63 tmp["CDR3 Length"] = fSummary["CDR3-IMGT length"]
|
|
64
|
|
65 tmp["CDR3 Seq DNA"] = fJunction["JUNCTION"]
|
|
66 tmp["CDR3 Length DNA"] = '1'
|
|
67 tmp["Strand"] = fSummary["Orientation"]
|
|
68 tmp["CDR3 Found How"] = 'a'
|
|
69
|
|
70 for col in added_summary_columns:
|
|
71 tmp[col] = fSummary[col]
|
|
72
|
|
73 for col in added_sequence_columns:
|
|
74 tmp[col] = fSequence[col]
|
|
75
|
|
76 for col in added_junction_columns:
|
|
77 tmp[col] = fJunction[col]
|
|
78
|
|
79 outFrame = tmp
|
|
80
|
|
81 for triple in triplets[1:]:
|
|
82 fSummary = pd.read_csv(triple[0], sep="\t")
|
|
83 fSequence = pd.read_csv(triple[1], sep="\t")
|
|
84 fJunction = pd.read_csv(triple[2], sep="\t")
|
|
85
|
|
86 tmp = fSummary[["Sequence ID", "JUNCTION frame", "V-GENE and allele", "D-GENE and allele", "J-GENE and allele"]]
|
|
87
|
|
88 tmp["CDR1 Seq"] = fSequence["CDR1-IMGT"]
|
|
89 tmp["CDR1 Length"] = fSummary["CDR1-IMGT length"]
|
|
90
|
|
91 tmp["CDR2 Seq"] = fSequence["CDR2-IMGT"]
|
|
92 tmp["CDR2 Length"] = fSummary["CDR2-IMGT length"]
|
|
93
|
|
94 tmp["CDR3 Seq"] = fSequence["CDR3-IMGT"]
|
|
95 tmp["CDR3 Length"] = fSummary["CDR3-IMGT length"]
|
|
96
|
|
97 tmp["CDR3 Seq DNA"] = fJunction["JUNCTION"]
|
|
98 tmp["CDR3 Length DNA"] = '1'
|
|
99 tmp["Strand"] = fSummary["Orientation"]
|
|
100 tmp["CDR3 Found How"] = 'a'
|
|
101
|
|
102 for col in added_summary_columns:
|
|
103 tmp[col] = fSummary[col]
|
|
104
|
|
105 for col in added_sequence_columns:
|
|
106 tmp[col] = fSequence[col]
|
|
107
|
|
108 for col in added_junction_columns:
|
|
109 tmp[col] = fJunction[col]
|
|
110
|
|
111 outFrame = outFrame.append(tmp)
|
|
112
|
|
113 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']
|
|
114
|
1
|
115 #vPattern = re.compile(r"|TRBV[0-9]{1,2}-?[0-9]?") #mouse
|
|
116 #vPattern = re.compile(r"IGHV[1-9]-[0-9ab]+-?[1-9]?") #human
|
|
117 vPattern = re.compile(r"IGHV[1-9]-[0-9ab]+-?[1-9]?|TRBV[0-9]{1,2}-?[0-9]?") #mouse and human
|
|
118
|
|
119 #dPattern = re.compile(r"TRBD1|TRBD2") #mouse
|
|
120 #dPattern = re.compile(r"IGHD[1-9]-[0-9ab]+") #human
|
|
121 dPattern = re.compile(r"IGHD[1-9]-[0-9ab]+|TRBD1|TRBD2") #mouse and human
|
|
122
|
|
123
|
|
124 #jPattern = re.compile(r"TRBJ[12]-[1-7]") #mouse
|
|
125 #jPattern = re.compile(r"IGHJ[1-6]") #human
|
|
126 jPattern = re.compile(r"IGHJ[1-6]|TRBJ[12]-[1-7]") #mouse and human
|
0
|
127
|
3
|
128
|
0
|
129 def filterGenes(s, pattern):
|
|
130 if type(s) is not str:
|
|
131 return "NA"
|
|
132 res = pattern.search(s)
|
|
133 if res:
|
|
134 return res.group(0)
|
|
135 return "NA"
|
|
136
|
|
137
|
|
138 outFrame["Top V Gene"] = outFrame["Top V Gene"].apply(lambda x: filterGenes(x, vPattern))
|
|
139 outFrame["Top D Gene"] = outFrame["Top D Gene"].apply(lambda x: filterGenes(x, dPattern))
|
|
140 outFrame["Top J Gene"] = outFrame["Top J Gene"].apply(lambda x: filterGenes(x, jPattern))
|
|
141
|
|
142
|
|
143
|
|
144 tmp = outFrame["VDJ Frame"]
|
|
145 tmp = tmp.replace("in-frame", "In-frame")
|
|
146 tmp = tmp.replace("null", "Out-of-frame")
|
|
147 tmp = tmp.replace("out-of-frame", "Out-of-frame")
|
|
148 outFrame["VDJ Frame"] = tmp
|
|
149 outFrame["CDR3 Length DNA"] = outFrame["CDR3 Seq DNA"].map(str).map(len)
|
|
150 safeLength = lambda x: len(x) if type(x) == str else 0
|
|
151 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
|
152 outFrame.to_csv(outFile, sep="\t", index=False, index_label="index")
|