SampleSet.py 6.54 KB
Newer Older
1
2
from abc import ABCMeta, abstractmethod

HERBERT Ryan's avatar
HERBERT Ryan committed
3
class SampleSet(object):
4
    __metaclass__ = ABCMeta
5

HERBERT Ryan's avatar
HERBERT Ryan committed
6
7
    def __init__(self, type):
        self.type = type
8
9
        prefix = get_tag_prefix()
        self.tag_decorator = TagDecorator(prefix)
10

11
12
13
    def get_type(self):
        return self.type

14
    def get_type_display(self):
15
        return self.type
16

17
18
19
    def __getitem__(self, key):
        return getattr(self, key, None)

HERBERT Ryan's avatar
HERBERT Ryan committed
20
    def get_name(self, data):
21
        return data['name']
22

Ryan Herbert's avatar
Ryan Herbert committed
23
    def get_display_name(self, data):
24
        return SPAN(self.get_name(data), _class="set_token generic_token")
Ryan Herbert's avatar
Ryan Herbert committed
25

HERBERT Ryan's avatar
HERBERT Ryan committed
26
    def get_info(self, data):
27
        return data['info']
28
29

    def get_tagged_info(self, data):
30
        text = self.tag_decorator.decorate(data['info'], 'tag', self.type, self.get_list_path())
31
        return self.tag_decorator.sanitize(text)
32

33
34
35
36
    def get_stats_tagged_info(self, data):
        text = self.tag_decorator.decorate(data.info, 'tag', self.type, self.get_stats_path())
        return self.tag_decorator.sanitize(text)

HERBERT Ryan's avatar
HERBERT Ryan committed
37
    def get_configs(self, data):
38
        return data['conf_list']
39

40
41
42
    def get_list_path(self):
        return '/sample_set/all'

43
44
45
    def get_stats_path(self):
        return '/sample_set/stats'

HERBERT Ryan's avatar
HERBERT Ryan committed
46
    def get_config_urls(self, data):
47
        configs = []
48
        for conf in data['conf_list']:
Ryan Herbert's avatar
Ryan Herbert committed
49
            filename =  "(%s %s)" % (self.get_display_name(data), conf['name'])
50
51
52
            if conf['fused_file'] is not None :
                configs.append(
                    str(A(conf['name'],
53
54
                        _href="index.html?sample_set_id=%d&config=%d" % (data['sample_set_id'], conf['id']), _type="text/html",
                        _onclick="event.preventDefault();event.stopPropagation();if( event.which == 2 ) { window.open(this.href); } else { myUrl.loadUrl(db, { 'sample_set_id' : '%d', 'config' :  %d }, '%s' ); }" % (data['sample_set_id'], conf['id'], filename))))
55
56
57
58
            else:
                configs.append(conf['name'])
        return XML(", ".join(configs))

HERBERT Ryan's avatar
HERBERT Ryan committed
59
    def get_groups(self, data):
60
        return data['group_list']
61

HERBERT Ryan's avatar
HERBERT Ryan committed
62
    def get_groups_string(self, data):
63
        return ', '.join([group for group in data['group_list'] if group != 'admin'])
64

HERBERT Ryan's avatar
HERBERT Ryan committed
65
    def get_creator(self, data):
66
        return data['creator']
67

HERBERT Ryan's avatar
HERBERT Ryan committed
68
    def get_files(self, data):
69
        return '%d (%s)' % (data['file_count'], vidjil_utils.format_size(data['size']))
70

71
72
    def get_fields(self):
        fields = []
Ryan Herbert's avatar
Ryan Herbert committed
73
        fields.append({'name': 'name', 'sort': 'name', 'call': self.get_display_name, 'width': 200, 'public': True})
74
        fields.append({'name': 'info', 'sort': 'info', 'call': self.get_tagged_info, 'width': None, 'public': True})
HERBERT Ryan's avatar
HERBERT Ryan committed
75
        fields.append({'name': 'results', 'sort': 'confs', 'call': self.get_config_urls, 'width': None, 'public': True})
76
        if auth.is_admin() or len(get_group_list(auth)) > 1:
77
78
            fields.append({'name': 'groups', 'sort': 'groups', 'call': self.get_groups_string, 'width': 100, 'public': True})
            fields.append({'name': 'creator', 'sort': 'creator', 'call': self.get_creator, 'width': 100, 'public': True})
HERBERT Ryan's avatar
HERBERT Ryan committed
79
        fields.append({'name': 'files', 'sort': 'file_count', 'call': self.get_files, 'width': 100, 'public': True})
80
81
        return fields

82
83
84
85
86
87
    def get_sort_fields(self):
        fields = {}
        for field in self.get_fields():
            fields[field['sort']] = field
        return fields

88
89
90
    def get_reduced_fields(self):
        fields = []
        fields.append({'name': 'name', 'sort': 'name', 'call': self.get_name, 'width': 200, 'public': True})
91
        fields.append({'name': 'info', 'sort': 'info', 'call': self.get_stats_tagged_info, 'width': None, 'public': True})
92
93
94
        fields.append({'name': 'files', 'sort': 'file_count', 'call': self.get_files, 'width': 100, 'public': True})
        return fields

HERBERT Ryan's avatar
HERBERT Ryan committed
95
    def get_sequence_count(self, data):
96
        if not hasattr(data, 'sequence_count'):
97
            data['sequence_count'] = db( (db.sequence_file.id == db.sample_set_membership.sequence_file_id)
HERBERT Ryan's avatar
HERBERT Ryan committed
98
                    &(db.sample_set_membership.sample_set_id == db[self.type].sample_set_id)
99
100
                    &(db[self.type].id == data['id'])).count()
        return data['sequence_count']
HERBERT Ryan's avatar
HERBERT Ryan committed
101

HERBERT Ryan's avatar
HERBERT Ryan committed
102
    def get_data_count(self, data):
103
        if not hasattr(data, 'data_count'):
104
            data['data_count'] = db( (db.sequence_file.id == db.sample_set_membership.sequence_file_id)
HERBERT Ryan's avatar
HERBERT Ryan committed
105
                &(db.sample_set_membership.sample_set_id == db[self.type].sample_set_id)
106
                &(db[self.type].id == data['id'])
107
                &(db.results_file.sequence_file_id == db.sequence_file.id)).count()
108
        return data['data_count']
HERBERT Ryan's avatar
HERBERT Ryan committed
109

110
111
112
113
114
115
116
    def create_filter_string(self, data, keys):
        for row in data:
            row['string'] = []
            for key in keys:
                if key in row:
                    row['string'].append(str(row[key]))

117
    def get_name_filter_query(self, query):
Ryan Herbert's avatar
Ryan Herbert committed
118
119
120
121
        if query is None or query == '':
            return (db[self.type].name != None)
        return (db[self.type].name.like('%' + query + "%"))

122
    @abstractmethod
HERBERT Ryan's avatar
HERBERT Ryan committed
123
    def filter(self, filter_str, data):
124
        pass
125

126
    @abstractmethod
127
    def get_info_dict(self, data):
128
        pass
129

130
    @abstractmethod
131
    def get_data(self, sample_set_id):
132
        pass
133

134
135
136
137
    @abstractmethod
    def get_id_string(self, data):
        pass

138
139
    def parse_id_string(self, string):
        try:
140
            value = string[string.rfind("(")+1:string.rfind(")")]
141
142
143
144
            return int(value)
        except:
            raise ValueError('invalid input %s' % string)

Ryan Herbert's avatar
Ryan Herbert committed
145
146
147
148
    @abstractmethod
    def validate(self, data):
        pass

149
150
151
152
153
154
155
def get_sample_name(sample_set_id):
    '''
    Return the name associated with a sample set (eg. a run or a
    patient) if any
    '''
    sample = db.sample_set[sample_set_id]
    if sample is None or (sample.sample_type != defs.SET_TYPE_PATIENT \
156
157
                          and sample.sample_type != defs.SET_TYPE_RUN \
                          and sample.sample_type != defs.SET_TYPE_GENERIC):
158
159
160
161
162
163
164
165
166
167
        return None

    sample_type = sample.sample_type
    patient_or_run = db[sample_type](db[sample_type].sample_set_id == sample_set_id)
    if patient_or_run is None:
        return None
    if sample.sample_type == defs.SET_TYPE_PATIENT:
        return vidjil_utils.anon_ids(patient_or_run.id)
    return patient_or_run.name

168
def get_set_group(sid):
169
170
171
    '''
    Return the group associated with the set
    '''
172
    perm = db((db.auth_permission.table_name == 'sample_set') &
173
174
175
176
              (db.auth_permission.record_id == sid) &
              (db.auth_permission.name == PermissionEnum.access.value)
            ).select().first()
    return perm.group_id