This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
212 lines (194 loc) · 6.98 KB
/
background.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
// https://docs.galaxyproject.org/en/master/api/api.html
// http://bioblend.readthedocs.io/en/latest/api_docs/galaxy/all.html
/************************************************************************************/
/** Observer Pattern */
/************************************************************************************/
var _GALAXY_URL_ = undefined;
var _API_KEY_ = undefined;
var _HISTORY_ID_ = undefined;
var _COLLECTION_NAME_ = undefined;
var _MAP_SIZE_ = undefined;
var ObservableMap = function() {
this.map = {};
}
ObservableMap.prototype.put = function( key, value ) {
this.map[key] = value;
//console.log("ObservableMap size: "+Object.keys(this.map).length);
if (Object.keys(this.map).length === _MAP_SIZE_) {
if (_COLLECTION_NAME_ !== undefined)
createCollection(_GALAXY_URL_, _HISTORY_ID_, _COLLECTION_NAME_, this.map);
}
}
ObservableMap.prototype.setSize = function( size ) {
_MAP_SIZE_ = size;
}
var file_ids = new ObservableMap();
/************************************************************************************/
function autofill() {
var popups = chrome.extension.getViews({type: "popup"});
console.log(popups);
if (popups.length > 0) {
popups[0].autofill(_GALAXY_URL_, _API_KEY_);
}
}
function resetGlobalVariables() {
_GALAXY_URL_ = undefined;
_API_KEY_ = undefined;
_HISTORY_ID_ = undefined;
_COLLECTION_NAME_ = undefined;
_MAP_SIZE_ = undefined;
file_ids = new ObservableMap();
}
// Send selected data to Galaxy.
//function sendToGalaxy(galaxy_url, galaxy_user, galaxy_pass, checkedLinks, collection_name) {
function sendToGalaxy(galaxy_url, api_key, checkedLinks, collection_name) {
resetGlobalVariables();
_GALAXY_URL_ = galaxy_url;
_API_KEY_ = api_key;
_COLLECTION_NAME_ = collection_name;
// TO-DO remove invalid links in checkedLinks
file_ids.setSize(checkedLinks.length);
// start by retrieving the api key for the selected user
// then retrieve the current history id
// finally send links to the upload tool
//retrieveApiKey(galaxy_url, galaxy_user, galaxy_pass, checkedLinks, collection_name);
getCurrentHistoryId(galaxy_url, api_key, checkedLinks, collection_name);
}
/*function retrieveApiKey(galaxy_url, galaxy_user, galaxy_pass, checkedLinks, collection_name) {
var path = "api/authenticate/baseauth";
if (!galaxy_url.endsWith("/"))
path = "/"+path;
var auth_url = galaxy_url+path;
$.ajax({
url: auth_url,
contentType: 'application/json',
headers: {
"Authorization": "Basic " + btoa(galaxy_user + ":" + galaxy_pass)
},
success: function( data, textStatus, jQxhr ){
var api_key = data["api_key"];
console.log("api_key: "+api_key);
getCurrentHistoryId(galaxy_url, api_key, checkedLinks, collection_name);
//return data['api_key'];
},
error: function( jqXhr, textStatus, errorThrown ){
return undefined;
}
});
}*/
function getCurrentHistoryId(galaxy_url, api_key, checkedLinks, collection_name) {
var path = "api/histories/most_recently_used";
if (!galaxy_url.endsWith("/"))
path = "/"+path;
var history_url = galaxy_url+path;
$.ajax({
url: history_url,
type: 'get',
//async: false,
dataType: 'json',
contentType: 'application/json',
success: function( data, textStatus, jQxhr ){
console.log(data);
console.log("history_id: "+data["id"]);
var history_id = data["id"];
_HISTORY_ID_ = history_id;
uploadData(galaxy_url, api_key, history_id, checkedLinks);
},
error: function( jqXhr, textStatus, errorThrown ){
return undefined;
}
});
}
//function uploadData(galaxy_url, api_key, history_id, file_name, file_url, file_type, dbkey) {
function uploadData(galaxy_url, api_key, history_id, checkedLinks) {
var path = "api/tools";
if (!galaxy_url.endsWith("/"))
path = "/"+path;
var upload_url = galaxy_url+path;
var file_type = "auto";
var dbkey = "?";
var files_0_names = "";
var files_0_type = "";
var files_0_url_paste = "";
for (var i=0; i<checkedLinks.length; i++) {
// send data to galaxy
var file_url = checkedLinks[i];
console.log("file_url: "+file_url);
files_0_url_paste += "\n" + file_url;
}
$.ajax({
url: upload_url,
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(
{
"key": api_key,
"tool_id": "upload1",
"history_id": history_id,
"inputs": {
"files_0|url_paste": files_0_url_paste,
"dbkey": dbkey,
"file_type": file_type
}
}
),
success: function( data, textStatus, jQxhr ){
console.log(data);
//return data;
var outputs_arr = data["outputs"];
for (var i=0; i<outputs_arr.length; i++) {
var file_id = outputs_arr[i]["id"];
var file_name = outputs_arr[i]["name"];
//file_ids[file_id] = file_name;
console.log("id: "+file_id+" - name: "+file_name);
file_ids.put(file_id, file_name);
//console.log("file_ids:"+Object.keys(file_ids).length);
}
},
error: function( jqXhr, textStatus, errorThrown ){
return undefined;
}
});
}
function createCollection(galaxy_url, history_id, collection_name, file_ids) {
var path = "api/histories/"+history_id+"/contents";
if (!galaxy_url.endsWith("/"))
path = "/"+path;
var collection_url = galaxy_url+path;
if (Object.keys(file_ids).length > 0) {
element_identifiers = [];
for (var key in file_ids) {
element = {};
element["id"] = key;
element["name"] = file_ids[key];
element["src"] = "hda";
element_identifiers.push(element);
}
$.ajax({
url: collection_url,
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(
{
"type": "dataset_collection",
"collection_type": "list",
"name": collection_name,
"element_identifiers": element_identifiers,
"hide_source_items": true
}
),
success: function( data, textStatus, jQxhr ){
console.log(data);
// TO-DO: hide datasets outside the collection
return data;
},
error: function( jqXhr, textStatus, errorThrown ){
//console.log(textStatus);
//console.log(errorThrown);
return undefined;
}
});
}
}