Skip to content

Commit

Permalink
Add GT_as_DS options to vcf to csv converters
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Jan 16, 2024
1 parent 0798f19 commit 5c2d7cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ public class VcfToCsvCommand implements Callable<Integer> {
@Option(names = "--format", description = "output format. xls or csv. default: csv", required = false)
private String format = "csv";

@Option(names = "--genotypes", description = "genotypes: GT or DS", required = false)
@Option(names = "--genotypes", description = "genotypes: `GT` or `DS` or `GT_as_DS`. default: GT", required = false)
private String genotypes = "GT";

@Option(names = "--separator", description = "separator for output file. default: ,", required = false)
private char separator = ',';

@Option(names = "--quotes", description = "use quote in output file. default: true", required = false)
private boolean quotes = true;

public void setInput(String input) {
this.input = input;
}
Expand Down Expand Up @@ -69,7 +75,7 @@ public Integer call() throws Exception {
if (format.equals("xls")) {
writer = new ExcelTableWriter(output);
} else {
writer = new CsvTableWriter(output);
writer = new CsvTableWriter(output, separator, quotes);
}

writer.setColumns(columns.toArray(new String[0]));
Expand All @@ -92,10 +98,16 @@ public Integer call() throws Exception {
writer.setString("r2", variant.getAttributeAsString("R2", ""));

for (String sample : samples) {
if (genotypes.equals("GT")) {
if (genotypes.equalsIgnoreCase("GT")) {
writer.setString(sample, variant.getGenotype(sample).getGenotypeString());
} else if (genotypes.equals("DS")) {
writer.setString(sample, variant.getGenotype(sample).getAttributeAsString("DS", ""));
}else if (genotypes.equalsIgnoreCase("GT_as_DS")){
writer.setString(sample,""+variant.getGenotype(sample).countAllele(variant.getAlternateAllele(0)));
} else if (genotypes.equalsIgnoreCase("DS")) {
String value = variant.getGenotype(sample).getAttributeAsString("DS", "");
if (value.isEmpty()) {
value = "" + variant.getGenotype(sample).countAllele(variant.getAlternateAllele(0));
}
writer.setString(sample, value);
}
}
writer.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
@Command
public class VcfToCsvTransposeCommand implements Callable<Integer> {

private static final char PARTS_SEPARATOR = ',';

private static final String SAMPLE_COLUMN = "sample";

@Option(names = "--input", description = "input vcf file", required = true)
Expand All @@ -34,7 +32,7 @@ public class VcfToCsvTransposeCommand implements Callable<Integer> {
@Option(names = "--format", description = "output format. xls or csv. default: csv", required = false)
private String format = "csv";

@Option(names = "--genotypes", description = "genotypes: GT or DS", required = false)
@Option(names = "--genotypes", description = "genotypes: `GT` or `DS` or `GT_as_DS`. default: GT", required = false)
private String genotypes = "GT";

@Option(names = "--name", description = "column name. id or pos. default: pos", required = false)
Expand All @@ -43,6 +41,12 @@ public class VcfToCsvTransposeCommand implements Callable<Integer> {
@Option(names = "--chunk-size", description = "column name. id or pos. default: 5000", required = false)
private int chunk = 100;

@Option(names = "--separator", description = "separator for output file. default: ,", required = false)
private char separator = ',';

@Option(names = "--quotes", description = "use quote in output file. default: true", required = false)
private boolean quotes = true;

public void setInput(String input) {
this.input = input;
}
Expand All @@ -59,6 +63,14 @@ public void setGenotypes(String genotypes) {
this.genotypes = genotypes;
}

public void setQuotes(boolean quotes) {
this.quotes = quotes;
}

public void setSeparator(char separator) {
this.separator = separator;
}

@Override
public Integer call() throws Exception {

Expand Down Expand Up @@ -92,13 +104,17 @@ public Integer call() throws Exception {

for (String sample : samples) {
String value = "";
if (genotypes.equals("GT")) {
if (genotypes.equalsIgnoreCase("GT")) {
value = variant.getGenotype(sample).getGenotypeString();
} else if (genotypes.equals("DS")) {
}else if (genotypes.equalsIgnoreCase("GT_as_DS")){
value = "" + variant.getGenotype(sample).countAllele(variant.getAlternateAllele(0));
} else if (genotypes.equalsIgnoreCase("DS")) {
value = variant.getGenotype(sample).getAttributeAsString("DS", "");
if (value.isEmpty()) {
value = "" + variant.getGenotype(sample).countAllele(variant.getAlternateAllele(0));
}
} else {
throw new RuntimeException("Unknown genotypes option.");
}
samplesGenotypes.get(sample).add(value);
}
Expand Down Expand Up @@ -133,7 +149,7 @@ private void mergePartFiles(String output, String format, List<String> parts) {
if (format.equals("xls")) {
writer = new ExcelTableWriter(output);
} else {
writer = new CsvTableWriter(output);
writer = new CsvTableWriter(output, separator, quotes);
}

ITableReader[] readers = new ITableReader[parts.size()];
Expand All @@ -142,7 +158,7 @@ private void mergePartFiles(String output, String format, List<String> parts) {
columns.add(SAMPLE_COLUMN);

for (int i = 0; i < parts.size(); i++) {
CsvTableReader reader = new CsvTableReader(parts.get(i), PARTS_SEPARATOR);
CsvTableReader reader = new CsvTableReader(parts.get(i), separator);
for (String column : reader.getColumns()) {
if (!column.equals(SAMPLE_COLUMN)) {
columns.add(column);
Expand Down Expand Up @@ -196,7 +212,7 @@ protected void writeToFile(String output, List<String> samples, List<String> snp
columns.add(SAMPLE_COLUMN);
columns.addAll(snps);

ITableWriter writer = new CsvTableWriter(output, PARTS_SEPARATOR);
ITableWriter writer = new CsvTableWriter(output, separator, quotes);
writer.setColumns(columns.toArray(new String[0]));

for (String sample : samples) {
Expand Down

0 comments on commit 5c2d7cb

Please sign in to comment.