# Renaming samples in vcf file

I am currently asking myself how to rename samples in a vcf file. This is necessary, because the naming of several samples included in thr vcf file was unfortunately not be chosen by me.

I am loading the vcf file via readVcf() from the VariantAnnotation package in R.

To check the sample names, i can use samples(header(vcf.file)), which will give me the names as characters.

It looks like this:

and so on..

(I am actually very sorry that i cannot give a reproducible example here :( )

I also have a vector with all the new names. For example

new.names <- c("1","2","3","4")


I tried to exchange the "old names" with the new.names vector:

samples(header(vcf.file)) <- new.names


which leads to the error Error in samples(header(data.vcf)) <- names.sample : could not find function "samples<-"

I also tried to use the rutilstimflutre package, which has a already designed function for this, but unfortunately the package is outdated and not available for the newest R version.

Is there a way to do this? Perhaps a better way to assign the new names to the old ones?

Thanks a lot.

Easiest to use bcftools, if samples.txt has the new sample names:

bcftools reheader -s samples.txt -o new.bcf old.bcf

• Thank you, since i only have a brief knowledge of R, is there an R solution?
– CoDa
Jun 9 at 14:23
• Maybe - I'm not sure. This approach doesn't require any knowledge of any programming - just doing this should work perfectly. Generally I prefer not to use R for working with .vcfs because it's slow to load them into memory. If you are working with .vcfs I would highly recommend getting familiar with bcftools as it is very fast and memory efficient. Jun 9 at 14:26

Here is an example in python how to rename samples using pyvcf submodule I wrote:

>>> from fuc import pyvcf
>>> data = {
...     'CHROM': ['chr1', 'chr2'],
...     'POS': [100, 101],
...     'ID': ['.', '.'],
...     'REF': ['G', 'T'],
...     'ALT': ['A', 'C'],
...     'QUAL': ['.', '.'],
...     'FILTER': ['.', '.'],
...     'INFO': ['.', '.'],
...     'FORMAT': ['GT', 'GT'],
...     'Steven': ['0/1', '1/1'],
...     'Sara': ['0/0', '0/1'],
... }
>>> vf = pyvcf.VcfFrame.from_dict([], data)
>>> # vf = pyvcf.VcfFrame.from_file('your_file.vcf')
>>> vf.df
CHROM  POS ID REF ALT QUAL FILTER INFO FORMAT Steven Sara
0  chr1  100  .   G   A    .      .    .     GT    0/1  0/0
1  chr2  101  .   T   C    .      .    .     GT    1/1  0/1
>>> vf.df = vf.df.rename(columns={'Steven': 'John', 'Sara': 'Rachel'})
>>> vf.df
CHROM  POS ID REF ALT QUAL FILTER INFO FORMAT John Rachel
0  chr1  100  .   G   A    .      .    .     GT  0/1    0/0
1  chr2  101  .   T   C    .      .    .     GT  1/1    0/1
>>> # vf.to_file('renamed.vcf')