-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveConverted.js
156 lines (145 loc) · 4.69 KB
/
moveConverted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const fs = require('fs');
const Promise = require('bluebird');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const _ = require('underscore');
const os = require('os');
const xnat = require("./index.js");
const help = function(){
console.error("Change converted files from one location to another");
console.error("Usage: node", process.argv[1],"-p <project id>");
console.error("Options:");
console.error("-p <project id>, project id in XNAT to upload the files.");
console.error("--out_files <filename to write found files>");
console.error("--in_files <filename to load input files instead of searching>");
console.error("--move , move the found files.");
console.error("--prompt , If set, forces prompt for login information again. It will use the previous server URL saved in the configuration file");
}
if(!argv["p"] || argv["h"] || argv["help"]){
help();
process.exit(1);
}
var projectid = argv["p"];
var promptlogin = argv["prompt"];
var outfiles = argv["out_files"];
var infiles = argv["in_files"];
var move = argv["move"];
xnat.start()
.then(function(res){
return xnat.getSubjects(projectid);
})
.then(function(res){
if(!infiles){
return Promise.map(_.pluck(res, "ID"), function(subjectid){
return xnat.getSubjectExperiments(projectid, subjectid)
.then(function(experiments){
return Promise.map(experiments.ResultSet.Result, function(exp){
return xnat.getScans(projectid, subjectid, exp.ID)
.then(function(subjectExperiments){
return Promise.map(subjectExperiments, function(scans){
return xnat.getScanFiles(projectid, subjectid, exp.ID, scans.ID)
.then(function(scanfiles){
var scanfilesfound = _.map(scanfiles, function(sfile){
return {
subjectid: subjectid,
experimentid: exp.ID,
scanid: scans.ID,
resourceid: sfile.cat_ID,
name: sfile.Name,
uri: sfile.URI,
collection: sfile.collection
}
});
return _.filter(scanfilesfound, function(sfile){
return (sfile.collection.indexOf("NRRD") == -1 && (sfile.name.indexOf(".nrrd") != -1 || sfile.name.indexOf(".nii.gz") != -1 || sfile.name.indexOf(".nii") != -1 || sfile.name.indexOf(".png") != -1 || sfile.name.indexOf(".txt") != -1 || sfile.name.indexOf(".bvals") != -1 || sfile.name.indexOf(".bvecs") != -1))
});
})
.catch(function(e){
console.error("getScanFiles", projectid, subjectid, exp.ID, scans.ID);
console.error(e);
return null;
});
}, {
concurrency: 1
});
})
.catch(function(e){
console.error("getScans", projectid, subjectid, exp.ID);
console.error(e);
return null;
});;
}, {
concurrency: 1
})
})
.catch(function(e){
console.error("getSubjectExperiments", projectid, subjectid);
console.error(e);
return null;
});
}, {
concurrency: 1
})
.then(function(allexperiments){
allexperiments = _.compact(_.flatten(allexperiments));
if(outfiles){
console.log("Writting file", outfiles);
fs.writeFileSync(outfiles, JSON.stringify(allexperiments));
}
return allexperiments;
})
.catch(function(e){
console.error("map ids");
console.error(e);
return null;
});
}else{
return JSON.parse(fs.readFileSync(infiles));
}
})
.then(function(allconvertedfiles){
if(move){
var uniqresource = _.uniq(_.map(allconvertedfiles, function(convfile){
return {
subjectid: convfile.subjectid,
experimentid: convfile.experimentid,
scanid: convfile.scanid
}
}), function(el, ind, l){
return el.subjectid + el.experimentid + el.scanid;
});
return Promise.map(uniqresource, function(ur){
return xnat.createResources(projectid, ur.subjectid, ur.experimentid, ur.scanid, "NRRD")
.then(function(res){
console.log("Resource created!", projectid, ur.subjectid, ur.experimentid, ur.scanid, "NRRD");
})
.catch(function(err){
console.error(err);
});
}, {
concurrency: 1
})
.then(function(){
return Promise.map(allconvertedfiles, function(convfile){
return xnat.moveResource(projectid, convfile.subjectid, convfile.experimentid, convfile.scanid, convfile.resourceid, convfile.name, convfile.subjectid, convfile.experimentid, convfile.scanid, "NRRD", convfile.name)
.then(function(res){
console.log("File Moved!", projectid, convfile.subjectid, convfile.experimentid, convfile.scanid, convfile.resourceid, convfile.name);
return res;
})
.catch(function(err){
console.error(err);
return err;
});
}, {
concurrency: 1
});
});
}
})
.then(function(){
return xnat.logout();
})
.catch(function(e){
console.error(e);
return xnat.logout();
})