annotate data_stores/vdb_kipper.py @ 2:269d246ce6d0 draft default tip

Uploaded
author damion
date Fri, 23 Oct 2015 17:53:29 -0400
parents 5c5027485f7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 #!/usr/bin/python
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 ## ********************************* Kipper *************************************
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3 ##
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5 import vdb_common
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 import vdb_data_stores
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7 import subprocess
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8 import json
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9 import glob
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10 import os
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 import sys
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13 class VDBKipperDataStore(vdb_data_stores.VDBDataStore):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15 metadata = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16 versions = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 command = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 def __init__(self, retrieval_obj, spec_file_id):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21 Metadata (version list) is expected in "master/" subfolder of data_store_path on server.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23 Future: allow for more than one database / .md file in a folder?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25 super(VDBKipperDataStore, self).__init__(retrieval_obj, spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26 # Ensure we're working with this tool's version of Kipper.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27 self.command = os.path.join(os.path.dirname(sys._getframe().f_code.co_filename),'kipper.py')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 self.versions = []
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30 metadata_path = os.path.join(self.data_store_path,'master','*.md')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31 metadata_files = glob.glob(metadata_path)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 if len(metadata_files) == 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34 return # Handled by empty list error in versioned_data_form.py
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36 metadata_file = metadata_files[0]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38 with open(metadata_file,'r') as metadata_handle:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39 self.metadata = json.load(metadata_handle)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40 for volume in self.metadata['volumes']:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41 self.versions.extend(volume['versions'])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 if len(self.versions) == 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44 print "Error: Unable to locate metadata file: " + metadata_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45 sys.exit(1)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47 self.versions = sorted(self.versions, key=lambda x: x['id'], reverse=True)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50 def get_version(self, version_name):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52 Trigger populating of the appropriate version folder.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53 Returns path information to version folder, and its appropriate library label.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 @param version_id string
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58 version = self.get_metadata_version(version_name)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60 if not version:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61 print 'Error: Galaxy was not able to find the given version id in the %s data store.' % self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62 sys.exit( 1 )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64 version_name = version['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65 self.version_path = os.path.join(self.data_store_path, version_name)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66 self.version_label = vdb_common.lightDate(version['created']) + '_v' + version_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68 print self.library_label_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69 print self.version_label
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71 self.library_version_path = os.path.join(self.library_label_path, self.version_label)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73 if not os.path.exists(self.version_path):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75 os.mkdir(self.version_path)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76 except:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77 print 'Error: Galaxy was not able to create data store folder "%s". Check permissions?' % self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78 sys.exit( 1 )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80 # Generate cache if folder is empty (can take a while):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 if os.listdir(self.version_path) == []:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83 db_file = os.path.join(self.data_store_path, 'master', self.metadata["db_file_name"] )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84 command = [self.command, db_file, '-e','-I', version_name, '-o', self.version_path ]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87 result = subprocess.call(command);
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88 except:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
89 print 'Error: Galaxy was not able to run the kipper.py program successfully for this job: "%s". Check permissions?' % self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
90 sys.exit( 1 )
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
91
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
92
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
93 def get_metadata_version(self, version_name=''):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
94 if version_name == '':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
95 return self.versions[0]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
96
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
97 for version in self.versions:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
98 if str(version['name']) == version_name:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
99 return version
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
100
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
101 return False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
102