@js4cytoscape/ndex-client 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/NDEx.js ADDED
@@ -0,0 +1,737 @@
1
+ const axios = require('axios');
2
+
3
+ const CX1_HEADER = {
4
+ numberVerification: [{
5
+ "longNumber": 283213721732712
6
+ }]
7
+ };
8
+
9
+ class NDEx {
10
+ constructor(hostprefix) {
11
+ if (hostprefix === undefined || hostprefix === null || hostprefix === '') {
12
+ throw new Error('NDEx server endpoint base URL is required in client constructor.');
13
+ }
14
+
15
+ // axios needs http to be at the front of the url.
16
+ // Otherwise any request will be redirected to localhost and fail
17
+ // with an ambiguous reason
18
+ let httpRegex = new RegExp("^(http|https)://", "i");
19
+ if (!httpRegex.test(hostprefix)) {
20
+ throw new Error(`"http://" or "https://" must be prefixed to the input url: ${hostprefix}`);
21
+ }
22
+
23
+ this._host = hostprefix;
24
+ this._v3baseURL = this._host.substring(0,this.host.lastIndexOf('v2')) + 'v3';
25
+ }
26
+ get host() { return this._host;}
27
+
28
+ get googleAuth() {
29
+ return this._googleAuth;
30
+ }
31
+
32
+ SetGoogleAuth(googleAuthObj) {
33
+ if (googleAuthObj !== undefined) {
34
+ this._googleAuth = googleAuthObj;
35
+ this._authType = 'g'; // valid values are 'g','b' or undefined
36
+ }
37
+ }
38
+
39
+ setAuthToken(authToken) {
40
+ if (authToken !== undefined ) {
41
+ this._authToken = authToken;
42
+ this._authType = 'g'; // valid values are 'g','b' or undefined
43
+ }
44
+ }
45
+
46
+ get authenticationType() {
47
+ return this._authType;
48
+ }
49
+
50
+ get username() {
51
+ return this._username;
52
+ }
53
+
54
+ // get authStr() { return this._authStr;}
55
+ get password() { return this._password;}
56
+
57
+ setBasicAuth(accountname, password) {
58
+ if (accountname !== undefined && accountname != null && accountname !== '') {
59
+ this._username = accountname;
60
+ this._password = password;
61
+ this._authType = 'b';
62
+ // this._authStr = 'Basic ' + btoa(username + ':' + password);
63
+ return;
64
+ }
65
+ this._username = undefined;
66
+ this._password = undefined;
67
+ this._authType = undefined;
68
+ // this._authStr = undefined;
69
+ }
70
+
71
+ _httpGetOpenObj(URL) {
72
+ let APIEndPointPrefix = this.host ;
73
+
74
+ return new Promise(function (resolve, reject) {
75
+ axios({
76
+ method: 'get',
77
+ url: URL,
78
+ baseURL: APIEndPointPrefix
79
+ }).then((response) => {
80
+ if (response.status === 200) {
81
+ return resolve(response.data);
82
+ }
83
+ return reject(response);
84
+ },
85
+ (response) =>{return reject(response);}
86
+ );
87
+ });
88
+ }
89
+
90
+ // access endpoints that supports authentication
91
+
92
+ _getIdToken() {
93
+ return this._authToken ? this._authToken : this._googleAuth.getAuthInstance().currentUser.get().getAuthResponse().id_token;
94
+ }
95
+
96
+ _setAuthHeader(config) {
97
+ if (this._authType === 'b') {
98
+ config ['auth'] = { username: this.username,
99
+ password: this.password};
100
+ } else if (this.authenticationType === 'g') {
101
+ const idToken = this._getIdToken();
102
+
103
+ if (config['headers'] === undefined) {config['headers'] = {};}
104
+ config['headers']['Authorization'] = 'Bearer ' + idToken;
105
+ }
106
+ }
107
+
108
+ _httpGetProtectedObjAux (prefix, URL, parameters) {
109
+
110
+ let config = {
111
+ method: 'get',
112
+ url: URL,
113
+ baseURL: prefix
114
+ };
115
+
116
+ this._setAuthHeader(config);
117
+
118
+ if (parameters !== undefined) {
119
+ config['params'] = parameters;
120
+ }
121
+ return new Promise(function (resolve, reject) {
122
+ axios(config).then((response) => {
123
+ if (response.status === 200) {
124
+ return resolve(response.data);
125
+ }
126
+ return reject(response);
127
+ },
128
+ (response) => {
129
+ return reject(response);
130
+ }
131
+ );
132
+ });
133
+ }
134
+
135
+ _httpGetV3ProtectedObj(URL, parameters) {
136
+ return this._httpGetProtectedObjAux(this._v3baseURL, URL, parameters);
137
+ }
138
+
139
+ _httpGetProtectedObj(URL, parameters) {
140
+ return this._httpGetProtectedObjAux(this.host, URL,parameters);
141
+ }
142
+
143
+ _httpPostProtectedObjAux(prefix, URL, parameters, data) {
144
+ let config = {
145
+ method: 'post',
146
+ url: URL,
147
+ baseURL: prefix
148
+ };
149
+
150
+ this._setAuthHeader(config);
151
+
152
+ if (parameters !== undefined) {
153
+ config['params'] = parameters;
154
+ }
155
+
156
+ config.data = data;
157
+ return new Promise(function (resolve, reject) {
158
+ axios(config).then((response) => {
159
+ if (response.status >= 200 && response.status <= 299) {
160
+ return resolve(response.data);
161
+ }
162
+ return reject(response);
163
+ },
164
+ (response) => {
165
+ return reject(response);
166
+ }
167
+ );
168
+ });
169
+ }
170
+
171
+ _httpPostProtectedObj(URL, parameters, data) {
172
+ return this._httpPostProtectedObjAux(this.host,URL,parameters, data);
173
+ }
174
+
175
+ _httpPostV3ProtectedObj(URL, parameters, data) {
176
+ return this._httpPostProtectedObjAux(this._v3baseURL,URL,parameters, data);
177
+ }
178
+
179
+ _httpPutObj(URL, parameters, data) {
180
+ let config = {
181
+ method: 'put',
182
+ url: URL,
183
+ baseURL: this.host
184
+ };
185
+
186
+ this._setAuthHeader(config);
187
+
188
+ if (parameters !== undefined) {
189
+ config['params'] = parameters;
190
+ }
191
+
192
+ config.data = data;
193
+ return new Promise(function (resolve, reject) {
194
+ axios(config).then((response) => {
195
+ if (response.status >= 200 && response.status <= 299) {
196
+ return resolve(response.data);
197
+ }
198
+ return reject(response);
199
+ },
200
+ (response) => {
201
+ return reject(response);
202
+ }
203
+ );
204
+ });
205
+ }
206
+
207
+ _httpDeleteObj(URL, parameters, data) {
208
+ let config = {
209
+ method: 'delete',
210
+ url: URL,
211
+ baseURL: this.host
212
+ };
213
+
214
+ this._setAuthHeader(config);
215
+
216
+ if (parameters !== undefined) {
217
+ config['params'] = parameters;
218
+ }
219
+
220
+ if (data !== undefined) {
221
+ config['data'] = data;
222
+ }
223
+
224
+ return new Promise(function (resolve, reject) {
225
+ axios(config).then((response) => {
226
+ if (response.status >= 200 && response.status <= 299) {
227
+ return resolve(response.data);
228
+ }
229
+ return reject(response);
230
+ },
231
+ (response) => {
232
+ return reject(response);
233
+ }
234
+ );
235
+ });
236
+
237
+ }
238
+
239
+ /* admin functions */
240
+
241
+ getStatus() {
242
+ return this._httpGetOpenObj('admin/status');
243
+ }
244
+
245
+ /* user functions */
246
+
247
+ getSignedInUser() {
248
+ if (this._authType == null) {
249
+ throw new Error('Authentication parameters are missing in NDEx client.');
250
+ }
251
+ return this._httpGetProtectedObj('user', {valid: true});
252
+ }
253
+
254
+ getUser(uuid) {
255
+ return this._httpGetProtectedObj('user/' + uuid);
256
+ }
257
+
258
+ getAccountPageNetworks(offset, limit) {
259
+ return new Promise((resolve, reject) => {
260
+ this.getSignedInUser().then((user) => {
261
+ let params = {};
262
+
263
+ if (offset !== undefined) {
264
+ params.offset = offset;
265
+ }
266
+ if (limit !== undefined) {
267
+ params.limit = limit;
268
+ }
269
+ this._httpGetProtectedObj('/user/' + user.externalId + '/networksummary', params).then(
270
+ (networkArray) => {resolve(networkArray);}, (err) => {reject(err);}
271
+ );
272
+ }, (err) => {reject(err);});
273
+
274
+ });
275
+ }
276
+
277
+ getAccountPageNetworksByUUID(uuid, offset, limit) {
278
+ let params = {};
279
+
280
+ if (offset !== undefined) {
281
+ params.offset = offset;
282
+ }
283
+ if (limit !== undefined) {
284
+ params.limit = limit;
285
+ }
286
+ return this._httpGetProtectedObj('user/' + uuid + '/networksummary', params);
287
+ }
288
+
289
+ /* group functions */
290
+
291
+ // return the UUID of the created group to the resolver
292
+ createGroup(group) {
293
+ return new Promise((resolve, reject)=> {
294
+ this._httpPostProtectedObj('group', undefined, group).then(
295
+ (response) => {
296
+ let uuidr = response.split('/');
297
+
298
+ let uuid = uuidr[uuidr.length - 1];
299
+
300
+ return resolve(uuid);
301
+ },
302
+ (err) => {reject(err);}
303
+ );
304
+ });
305
+ }
306
+
307
+ getGroup(uuid) {
308
+ return this._httpGetProtectedObj('group/' + uuid);
309
+ }
310
+
311
+ updateGroup(group) {
312
+ return new Promise((resolve, reject)=> {
313
+ this._httpPutObj('group/' + group.externalId, undefined, group).then(
314
+ (response) => {
315
+ return resolve(response);
316
+ },
317
+ (err) => {reject(err);}
318
+ );
319
+ });
320
+ }
321
+
322
+ deleteGroup(uuid) {
323
+ return this._httpDeleteObj('group/' + uuid);
324
+ }
325
+
326
+ /* network functions */
327
+
328
+ getRawNetwork(uuid, accessKey) {
329
+
330
+ let parameters = {};
331
+
332
+ if (accessKey !== undefined) {
333
+ parameters = {accesskey: accessKey};
334
+ }
335
+
336
+ return this._httpGetProtectedObj('network/' + uuid, parameters);
337
+
338
+ }
339
+
340
+ getCX2Network(uuid, accessKey) {
341
+
342
+ let parameters = {};
343
+
344
+ if (accessKey !== undefined) {
345
+ parameters = {accesskey: accessKey};
346
+ }
347
+
348
+ return this._httpGetV3ProtectedObj('networks/' + uuid, parameters);
349
+
350
+ }
351
+
352
+ getNetworkSummary(uuid, accessKey) {
353
+ let parameters = {};
354
+
355
+ if (accessKey !== undefined) {
356
+ parameters = {accesskey: accessKey};
357
+ }
358
+
359
+ return this._httpGetProtectedObj('network/' + uuid + '/summary', parameters);
360
+ }
361
+
362
+ createNetworkFromRawCX(rawCX, parameters) {
363
+ return new Promise((resolve, reject) =>{
364
+ this._httpPostProtectedObj('network', parameters, rawCX).then(
365
+ (response) => {
366
+ let uuidr = response.split('/');
367
+
368
+ let uuid = uuidr[uuidr.length - 1];
369
+
370
+ return resolve(uuid);
371
+ },
372
+ (err) => {reject(err);}
373
+ );
374
+ });
375
+ }
376
+
377
+ updateNetworkFromRawCX(uuid, rawcx) {
378
+ return this._httpPutObj('network/' + uuid, undefined, rawcx);
379
+ }
380
+
381
+ deleteNetwork(uuid) {
382
+ return this._httpDeleteObj('network/' + uuid);
383
+ }
384
+
385
+ /* task functions */
386
+
387
+ /* search functions */
388
+ searchUsers(searchTerms, start, size) {
389
+ let params = {};
390
+
391
+ if (start !== undefined) {
392
+ params.start = start;
393
+ }
394
+ if (size !== undefined) {
395
+ params.limit = size;
396
+ }
397
+ let data = {searchString: searchTerms};
398
+
399
+ return this._httpPostProtectedObj('search/user', params, data);
400
+
401
+ }
402
+
403
+ searchGroups(searchTerms, start, size) {
404
+ let params = {};
405
+
406
+ if (start !== undefined) {
407
+ params.start = start;
408
+ }
409
+ if (size !== undefined) {
410
+ params.limit = size;
411
+ }
412
+ let data = {searchString: searchTerms};
413
+
414
+ return this._httpPostProtectedObj('search/group', params, data);
415
+
416
+ }
417
+
418
+ searchNetworks(searchTerms, start, size, optionalParameters) {
419
+ let params = {};
420
+
421
+ if (start !== undefined) {
422
+ params.start = start;
423
+ }
424
+ if (size !== undefined) {
425
+ params.limit = size;
426
+ }
427
+
428
+ let data = {searchString: searchTerms};
429
+
430
+ if (optionalParameters !== undefined) {
431
+ if (optionalParameters.permission !== undefined) {
432
+ data.permission = optionalParameters.permission;
433
+ }
434
+ if (optionalParameters.includeGroups !== undefined) {
435
+ data.includeGroups = optionalParameters.includeGroups;
436
+ }
437
+ if (optionalParameters.accountName !== undefined) {
438
+ data.accountName = optionalParameters.accountName;
439
+ }
440
+ }
441
+
442
+ return this._httpPostProtectedObj('search/network', params, data);
443
+ }
444
+
445
+ neighborhoodQuery(uuid, searchTerms, saveResult, parameters) {
446
+ let params = {};
447
+
448
+ if (saveResult !== undefined && saveResult === true) {params.save = true;}
449
+
450
+ let data = {searchString: searchTerms,
451
+ searchDepth: 1};
452
+
453
+ if (parameters !== undefined) {
454
+ if (parameters.searchDepth !== undefined) {
455
+ data.searchDepth = parameters.searchDepth;
456
+ }
457
+ if (parameters.edgeLimit !== undefined) {
458
+ data.edgeLimit = parameters.edgeLimit;
459
+ }
460
+ if (parameters.errorWhenLimitIsOver !== undefined) {
461
+ data.errorWhenLimitIsOver = parameters.errorWhenLimitIsOver;
462
+ }
463
+ if (parameters.directOnly !== undefined) {
464
+ data.directOnly = parameters.directOnly;
465
+ }
466
+ }
467
+ return this._httpPostProtectedObj('search/network/' + uuid + '/query', params, data);
468
+
469
+ }
470
+
471
+ interConnectQuery(uuid, searchTerms, saveResult, parameters) {
472
+ let params = {};
473
+
474
+ if (saveResult !== undefined && saveResult === true) {params.save = true;}
475
+
476
+ let data = {searchString: searchTerms};
477
+
478
+ if (parameters !== undefined) {
479
+ if (parameters.edgeLimit !== undefined) {
480
+ data.edgeLimit = parameters.edgeLimit;
481
+ }
482
+ if (parameters.errorWhenLimitIsOver !== undefined) {
483
+ data.errorWhenLimitIsOver = parameters.errorWhenLimitIsOver;
484
+ }
485
+ }
486
+ return this._httpPostProtectedObj('search/network/' + uuid + '/interconnectquery', params, data);
487
+
488
+ }
489
+
490
+ /* batch functions */
491
+ getUsersByUUIDs(uuidList) {
492
+ return this._httpPostProtectedObj('batch/user', undefined, uuidList);
493
+ }
494
+
495
+ getGroupsByUUIDs(uuidList) {
496
+ return this._httpPostProtectedObj('batch/group', undefined, uuidList);
497
+ }
498
+
499
+ getNetworkSummariesByUUIDs(uuidList, accessKey) {
500
+ let parameter = accessKey === undefined ? undefined :
501
+ {accesskey: accessKey};
502
+
503
+ return this._httpPostProtectedObj('batch/network/summary', parameter, uuidList);
504
+ }
505
+
506
+ getNetworkPermissionsByUUIDs(uuidList) {
507
+ return this._httpPostProtectedObj('batch/network/permission', undefined, uuidList);
508
+ }
509
+
510
+ exportNetworks(exportJob) {
511
+ return this._httpPostProtectedObj('batch/network/export', undefined, exportJob);
512
+ }
513
+
514
+ /* network set functions */
515
+ createNetworkSet({name, description}){
516
+ return new Promise((resolve, reject)=> {
517
+ this._httpPostProtectedObj('networkset', undefined, {name, description}).then(
518
+ (response) => {
519
+ let uuidr = response.split('/');
520
+
521
+ let uuid = uuidr[uuidr.length - 1];
522
+
523
+ return resolve(uuid);
524
+ },
525
+ (err) => {reject(err);}
526
+ );
527
+ });
528
+ }
529
+
530
+ updateNetworkSet(uuid, {name, description}){
531
+ return new Promise((resolve, reject)=> {
532
+ this._httpPutObj('networkset/' + uuid, undefined, {uuid, name, description}).then(
533
+ (response) => {
534
+ return resolve(response);
535
+ },
536
+ (err) => {reject(err);}
537
+ );
538
+ });
539
+ }
540
+
541
+ deleteNetworkSet(uuid){
542
+ return this._httpDeleteObj('networkset/' + uuid);
543
+ }
544
+
545
+ getNetworkSet(uuid, accessKey){
546
+ let parameters = {};
547
+
548
+ if (accessKey !== undefined) {
549
+ parameters = {accesskey: accessKey};
550
+ }
551
+
552
+ return this._httpGetProtectedObj('networkset/' + uuid, parameters);
553
+ }
554
+
555
+ addToNetworkSet(networkSetUUID, networkUUIDs){
556
+ return this._httpPostProtectedObj('networkset/' + networkSetUUID + '/members', undefined, networkUUIDs);
557
+ }
558
+
559
+ deleteFromNetworkSet(networkSetUUID, networkUUIDS){
560
+ return this._httpDeleteObj('networkset/' + networkSetUUID + '/members', undefined, networkUUIDS);
561
+ }
562
+
563
+ updateNetworkSetSystemProperty(networksetUUID, data){
564
+ return this._httpPutObj('networkset/' + networksetUUID + '/systemproperty', undefined, data);
565
+ }
566
+
567
+
568
+ /* undocumented functions. Might be changed ... */
569
+
570
+ // layout update is a list of objects:
571
+ // [{ node: 1, x: 100, y: 100}, {node: 2, x: 1003, y: 200 }...]
572
+ updateCartesianLayoutAspect(uuid, nodePositions){
573
+
574
+ // 1. get node coordinates from cy.js model in the format of cartesian aspect
575
+ // 2. generate CX for the put request
576
+ // 3. example
577
+ // [{
578
+ // "numberVerification": [
579
+ // {
580
+ // "longNumber": 283213721732712
581
+ // }
582
+ // ]
583
+ // }, {
584
+ // "metaData": [{
585
+ // "name": "cartesianLayout", "elementCount": 100 // num nodes here
586
+ // }],
587
+ // "cartesianLayout": [{
588
+ // "node": 100, "x": 23213.12, "y": 3234.5
589
+ // }]
590
+ // }]
591
+ const cartesianLayoutUpdate = [
592
+ CX1_HEADER,
593
+ {
594
+ metaData: [{
595
+ name: 'cartesianLayout',
596
+ elementCount: nodePositions.length
597
+ }]
598
+ },
599
+ {
600
+ cartesianLayout: nodePositions
601
+ },
602
+ {
603
+ status: [{
604
+ error: '',
605
+ success: true
606
+ }]
607
+ }
608
+ ]
609
+
610
+ return this._httpPutObj(`network/${uuid}/aspects`, undefined, cartesianLayoutUpdate);
611
+ }
612
+
613
+ // new v3 functions
614
+ getRandomEdges(uuid, limit, accessKey ) {
615
+
616
+ if ( limit <=0)
617
+ throw new Error("Value of parameter limit has to be greater than 0.");
618
+
619
+ let parameters = {
620
+ size: limit,
621
+ method: "random"
622
+ };
623
+
624
+ if (accessKey !== undefined) {
625
+ parameters = {accesskey: accessKey};
626
+ }
627
+
628
+ return this._httpGetV3ProtectedObj('networks/' + uuid + '/aspects/edges', parameters);
629
+ }
630
+
631
+ getMetaData(uuid, accessKey) {
632
+ let parameters = {
633
+ };
634
+
635
+ if (accessKey !== undefined) {
636
+ parameters ['accesskey'] =accessKey;
637
+ }
638
+
639
+ return this._httpGetProtectedObj('network/' + uuid + '/aspect', parameters);
640
+ }
641
+
642
+ getAspectElements(uuid, aspectName, limit, accessKey ) {
643
+
644
+ let parameters = {
645
+ };
646
+
647
+ if ( limit !== undefined) {
648
+ parameters = {
649
+ size: limit
650
+ };
651
+ }
652
+
653
+ if (accessKey !== undefined) {
654
+ parameters ['accesskey'] =accessKey;
655
+ }
656
+
657
+ return this._httpGetV3ProtectedObj('networks/' + uuid + '/aspects/' + aspectName, parameters);
658
+ }
659
+
660
+ getFilteredEdges(uuid, columnName, valueString, operator, limit, order, format, accessKey ) {
661
+
662
+ let parameters = {
663
+ };
664
+
665
+
666
+ if ( limit !== undefined) {
667
+ parameters = {
668
+ size: limit
669
+ };
670
+ }
671
+
672
+ if ( order !== undefined) {
673
+ parameters['order'] = order;
674
+ }
675
+
676
+ if (accessKey !== undefined) {
677
+ parameters ['accesskey'] = accessKey;
678
+ }
679
+
680
+ if ( format !== undefined ) {
681
+ parameters['format'] = format;
682
+ }
683
+
684
+ let data = {
685
+ "name": columnName,
686
+ "value": valueString,
687
+ "operator": operator
688
+ };
689
+
690
+ return this._httpPostV3ProtectedObj('/search/networks/' + uuid + '/edges', parameters, data);
691
+ }
692
+
693
+ getCX2MetaData(uuid, accessKey ) {
694
+
695
+ let parameters = {
696
+ };
697
+
698
+ if (accessKey !== undefined) {
699
+ parameters ['accesskey'] =accessKey;
700
+ }
701
+
702
+ return this._httpGetV3ProtectedObj('networks/' + uuid + '/aspects', parameters);
703
+ }
704
+
705
+ cancelDOIRequest(uuid) {
706
+ const cancelBody =
707
+ {type: "Cancel_DOI", networkId: uuid}
708
+ ;
709
+
710
+ return this._httpPostProtectedObj('admin/request', {}, cancelBody);
711
+ }
712
+
713
+
714
+ // unstable function to upload CX2 to NDEx
715
+ createNetworkFromRawCX2(rawCX2, makePublic = false) {
716
+ let config = {
717
+ method: 'post',
718
+ url: 'networks',
719
+ baseURL: this._v3baseURL,
720
+ params: {
721
+ visibility: makePublic ? 'PUBLIC' : 'PRIVATE'
722
+ }
723
+ };
724
+
725
+ this._setAuthHeader(config);
726
+ config.data = rawCX2;
727
+
728
+ return axios(config).then(res => {
729
+ let { location } = res.headers;
730
+ let uuid = location.split('/').pop();
731
+
732
+ return { uuid };
733
+ });
734
+ }
735
+
736
+ }
737
+ module.exports = { NDEx };