-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeSubjectLabel.js
179 lines (158 loc) · 4.93 KB
/
changeSubjectLabel.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
module.exports = function(xnat){
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 csvtojson = require('csvtojson');
const help = function(){
console.error("Upload a DICOM directory to XNAT.");
console.error("Usage: node", process.argv[1],"-p <project id>");
console.error("Options:");
console.error("-s <subject id or current subject label>");
console.error("-e <experiment id or current experiment label (you need to set the -s flag as well)>");
console.error("-l <label to modify the user>");
console.error("--csv <csv file with columns 'subject_ID,label' to modify the subject_label to label or 'subject_ID,experiment_ID,label' to modify the experiment_label to label>")
console.error("--prompt , If set, forces prompt for login information again. It will use the previous server URL saved in the configuration file");
console.error("--server <server url>, XNAT server url");
}
if( !argv["p"] || argv["h"] || argv["help"] || !(argv["csv"] || (argv["l"] && argv["s"]))){
help();
process.exit(1);
}
var projectid = argv["p"];
var subjectid = argv["s"];
var experimentid = argv["e"];
var label = argv["l"];
var promptlogin = argv["prompt"];
var csvfilename = argv["csv"];
const getConfigFile = function () {
return new Promise(function(resolve, reject){
try {
// Try to load the user's personal configuration file
var conf = path.join(os.homedir(), '.xnat.json');
resolve(require(conf));
} catch (e) {
reject(e);
}
});
};
const readCSV = function(filename){
var self = this;
return new Promise(function(resolve, reject){
var objarr = [];
csvtojson()
.fromFile(filename)
.on('json', function(jsonObj){
objarr.push(jsonObj);
})
.on('end', function(){
resolve(objarr);
})
.on('error', function(err){
reject(err);
})
});
}
var loginprom = undefined;
if(argv["server"]){
var conf = {};
conf.server = argv["server"];
loginprom = xnat.promptUsernamePassword()
.then(function(user){
conf.user = user;
xnat.writeConfFile(conf);
return conf;
});
}else{
loginprom = getConfigFile()
.then(function(conf){
if(promptlogin){
return xnat.promptUsernamePassword()
.then(function(user){
conf.user = user;
xnat.writeConfFile(conf);
return conf;
});
}else{
return conf;
}
})
.catch(function(e){
throw "Config file not found. Use -h or --help to learn how to use this program";
});
}
loginprom
.then(function(conf){
console.log("Setting server url to ", conf.server);
xnat.setXnatUrl(conf.server);
console.log("Login to xnat.", conf.server);
return xnat.login(conf.user);
})
.then(function(){
if(subjectid && label && experimentid){
var params = {
label: label
}
return xnat.setExperiment(projectid, subjectid, experimentid, params);
}else if(subjectid && label){
var params = {
label: label
}
return xnat.setSubject(projectid, subjectid, params);
}else if(csvfilename){
return readCSV(csvfilename)
.then(function(data){
return Promise.map(data, function(d){
if(d.label && d.subject_ID && d.experiment_ID){
return xnat.getSubjectExperiment(projectid, d.subject_ID, d.experiment_ID)
.then(function(res_exp){
if(res_exp.items && res_exp.items.length > 0){
var current_experiment = res_exp.items[0];
if(current_experiment && current_experiment.data_fields && current_experiment.data_fields.label != d.label.trim()){
var params = {
label: d.label.trim()
}
console.log("Renaming experiment:", d.experiment_ID, "to", d.label.trim());
return xnat.setExperiment(projectid, d.subject_ID, d.experiment_ID, params)
.then(function(res){
console.log(res);
})
.catch(function(err){
console.error(err);
});
}
}
return Promise.resolve();
});
}else if(d.label && d.subject_ID){
var params = {
label: d.label.trim()
}
console.log("Renaming subject:", d.subject_ID, "to", d.label);
return xnat.setSubject(projectid, d.subject_ID, params)
.then(function(res){
console.log(res);
})
.catch(function(err){
console.error(err);
});
}else{
console.error("No subject_ID, experiment_ID or label:", d);
return Promise.resolve(false);
}
}, {
concurrency: 1
})
});
}
})
.then(function(uploaded){
return xnat.logout();
})
.catch(function(error){
console.error(error);
return xnat.logout();
});
}