Skip to content

Commit

Permalink
Export best n hits, solves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
seppinho committed Dec 14, 2018
1 parent 0b3cac9 commit 497cf6c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/main/java/genepi/haplogrep/main/Haplogrep.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public void createParameters() {
addFlag("rsrs", "use RSRS Version");
addFlag("extend-report", "add flag for a extended final output");
addFlag("chip", "VCF data from a genotype chip");
addOptionalParameter("metric", "specifiy other metric (hamming or jaccard)", Tool.STRING);
addOptionalParameter("metric", "specifiy other metric (hamming or jaccard) than default (kulczynski)", Tool.STRING);
addFlag("lineage", "export lineage information");
addOptionalParameter("hits", "calculate best n hits", Tool.STRING);
}

@Override
Expand All @@ -55,6 +56,7 @@ public int run() {
String tree = (String) getValue("phylotree");
String format = (String) getValue("format");
String metric = (String) getValue("metric");
String hits = (String) getValue("hits");

boolean extended = isFlagSet("extend-report");

Expand All @@ -78,6 +80,10 @@ public int run() {
tree = "17";
}

if (hits == null) {
hits = "1";
}

File input = new File(in);

if (!input.exists()) {
Expand Down Expand Up @@ -151,15 +157,18 @@ else if (format.equals("fasta")) {

SampleFile newSampleFile = new SampleFile(lines);

HgClassifier.run(newSampleFile, phylotree, fluctrates, metric);

HgClassifier classifier = new HgClassifier();

classifier.run(newSampleFile, phylotree, fluctrates, metric, Integer.valueOf(hits));

ArrayList<TestSample> samples = newSampleFile.getTestSamples();

ExportUtils.createReport(samples, out, extended);

if (lineage) {
ExportUtils.calcLineage(samples, out);
}

}

} else {
Expand All @@ -182,9 +191,8 @@ public static void main(String[] args) throws IOException {

Haplogrep haplogrep = new Haplogrep(args);

//haplogrep = new Haplogrep(
// new String[] { "--in", "/home/seb/Desktop/1000G_BAQ.vcf.gz",
// "--out", "/home/seb/Desktop/test.txt", "--format", "vcf", "--extend-report" });
//haplogrep = new Haplogrep(new String[] { "--in", "test-data/vcf/HG00097.vcf", "--out",
// "test-data/test.txt", "--format", "vcf","--hits", "5"});

haplogrep.start();

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/genepi/haplogrep/util/HgClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
import search.ranking.RankingMethod;

public class HgClassifier {

public static void run(SampleFile newSampleFile, String phyloTree, String fluctrates, String metric)
throws JDOMException, IOException, InvalidRangeException {

public void run(SampleFile newSampleFile, String phyloTree, String fluctrates, String metric) throws InvalidRangeException, JDOMException, IOException {
run(newSampleFile, phyloTree, fluctrates, metric,1);
}

public void run(SampleFile newSampleFile, String phyloTree, String fluctrates, String metric,
int amountResults) throws JDOMException, IOException, InvalidRangeException {

Phylotree phylotree = PhylotreeManager.getInstance().getPhylotree(phyloTree, fluctrates);

Expand All @@ -25,19 +29,19 @@ public static void run(SampleFile newSampleFile, String phyloTree, String fluctr
switch (metric) {

case "kulczynski":
newRanker = new KulczynskiRanking(1);
newRanker = new KulczynskiRanking(amountResults);
break;

case "hamming":
newRanker = new HammingRanking(1);
newRanker = new HammingRanking(amountResults);
break;

case "jaccard":
newRanker = new JaccardRanking(1);
newRanker = new JaccardRanking(amountResults);
break;

default:
newRanker = new KulczynskiRanking(1);
newRanker = new KulczynskiRanking(amountResults);

}

Expand Down
71 changes: 71 additions & 0 deletions src/test/java/genepi/haplogrep/HaplogrepCmdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package genepi.haplogrep;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.junit.Test;

import contamination.objects.Sample;
import core.SampleFile;
import genepi.haplogrep.util.HgClassifier;
import genepi.io.FileUtil;
import genepi.io.table.reader.CsvTableReader;
import importer.VcfImporter;
import util.ExportUtils;

public class HaplogrepCmdTest {

@Test
public void HaplogrepCmdTest() throws Exception {

String file = "test-data/vcf/HG00097.vcf";
String phylo = "data/phylotree/phylotree17.xml";
String weights = "data/weights/weights17.txt";
String out = "test-data/out.txt";
VcfImporter impvcf = new VcfImporter();

HashMap<String, Sample> samples = impvcf.load(new File(file), false);
ArrayList<String> lines = ExportUtils.samplesMapToHsd(samples);
SampleFile newSampleFile = new SampleFile(lines);

HgClassifier classifier = new HgClassifier();

classifier.run(newSampleFile, phylo, weights, "kulczynski", 1);

ExportUtils.createReport(newSampleFile.getTestSamples(), out, false);

CsvTableReader reader = new CsvTableReader(out, '\t');

int count = 0;
while (reader.next()) {
count++;
String hg = reader.getString("Haplogroup");
double quality = reader.getDouble("Quality");
assertEquals("T2f1a1", hg);
assertEquals(quality, quality,0.0);
}

assertEquals(1, count);


classifier.run(newSampleFile, phylo, weights, "kulczynski", 10);

ExportUtils.createReport(newSampleFile.getTestSamples(), out, false);

reader = new CsvTableReader(out, '\t');

count = 0;
while (reader.next()) {
count++;
}

assertEquals(10, count);

FileUtil.deleteFile(out);

}

}

0 comments on commit 497cf6c

Please sign in to comment.