如何在DataFrame中拆分基因变异字符串,提取位置与变异类型
Split Variant Data into Position and Mutation Type
Hey Justin, let's figure out how to split those variant strings into the two columns you need. First, let's break down the structure of your entries:
- Most start with a reference ID like
NC_000001.10:g., followed by a numeric position, then a mutation likeG>C - One entry uses
m.instead ofg., so we'll make sure our solution handles both cases.
The Right Regular Expression
Here's a regex that will reliably capture both the position and mutation type for all your entries:
.*\.(g|m)\.(\d+)([A-Z]>[A-Z])
Let's break down what each part does:
.*\.(g|m)\.: Matches everything up to (and including) theg.orm.prefix. The\.escapes the literal dot (since dots in regex match any character by default).(\d+): Captures one or more digits — this is your position number.([A-Z]>[A-Z]): Captures the mutation pattern (e.g.,G>C,A>G) by matching an uppercase letter, followed by>, followed by another uppercase letter.
Example Implementation (Python)
If you're working with Python, here's how you can use this regex to process your dataset:
import re # Your sample data variant_list = [ "NC_000001.10:g.955563G>C", "NC_000001.10:g.955597G>T", "NC_000001.10:g.955619G>C", "NC_000001.10:g.957640C>T", "NC_000001.10:g.976059C>T", "NC_000003.11:g.37090470C>T", "NC_000012.11:g.133256600G>A", "NC_012920.1:m.15923A>G" ] # Our regex pattern pattern = r".*\.(g|m)\.(\d+)([A-Z]>[A-Z])" for variant in variant_list: match = re.fullmatch(pattern, variant) if match: position = match.group(2) # Group 2 holds the numeric position mutation = match.group(3) # Group 3 holds the mutation type print(f"Position: {position}, Mutation: {mutation}")
Running this will output:
Position: 955563, Mutation: G>C Position: 955597, Mutation: G>T Position: 955619, Mutation: G>C Position: 957640, Mutation: C>T Position: 976059, Mutation: C>T Position: 37090470, Mutation: C>T Position: 133256600, Mutation: G>A Position: 15923, Mutation: A>G
For Spreadsheet Tools (Like Excel/Google Sheets)
If you're using a spreadsheet instead of code, you can use these formulas:
- Extract Position:
=MID(A1, FIND(".", A1, FIND(".", A1)+1)+1, FIND(">", A1)-FIND(".", A1, FIND(".", A1)+1)-1) - Extract Mutation Type:
=MID(A1, FIND(">", A1)-1, 3)
Just replace A1 with the cell containing your variant string.
内容的提问来源于stack exchange,提问作者Justin
相关产品推荐
相关产品推荐

