comparison gafa_datatypes.py @ 0:af9f72ddf7f9 draft

planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/GAFA/ commit 822c798d43a72724eeab174043fdaafcfdac845f-dirty
author earlhaminst
date Wed, 21 Dec 2016 07:31:50 -0500
parents
children c15476d4271c
comparison
equal deleted inserted replaced
-1:000000000000 0:af9f72ddf7f9
1 import logging
2
3 from galaxy.datatypes.binary import Binary, SQlite
4 from galaxy.datatypes.metadata import MetadataElement, MetadataParameter
5 from galaxy.util import sqlite
6
7 log = logging.getLogger(__name__)
8
9
10 class GAFASQLite(SQlite):
11 """Class describing a GAFA SQLite database"""
12 MetadataElement(name='gafa_schema_version', default='0.1.0', param=MetadataParameter, desc='GAFA schema version',
13 readonly=True, visible=True, no_value='0.1.0')
14 file_ext = 'gafa.sqlite'
15
16 def set_meta(self, dataset, overwrite=True, **kwd):
17 super(GAFASQLite, self).set_meta(dataset, overwrite=overwrite, **kwd)
18 try:
19 conn = sqlite.connect(dataset.file_name)
20 c = conn.cursor()
21 version_query = 'SELECT version FROM meta'
22 results = c.execute(version_query).fetchall()
23 if len(results) == 0:
24 raise Exception('version not found in meta table')
25 elif len(results) > 1:
26 raise Exception('Multiple versions found in meta table')
27 dataset.metadata.gafa_schema_version = results[0][0]
28 except Exception as e:
29 log.warn("%s, set_meta Exception: %s", self, e)
30
31 def sniff(self, filename):
32 if super(GAFASQLite, self).sniff(filename):
33 gafa_table_names = frozenset(['gene', 'gene_family', 'gene_family_member', 'meta', 'transcript'])
34 conn = sqlite.connect(filename)
35 c = conn.cursor()
36 tables_query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
37 results = c.execute(tables_query).fetchall()
38 found_table_names = frozenset(_[0] for _ in results)
39 return gafa_table_names <= found_table_names
40 return False
41
42
43 # Since Binary.register_sniffable_binary_format() ignores the sniff order declared in datatypes_conf.xml and put TS datatypes at the end, instead of simply doing:
44 # Binary.register_sniffable_binary_format("sqlite", "sqlite", SQlite)
45 # we need to register specialized SQLite datatypes before SQlite
46 for i, format_dict in enumerate(Binary.sniffable_binary_formats):
47 if format_dict['class'] == SQlite:
48 break
49 else:
50 i += 1
51 Binary.sniffable_binary_formats.insert(i, {'type': 'gafa.sqlite', 'ext': 'gafa.sqlite', 'class': GAFASQLite})