Mercurial > repos > davidvanzessen > mutation_analysis
comparison imgt_loader.py @ 47:099cc1254f74 draft
Uploaded
author | davidvanzessen |
---|---|
date | Mon, 09 Nov 2015 07:21:09 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
46:42e39033514b | 47:099cc1254f74 |
---|---|
1 import pandas as pd | |
2 try: | |
3 pd.options.mode.chained_assignment = None # default='warn' | |
4 except: | |
5 pass | |
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("--summ", help="The 1_Summary file from the imgt output") | |
17 parser.add_argument("--aa", help="The 5_AA-Sequence file from the imgt output") | |
18 parser.add_argument("--junction", help="The 6_Junction file from the imgt output") | |
19 parser.add_argument("--output", help="Output file") | |
20 | |
21 args = parser.parse_args() | |
22 | |
23 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'] | |
24 old_sequence_columns = [u'CDR1-IMGT', u'CDR2-IMGT', u'CDR3-IMGT'] | |
25 old_junction_columns = [u'JUNCTION'] | |
26 | |
27 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'] | |
28 added_sequence_columns = [u'FR1-IMGT', u'FR2-IMGT', u'FR3-IMGT', u'CDR3-IMGT', u'JUNCTION', u'J-REGION', u'FR4-IMGT'] | |
29 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"] | |
30 | |
31 outFile = args.output | |
32 | |
33 #fSummary = pd.read_csv(triplets[0][0], sep="\t", low_memory=False) | |
34 fSummary = pd.read_csv(args.summ, sep="\t", dtype=object) | |
35 #fSequence = pd.read_csv(triplets[0][1], sep="\t", low_memory=False) | |
36 fSequence = pd.read_csv(args.aa, sep="\t", dtype=object) | |
37 #fJunction = pd.read_csv(triplets[0][2], sep="\t", low_memory=False) | |
38 fJunction = pd.read_csv(args.junction, sep="\t", dtype=object) | |
39 tmp = fSummary[["Sequence ID", "JUNCTION frame", "V-GENE and allele", "D-GENE and allele", "J-GENE and allele"]] | |
40 | |
41 tmp["CDR1 Seq"] = fSequence["CDR1-IMGT"] | |
42 tmp["CDR1 Length"] = fSummary["CDR1-IMGT length"] | |
43 | |
44 tmp["CDR2 Seq"] = fSequence["CDR2-IMGT"] | |
45 tmp["CDR2 Length"] = fSummary["CDR2-IMGT length"] | |
46 | |
47 tmp["CDR3 Seq"] = fSequence["CDR3-IMGT"] | |
48 tmp["CDR3 Length"] = fSummary["CDR3-IMGT length"] | |
49 | |
50 tmp["CDR3 Seq DNA"] = fJunction["JUNCTION"] | |
51 tmp["CDR3 Length DNA"] = '1' | |
52 tmp["Strand"] = fSummary["Orientation"] | |
53 tmp["CDR3 Found How"] = 'a' | |
54 | |
55 for col in added_summary_columns: | |
56 tmp[col] = fSummary[col] | |
57 | |
58 for col in added_sequence_columns: | |
59 tmp[col] = fSequence[col] | |
60 | |
61 for col in added_junction_columns: | |
62 tmp[col] = fJunction[col] | |
63 | |
64 outFrame = tmp | |
65 | |
66 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'] | |
67 | |
68 """ | |
69 IGHV[0-9]-[0-9ab]+-?[0-9]?D? | |
70 TRBV[0-9]{1,2}-?[0-9]?-?[123]? | |
71 IGKV[0-3]D?-[0-9]{1,2} | |
72 IGLV[0-9]-[0-9]{1,2} | |
73 TRAV[0-9]{1,2}(-[1-46])?(/DV[45678])? | |
74 TRGV[234589] | |
75 TRDV[1-3] | |
76 | |
77 IGHD[0-9]-[0-9ab]+ | |
78 TRBD[12] | |
79 TRDD[1-3] | |
80 | |
81 IGHJ[1-6] | |
82 TRBJ[12]-[1-7] | |
83 IGKJ[1-5] | |
84 IGLJ[12367] | |
85 TRAJ[0-9]{1,2} | |
86 TRGJP?[12] | |
87 TRDJ[1-4] | |
88 """ | |
89 | |
90 vPattern = [r"(IGHV[0-9]-[0-9ab]+-?[0-9]?D?)", | |
91 r"(TRBV[0-9]{1,2}-?[0-9]?-?[123]?)", | |
92 r"(IGKV[0-3]D?-[0-9]{1,2})", | |
93 r"(IGLV[0-9]-[0-9]{1,2})", | |
94 r"(TRAV[0-9]{1,2}(-[1-46])?(/DV[45678])?)", | |
95 r"(TRGV[234589])", | |
96 r"(TRDV[1-3])"] | |
97 | |
98 dPattern = [r"(IGHD[0-9]-[0-9ab]+)", | |
99 r"(TRBD[12])", | |
100 r"(TRDD[1-3])"] | |
101 | |
102 jPattern = [r"(IGHJ[1-6])", | |
103 r"(TRBJ[12]-[1-7])", | |
104 r"(IGKJ[1-5])", | |
105 r"(IGLJ[12367])", | |
106 r"(TRAJ[0-9]{1,2})", | |
107 r"(TRGJP?[12])", | |
108 r"(TRDJ[1-4])"] | |
109 | |
110 vPattern = re.compile(r"|".join(vPattern)) | |
111 | |
112 dPattern = re.compile(r"|".join(dPattern)) | |
113 | |
114 jPattern = re.compile(r"|".join(jPattern)) | |
115 | |
116 | |
117 def filterGenes(s, pattern): | |
118 if type(s) is not str: | |
119 return "NA" | |
120 res = pattern.search(s) | |
121 if res: | |
122 return res.group(0) | |
123 return "NA" | |
124 | |
125 | |
126 | |
127 outFrame["Top V Gene"] = outFrame["Top V Gene"].apply(lambda x: filterGenes(x, vPattern)) | |
128 outFrame["Top D Gene"] = outFrame["Top D Gene"].apply(lambda x: filterGenes(x, dPattern)) | |
129 outFrame["Top J Gene"] = outFrame["Top J Gene"].apply(lambda x: filterGenes(x, jPattern)) | |
130 | |
131 | |
132 tmp = outFrame["VDJ Frame"] | |
133 tmp = tmp.replace("in-frame", "In-frame") | |
134 tmp = tmp.replace("null", "Out-of-frame") | |
135 tmp = tmp.replace("out-of-frame", "Out-of-frame") | |
136 outFrame["VDJ Frame"] = tmp | |
137 outFrame["CDR3 Length DNA"] = outFrame["CDR3 Seq DNA"].map(str).map(len) | |
138 safeLength = lambda x: len(x) if type(x) == str else 0 | |
139 #outFrame = outFrame[(outFrame["CDR3 Seq DNA"].map(safeLength) > 0) & (outFrame["Top V Gene"] != "NA") & (outFrame["Top J Gene"] != "NA")] #filter out weird rows? | |
140 #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? | |
141 outFrame.to_csv(outFile, sep="\t", index=False, index_label="index") |