annotate gafa_datatypes.py @ 7:b9f1bcf5ee59 draft default tip

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