Created
June 13, 2015 07:48
-
-
Save kellrott/ab68ecc1916964f1c2bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This script scans a directory for files then loads the file into library | |
Sample call: | |
python galaxy_lib_sync.py [--apikey <api_key>] <api_url> /data/folder "API Imports" | |
NOTE: The upload method used requires the data library filesystem upload allow_library_path_paste | |
""" | |
import os | |
import shutil | |
import sys | |
import re | |
import json | |
import time | |
import requests | |
import argparse | |
from glob import glob | |
GALAXY_API_KEY_FILE = "/etc/galaxy/api.key" | |
class RemoteGalaxy(object): | |
def __init__(self, url, api_key): | |
self.url = url | |
self.api_key = api_key | |
def get(self, path): | |
c_url = self.url + path | |
params = {} | |
params['key'] = self.api_key | |
req = requests.get(c_url, params=params) | |
return req.json() | |
def post(self, path, payload): | |
c_url = self.url + path | |
params = {} | |
params['key'] = self.api_key | |
print "POSTING", c_url, json.dumps(payload) | |
req = requests.post(c_url, data=json.dumps(payload), params=params, headers = {'Content-Type': 'application/json'} ) | |
return req.json() | |
def post_text(self, path, payload, params=None): | |
c_url = self.url + path | |
if params is None: | |
params = {} | |
params['key'] = self.api_key | |
print "POSTING", c_url, json.dumps(payload) | |
req = requests.post(c_url, data=json.dumps(payload), params=params, headers = {'Content-Type': 'application/json'} ) | |
return req.text | |
class LibrarySync: | |
def __init__(self, remote_galaxy, library_name): | |
self.remote_galaxy = remote_galaxy | |
res = remote_galaxy.post("/api/search", {"query" : "select * from library where name='%s' and deleted=False" % (library_name)}) | |
if 'error' in res: | |
raise Exception(res['error']) | |
library_id = None | |
for row in res['results']: | |
library_id = row['id'] | |
if library_id is None: | |
lib_create_data = {'name':library_name} | |
library = remote_galaxy.post('/api/libraries', lib_create_data) | |
library_id = library['id'] | |
self.library_id = library_id | |
self.path_map = {} | |
def get_library_folder_id(self, folder): | |
library_path = "/" + "/".join(folder) | |
if library_path not in self.path_map: | |
res = self.remote_galaxy.post("/api/search", {"query" : "select * from library_folder where library_path='%s' and parent_library_id='%s'" % (library_path, self.library_id)}) | |
if 'error' in res: | |
raise Exception(res['error']) | |
id_value = None | |
for row in res['results']: | |
id_value = row['id'] | |
if id_value is not None: | |
self.path_map[library_path] = id_value | |
else: | |
parentId = self.get_library_folder_id(folder[:-1]) | |
if parentId is not None: | |
create_request = { "create_type" : 'folder', 'folder_id' : parentId, 'name' : folder[-1] } | |
res = self.remote_galaxy.post("/api/libraries/%s/contents" % (self.library_id), create_request) | |
print "Created Folder:", res | |
id_value = res[0]['id'][1:] #remove preceeding F (why do I have to do this?) | |
self.path_map[library_path] = id_value | |
else: | |
return None | |
return self.path_map[library_path] | |
def get_library_dataset_id(self, folder_id, dataset_name): | |
res = self.remote_galaxy.post("/api/search", {'query' : "select * from library_dataset where folder_id='%s' and name='%s'" % (folder_id, dataset_name)} ) | |
if 'error' in res: | |
raise Exception(res['error']) | |
dataset_id = None | |
for row in res['results']: | |
dataset_id = row['id'] | |
return dataset_id | |
def library_paste_file(self, library_folder_id, name, datapath, meta=None): | |
data = {} | |
data['folder_id'] = library_folder_id | |
data['file_type'] = 'auto' | |
data['name'] = name | |
data['dbkey'] = '' | |
data['upload_option'] = 'upload_paths' | |
data['create_type'] = 'file' | |
data['link_data_only'] = 'link_to_files' | |
data['filesystem_paths'] = datapath | |
if meta is not None: | |
data['extended_metadata'] = meta | |
libset = self.remote_galaxy.post("/api/libraries/%s/contents" % self.library_id, data) | |
return libset | |
def item_rename(self, library_folder_id, item_id, name, info, message): | |
#rename the entry | |
#/library_common/ldda_edit_info?library_id=03501d7626bd192f&show_deleted=False&cntrller=library_admin&folder_id=7ca8f1b7f24e5a2d&use_panels=False | |
self.remote_galaxy.post_text( "/library_common/ldda_edit_info", | |
{ | |
'name' : name, | |
'message' : message, | |
'info' : info | |
}, | |
{ | |
'show_deleted' : 'False', | |
'cntrller' : 'library_admin', | |
'folder_id' : library_folder_id, | |
'id' : item_id, | |
'use_panels' : 'False', | |
'library_id' : self.library_id, | |
} | |
) | |
def main(api_key, api_url, data_dir, data_library ): | |
remote_galaxy = RemoteGalaxy(api_url, api_key) | |
lsync = LibrarySync(remote_galaxy, data_library ) | |
library_folder_id = lsync.get_library_folder_id("/") | |
for file_path in glob( os.path.join(os.path.abspath(data_dir), "*") ): | |
if os.path.isfile(file_path): | |
dataset_name = os.path.basename(file_path) | |
ldda_id = lsync.get_library_dataset_id(library_folder_id, dataset_name) | |
if ldda_id is None: | |
new_ent = lsync.library_paste_file(library_folder_id, dataset_name, file_path) | |
print new_ent | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--apikey", default=None) | |
parser.add_argument("api_url") | |
parser.add_argument("data_dir") | |
parser.add_argument("data_library") | |
args = parser.parse_args() | |
if args.apikey is None: | |
if os.path.exists( GALAXY_API_KEY_FILE ): | |
with open( GALAXY_API_KEY_FILE ) as handle: | |
args.apikey = handle.read().rstrip() | |
else: | |
print "Need Galaxy API key: --apikey" | |
main(args.apikey, args.api_url, args.data_dir, args.data_library ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment