Skip to content

Commit

Permalink
Create index only if it not exists (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
seppinho authored Oct 22, 2024
1 parent 2c2be87 commit df8e027
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/main/java/genepi/nf/test/vcf/VcfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,27 @@ public String getSummary() {
return this.toString();
}


@Override
public String toString() {
return "VcfFile [chromosomes=" + chromosomes + ", sampleCount=" + sampleCount + ", variantCount=" + variantCount + ", phased=" + phased
+ ", phasedAutodetect=" + phasedAutodetect + "]";
return "VcfFile [chromosomes=" + chromosomes + ", sampleCount=" + sampleCount + ", variantCount=" + variantCount
+ ", phased=" + phased + ", phasedAutodetect=" + phasedAutodetect + "]";
}

public VariantContext getVariant(String chromosome, int position) throws IOException {
createIndex();
VCFFileReader reader = new VCFFileReader(new File(vcfFilename), true);
CloseableIterator<VariantContext> it = reader.query(chromosome, position, position);

while (it.hasNext()) {
VariantContext line = it.next();
reader.close();
return line;
}
}

reader.close();
return null;
}

public ArrayList<VariantContext> getVariants() throws IOException {
return getVariants(-1);
}
Expand Down Expand Up @@ -185,33 +184,33 @@ public String getVariantsMD5() throws IOException, NoSuchAlgorithmException {
lineReader.close();
return new BigInteger(1, md.digest()).toString(16);
}

public ArrayList<VariantContext> getVariantsByRange(String chromosome, int start, int stop) throws IOException {

createIndex();
VCFFileReader reader = new VCFFileReader(new File(vcfFilename), true);
ArrayList<VariantContext> variants = new ArrayList<VariantContext>();
CloseableIterator<VariantContext> it = reader.query(chromosome, start, stop);

while (it.hasNext()) {
VariantContext line = it.next();
variants.add(line);
}
}

reader.close();
return variants;

}
}

public double getInfoR2(String chromosome, int position) throws IOException {
String value = getInfoTag("R2", chromosome, position);
return Double.valueOf(value);
}

public String getInfoTag(String tag, String chromosome, int position) throws IOException {
VariantContext vc = getVariant(chromosome, position);
return vc.getAttributeAsString(tag, "N/A");
}
}

@Deprecated
public int getNoSnps() {
Expand All @@ -234,12 +233,19 @@ public void setNoSamples(int noSamples) {
}

public void createIndex() throws IOException {
File vcfFile = new File(vcfFilename);
VCFFileReader reader = new VCFFileReader(vcfFile, false);
SAMSequenceDictionary vcfDict = reader.getFileHeader().getSequenceDictionary();
TabixIndex index = IndexFactory.createTabixIndex(new File(vcfFilename), new VCFCodec(), TabixFormat.VCF, vcfDict);
index.writeBasedOnFeatureFile(new File(vcfFilename));
reader.close();
File vcfFile = new File(vcfFilename);

String tbiFilename = vcfFilename + ".tbi";
File tbiFile = new File(tbiFilename);

if (!tbiFile.exists()) {
VCFFileReader reader = new VCFFileReader(vcfFile, false);
SAMSequenceDictionary vcfDict = reader.getFileHeader().getSequenceDictionary();
TabixIndex index = IndexFactory.createTabixIndex(new File(vcfFilename), new VCFCodec(), TabixFormat.VCF,
vcfDict);
index.writeBasedOnFeatureFile(new File(vcfFilename));
reader.close();
}
}

}

0 comments on commit df8e027

Please sign in to comment.