-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
255 lines (232 loc) · 7.84 KB
/
sync.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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 xnat_target = new xnat.constructor();
const csvtojson = require("csvtojson");
const help = function(){
console.log("Sync a local xnat to a remote one. The local patient id (patient label) and session id (session label) will be used in the remote xnat instance");
console.log("Usage: node", process.argv[1],"-csv <filename>");
console.log("Options:");
console.log("-p <project id>, project id in local XNAT");
console.log("--pid <patient id>");
console.log("--sid <session id>");
console.log("--tp <target project id>");
console.log("--sync_dicom <Sync dicom resource 1 or 0, default 1>");
console.log("--sync_qc <Sync quality control 1 or 0, default 1>");
console.log("--sync_resources <Sync the following resources, use many '--sync_resources' flag if there is more than one resource to sync>");
}
if(!argv["p"] || !argv["pid"] || !argv["sid"] || argv["h"] || argv["help"]){
help();
process.exit(1);
}
var csvfilename = argv["csv"];
var projectid = argv["p"];
var target_projectid = argv["tp"];
var subjectid = argv["pid"];
var sessionid = argv["sid"];
var sync_resources = _.compact(_.isArray(argv["sync_resources"])? argv["sync_resources"] : [argv["sync_resources"]]);
var sync_dicom_resource = argv["sync_dicom"] == undefined? 1 : argv["sync_dicom"];
var sync_qc = argv["sync_qc"] == undefined? 1 : argv["sync_qc"];
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);
})
});
}
const testSubjectSessionRec = function(projectid, pid, expid, repeat){
return xnat_target.getSubjectSession(projectid, pid)
.then(function(sessions){
if(sessions.ResultSet && sessions.ResultSet.Result){
var allsessions = sessions.ResultSet.Result;
var session = _.find(allsessions, function(sess){
return sess.label == expid;
});
if(session){
return Promise.resolve();
}else{
var wait;
if(repeat > 0){
console.log("Waiting:", repeat, "minute(s) left, the session is not there yet");
wait = Promise.delay(60000)
.then(function(){
repeat--;
return testSubjectSessionRec(projectid, pid, expid, repeat);
});
}else{
wait = Promise.resolve();
}
return wait;
}
}
return Promise.reject("Subject exists but session not found.");
})
.catch(function(e){
var wait;
if(repeat > 0){
console.error(e);
console.log("Waiting:", repeat, "minute(s) left, the session is not there yet");
wait = Promise.delay(60000)
.then(function(){
repeat--;
return testSubjectSessionRec(projectid, pid, expid, repeat);
});
}else{
wait = Promise.resolve();
}
return wait;
});
}
const waitDicomSession = function(projectid, pid, expid, repeat){
return testSubjectSessionRec(projectid, pid, expid, repeat);
}
xnat.start()
.then(function(){
return xnat_target.start('intradb');
})
.then(function(){
return xnat.getSubjectExperiment(projectid, subjectid, sessionid);
})
.then(function(res){
var scan = [];
_.each(res.items, function(item){
_.each(item.children, function(children){
_.each(children.items, function(items){
scan.push(items.data_fields);
});
});
});
return scan;
})
.then(function(scan_items_data_fields){
var sync_dicom_resource_promise = Promise.resolve();
if(sync_dicom_resource){
console.log("Syncing dicom images ...");
sync_dicom_resource_promise = Promise.map(scan_items_data_fields, function(data_fields){
return xnat.getScanFiles(projectid, subjectid, sessionid, data_fields.ID)
.then(function(files){
return _.filter(files, function(file){
return file.collection && file.collection == "DICOM";
})
})
.then(function(res){
return _.flatten(res);
})
.then(function(scan_files){
return Promise.map(scan_files, function(files){
return xnat.getFileStream(files.URI)
.then(function(fstream){
// return new Promise(function(resolve, reject){
// var wstream = fs.createWriteStream(path.join("/work/jprieto/source/xnat-rest/temp_out", files.Name));
// fstream.pipe(wstream);
// wstream.on('finish', function(err){
// if(err){
// reject(files);
// }else{
// resolve();
// }
// });
// });
return xnat_target.uploadImageStream(target_projectid, subjectid, sessionid, fstream);
});
}, {concurrency: 1});
});
}, {concurrency: 1})
.catch(function(e){
console.error(e);
return Promise.resolve();
});
}
return sync_dicom_resource_promise
.then(function(){
return waitDicomSession(target_projectid, subjectid, sessionid, 30);
})
.then(function(){
var sync_qc_promise = Promise.resolve();
if(sync_qc){
console.log("Syncing quality control now ...");
sync_qc_promise = Promise.map(scan_items_data_fields, function(data_fields){
return xnat.getScanData(projectid, subjectid, sessionid, data_fields.ID)
.then(function(scan_data){
if(scan_data.items && scan_data.items[0] && scan_data.items[0].data_fields){
var quality = scan_data.items[0].data_fields["quality"]? scan_data.items[0].data_fields["quality"]: " ";
var note = scan_data.items[0].data_fields["note"]? scan_data.items[0].data_fields["note"] : " ";
return xnat_target.setScanData(target_projectid, subjectid, sessionid, data_fields.ID, {
"xsiType": "xnat:mrScanData",
"xnat:mrScanData/quality": quality,
"xnat:mrScanData/note": note
});
}
return Promise.resolve();
});
}, {concurrency: 1})
.catch(function(e){
console.error(e);
return Promise.resolve();
});
}
return sync_qc_promise;
})
.then(function(){
return Promise.map(sync_resources, function(resource){
console.log("Syncing resource", resource, "...");
return Promise.map(scan_items_data_fields, function(data_fields){
return xnat.getScanFiles(projectid, subjectid, sessionid, data_fields.ID)
.then(function(files){
return _.filter(files, function(file){
return file.collection && file.collection == resource;
})
})
.then(function(res){
return _.flatten(res);
})
.then(function(scan_files){
if(scan_files.length > 0){
return xnat_target.createResources(target_projectid, subjectid, sessionid, data_fields.ID, resource)
.catch(function(err){
console.error(err);
return Promise.resolve();
})
.then(function(){
return Promise.map(scan_files, function(files){
return xnat.getFileStream(files.URI)
.then(function(fstream){
console.log(data_fields.ID, files.URI)
return xnat_target.uploadConvertedImageStream(target_projectid, subjectid, sessionid, data_fields.ID, files.URI, fstream);
});
}, {concurrency: 1});
})
.catch(function(err){
console.error(err);
return Promise.resolve();
})
}else{
return Promise.resolve();
}
});
}, {concurrency: 1});
});
});
})
.then(function(){
return Promise.all([xnat.logout(), xnat_target.logout()]);
})
.catch(function(e){
console.error(e);
return Promise.all([xnat.logout(), xnat_target.logout()]);
})
}