<p>I am pretty sure that there is a reason that the first nucleotide of the <code>ALT</code> is the same as that of <code>REF</code> in <code>VCF</code> files, however I was not able to find a reference for this.</p>

<p>If you really need to do this for some reason, you will need to compare the alt and ref entries base by base starting from base 1 of each and until a mismatch. Here is some R code doing that:</p>

<pre><code>library(data.table)
my_csv &lt;- fread("example_vcf.txt")
print(my_csv)

# loop over each row
for(i in 1:nrow(my_csv)) {

  # make "strings" from the ref and alt columns, make "arrays" of letters out of these strings
  ref &lt;- as.character(my_csv[i, "REF"])
  ref_array &lt;- unlist(strsplit(as.character(my_csv[i, "REF"]), split = ""))

  alt &lt;- as.character(my_csv[i, "ALT"])
  alt_array &lt;- unlist(strsplit(as.character(my_csv[i, "ALT"]), split = ""))

  # which of the ref alt-pair shorter and how long
  shortest &lt;- c(nchar(ref), nchar(alt))[which.min(c(nchar(ref), nchar(alt)))]

  # compare ref and alt base by base, remove from alt if same nucleotide present in both, stop at first mismatch
  index &lt;- 1
  new_alt &lt;- alt_array
  while(index &lt;= shortest) {
    if (ref_array[index] == alt_array[index]) {
      new_alt &lt;- new_alt[-1]
    } else {
      break()
    }
    index &lt;- index + 1
  }

  my_csv[i,"ALT"] &lt;- paste(new_alt, collapse = "")

  # use "-" if alt is empty
  if (my_csv[i,"ALT"] == "") {
    my_csv[i,"ALT"] &lt;- "-"
  }
}

print(my_csv)
</code></pre>

<p>I have added some test cases at the end of the sample piece in your sample and the code is adding another column so that the changes made to the <code>ALT</code> are easier to see:</p>

<pre><code>&gt; print(my_csv)
    #CHROM       POS ID    REF            ALT       NEW_ALT
 1:   chr4   5708160  .      G         GATAAC         ATAAC
 2:   chr4  30280292  .      C        CACGTAT        ACGTAT
 3:   chr6 166253422  .      C        CCTTTCT        CTTTCT
 4:  chr11  21931340  .      A      AACACACGC      ACACACGC
 5:  chr12 118404422  .      G        GCACACA        CACACA
 6:  chr15  22672047  .      T TAAGTAGATTTAAG AAGTAGATTTAAG
 7:  chr16  32656293  . ATGATT              A             -
 8: chrXXX       XXX  .   TCTC              T             -
 9: chrXXX       XXX  .   TCTC           TCTC             -
10: chrXXX       XXX  .   TCTC             TA             A
11: chrXXX       XXX  .   TCTC            TAT            AT
</code></pre>

CC BY-SA 4.0
2019-12-05T01:50:26.470Z
Possibly due to left-normalisation?

https://genome.sph.umich.edu/wiki/Variant_Normalization
gringer
CC BY-SA 4.0
2019-12-05T12:24:59.877Z
The reason given in the specification is to avoid empty fields (see §1.6.1 Fixed fields, 4 REF). In particular for insertions `chr4  5708161  .  -  ATAAC` it would be unclear whether the insertion being described was supposed to be before or after the base at 5708161. In `chr4  5708160  .  G  GATAAC` the anchoring G makes it crystal clear that the insertion is between the bases at positions 5708160 and 5708161.
John Marshall
CC BY-SA 4.0
2019-12-05T13:28:17.403Z
Thank you to be such nice people. Actually I am trying to annotate my INDELs called by Strelka caller using ANNOVAR but annovar was complaining about something then I guessed that may be allele repetition.  @haci your code worked on annovar but annovar identifies all as SNP or DELETION and no INSERTION. To be honest I even don't know how to change INDEL vcf to be correctly recognized by ANNOVAR. However what I assumed would work you helped me to try that and thank you. It is not your fault rather I must first know what change needed for INDEL for annovar annotation. I posted results above
Zizogolu
CC BY-SA 4.0
2019-12-04T23:56:33.340Z
2019-12-04T23:56:33.340Z
haci
<p>I have 200 .vcf files like this on a Linux system:</p>

<pre><code>#CHROM  POS ID  REF ALT
chr4    5708160 .   G   GATAAC
chr4    30280292    .   C   CACGTAT
chr6    166253422   .   C   CCTTTCT
chr11   21931340    .   A   AACACACGC
chr12   118404422   .   G   GCACACA
chr15   22672047    .   T   TAAGTAGATTTAAG
chr16   32656293    .   ATGATT  A
</code></pre>

<p>I want to modify my .vcf files in this way</p>

<p>For instance if the reference allele in <code>REF column</code> is <code>TCTC</code> and alteration allele in <code>ALT column</code> is <code>T</code>, because T is the first common nucleotide in both reference and alteration so we remove <code>T</code> from <code>ALT column</code> so REF would remain as <code>TCTC</code> and ALT would be <code>-</code> . In total I mean removing whatever in ALT which is common with the first allele in REF.</p>

<p>Any help please?</p>

<p>This is what I got by @haci's code</p>

<pre><code>&gt; head(var.annovar.maf2[1:6,1:13])
   Tumor_Sample_Barcode Chromosome Start_Position End_Position Reference_Allele Tumor_Seq_Allele2 Hugo_Symbol Variant_Classification
1:                  new       chr1       89616151     89616151                G                 T        GBP7      Missense_Mutation
2:                  new      chr16       20556547     20556547                C                 G      ACSM2B      Missense_Mutation
3:                  new      chr17       66992092     66992092                C                 A       ABCA9      Missense_Mutation
4:                  new      chr20       13251339     13251339                C                 T        ISM1                 Silent
5:                  new       chr1          56987        56987               CA                 -           .                   &lt;NA&gt;
6:                  new       chr1        4133415      4133415               AG                 -           .                   &lt;NA&gt;
             tx   exon txChange aaChange Variant_Type
1:    NM_207398  exon6  c.C733A  p.L245I          SNP
2: NM_001105069 exon10 c.G1213C  p.G405R          SNP
3:    NM_080283 exon26 c.G3499T p.G1167C          SNP
4:    NM_080826  exon2  c.C327T  p.N109N          SNP
5:         &lt;NA&gt;   &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;          DEL
6:         &lt;NA&gt;   &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;          DEL
&gt;
</code></pre>

CC BY-SA 4.0
2019-12-04T19:14:07.067Z
Thanks for posting the new question! But please also include the output you expect. For example, your last line looks like a deletion of 5 nucleotides, changing `ATGATT` to `A`, what do you want to see for that line?
terdon
CC BY-SA 4.0
2019-12-04T19:29:42.763Z
Please provide a final expected output as suggested by @terdon. I think I may understand what you mean, however what would you do in the case of the final row in the table - for chr16 ?
h3ab74
<vcf><text-processing>
Modifying .vcf files
# Modifying .vcf files

(Asked by: username_0 on 2019-12-04)

I have 200 .vcf files like this on a Linux system:
#CHROM  POS ID  REF ALT
chr4    5708160 .   G   GATAAC
chr4    30280292    .   C   CACGTAT
chr6    166253422   .   C   CCTTTCT
chr11   21931340    .   A   AACACACGC
chr12   118404422   .   G   GCACACA
chr15   22672047    .   T   TAAGTAGATTTAAG
chr16   32656293    .   ATGATT  A

I want to modify my .vcf files in this way
For instance if the reference allele in REF column is TCTC and alteration allele in ALT column is T, because T is the first common nucleotide in both reference and alteration so we remove T from ALT column so REF would remain as TCTC and ALT would be - . In total I mean removing whatever in ALT which is common with the first allele in REF.
Any help please?
This is what I got by @haci's code
> head(var.annovar.maf2[1:6,1:13])
   Tumor_Sample_Barcode Chromosome Start_Position End_Position Reference_Allele Tumor_Seq_Allele2 Hugo_Symbol Variant_Classification
1:                  new       chr1       89616151     89616151                G                 T        GBP7      Missense_Mutation
2:                  new      chr16       20556547     20556547                C                 G      ACSM2B      Missense_Mutation
3:                  new      chr17       66992092     66992092                C                 A       ABCA9      Missense_Mutation
4:                  new      chr20       13251339     13251339                C                 T        ISM1                 Silent
5:                  new       chr1          56987        56987               CA                 -           .                   
6:                  new       chr1        4133415      4133415               AG                 -           .                   
             tx   exon txChange aaChange Variant_Type
1:    NM_207398  exon6  c.C733A  p.L245I          SNP
2: NM_001105069 exon10 c.G1213C  p.G405R          SNP
3:    NM_080283 exon26 c.G3499T p.G1167C          SNP
4:    NM_080826  exon2  c.C327T  p.N109N          SNP
5:                                DEL
6:                                DEL
>

**Tags:** vcf, text-processing
> **Comments:**
> **username_1**: Thanks for posting the new question! But please also include the output you expect. For example, your last line looks like a deletion of 5 nucleotides, changing `ATGATT` to `A`, what do you want to see for that line?  
> **username_2**: Please provide a final expected output as suggested by @terdon. I think I may understand what you mean, however what would you do in the case of the final row in the table - for chr16 ?  

---

## 1 answers

### Answer by username_3 on 2019-12-04. Score: 1 | Accepted answer

I am pretty sure that there is a reason that the first nucleotide of the ALT is the same as that of REF in VCF files, however I was not able to find a reference for this.
If you really need to do this for some reason, you will need to compare the alt and ref entries base by base starting from base 1 of each and until a mismatch. Here is some R code doing that:
library(data.table)
my_csv <- fread("example_vcf.txt")
print(my_csv)

# loop over each row
for(i in 1:nrow(my_csv)) {

  # make "strings" from the ref and alt columns, make "arrays" of letters out of these strings
  ref <- as.character(my_csv[i, "REF"])
  ref_array <- unlist(strsplit(as.character(my_csv[i, "REF"]), split = ""))

  alt <- as.character(my_csv[i, "ALT"])
  alt_array <- unlist(strsplit(as.character(my_csv[i, "ALT"]), split = ""))

  # which of the ref alt-pair shorter and how long
  shortest <- c(nchar(ref), nchar(alt))[which.min(c(nchar(ref), nchar(alt)))]

  # compare ref and alt base by base, remove from alt if same nucleotide present in both, stop at first mismatch
  index <- 1
  new_alt <- alt_array
  while(index <= shortest) {
    if (ref_array[index] == alt_array[index]) {
      new_alt <- new_alt[-1]
    } else {
      break()
    }
    index <- index + 1
  }

  my_csv[i,"ALT"] <- paste(new_alt, collapse = "")

  # use "-" if alt is empty
  if (my_csv[i,"ALT"] == "") {
    my_csv[i,"ALT"] <- "-"
  }
}

print(my_csv)

I have added some test cases at the end of the sample piece in your sample and the code is adding another column so that the changes made to the ALT are easier to see:
> print(my_csv)
    #CHROM       POS ID    REF            ALT       NEW_ALT
 1:   chr4   5708160  .      G         GATAAC         ATAAC
 2:   chr4  30280292  .      C        CACGTAT        ACGTAT
 3:   chr6 166253422  .      C        CCTTTCT        CTTTCT
 4:  chr11  21931340  .      A      AACACACGC      ACACACGC
 5:  chr12 118404422  .      G        GCACACA        CACACA
 6:  chr15  22672047  .      T TAAGTAGATTTAAG AAGTAGATTTAAG
 7:  chr16  32656293  . ATGATT              A             -
 8: chrXXX       XXX  .   TCTC              T             -
 9: chrXXX       XXX  .   TCTC           TCTC             -
10: chrXXX       XXX  .   TCTC             TA             A
11: chrXXX       XXX  .   TCTC            TAT            AT

> **Comments:**
> **username_4**: Possibly due to left-normalisation?  https://genome.sph.umich.edu/wiki/Variant_Normalization  
> **username_5**: The reason given in the specification is to avoid empty fields (see §1.6.1 Fixed fields, 4 REF). In particular for insertions `chr4  5708161  .  -  ATAAC` it would be unclear whether the insertion being described was supposed to be before or after the base at 5708161. In `chr4  5708160  .  G  GATAAC` the anchoring G makes it crystal clear that the insertion is between the bases at positions 5708160 and 5708161.  
> **username_0**: Thank you to be such nice people. Actually I am trying to annotate my INDELs called by Strelka caller using ANNOVAR but annovar was complaining about something then I guessed that may be allele repetition.  @haci your code worked on annovar but annovar identifies all as SNP or DELETION and no INSERTION. To be honest I even don't know how to change INDEL vcf to be correctly recognized by ANNOVAR. However what I assumed would work you helped me to try that and thank you. It is not your fault rather I must first know what change needed for INDEL for annovar annotation. I posted results above  

---

