Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVA-1665 VCF headers list the contigs in accession order #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import uk.ac.ebi.eva.accession.core.contig.ContigNaming;
import uk.ac.ebi.eva.accession.core.contig.ContigSynonyms;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.List;
Expand Down Expand Up @@ -69,6 +71,30 @@ public void update(ExecutionContext executionContext) throws ItemStreamException
@Override
public void close() throws ItemStreamException {
printWriter.close();
sortContigFile();
}

private void sortContigFile() {
try {
Process process = Runtime.getRuntime().exec(new String[]{"sort", this.output.getAbsolutePath()});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of reading sort's standard output, why not using

Suggested change
Process process = Runtime.getRuntime().exec(new String[]{"sort", this.output.getAbsolutePath()});
String[] command = {"sort", "-o", this.output.getAbsolutePath(), this.output.getAbsolutePath()};
Process process = Runtime.getRuntime().exec(command);

?
it was my bad to use sort without arguments but this is what I had in mind from the beginning.

int exitCode = process.waitFor();

if (exitCode != 0) {
throw new ItemStreamException(
"Trying to sort the contig list, the 'sort' command returned exit code'" + exitCode + "'.");
}

BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
PrintWriter printWriter2 = new PrintWriter(new FileWriter(this.output));
while ((line = br.readLine()) != null) {
printWriter2.println(line);
}
printWriter2.close();

} catch (IOException | InterruptedException e) {
throw new ItemStreamException("Failed sorting contig list. ", e);
}
}

@Override
Expand All @@ -85,7 +111,7 @@ public void write(List<? extends String> contigs) {
throw new IllegalArgumentException("Could not find the corresponding sequence name for contig " + contig);
}

printWriter.println(contig + "," + sequenceName);
printWriter.println(sequenceName + "," + contig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ private void addContigs(Set<VCFHeaderLine> metaData) {
BufferedReader bufferedReader = new BufferedReader(new FileReader(contigsFilePath));
String contigLine;
while ((contigLine = bufferedReader.readLine()) != null) {
String[] contigAndName = contigLine.split(",");
String contig = contigAndName[0];
String name = contigAndName[1];
String[] nameAndContig = contigLine.split(",");
String name = nameAndContig[0];
String contig = nameAndContig[1];
metaData.add(new VCFHeaderLine("contig", "<ID=" + name + ",accession=\"" + contig + "\">"));
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void assertStepExecutesAndCompletes() {
public void contigsWritten() throws Exception {
assertStepExecutesAndCompletes();

assertEquals(new HashSet<>(Arrays.asList("CM001954.1,CAE13", "CM001941.2,CAE1")),
assertEquals(new HashSet<>(Arrays.asList("CAE13,CM001954.1", "CAE1,CM001941.2")),
setOfLines(getActiveContigsFilePath(inputParameters.getOutputFolder(),
inputParameters.getAssemblyAccession())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void assertStepExecutesAndCompletes() {
public void contigsWritten() throws Exception {
assertStepExecutesAndCompletes();

assertEquals(new HashSet<>(Arrays.asList("CM001954.1,CAE13", "CM001941.2,CAE1")),
assertEquals(new HashSet<>(Arrays.asList("CAE13,CM001954.1", "CAE1,CM001941.2")),
setOfLines(ContigWriter.getMergedContigsFilePath(inputParameters.getOutputFolder(),
inputParameters.getAssemblyAccession())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,22 @@ public void write() throws Exception {

assertEquals(contigs.size(), numberOfLines(output));

List<String> expectedLines = Arrays.asList("CM0001.1,Chr1", "CM0002.1,Chr2", "CM0003.1,Chr3");
assertContigFileContent(output, expectedLines);
List<String> expectedLinesSorted = Arrays.asList("Chr1,CM0001.1", "Chr2,CM0002.1", "Chr3,CM0003.1");
assertContigFileContentAndOrder(output, expectedLinesSorted);
}

private long numberOfLines(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
return br.lines().count();
}

private void assertContigFileContent(File file, List<String> expectedLines) throws IOException {
private void assertContigFileContentAndOrder(File file, List<String> expectedLines) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
int index = 0;
String line;
while ((line = br.readLine()) != null) {
assertTrue(expectedLines.contains(line));
assertEquals(expectedLines.get(index), line);
index++;
}
}

Expand All @@ -117,14 +119,14 @@ public void useSequenceNameIfContigIsRefSeqAccession() throws IOException {
ContigWriter contigWriter = new ContigWriter(output, contigMapping);

contigWriter.open(null);
List<String> contigs = Arrays.asList(REFSEQ_ACCESSION_1, REFSEQ_ACCESSION_2, GENBANK_ACCESSION_3);
List<String> contigs = Arrays.asList(REFSEQ_ACCESSION_2, GENBANK_ACCESSION_3, REFSEQ_ACCESSION_1);
contigWriter.write(contigs);
contigWriter.close();

assertEquals(contigs.size(), numberOfLines(output));

List<String> expectedLines = Arrays.asList("NC0001.1,Chr1", "NC0002.1,Chr2", "CM0003.1,Chr3");
assertContigFileContent(output, expectedLines);
List<String> expectedLinesSorted = Arrays.asList("Chr1,NC0001.1", "Chr2,NC0002.1", "Chr3,CM0003.1");
assertContigFileContentAndOrder(output, expectedLinesSorted);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private List<String> grepFile(File file, String regex) throws IOException {
public void checkMetadataSection() throws Exception {
File outputFolder = temporaryFolder.newFolder();
FileWriter fileWriter = new FileWriter(ContigWriter.getActiveContigsFilePath(outputFolder, REFERENCE_ASSEMBLY));
String contig = "CM0001.1,Chr1";
String contig = "Chr1,CM0001.1";
fileWriter.write(contig);
fileWriter.close();

Expand Down