Skip to content

Instantly share code, notes, and snippets.

@nathandunn
Last active September 20, 2019 20:07
Show Gist options
  • Save nathandunn/c736b1d21c906c2d054f37bfbafa053c to your computer and use it in GitHub Desktop.
Save nathandunn/c736b1d21c906c2d054f37bfbafa053c to your computer and use it in GitHub Desktop.
tsv to json converter
function tsvJSON(tsv){
var lines=tsv.split("\n");
var result = [];
for(var i=0;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split("\t");
if(currentline.length>2){
obj.gene = currentline[1].split(",");
if(currentline[0].indexOf('(GO:')>0){
const startIndex = currentline[0].indexOf('(GO:');
const endIndex = currentline[0].indexOf(')');
console.log(startIndex,endIndex,currentline[0].substr(startIndex+1,endIndex-startIndex-1));
obj.goid = currentline[0].substr(startIndex+1,endIndex-startIndex-1);
obj.golabel = currentline[0].substr(0,startIndex-1);
}
else{
obj.golabel = currentline[0];
}
result.push(obj);
}
}
return JSON.stringify(result); //JSON
}
const fs = require('fs')
let dataString = '';
fs.readFile('input.tsv', (err, data) => {
if (err) throw err;
dataString = data.toString();
const output = tsvJSON(data.toString());
fs.writeFileSync('./output.json', output, 'utf-8');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment