annotate CreateGermlines.py @ 0:183edf446dcf draft default tip

Uploaded
author davidvanzessen
date Mon, 17 Jul 2017 07:44:27 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
1 #!/usr/bin/env python3
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
2 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
3 Reconstructs germline sequences from alignment data
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
4 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
5 # Info
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
6 __author__ = 'Namita Gupta, Jason Anthony Vander Heiden'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
7 from changeo import __version__, __date__
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
8
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
9 # Imports
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
10 import os
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
11 import sys
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
12 from argparse import ArgumentParser
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
13 from collections import OrderedDict
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
14 from textwrap import dedent
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
15 from time import time
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
16
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
17 # Presto and change imports
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
18 from presto.Defaults import default_out_args
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
19 from presto.IO import getOutputHandle, printLog, printProgress
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
20 from changeo.Commandline import CommonHelpFormatter, checkArgs, getCommonArgParser, parseCommonArgs
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
21 from changeo.IO import getDbWriter, readDbFile, countDbFile, readRepo
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
22 from changeo.Receptor import allele_regex, parseAllele
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
23
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
24 # Defaults
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
25 default_germ_types = 'dmask'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
26 default_v_field = 'V_CALL'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
27 default_seq_field = 'SEQUENCE_IMGT'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
28
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
29
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
30 def joinGermline(align, repo_dict, germ_types, v_field, seq_field):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
31 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
32 Join gapped germline sequences aligned with sample sequences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
33
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
34 Arguments:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
35 align = iterable yielding dictionaries of sample sequence data
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
36 repo_dict = dictionary of IMGT gapped germline sequences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
37 germ_types = types of germline sequences to be output
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
38 (full germline, D-region masked, only V-region germline)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
39 v_field = field in which to look for V call
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
40 seq_field = field in which to look for sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
41
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
42 Returns:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
43 dictionary of germline_type: germline_sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
44 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
45 j_field = 'J_CALL'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
46 germlines = {'full': '', 'dmask': '', 'vonly': '', 'regions': ''}
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
47 result_log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
48 result_log['ID'] = align['SEQUENCE_ID']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
49
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
50 # Find germline V-region gene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
51 if v_field == 'V_CALL_GENOTYPED':
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
52 vgene = parseAllele(align[v_field], allele_regex, 'list')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
53 vkey = vgene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
54 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
55 vgene = parseAllele(align[v_field], allele_regex, 'first')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
56 vkey = (vgene, )
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
57
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
58 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
59 int(align['P3V_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
60 int(align['N1_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
61 int(align['P5D_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
62 int(align['P3D_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
63 int(align['N2_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
64 int(align['P5J_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
65 except:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
66 regions_style = 'IgBLAST'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
67 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
68 regions_style = 'IMGT'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
69
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
70 # Build V-region germline
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
71 if vgene is not None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
72 result_log['V_CALL'] = ','.join(vkey)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
73 if vkey in repo_dict:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
74 vseq = repo_dict[vkey]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
75 # Germline start
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
76 try: vstart = int(align['V_GERM_START_IMGT']) - 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
77 except (TypeError, ValueError): vstart = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
78 # Germline length
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
79 try: vlen = int(align['V_GERM_LENGTH_IMGT'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
80 except (TypeError, ValueError): vlen = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
81 # TODO: not sure what this line is doing here. it no make no sense.
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
82 vpad = vlen - len(vseq[vstart:])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
83 if vpad < 0: vpad = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
84 germ_vseq = vseq[vstart:(vstart + vlen)] + ('N' * vpad)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
85 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
86 result_log['ERROR'] = 'Germline %s not in repertoire' % ','.join(vkey)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
87 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
88 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
89 result_log['V_CALL'] = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
90 try: vlen = int(align['V_GERM_LENGTH_IMGT'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
91 except (TypeError, ValueError): vlen = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
92 germ_vseq = 'N' * vlen
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
93
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
94 # Find germline D-region gene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
95 dgene = parseAllele(align['D_CALL'], allele_regex, 'first')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
96
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
97 # Build D-region germline
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
98 if dgene is not None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
99 result_log['D_CALL'] = dgene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
100 dkey = (dgene, )
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
101 if dkey in repo_dict:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
102 dseq = repo_dict[dkey]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
103 # Germline start
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
104 try: dstart = int(align['D_GERM_START']) - 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
105 except (TypeError, ValueError): dstart = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
106 # Germline length
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
107 try: dlen = int(align['D_GERM_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
108 except (TypeError, ValueError): dlen = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
109 germ_dseq = repo_dict[dkey][dstart:(dstart + dlen)]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
110 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
111 result_log['ERROR'] = 'Germline %s not in repertoire' % dgene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
112 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
113 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
114 result_log['D_CALL'] = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
115 germ_dseq = ''
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
116
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
117 # Find germline J-region gene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
118 jgene = parseAllele(align[j_field], allele_regex, 'first')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
119
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
120 # Build D-region germline
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
121 if jgene is not None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
122 result_log['J_CALL'] = jgene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
123 jkey = (jgene, )
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
124 if jkey in repo_dict:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
125 jseq = repo_dict[jkey]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
126 # Germline start
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
127 try: jstart = int(align['J_GERM_START']) - 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
128 except (TypeError, ValueError): jstart = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
129 # Germline length
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
130 try: jlen = int(align['J_GERM_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
131 except (TypeError, ValueError): jlen = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
132 # TODO: not sure what this line is doing either
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
133 jpad = jlen - len(jseq[jstart:])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
134 if jpad < 0: jpad = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
135 germ_jseq = jseq[jstart:(jstart + jlen)] + ('N' * jpad)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
136 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
137 result_log['ERROR'] = 'Germline %s not in repertoire' % jgene
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
138 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
139 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
140 result_log['J_CALL'] = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
141 try: jlen = int(align['J_GERM_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
142 except (TypeError, ValueError): jlen = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
143 germ_jseq = 'N' * jlen
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
144
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
145 # Assemble pieces starting with V-region
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
146 germ_seq = germ_vseq
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
147 regions = 'V' * len(germ_vseq)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
148
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
149 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
150 np1_len = int(align['NP1_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
151 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
152 np1_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
153
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
154 # NP nucleotide additions after V
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
155 if regions_style == 'IMGT':
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
156 # P nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
157 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
158 p3v_len = int(align['P3V_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
159 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
160 p3v_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
161 if p3v_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
162 result_log['ERROR'] = 'P3V_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
163 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
164
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
165 regions += 'P' * p3v_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
166
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
167 # N1 nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
168 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
169 n1_len = int(align['N1_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
170 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
171 n1_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
172 if n1_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
173 result_log['ERROR'] = 'N1_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
174 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
175
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
176 regions += 'N' * n1_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
177
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
178 # P nucleotide additions before D
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
179 try: p5d_len = int(align['P5D_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
180 except (TypeError, ValueError): p5d_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
181 if p5d_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
182 result_log['ERROR'] = 'P5D_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
183 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
184
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
185 regions += 'P' * p5d_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
186 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
187 # IgBLAST style
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
188 # PNP nucleotide additions after V
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
189 if np1_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
190 result_log['ERROR'] = 'NP1_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
191 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
192
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
193 regions += 'N' * np1_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
194
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
195 germ_seq += 'N' * np1_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
196
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
197 # Add D-region
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
198 germ_seq += germ_dseq
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
199 regions += 'D' * len(germ_dseq)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
200
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
201 #print 'VD>', germ_seq, '\nVD>', regions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
202
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
203 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
204 np2_len = int(align['NP2_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
205 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
206 np2_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
207
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
208 # NP nucleotide additions before J
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
209 if regions_style == 'IMGT':
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
210 # P nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
211 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
212 p3d_len = int(align['P3D_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
213 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
214 p3d_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
215 if p3d_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
216 result_log['ERROR'] = 'P3D_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
217 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
218
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
219 regions += 'P' * p3d_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
220
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
221 # N2 nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
222 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
223 n2_len = int(align['N2_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
224 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
225 n2_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
226 if n2_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
227 result_log['ERROR'] = 'N2_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
228 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
229
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
230 regions += 'N' * n2_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
231
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
232 # P nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
233 try:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
234 p5j_len = int(align['P5J_LENGTH'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
235 except (TypeError, ValueError):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
236 p5j_len = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
237 if p5j_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
238 result_log['ERROR'] = 'P5J_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
239 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
240
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
241 regions += 'P' * p5j_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
242 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
243 # IgBLAST style
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
244 # NP nucleotide additions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
245 if np2_len < 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
246 result_log['ERROR'] = 'NP2_LENGTH is negative'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
247 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
248
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
249 regions += 'N' * np2_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
250
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
251 germ_seq += 'N' * np2_len
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
252
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
253 # Add J-region
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
254 germ_seq += germ_jseq
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
255 regions += 'J' * len(germ_jseq)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
256
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
257 #print('\nREGIONS>',regions,'\n')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
258
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
259 # Define return germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
260 germlines['full'] = germ_seq
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
261 germlines['regions'] = regions
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
262
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
263 if 'dmask' in germ_types:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
264 germlines['dmask'] = germ_seq[:len(germ_vseq)] + \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
265 'N' * (len(germ_seq) - len(germ_vseq) - len(germ_jseq)) + \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
266 germ_seq[-len(germ_jseq):]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
267 if 'vonly' in germ_types:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
268 germlines['vonly'] = germ_vseq
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
269
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
270 # Check that input and germline sequence match
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
271 if len(align[seq_field]) == 0:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
272 result_log['ERROR'] = 'Sequence is missing from %s column' % seq_field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
273 elif len(germlines['full']) != len(align[seq_field]):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
274 result_log['ERROR'] = 'Germline sequence is %d nucleotides longer than input sequence' % \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
275 (len(germlines['full']) - len(align[seq_field]))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
276
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
277 # Convert to uppercase
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
278 for k, v in germlines.items(): germlines[k] = v.upper()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
279
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
280 return result_log, germlines
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
281
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
282
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
283 def assembleEachGermline(db_file, repo, germ_types, v_field, seq_field, out_args=default_out_args):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
284 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
285 Write germline sequences to tab-delimited database file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
286
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
287 Arguments:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
288 db_file = input tab-delimited database file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
289 repo = folder with germline repertoire files
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
290 germ_types = types of germline sequences to be output
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
291 (full germline, D-region masked, only V-region germline)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
292 v_field = field in which to look for V call
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
293 seq_field = field in which to look for sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
294 out_args = arguments for output preferences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
295
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
296 Returns:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
297 None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
298 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
299 # Print parameter info
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
300 log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
301 log['START'] = 'CreateGermlines'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
302 log['DB_FILE'] = os.path.basename(db_file)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
303 log['GERM_TYPES'] = germ_types if isinstance(germ_types, str) else ','.join(germ_types)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
304 log['CLONED'] = 'False'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
305 log['V_FIELD'] = v_field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
306 log['SEQ_FIELD'] = seq_field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
307 printLog(log)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
308
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
309 # Get repertoire and open Db reader
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
310 repo_dict = readRepo(repo)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
311 reader = readDbFile(db_file, ig=False)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
312
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
313 # Exit if V call field does not exist in reader
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
314 if v_field not in reader.fieldnames:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
315 sys.exit('Error: V field does not exist in input database file.')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
316
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
317 # Define log handle
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
318 if out_args['log_file'] is None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
319 log_handle = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
320 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
321 log_handle = open(out_args['log_file'], 'w')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
322
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
323 add_fields = []
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
324 seq_type = seq_field.split('_')[-1]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
325 if 'full' in germ_types: add_fields += ['GERMLINE_' + seq_type]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
326 if 'dmask' in germ_types: add_fields += ['GERMLINE_' + seq_type + '_D_MASK']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
327 if 'vonly' in germ_types: add_fields += ['GERMLINE_' + seq_type + '_V_REGION']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
328 if 'regions' in germ_types: add_fields += ['GERMLINE_REGIONS']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
329
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
330 # Create output file handle and Db writer
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
331 pass_handle = getOutputHandle(db_file, 'germ-pass',
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
332 out_dir=out_args['out_dir'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
333 out_name=out_args['out_name'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
334 out_type=out_args['out_type'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
335 pass_writer = getDbWriter(pass_handle, db_file, add_fields=add_fields)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
336
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
337 if out_args['failed']:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
338 fail_handle = getOutputHandle(db_file, 'germ-fail',
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
339 out_dir=out_args['out_dir'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
340 out_name=out_args['out_name'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
341 out_type=out_args['out_type'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
342 fail_writer = getDbWriter(fail_handle, db_file, add_fields=add_fields)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
343 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
344 fail_handle = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
345 fail_writer = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
346
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
347 # Initialize time and total count for progress bar
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
348 start_time = time()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
349 rec_count = countDbFile(db_file)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
350 pass_count = fail_count = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
351 # Iterate over rows
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
352 for i, row in enumerate(reader):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
353 # Print progress
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
354 printProgress(i, rec_count, 0.05, start_time)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
355
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
356 result_log, germlines = joinGermline(row, repo_dict, germ_types, v_field, seq_field)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
357
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
358 # Add germline field(s) to dictionary
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
359 if 'full' in germ_types: row['GERMLINE_' + seq_type] = germlines['full']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
360 if 'dmask' in germ_types: row['GERMLINE_' + seq_type + '_D_MASK'] = germlines['dmask']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
361 if 'vonly' in germ_types: row['GERMLINE_' + seq_type + '_V_REGION'] = germlines['vonly']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
362 if 'regions' in germ_types: row['GERMLINE_REGIONS'] = germlines['regions']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
363
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
364 # Write row to pass or fail file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
365 if 'ERROR' in result_log:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
366 fail_count += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
367 if fail_writer is not None: fail_writer.writerow(row)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
368 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
369 result_log['SEQUENCE'] = row[seq_field]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
370 result_log['GERMLINE'] = germlines['full']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
371 result_log['REGIONS'] = germlines['regions']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
372
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
373 pass_count += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
374 pass_writer.writerow(row)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
375 printLog(result_log, handle=log_handle)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
376
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
377 # Print log
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
378 printProgress(i + 1, rec_count, 0.05, start_time)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
379 log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
380 log['OUTPUT'] = os.path.basename(pass_handle.name)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
381 log['RECORDS'] = rec_count
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
382 log['PASS'] = pass_count
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
383 log['FAIL'] = fail_count
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
384 log['END'] = 'CreateGermlines'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
385 printLog(log)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
386
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
387 # Close file handles
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
388 pass_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
389 if fail_handle is not None: fail_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
390 if log_handle is not None: log_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
391
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
392
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
393 def makeCloneGermline(clone, clone_dict, repo_dict, germ_types, v_field,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
394 seq_field, counts, writers, out_args):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
395 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
396 Determine consensus clone sequence and create germline for clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
397
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
398 Arguments:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
399 clone = clone ID
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
400 clone_dict = iterable yielding dictionaries of sequence data from clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
401 repo_dict = dictionary of IMGT gapped germline sequences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
402 germ_types = types of germline sequences to be output
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
403 (full germline, D-region masked, only V-region germline)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
404 v_field = field in which to look for V call
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
405 seq_field = field in which to look for sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
406 counts = dictionary of pass counter and fail counter
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
407 writers = dictionary with pass and fail DB writers
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
408 out_args = arguments for output preferences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
409
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
410 Returns:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
411 None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
412 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
413 seq_type = seq_field.split('_')[-1]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
414 j_field = 'J_CALL'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
415
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
416 # Create dictionaries to count observed V/J calls
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
417 v_dict = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
418 j_dict = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
419
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
420 # Find longest sequence in clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
421 max_length = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
422 for val in clone_dict.values():
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
423 v = val[v_field]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
424 v_dict[v] = v_dict.get(v, 0) + 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
425 j = val[j_field]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
426 j_dict[j] = j_dict.get(j, 0) + 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
427 if len(val[seq_field]) > max_length:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
428 max_length = len(val[seq_field])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
429
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
430 # Consensus V and J having most observations
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
431 v_cons = [k for k in list(v_dict.keys()) if v_dict[k] == max(v_dict.values())]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
432 j_cons = [k for k in list(j_dict.keys()) if j_dict[k] == max(j_dict.values())]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
433
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
434 # Consensus sequence(s) with consensus V/J calls and longest sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
435 cons = [val for val in list(clone_dict.values()) \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
436 if val.get(v_field, '') in v_cons and \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
437 val.get(j_field, '') in j_cons and \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
438 len(val[seq_field]) == max_length]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
439
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
440 # Sequence(s) with consensus V/J are not longest
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
441 if not cons:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
442 # Sequence(s) with consensus V/J (not longest)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
443 cons = [val for val in list(clone_dict.values()) \
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
444 if val.get(v_field, '') in v_cons and val.get(j_field, '') in j_cons]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
445
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
446 # No sequence has both consensus V and J call
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
447 if not cons:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
448 result_log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
449 result_log['ID'] = clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
450 result_log['V_CALL'] = ','.join(v_cons)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
451 result_log['J_CALL'] = ','.join(j_cons)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
452 result_log['ERROR'] = 'No consensus sequence for clone found'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
453 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
454 # Pad end of consensus sequence with gaps to make it the max length
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
455 cons = cons[0]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
456 cons['J_GERM_LENGTH'] = str(int(cons['J_GERM_LENGTH'] or 0) + max_length - len(cons[seq_field]))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
457 cons[seq_field] += '.' * (max_length - len(cons[seq_field]))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
458 result_log, germlines = joinGermline(cons, repo_dict, germ_types, v_field, seq_field)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
459 result_log['ID'] = clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
460 result_log['CONSENSUS'] = cons['SEQUENCE_ID']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
461 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
462 cons = cons[0]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
463 result_log, germlines = joinGermline(cons, repo_dict, germ_types, v_field, seq_field)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
464 result_log['ID'] = clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
465 result_log['CONSENSUS'] = cons['SEQUENCE_ID']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
466
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
467 # Write sequences of clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
468 for val in clone_dict.values():
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
469 if 'ERROR' not in result_log:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
470 # Update lengths padded to longest sequence in clone
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
471 val['J_GERM_LENGTH'] = str(int(val['J_GERM_LENGTH'] or 0) + max_length - len(val[seq_field]))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
472 val[seq_field] += '.' * (max_length - len(val[seq_field]))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
473
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
474 # Add column(s) to tab-delimited database file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
475 if 'full' in germ_types: val['GERMLINE_' + seq_type] = germlines['full']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
476 if 'dmask' in germ_types: val['GERMLINE_' + seq_type + '_D_MASK'] = germlines['dmask']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
477 if 'vonly' in germ_types: val['GERMLINE_' + seq_type + '_V_REGION'] = germlines['vonly']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
478 if 'regions' in germ_types: val['GERMLINE_REGIONS'] = germlines['regions']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
479
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
480 # Add field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
481 val['GERMLINE_V_CALL'] = result_log['V_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
482 val['GERMLINE_D_CALL'] = result_log['D_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
483 val['GERMLINE_J_CALL'] = result_log['J_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
484
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
485 result_log['SEQUENCE'] = cons[seq_field]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
486 result_log['GERMLINE'] = germlines['full']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
487 result_log['REGIONS'] = germlines['regions']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
488
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
489 # Write to pass file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
490 counts['pass'] += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
491 writers['pass'].writerow(val)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
492 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
493 # Write to fail file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
494 counts['fail'] += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
495 if writers['fail'] is not None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
496 writers['fail'].writerow(val)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
497 # Return log
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
498 return result_log
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
499
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
500
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
501 def assembleCloneGermline(db_file, repo, germ_types, v_field, seq_field, out_args=default_out_args):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
502 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
503 Assemble one germline sequence for each clone in a tab-delimited database file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
504
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
505 Arguments:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
506 db_file = input tab-delimited database file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
507 repo = folder with germline repertoire files
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
508 germ_types = types of germline sequences to be output
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
509 (full germline, D-region masked, only V-region germline)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
510 v_field = field in which to look for V call
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
511 seq_field = field in which to look for sequence
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
512 out_args = arguments for output preferences
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
513
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
514 Returns:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
515 None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
516 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
517 # Print parameter info
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
518 log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
519 log['START'] = 'CreateGermlines'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
520 log['DB_FILE'] = os.path.basename(db_file)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
521 log['GERM_TYPES'] = germ_types if isinstance(germ_types, str) else ','.join(germ_types)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
522 log['CLONED'] = 'True'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
523 log['V_FIELD'] = v_field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
524 log['SEQ_FIELD'] = seq_field
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
525 printLog(log)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
526
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
527 # Get repertoire and open Db reader
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
528 repo_dict = readRepo(repo)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
529 reader = readDbFile(db_file, ig=False)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
530
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
531 # Exit if V call field does not exist in reader
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
532 if v_field not in reader.fieldnames:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
533 sys.exit('Error: V field does not exist in input database file.')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
534
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
535 # Define log handle
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
536 if out_args['log_file'] is None:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
537 log_handle = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
538 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
539 log_handle = open(out_args['log_file'], 'w')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
540
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
541 add_fields = []
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
542 seq_type = seq_field.split('_')[-1]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
543 if 'full' in germ_types: add_fields += ['GERMLINE_' + seq_type]
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
544 if 'dmask' in germ_types: add_fields += ['GERMLINE_' + seq_type + '_D_MASK']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
545 if 'vonly' in germ_types: add_fields += ['GERMLINE_' + seq_type + '_V_REGION']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
546 if 'regions' in germ_types: add_fields += ['GERMLINE_REGIONS']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
547
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
548 add_fields += ['GERMLINE_V_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
549 add_fields += ['GERMLINE_D_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
550 add_fields += ['GERMLINE_J_CALL']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
551
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
552 # Create output file handle and Db writer
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
553 writers = {}
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
554 pass_handle = getOutputHandle(db_file, 'germ-pass', out_dir=out_args['out_dir'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
555 out_name=out_args['out_name'], out_type=out_args['out_type'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
556 writers['pass'] = getDbWriter(pass_handle, db_file, add_fields=add_fields)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
557
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
558 if out_args['failed']:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
559 fail_handle = getOutputHandle(db_file, 'germ-fail', out_dir=out_args['out_dir'],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
560 out_name=out_args['out_name'], out_type=out_args['out_type'])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
561 writers['fail'] = getDbWriter(fail_handle, db_file, add_fields=add_fields)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
562 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
563 fail_handle = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
564 writers['fail'] = None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
565
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
566 # Initialize time and total count for progress bar
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
567 start_time = time()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
568 rec_count = countDbFile(db_file)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
569 counts = {}
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
570 clone_count = counts['pass'] = counts['fail'] = 0
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
571 # Iterate over rows
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
572 clone = 'initial'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
573 clone_dict = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
574 for i, row in enumerate(reader):
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
575 # Print progress
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
576 printProgress(i, rec_count, 0.05, start_time)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
577
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
578 # Clone isn't over yet
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
579 if row.get('CLONE', '') == clone:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
580 clone_dict[i] = row
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
581 # Clone just finished
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
582 elif clone_dict:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
583 clone_count += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
584 result_log = makeCloneGermline(clone, clone_dict, repo_dict, germ_types,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
585 v_field, seq_field, counts, writers, out_args)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
586 printLog(result_log, handle=log_handle)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
587 # Now deal with current row (first of next clone)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
588 clone = row['CLONE']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
589 clone_dict = OrderedDict([(i, row)])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
590 # Last case is only for first row of file
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
591 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
592 clone = row['CLONE']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
593 clone_dict = OrderedDict([(i, row)])
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
594
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
595 clone_count += 1
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
596 result_log = makeCloneGermline(clone, clone_dict, repo_dict, germ_types, v_field,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
597 seq_field, counts, writers, out_args)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
598 printLog(result_log, handle=log_handle)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
599
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
600 # Print log
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
601 printProgress(i + 1, rec_count, 0.05, start_time)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
602 log = OrderedDict()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
603 log['OUTPUT'] = os.path.basename(pass_handle.name)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
604 log['CLONES'] = clone_count
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
605 log['RECORDS'] = rec_count
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
606 log['PASS'] = counts['pass']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
607 log['FAIL'] = counts['fail']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
608 log['END'] = 'CreateGermlines'
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
609 printLog(log)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
610
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
611 # Close file handles
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
612 pass_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
613 if fail_handle is not None: fail_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
614 if log_handle is not None: log_handle.close()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
615
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
616
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
617 def getArgParser():
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
618 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
619 Defines the ArgumentParser
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
620
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
621 Arguments:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
622 None
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
623
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
624 Returns:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
625 an ArgumentParser object
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
626 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
627 # Define input and output field help message
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
628 fields = dedent(
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
629 '''
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
630 output files:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
631 germ-pass
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
632 database with assigned germline sequences.
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
633 germ-fail
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
634 database with records failing germline assignment.
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
635
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
636 required fields:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
637 SEQUENCE_ID, SEQUENCE_VDJ or SEQUENCE_IMGT,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
638 V_CALL or V_CALL_GENOTYPED, D_CALL, J_CALL,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
639 V_SEQ_START, V_SEQ_LENGTH, V_GERM_START_IMGT, V_GERM_LENGTH_IMGT,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
640 D_SEQ_START, D_SEQ_LENGTH, D_GERM_START, D_GERM_LENGTH,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
641 J_SEQ_START, J_SEQ_LENGTH, J_GERM_START, J_GERM_LENGTH,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
642 NP1_LENGTH, NP2_LENGTH
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
643
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
644 optional fields:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
645 N1_LENGTH, N2_LENGTH, P3V_LENGTH, P5D_LENGTH, P3D_LENGTH, P5J_LENGTH,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
646 CLONE
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
647
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
648
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
649 output fields:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
650 GERMLINE_VDJ, GERMLINE_VDJ_D_MASK, GERMLINE_VDJ_V_REGION,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
651 GERMLINE_IMGT, GERMLINE_IMGT_D_MASK, GERMLINE_IMGT_V_REGION,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
652 GERMLINE_V_CALL, GERMLINE_D_CALL, GERMLINE_J_CALL,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
653 GERMLINE_REGIONS
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
654 ''')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
655
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
656 # Parent parser
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
657 parser_parent = getCommonArgParser(seq_in=False, seq_out=False, db_in=True,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
658 annotation=False)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
659 # Define argument parser
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
660 parser = ArgumentParser(description=__doc__, epilog=fields,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
661 parents=[parser_parent],
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
662 formatter_class=CommonHelpFormatter)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
663 parser.add_argument('--version', action='version',
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
664 version='%(prog)s:' + ' %s-%s' %(__version__, __date__))
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
665
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
666 parser.add_argument('-r', nargs='+', action='store', dest='repo', required=True,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
667 help='''List of folders and/or fasta files (with .fasta, .fna or .fa
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
668 extension) with germline sequences.''')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
669 parser.add_argument('-g', action='store', dest='germ_types', default=default_germ_types,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
670 nargs='+', choices=('full', 'dmask', 'vonly', 'regions'),
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
671 help='''Specify type(s) of germlines to include full germline,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
672 germline with D-region masked, or germline for V region only.''')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
673 parser.add_argument('--cloned', action='store_true', dest='cloned',
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
674 help='''Specify to create only one germline per clone. Assumes input file is
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
675 sorted by clone column, and will not yield correct results if the data
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
676 is unsorted. Note, if allele calls are ambiguous within a clonal group,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
677 this will place the germline call used for the entire clone within the
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
678 GERMLINE_V_CALL, GERMLINE_D_CALL and GERMLINE_J_CALL fields.''')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
679 parser.add_argument('--vf', action='store', dest='v_field', default=default_v_field,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
680 help='Specify field to use for germline V call')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
681 parser.add_argument('--sf', action='store', dest='seq_field', default=default_seq_field,
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
682 help='Specify field to use for sequence')
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
683
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
684 return parser
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
685
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
686
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
687 if __name__ == "__main__":
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
688 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
689 Parses command line arguments and calls main
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
690 """
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
691
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
692 # Parse command line arguments
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
693 parser = getArgParser()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
694 checkArgs(parser)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
695 args = parser.parse_args()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
696 args_dict = parseCommonArgs(args)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
697 del args_dict['db_files']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
698 del args_dict['cloned']
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
699 args_dict['v_field'] = args_dict['v_field'].upper()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
700 args_dict['seq_field'] = args_dict['seq_field'].upper()
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
701
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
702 for f in args.__dict__['db_files']:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
703 args_dict['db_file'] = f
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
704 if args.__dict__['cloned']:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
705 assembleCloneGermline(**args_dict)
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
706 else:
183edf446dcf Uploaded
davidvanzessen
parents:
diff changeset
707 assembleEachGermline(**args_dict)