<p>This proved to be so simple that it took a while to discover: Just use colnames.</p>
<pre class="lang-r prettyprint-override"><code>x = readVcf(&quot;in.vcf&quot;)
colnames(x) &lt;- c(&quot;new1&quot;, &quot;new2&quot;)
# At this point, colnames(x) and samples(header(x)) will show different names

# ... but if you write it out, it should use the colnames for both:
writeVcf(x, &quot;out.vcf&quot;)
y = readVcf(&quot;out.vcf&quot;)

# samples(header(y)) and colnames(y) should both be &quot;new1&quot;,&quot;new2&quot; here
</code></pre>

CC BY-SA 4.0
2023-06-16T14:12:02.033Z
Thank you for posting a solution in R.
Ram RS
CC BY-SA 4.0
2023-06-16T09:55:58.470Z
2023-06-16T14:11:35.043Z
2023-06-16T14:11:35.043Z
dnebdal
<p>Here is an example in python how to rename samples using <code>pyvcf</code> <a href="https://sbslee-fuc.readthedocs.io/en/latest/api.html#module-fuc.api.pyvcf" rel="nofollow noreferrer">submodule</a> I wrote:</p>
<pre><code>&gt;&gt;&gt; from fuc import pyvcf
&gt;&gt;&gt; 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'],
... }
&gt;&gt;&gt; vf = pyvcf.VcfFrame.from_dict([], data)
&gt;&gt;&gt; # vf = pyvcf.VcfFrame.from_file('your_file.vcf')
&gt;&gt;&gt; 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
&gt;&gt;&gt; vf.df = vf.df.rename(columns={'Steven': 'John', 'Sara': 'Rachel'})
&gt;&gt;&gt; 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
&gt;&gt;&gt; # vf.to_file('renamed.vcf')
</code></pre>

CC BY-SA 4.0
2021-06-10T20:41:00.293Z
2021-06-11T12:39:16.257Z
2021-06-11T12:39:16.257Z
Seung-been Steven Lee
<p>Easiest to use <code>bcftools</code>, if <code>samples.txt</code> has the new sample names:</p>
<pre><code>bcftools reheader -s samples.txt -o new.bcf old.bcf
</code></pre>

CC BY-SA 4.0
2021-06-09T14:23:12.447Z
Thank you, since i only have a brief knowledge of R, is there an R solution?
CoDa
CC BY-SA 4.0
2021-06-09T14:26:11.630Z
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.
user438383
CC BY-SA 4.0
2021-06-09T14:20:58.320Z
2021-06-09T14:20:58.320Z
user438383
<p>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.</p>
<p>I am loading the vcf file via <code>readVcf()</code> from the <code>VariantAnnotation</code> package in R.</p>
<p>To check the sample names, I can use <code>samples(header(vcf.file))</code>, which will give me the names as characters.</p>
<p>It looks like this:</p>
<p><a href="https://i.sstatic.net/nSsBq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nSsBq.png" alt="enter image description here" /></a></p>
<p>and so on..</p>
<p>(I am actually very sorry that I cannot give a reproducible example here :( )</p>
<p>I also have a vector with all the new names. For example</p>
<pre class="lang-r prettyprint-override"><code>new.names &lt;- c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;)
</code></pre>
<p>I tried to exchange the &quot;old names&quot; with the new.names vector:</p>
<pre class="lang-r prettyprint-override"><code>samples(header(vcf.file)) &lt;- new.names
</code></pre>
<p>which leads to the error</p>
<pre class="lang-r prettyprint-override"><code>Error in samples(header(data.vcf)) &lt;- names.sample : 
  could not find function &quot;samples&lt;-&quot; 
</code></pre>
<p>I also tried to use the <code>rutilstimflutre</code> package, which has a already designed function for this, but unfortunately the package is outdated and not available for the newest R version.</p>
<p>Is there a way to do this? Perhaps a better way to assign the new names to the old ones?</p>
<p>Thanks a lot.</p>

<r><vcf>
Renaming samples in vcf file
# Renaming samples in vcf file

(Asked by: username_0 on 2021-06-09)

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.

**Tags:** r, vcf

---

## 3 answers

### Answer by username_4 on 2021-06-09. Score: 4 | Accepted answer

Easiest to use bcftools, if samples.txt has the new sample names:
bcftools reheader -s samples.txt -o new.bcf old.bcf

> **Comments:**
> **username_0**: Thank you, since i only have a brief knowledge of R, is there an R solution?  
> **username_4**: 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.  

---

### Answer by username_1 on 2023-06-16. Score: 4

This proved to be so simple that it took a while to discover: Just use colnames.
x = readVcf("in.vcf")
colnames(x) <- c("new1", "new2")
# At this point, colnames(x) and samples(header(x)) will show different names

# ... but if you write it out, it should use the colnames for both:
writeVcf(x, "out.vcf")
y = readVcf("out.vcf")

# samples(header(y)) and colnames(y) should both be "new1","new2" here

> **Comments:**
> **username_2**: Thank you for posting a solution in R.  

---

### Answer by username_3 on 2021-06-10. Score: 0

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')


---

