Or grep one-liner: grep -f file1.map file2.map | cut -f2 > common_snps.txt
Is there a quick way to combine two datasets so that only the common markers are kept? Currently, if I have two datasets, I have to first get the intersection of the two BIM/MAP files, then extract those markers for each dataset, then merge the two.
The merge-mode doesn't seem to have the option I'm looking for either
6 answers
Hi I suggest to combine R & Plink this way:
> R code:
> map2 = read.delim("file2.map", header=F, quote="")
> map1 = read.delim("file1.map", header=F, quote="")
> common.snps = which(map2$V2 %in% map1$V2)
> write.table(map2$V2[common.snps], file="list.snps", sep="\t", col.names=F, row.names=F, quote=F )
and finally
> the Plink commands:
> plink --bfile <file1> --extract list.snps --make-bed --out data1
> plink --bfile <file2> --extract list.snps --make-bed --out data2
> plink --bfile data1 --bmerge data2.bed data2.bim data2.fam --make-bed --out merge
plink --bfile file1 --bmerge file2.bed file2.bim file2.fam --extract list.snps --make-bed --out merge
I would do this in R (as you can do this for an infinite number of sets by doing an intersection of the intersection...)
common.snps <- intersect(map1$V2, map2$V2)
write.table(common.snps, file = "common_snps.txt", sep ="\t", col.names = FALSE, row.names = FALSE, quote = FALSE )
Why don't you figure out the common snps between the two datasets using a shell command (awk) or a R one-liner. You can then reduce each of those datasets to the same set of SNPS using the "--extract" option and then merge the datasets. Also, you should check if the two datasets have the same build.
--write-snplist + --extract/--make-bed lets you do this purely with plink, though the other solutions also work.
Would you illustrate with a command how --write-snplist + --extract / --make-bed would do this in PLINK2. I am merging two data sets in PLINK2 and I want to keep the common SNPs.
Why not use plink --merge-mode 1, which would output Consensus calls.
Because the resulting merged dataset will have the union of the individual datasets' markers, and the original poster wanted the intersection.
The grep command doesn't work for me: grep -f file1.map file2.map | cut -f2 > common_snps.txt
I use this:
sort file1.map file2.map|uniq -d > common_lines.txt
awk '{print $2}' common_lines.txt > common_snps.txt
Then use plink --extract and merge
Log in to answer this question.
A small script in R would be really helpful in such case, check for
%in%in R manual.Hi. I have the same issue. There is any plink function that can help me to perform the intersection of two datasets? I mean, all but R function? Thanks