pzweuj commited on
Commit
d7b0215
·
verified ·
1 Parent(s): 937dec3

Upload bin/filter_civic_vcf.py

Browse files
Files changed (1) hide show
  1. bin/filter_civic_vcf.py +198 -0
bin/filter_civic_vcf.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ 过滤CIViC VCF文件,保留核心治疗相关字段和辅助参考字段
4
+
5
+ 保留的字段(基于CSQ索引):
6
+ 核心治疗相关字段:
7
+ - 26: CIViC Entity Significance (变异意义)
8
+ - 27: CIViC Entity Therapies (靶向药物)
9
+ - 28: CIViC Entity Direction (方向)
10
+ - 29: CIViC Entity Disease (关联疾病)
11
+ - 33: CIViC Evidence Level (证据等级)
12
+
13
+ 辅助参考字段:
14
+ - 34: CIViC Evidence Rating (证据评分)
15
+ - 35: CIViC Assertion ACMG Codes (ACMG代码)
16
+ - 36: CIViC Assertion AMP Category (AMP分类)
17
+ - 37: CIViC Assertion NCCN Guideline (NCCN指南)
18
+ - 38: CIViC Assertion Regulatory Approval (监管批准)
19
+ - 39: CIViC Assertion FDA Companion Test (FDA伴随诊断)
20
+ """
21
+
22
+ import re
23
+ import sys
24
+
25
+ # 新字段的描述名称(按索引顺序)- 用于键值对格式
26
+ NEW_FIELD_NAMES = [
27
+ "Significance", # 26
28
+ "Direction", # 27
29
+ "Disease", # 28
30
+ "Therapies", # 29
31
+ "Evidence_Level", # 32
32
+ "Evidence_Rating", # 33
33
+ "ACMG_Codes", # 34
34
+ "AMP_Category", # 35
35
+ "NCCN_Guideline", # 36
36
+ "Regulatory_Approval", # 37
37
+ "FDA_Companion_Test" # 38
38
+ ]
39
+
40
+ # 要保留的CSQ字段索引
41
+ # 核心治疗相关字段(索引26-29)+ 辅助参考字段(索引32-38)
42
+ KEEP_FIELD_INDICES = [26, 27, 28, 29, 32, 33, 34, 35, 36, 37, 38]
43
+
44
+ def parse_csq_line(csq_description):
45
+ """从VCF header中提取CSQ字段描述"""
46
+ # 提取Description中的字段列表
47
+ match = re.search(r'Format: (.+)"', csq_description)
48
+ if match:
49
+ fields = match.group(1).split('|')
50
+ return fields
51
+ return []
52
+
53
+ def filter_csq_record(csq_record, keep_indices, field_names):
54
+ """过滤单个CSQ记录,并转换为Key=Value格式"""
55
+ fields = csq_record.split('|')
56
+ filtered = [fields[i] for i in keep_indices if i < len(fields)]
57
+
58
+ # 转换为Key=Value格式(用分号分隔)
59
+ kv_pairs = []
60
+ for i, value in enumerate(filtered):
61
+ if value: # 只添加非空值
62
+ kv_pairs.append(f"{field_names[i]}={value}")
63
+
64
+ return ';'.join(kv_pairs) if kv_pairs else ""
65
+
66
+ def filter_csq_record(csq_record, keep_indices, field_names):
67
+ """将单个CSQ记录转换为Key=Value格式"""
68
+ fields = csq_record.split('|')
69
+ filtered = [fields[i] for i in keep_indices if i < len(fields)]
70
+
71
+ # 转换为Key=Value格式(用分号分隔)
72
+ kv_pairs = []
73
+ for i, value in enumerate(filtered):
74
+ if value: # 只添加非空值
75
+ kv_pairs.append(f"{field_names[i]}={value}")
76
+
77
+ return ';'.join(kv_pairs) if kv_pairs else ""
78
+
79
+ def merge_csq_records(records_string):
80
+ """合并多条已转换为Key=Value格式的CSQ记录,每个Key保留所有值,用|分隔"""
81
+ # 分割多条记录
82
+ records = records_string.split(',')
83
+
84
+ # 收集每个字段的所有值(不去重,按顺序保留)
85
+ field_values = {}
86
+
87
+ for record in records:
88
+ if not record:
89
+ continue
90
+ # 解析Key=Value对
91
+ pairs = record.split(';')
92
+ for pair in pairs:
93
+ if '=' in pair:
94
+ key, value = pair.split('=', 1)
95
+ if key not in field_values:
96
+ field_values[key] = []
97
+ if value:
98
+ field_values[key].append(value)
99
+
100
+ # 按原始字段顺序构建合并后的记录
101
+ merged_pairs = []
102
+ for name in NEW_FIELD_NAMES:
103
+ if name in field_values and field_values[name]:
104
+ # 多个值用|连接
105
+ merged_pairs.append(f"{name}={'|'.join(field_values[name])}")
106
+
107
+ return ';'.join(merged_pairs) if merged_pairs else ""
108
+
109
+ def modify_header(info_line, keep_indices, new_field_names):
110
+ """修改VCF header中的INFO行"""
111
+ # 找到包含CSQ的INFO行
112
+ if 'ID=CSQ' not in info_line:
113
+ return info_line
114
+
115
+ # 提取Description部分 - 更精确的匹配
116
+ match = re.search(r'(##INFO=<ID=CSQ,Number=\.,Type=String,Description=")(.+?)(">)', info_line)
117
+ if not match:
118
+ return info_line
119
+
120
+ prefix = match.group(1)
121
+ old_desc = match.group(2)
122
+ suffix = match.group(3)
123
+
124
+ # 直接使用新的字段名
125
+ new_desc = '|'.join(new_field_names)
126
+
127
+ # 替换
128
+ new_line = prefix + new_desc + suffix
129
+
130
+ return new_line
131
+
132
+ def main(input_file, output_file):
133
+ with open(input_file, 'r', encoding='utf-8') as f_in, \
134
+ open(output_file, 'w', encoding='utf-8') as f_out:
135
+
136
+ csq_fields = None
137
+
138
+ for line in f_in:
139
+ # 处理header行
140
+ if line.startswith('##'):
141
+ # 找到CSQ的INFO行并修改
142
+ if 'ID=CSQ' in line:
143
+ # 先解析字段列表
144
+ csq_match = re.search(r'Format: (.+)"', line)
145
+ if csq_match:
146
+ csq_fields = csq_match.group(1).split('|')
147
+
148
+ # 修改header
149
+ modified_line = modify_header(line.strip(), KEEP_FIELD_INDICES, NEW_FIELD_NAMES)
150
+ f_out.write(modified_line + '\n')
151
+ else:
152
+ f_out.write(line)
153
+ # 处理列头
154
+ elif line.startswith('#CHROM'):
155
+ f_out.write(line)
156
+ # 处理数据行
157
+ else:
158
+ fields = line.strip().split('\t')
159
+ if len(fields) >= 8:
160
+ info_field = fields[7]
161
+
162
+ # 查找并替换CSQ字段
163
+ csq_match = re.search(r'CSQ=([^;]+)', info_field)
164
+ if csq_match:
165
+ old_csq = csq_match.group(1)
166
+
167
+ # 处理多个CSQ记录(用逗号分隔)
168
+ csq_records = old_csq.split(',')
169
+ filtered_records = []
170
+
171
+ for csq_record in csq_records:
172
+ # 先转换为Key=Value格式
173
+ filtered = filter_csq_record(csq_record, KEEP_FIELD_INDICES, NEW_FIELD_NAMES)
174
+ if filtered:
175
+ filtered_records.append(filtered)
176
+
177
+ # 合并所有记录
178
+ merged_csq = merge_csq_records(','.join(filtered_records))
179
+
180
+ # 替换info字段中的CSQ
181
+ new_info = re.sub(r'CSQ=[^;]+', f'CSQ={merged_csq}', info_field)
182
+ fields[7] = new_info
183
+
184
+ f_out.write('\t'.join(fields) + '\n')
185
+
186
+ print(f"过滤完成!输出文件: {output_file}")
187
+ print(f"保留的字段: {', '.join(NEW_FIELD_NAMES)}")
188
+
189
+ if __name__ == '__main__':
190
+ input_vcf = '01-Mar-2026-civic_accepted.vcf'
191
+ output_vcf = 'hg19_civic.vcf'
192
+
193
+ if len(sys.argv) > 1:
194
+ input_vcf = sys.argv[1]
195
+ if len(sys.argv) > 2:
196
+ output_vcf = sys.argv[2]
197
+
198
+ main(input_vcf, output_vcf)