Is it Possible to "Merge" data from Replicates within a VCF SNP File
1
0
Entering edit mode
12 days ago

I have VCF SNP dataset that contains 55K markers and 623 samples. We included in our analysis replicates of some samples that are coded with the exact same name. We also know there are replicates with different names as they came from separate sources.

Is it possible to fill in all the "missing" data in one replicate of the sample using the other replicate as the reference. I would want to only fill in missing data and not change anything else.

I have been able to write some R code that can do this to files in STRUCTURE format as that format is amenable to import into R.

I am wondering if it is possible to do something similar directly to the VCF file or to all the .bed,.fam,.bim files generated from PLINK from a VCF.

I have looked into VCFtools and bcfTools but the merge funtions and collate functions of those programs are for multiple file sets. That is not what I am trying to do. I am trying to alter data within a single file.

Here is my R code if this makes it any clearer what exatcly I am trying to do.

My DF is R_Test_DFSg60

##Replacing 0s in row 371 with the values from row 573 if not = 0, In STRUCTURE format
for (col in 2:ncol(R_Test_DFSg60)) {

   #Check if the value in row 371 of the current column is "0"

   if (R_Test_DFSg60[371, col] == 0) {

     # Check if the value in row 573 of the current column is not "0"
     if (R_Test_DFSg60[573, col] != 0) {

       # Check if the values in row 371 and row 573 of the current column are different
       if (R_Test_DFSg60[371, col] != R_Test_DFSg60[573, col]) {

         # Replace the value in row 371 of the current column with the value from row 573
         R_Test_DFSg60[371, col] <- R_Test_DFSg60[573, col]
       }
     }
   }
 }

#Then delete row 573, removing it from the dataset
snp vcf genomics plink • 547 views
ADD COMMENT
0
Entering edit mode

cross posted: https://stackoverflow.com/questions/78462732/

Please mind that posting the same question to multiple sites can be perceived as bad etiquette, because efforts may be made to address a problem that has already been solved elsewhere in the meantime."

ADD REPLY
0
Entering edit mode

Thanks for the crosspost! Just trying to get things figured out.

ADD REPLY
0
Entering edit mode
12 days ago

use bcftools merge with --force-samples and then use jvarkit https://jvarkit.readthedocs.io/en/latest/VcfFilterJdk/ to replace the no-call with another called genotype for the same sample.

bcftools merge --force-samples --file-list files.list |  java -jar  jvarkit.jar vcffilterjdk -f biostar.code 

with biostar.code:

final UnaryOperator<String> toSample = S->{
    int colon = S.indexOf(":");
    return colon==-1?S:S.substring(colon+1);
    };
final List<Genotype> genotypes = new ArrayList<>(variant.getNSamples());
for(Genotype gt1: variant.getGenotypes()) {
    if(!gt1.isNoCall()) {
        genotypes.add(gt1);
        }
    else
        {
        Genotype newGt = gt1;
        final String sn1 = toSample.apply(gt1.getSampleName());
        for(Genotype gt2: variant.getGenotypes()) {
            if(gt2.isNoCall()) continue;
            final String sn2 = toSample.apply(gt2.getSampleName());
            if(!sn1.equals(sn2)) continue;
            newGt = new GenotypeBuilder(gt2).name(gt1.getSampleName()).make();
            break;
            }
        genotypes.add(newGt);
        }
    }

return new VariantContextBuilder(variant).genotypes(genotypes).make();

and then downstream, you can use bcftools view --samples-file to remove the duplicate samples.

ADD COMMENT
0
Entering edit mode

Thank you Pierre, I am going to take some time to get my head around this and report back if it works.

ADD REPLY
0
Entering edit mode

Pierre,

I am a novice at all of this in general and I have never used Java-based programming before. I may not be able to properly wrangle this with my ability. I think I am mostly struggling with what would need to be coded to my dataset here and how to run this loop on my set in general. Thank you again for the help.

ADD REPLY
0
Entering edit mode

I think I am mostly struggling with what would need to be coded to my dataset here and how to run this loop on my set in general.

i don't understand your question. You 'just' have to execute this command line:

bcftools merge --force-samples --file-list files.list |  java -jar  jvarkit.jar vcffilterjdk -f biostar.code > out.vcf
ADD REPLY
0
Entering edit mode

Hi Pierre,

I am attempting to run this code and am running into this error:

(jvar) $ jvarkit vcffilterjdk -f biostar.code Dart_VCF2_Original.vcf > Fileted_Dart2.vcf

[SEVERE][VcfFilterJdk]Cannot read non-existent file: file:///local/home/User/VCF_Filtering/biostar.code htsjdk.samtools.SAMException: Cannot read non-existent file: file:///local/home/User/VCF_Filtering/biostar.code at htsjdk.samtools.util.IOUtil.assertFileIsReadable(IOUtil.java:498) at com.github.lindenb.jvarkit.io.IOUtils.slurpPath(IOUtils.java:868) at com.github.lindenb.jvarkit.tools.vcffilterjs.VcfFilterJdk.run(VcfFilterJdk.java:534) at com.github.lindenb.jvarkit.tools.vcffilterjs.VcfFilterJdk.doWork(VcfFilterJdk.java:800) at com.github.lindenb.jvarkit.util.jcommander.Launcher.instanceMain(Launcher.java:819) at com.github.lindenb.jvarkit.util.jcommander.Launcher.instanceMainWithExit(Launcher.java:982) at com.github.lindenb.jvarkit.tools.vcffilterjs.VcfFilterJdk.main(VcfFilterJdk.java:817) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at com.github.lindenb.jvarkit.tools.jvarkit.JvarkitCentral$Command.execute(JvarkitCentral.java:290) at com.github.lindenb.jvarkit.tools.jvarkit.JvarkitCentral.run(JvarkitCentral.java:777) at com.github.lindenb.jvarkit.tools.jvarkit.JvarkitCentral.main(JvarkitCentral.java:788) [INFO][Launcher]vcffilterjdk Exited with failure (-1)

If I try to run it without the file input i get an error for not being able to find the #CHROM header.

Any more assistance would be greatly appreciated.

ADD REPLY
0
Entering edit mode

Cannot read non-existent file: file:///local/home/User/VCF_Filtering/biostar.code

your file doesn't exist, is at the wrong place etc.

ADD REPLY

Login before adding your answer.

Traffic: 2424 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6