如何去除SNP rsid名称中的芯片分析冗余信息(Plink二进制格式)
Got it, let's fix those messy SNP IDs in your Plink binary dataset. Here are two solid approaches to strip away the chip-specific prefixes (like GSA-, psy-) and suffixes (like _CNV_SULT1A3) and get back clean rsNUMBER format IDs:
Method 1: Use Plink's Built-in Command (Recommended, Safer)
Plink 1.9+ has a powerful --set-all-var-ids flag that lets you use regex substitutions to rewrite SNP IDs. This method ensures you don't mess up the structure of your binary files (.bed/.bim/.fam), which is crucial for downstream analyses.
Run this command, replacing your_dataset with your actual file prefix and cleaned_dataset with your desired output prefix:
plink --bfile your_dataset --set-all-var-ids '@{s/.*(rs\d+).*/$1/}' --make-bed --out cleaned_dataset
What this does:
- The regex
.*(rs\d+).*matches any string that containsrsfollowed by numbers, capturing just thersNUMBERpart. - The substitution replaces the full messy ID with the captured
rsNUMBERsegment. --make-bedgenerates a new set of clean binary files, leaving your original data untouched (always a good practice!).
Method 2: Directly Edit the .bim File (Fast, Manual Control)
Since the .bim file is plain text (each line's second column is the SNP ID), you can use a command-line text editor like sed to batch-clean the IDs in place.
First, backup your original .bim file (critical in case of typos!):
cp your_dataset.bim your_dataset.bim.backup
Then run this sed command to clean the IDs:
sed -i 's/.*\(rs[0-9]\+\).*/\1/' your_dataset.bim
What this does:
-itellssedto edit the file in place.- The regex targets any string containing
rsfollowed by digits, replacing the entire ID with just thersNUMBERportion. - This is faster than re-generating the binary files, but only modify the .bim file—never touch the .bed or .fam files directly!
Quick Validation
After either method, check a few lines of the cleaned .bim file to confirm the IDs are fixed:
head cleaned_dataset.bim # For Method 1 # OR head your_dataset.bim # For Method 2
You should see only rsNUMBER entries in the second column now.
内容的提问来源于stack exchange,提问作者Inken

