This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Merge two SNP vcf files

File1

#CHROM  POS     ID      REF_Zv  ALT_lm                             
chr1A   219620  .       T       A
chr1A   219648  .       A       G
chr1A   219867  .       A       G

file2

#CHROM  POS     ID      REF_Zv  ALT_RV                             
chr1A   219457  .       C       T
chr1A   219670  .       A       G
chr1A   219867  .       A       C

File3

#CHROM  POS     ID      REF_Zv  ALT_lm ALT_RV                            
chr1A   219620  .       T       A    NA
chr1A   219648  .       A       G    NA
chr1A   219867  .       A       G    C
chr1A   219457  .       C       NA   T
chr1A   219670  .       A       NA   C

My command is

awk 'FNR==NR{a[$1,$2];next} {if(a[$1,$2]==""){a[$1,$2]=0};print $1,$2,$3,$4,$5, a[$4,$5]} ' file1 file2 > file3

However, I can not get the file3 which I want. Could you help me improve the command? Thanks, Fuyou

snp vcf awk

My SNP vcf files do not have other columns. such as "GT". Thanks, Fuyou

Then those are not vcf files and you make things harder by not using standardised file formats.

But my data has no SNP format. So vcf-merge does not work.

1 answer

Using R merge:

# example files
file1 <- read.table(text = "#CHROM  POS     ID      REF_Zv  ALT_lm                             
chr1A   219620  .       T       A
chr1A   219648  .       A       G
chr1A   219867  .       A       G", header = TRUE, stringsAsFactors = FALSE,
                    comment.char = "")
file2 <- read.table(text = "#CHROM  POS     ID      REF_Zv  ALT_RV                             
chr1A   219457  .       C       T
chr1A   219670  .       A       G
chr1A   219867  .       A       C", header = TRUE, stringsAsFactors = FALSE,
                        comment.char = "")

merge(file1, file2, by.x = c("X.CHROM", "POS", "ID", "REF_Zv"), all = TRUE)
#   X.CHROM    POS ID REF_Zv ALT_lm ALT_RV
# 1   chr1A 219457  .      C   <NA>      T
# 2   chr1A 219620  .      T      A   <NA>
# 3   chr1A 219648  .      A      G   <NA>
# 4   chr1A 219670  .      A   <NA>      G
# 5   chr1A 219867  .      A      G      C

Thanks. It is working well now.

If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted. Upvote|Bookmark|Accept

I have marked. Thanks, Fuyou

Log in to answer this question.