Skip to content

Commit

Permalink
Add assay extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
vdkkia committed Feb 1, 2021
1 parent 9572c49 commit 46714eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const extractor = require("./src/extractor");

// The source extractor
const options = {
// *** The source extractor
const sourceOptions = {
ENAChecklist: "./input/Source/ENA.xml",
ISAConfiguration: "./input/Source/ISA.xml",
outputPath: "./output/result.txt",
outputPath: "./output/sourceMerged.txt",
};
extractor.sourceExtractor.getData(options);
extractor.sourceExtractor.getData(sourceOptions);

// // The assay extractor
// extractor.assayExtractor.getData();
// *** The assay extractor
const assayOptions = {
ISAConfiguration: "./input/Assay/genome_seq.xml",
outputPath: "./output/assayMerged.txt",
};
extractor.assayExtractor.getData(assayOptions);
58 changes: 58 additions & 0 deletions src/extractor/assayExtractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const xml2js = require("xml2js"); //.parseString;
const parser = new xml2js.Parser({ explicitChildren: true, preserveChildrenOrder: true }).parseString;
const fs = require("fs");
const fsPromises = fs.promises;

const getData = async (options) => {
const { ISAConfiguration, outputPath } = options;
const ISA_XML = await fsPromises.readFile(ISAConfiguration);
const ISAParsed = await dataParse(ISA_XML);
const ISAData = ISAExtract(ISAParsed);
writeOutput(ISAData, outputPath);
};

const dataParse = async (data) => {
return new Promise((r) => {
parser(data, (e, result) => {
r(result);
});
});
};

const ISAExtract = (data) => {
const temp = data["isatab-config-file"]["isatab-configuration"][0]["$$"];
let result = { meta: {}, data: {} };
let lastProtocol = "";
temp.forEach((x) => {
if (x["#name"] == "protocol-field") {
result.data[x["$"]["protocol-type"]] = [];
lastProtocol = x["$"]["protocol-type"];
} else if (x["#name"] == "field") {
if (lastProtocol != "") {
result.data[lastProtocol].push({
name: x["$"]["header"],
description: x["description"][0].trim(),
dataType: x["$"]["data-type"],
required: x["$"]["is-required"],
});
}
} else if (x["#name"] == "measurement") {
result.meta["measurement"] = x["$"]["term-label"];
} else if (x["#name"] == "technology") {
result.meta["technology"] = x["$"]["term-label"];
}
});
// console.log(result);
return result;
};

const writeOutput = async (data, outputPath) => {
try {
await fsPromises.writeFile(outputPath, JSON.stringify(data));
console.log("Data was extracted successfully!");
} catch (e) {
console.log(`Error writing to file: ${e}`);
}
};

module.exports = { getData };

0 comments on commit 46714eb

Please sign in to comment.