Skip to content

Commit

Permalink
copyHeader with specific samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilong-Li committed Aug 27, 2024
1 parent 7601e1b commit 10c8b56
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ clean:

test:
./vcf_addDS -i test.vcf.gz -o out.vcf.gz
./vcf_addDS -i test.vcf.gz -s Ind3,Ind1 -o out.vcf.gz
./vcf_addINFO -i test.vcf.gz -o out.vcf.gz
./vcf_addINFO -i test.vcf.gz -s Ind0,Ind2 -o out.vcf.gz
4 changes: 2 additions & 2 deletions tools/vcf_addDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ int main(int argc, char * argv[])
}
// ========= core calculation part ===========================================
BcfReader vcf(invcf, region, samples);
BcfWriter bw(outvcf, vcf.header);
bw.copyHeader(invcf);
BcfWriter bw(outvcf);
bw.copyHeader(invcf, samples);
bw.header.addFORMAT("DS", "1", "Float", "Diploid Genotype Dosage"); // add DS tag into the header
int nsamples = vcf.nsamples;
vector<float> gps, ds(nsamples);
Expand Down
2 changes: 1 addition & 1 deletion tools/vcf_addINFO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char * argv[])
// ========= core calculation part ===========================================
BcfReader vcf(invcf, region, samples);
BcfWriter bw(outvcf);
bw.copyHeader(invcf);
bw.copyHeader(invcf, samples);
bw.header.addINFO("INFO", "1", "Float", "INFO score given genotype probability");
bw.header.addINFO("EAF", "1", "Float", "Estimated Allele Frequency");
int N = vcf.nsamples, i{0};
Expand Down
5 changes: 3 additions & 2 deletions vcfpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1750,12 +1750,13 @@ class BcfWriter
hp = &h;
}

/// copy header of given VCF
void copyHeader(const std::string & vcffile)
/// copy header of given VCF and restrict on samples
void copyHeader(const std::string & vcffile, std::string samples = "-")
{
htsFile * fp2 = hts_open(vcffile.c_str(), "r");
if(!fp2) throw std::invalid_argument("I/O error: input file is invalid");
header.hdr = bcf_hdr_read(fp2);
header.setSamples(samples);
hts_close(fp2);
initalHeader(header);
}
Expand Down

0 comments on commit 10c8b56

Please sign in to comment.