/
mock_database.py
84 lines (70 loc) · 3.13 KB
/
mock_database.py
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
# mock database for calour gui unit testing
# simulates a simple database for querying sequences
from logging import getLogger
from calour.database import Database
logger = getLogger(__name__)
class MockDatabase(Database):
def __init__(self, exp=None):
'''Initialize the database interface
Parameters
----------
database_name : str, optional
name of the database
methods : list of str, optional
'get' if database interface supports get_seq_annotation_strings()
'annotate' if database interface supports add_annotation()
'enrichment' if database interface supports get_feature_terms()
'''
super().__init__(exp=exp, database_name='mock_db', methods=['get', 'annotate', 'enrichment'])
self.dbinfo = {
'TACGTATGTCACAAGCGTTATCCGGATTTATTGGGTTTAAAGGGAGCGTAGGCCGTGGATTAAGCGTGTTGTGAAATGTAGACGCTCAACGTCTGAATCGCAGCGCGAACTGGTTCACTTGAGTATGCACAACGTAGGCGGAATTCGTCG': ['seq1', 'nice'],
'TACATAGGTCGCAAGCGTTATCCGGAATTATTGGGCGTAAAGCGTTCGTAGGCTGTTTATTAAGTCTGGAGTCAAATCCCAGGGCTCAACCCTGGCTCGCTTTGGATACTGGTAAACTAGAGTTAGATAGAGGTAAGCAGAATTCCATGT': ['seq2'],
'TACGTAGGGTGCAAGCGTTAATCGGAATTACTGGGCGTAAAGCGTGCGCAGGCGGTTTTGTAAGTCTGATGTGAAATCCCCGGGCTCAACCTGGGAATTGCATTGGAGACTGCAAGGCTAGAATCTGGCAGAGGGGGGTAGAATTCCACG': ['seq3', 'nice']
}
def get_seq_annotation_strings(self, sequence):
'''Get nice string summaries of annotations for a given sequence
Parameters
----------
sequence : str
the DNA sequence to query the annotation strings about
Returns
-------
shortdesc : list of (dict,str) (annotationdetails,annotationsummary)
a list of:
annotationdetails : dict
'annotationid' : int, the annotation id in the database
'annotationtype : str
...
annotationsummary : str
a short summary of the annotation
'''
annotations = []
if sequence not in self.dbinfo:
return []
for cstr in self.dbinfo[sequence]:
cannotation = ({'annotationid': 1, 'annotationtype': 'other'}, cstr)
annotations.append(cannotation)
return annotations
def get_feature_terms(self, features, exp=None):
'''Get list of terms per feature
Parameters
----------
features : list of str
the features to get the terms for
exp : Experiment, optional
not None to store results inthe exp (to save time for multiple queries)
Returns
-------
feature_terms : dict of list of str/int
key is the feature, list contains all terms associated with the feature
'''
feature_terms = {}
for cseq, cterms in self.dbinfo.items():
feature_terms[cseq] = []
for cstr in cterms:
feature_terms[cseq].append(cstr)
return feature_terms
def version(self):
'''return the current database version
'''
return '1.1.3rc1'