From 92b3768b6f297cda1308c9d03fa4f6d772c897fe Mon Sep 17 00:00:00 2001 From: root Date: Mon, 21 Aug 2023 20:09:18 +0000 Subject: [PATCH 1/3] added phenotype data to dataset -> file names and combined json data --- bids-walker.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/bids-walker.js b/bids-walker.js index 91dabb8..79e6d62 100644 --- a/bids-walker.js +++ b/bids-walker.js @@ -55,6 +55,7 @@ exports.walk = (root, cb)=>{ participants: [], //from participants.tsv participants_json: null, //from participants.json datasets: [], //{dataset, files} ... you have to do dataset.dataset.meta... maybe I should rename it to items" + phenotypes_json: {}, } let tsv = null; @@ -83,6 +84,29 @@ exports.walk = (root, cb)=>{ ///mnt/datalad/datasets.datalad.org/openfmri/ds000201 contains participants.json that's basically the participants.tsv } + if(exists(root+"/phenotype")) { + console.log("phenotype directory found.. loading"); + let files = fs.readdirSync(root+"/phenotype"); + + // If there are files in the directory, set the phenotype_folder + if (files.length > 0) { + bids.phenotype_files = files; + } + + //just load and combine json files + bids.phenotypes_json = {}; + files.forEach(file=>{ + if(file.startsWith(".")) return; + if(!file.endsWith(".json")) return; + let json = fs.readFileSync(root+"/phenotype/"+file, "utf8"); + let obj = JSON.parse(json); + for(let key in obj) { + bids.phenotypes_json[key] = obj[key]; + } + }); + } + + //TODO - should I create a default participants.json if it's missing so that brainlife UI will at least show each columns? if(exists(root+"/README")) { @@ -1277,4 +1301,4 @@ exports.walk = (root, cb)=>{ bids.datasets.push({dataset, files}); cb(); } -} +} \ No newline at end of file From 96acabbda699f25f84ce38962992a19a10976b7a Mon Sep 17 00:00:00 2001 From: bhatiadheeraj Date: Wed, 23 Aug 2023 20:00:09 +0000 Subject: [PATCH 2/3] groups json and .tsv file --- bids-walker.js | 57 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/bids-walker.js b/bids-walker.js index 79e6d62..435417b 100644 --- a/bids-walker.js +++ b/bids-walker.js @@ -88,22 +88,53 @@ exports.walk = (root, cb)=>{ console.log("phenotype directory found.. loading"); let files = fs.readdirSync(root+"/phenotype"); - // If there are files in the directory, set the phenotype_folder - if (files.length > 0) { - bids.phenotype_files = files; - } - - //just load and combine json files - bids.phenotypes_json = {}; + bids.phenotypes = []; + // needs fix to group them to just one entry for file with same name but different extension + + //group files with same name but different extensios + let groups = {}; files.forEach(file=>{ if(file.startsWith(".")) return; - if(!file.endsWith(".json")) return; - let json = fs.readFileSync(root+"/phenotype/"+file, "utf8"); - let obj = JSON.parse(json); - for(let key in obj) { - bids.phenotypes_json[key] = obj[key]; - } + let key = file.split(".")[0]; + if(!groups[key]) groups[key] = []; + groups[key].push(file); }); + for(let key in groups) { + let entry = { + //set the name to the file name without the extension + name: key, + //tsv file name + file: "phenotype/"+key+".tsv", + sidecar: null, + columns: {}, + data: [], + } + if(groups[key].includes(key+".json")) { + //sidecar + entry.sidecar = "phenotype/"+key+".json"; + let json = fs.readFileSync(root+"/phenotype/"+key+".json", "utf8"); + let obj = JSON.parse(json); + entry.columns = obj + } + + //load tsv + let tsv = fs.readFileSync(root+"/phenotype/"+key+".tsv", "utf8").trim().split("\n"); + let header = tsv.shift().split("\t"); + tsv.forEach((line, lineNumber) => { + try { + let obj = {}; + let tokens = line.split("\t"); + header.forEach((key, idx) => { + obj[key] = tokens[idx]; + }); + entry.data.push(obj); + } catch (error) { + console.error(`Error parsing line ${lineNumber} of ${file}: ${error.message}`); + console.error(`Line content: ${line}`); + } + }); + bids.phenotypes.push(entry); + } } From ca20ec53a80c677fbf43d0ddb7298152448ec1dd Mon Sep 17 00:00:00 2001 From: bhatiadheeraj Date: Wed, 30 Aug 2023 20:35:27 +0000 Subject: [PATCH 3/3] removed comment and whitespace --- bids-walker.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bids-walker.js b/bids-walker.js index 435417b..45c2a82 100644 --- a/bids-walker.js +++ b/bids-walker.js @@ -87,9 +87,7 @@ exports.walk = (root, cb)=>{ if(exists(root+"/phenotype")) { console.log("phenotype directory found.. loading"); let files = fs.readdirSync(root+"/phenotype"); - bids.phenotypes = []; - // needs fix to group them to just one entry for file with same name but different extension //group files with same name but different extensios let groups = {}; @@ -1332,4 +1330,4 @@ exports.walk = (root, cb)=>{ bids.datasets.push({dataset, files}); cb(); } -} \ No newline at end of file +}