I have a huge fileList
array where many elements of array start from sub-**
pattern. I want to group these files belonging to same sub-**
(for e.g.: files which start with sub-01
) into one subject specific array. Thus after creating all these sub specific array they can be pushed to a large array nifti_fileList
.
I have written following code in JavaScript with very crude logic:
subFilelist = [];
var previous_sub = null;
var capture = null;
var file_no = 0
async.eachOfLimit(fileList, 200, function (file, key, cb) {
testfile = file.relativePath;
sub_capture_re = /sub-[a-zA-Z0-9]*/gi;
var current_sub = testfile.match(sub_capture_re)
if (current_sub) {
if((file_no === 0)){
subFilelist.push(testfile)
file_no = file_no + 1;
previous_sub = current_sub[0];
}
if ((current_sub[0] === previous_sub) && (file_no > 0)){
previous_sub = current_sub[0];
subFilelist.push(file.relativePath)
} else {
nifti_fileList.push(subFilelist)
subFilelist = [];
file_no =0
}
}
process.nextTick(cb);
});
console.log(nifti_fileList);
Sample input:
fileList = [/sub-09/func/sub-09_task-genInstrAv_run-05_physio.tsv.gz,
/sub-10/anat/sub-10_T1w.json,
/sub-10/anat/sub-10_T1w.nii.gz,
/sub-10/fmap/sub-10_magnitude1.nii.gz,
/sub-10/fmap/sub-10_magnitude2.nii.gz,
/sub-10/fmap/sub-10_phasediff.json,
/sub-10/fmap/sub-10_phasediff.nii.gz,
/sub-11/anat/sub-11_T1w.nii.gz]
nifti_fileList = [[/sub-09/func/sub-09_task-genInstrAv_run-05_physio.tsv.gz],
[/sub-10/anat/sub-10_T1w.json,
/sub-10/anat/sub-10_T1w.nii.gz,
/sub-10/fmap/sub-10_magnitude1.nii.gz,
/sub-10/fmap/sub-10_magnitude2.nii.gz,
/sub-10/fmap/sub-10_phasediff.json,
/sub-10/fmap/sub-10_phasediff.nii.gz],
[/sub-11/anat/sub-11_T1w.nii.gz]]
Can this be made more efficient?