aboutsummaryrefslogtreecommitdiff
path: root/wqflask/wqflask/show_trait/SampleList.py
blob: 6be6ddcdf078f5cc3d580b3e5b121407bd65b73a (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import absolute_import, print_function, division

from flask import Flask, g

from base import webqtlCaseData
from utility import webqtlUtil, Plot, Bunch
from base.trait import GeneralTrait

import numpy as np
from scipy import stats
from pprint import pformat as pf

class SampleList(object):
    def __init__(self,
                 dataset,
                 sample_names,
                 this_trait,
                 sample_group_type,
                 header):

        self.dataset = dataset
        self.this_trait = this_trait
        self.sample_group_type = sample_group_type    # primary or other
        self.header = header

        self.sample_list = [] # The actual list

        self.get_attributes()
        
        print("camera: attributes are:", pf(self.attributes))

        for counter, sample_name in enumerate(sample_names, 1):
            sample_name = sample_name.replace("_2nd_", "")

            #ZS - If there's no value for the sample/strain, create the sample object (so samples with no value are still displayed in the table)
            try:
                sample = self.this_trait.data[sample_name]
            except KeyError:
                print("No sample %s, let's create it now" % sample_name)
                sample = webqtlCaseData.webqtlCaseData(sample_name)
            
            #sampleNameAdd = ''
            #if fd.RISet == 'AXBXA' and sampleName in ('AXB18/19/20','AXB13/14','BXA8/17'):
            #    sampleNameAdd = HT.Href(url='/mouseCross.html#AXB/BXA', text=HT.Sup('#'), Class='fs12', target="_blank")
            sample.extra_info = {}
            if self.dataset.group.name == 'AXBXA' and sample_name in ('AXB18/19/20','AXB13/14','BXA8/17'):   
                sample.extra_info['url'] = "/mouseCross.html#AXB/BXA"
                sample.extra_info['css_class'] = "fs12" 

            print("  type of sample:", type(sample))

            if sample_group_type == 'primary':
                sample.this_id = "Primary_" + str(counter)
            else:
                sample.this_id = "Other_" + str(counter)

            #### For extra attribute columns; currently only used by several datasets - Zach
            if self.this_trait and self.dataset and self.dataset.type == 'ProbeSet':
                sample.extra_attributes = self.get_extra_attribute_values(sample_name)
                print("sample.extra_attributes is", pf(sample.extra_attributes))
            
            self.sample_list.append(sample)

        print("self.attributes is", pf(self.attributes))

        self.do_outliers()
        #do_outliers(the_samples)
        print("*the_samples are [%i]: %s" % (len(self.sample_list), pf(self.sample_list)))
        for sample in self.sample_list:
            print("apple:", type(sample), sample)
        #return the_samples

    def __repr__(self):
        return "<SampleList> --> %s" % (pf(self.__dict__))

    def get_z_scores(self):
        values = [sample.value for sample in self.sample_list if sample.value != None]
        numpy_array = np.array(values)
        z_scores = stats.zscore(numpy_array)
        
        for i, sample in enumerate(self.sample_list):
            if sample.value:
                sample.z_score = z_scores[i]
        
    def do_outliers(self):
        values = [sample.value for sample in self.sample_list if sample.value != None]
        upper_bound, lower_bound = Plot.find_outliers(values)
        
        for sample in self.sample_list:
            if sample.value:
                if upper_bound and sample.value > upper_bound:
                    sample.outlier = True
                elif lower_bound and sample.value < lower_bound:
                    sample.outlier = True
                else:
                    sample.outlier = False
                    
                    
    def get_attributes(self):
        """Finds which extra attributes apply to this dataset"""
        
        #ZS: Id and name values for this trait's extra attributes
        case_attributes = g.db.execute('''SELECT CaseAttribute.Id, CaseAttribute.Name
                                        FROM CaseAttribute, CaseAttributeXRef
                                        WHERE CaseAttributeXRef.ProbeSetFreezeId = %s AND
                                            CaseAttribute.Id = CaseAttributeXRef.CaseAttributeId
                                                group by CaseAttributeXRef.CaseAttributeId''',
                                                (str(self.dataset.id),))


        self.attributes = {}
        for key, value in case_attributes.fetchall():
            print("radish: %s - %s" % (key, value))
            self.attributes[key] = Bunch()
            self.attributes[key].name = value

            attribute_values = g.db.execute('''SELECT DISTINCT CaseAttributeXRef.Value
                                               FROM CaseAttribute, CaseAttributeXRef
                                               WHERE CaseAttribute.Name = %s AND
                                CaseAttributeXRef.CaseAttributeId = CaseAttribute.Id''', (value,))

            self.attributes[key].distinct_values = [item[0] for item in attribute_values.fetchall()]
            self.attributes[key].distinct_values.sort(key=natural_sort_key)


    def get_extra_attribute_values(self, sample_name):

        attribute_values = {}

        if self.attributes:

            #ZS: Get StrainId value for the next query
            result = g.db.execute("""SELECT Strain.Id
                                        FROM Strain, StrainXRef, InbredSet
                                        WHERE Strain.Name = %s and
                                            StrainXRef.StrainId = Strain.Id and
                                            InbredSet.Id = StrainXRef.InbredSetId and
                                            InbredSet.Name = %s""", (sample_name,
                                                                     self.dataset.group.name))

            sample_id = result.fetchone().Id

            for attribute in self.attributes:

                #ZS: Add extra case attribute values (if any)
                result = g.db.execute("""SELECT Value
                                                FROM CaseAttributeXRef
                                        WHERE ProbeSetFreezeId = %s AND
                                                StrainId = %s AND
                                                CaseAttributeId = %s
                                        group by CaseAttributeXRef.CaseAttributeId""", (
                                            self.this_trait.dataset.id, sample_id, str(attribute)))

                attribute_value = result.fetchone().Value #Trait-specific attributes, if any

                #ZS: If it's an int, turn it into one for sorting
                #(for example, 101 would be lower than 80 if they're strings instead of ints)
                try:
                    attribute_value = int(attribute_value)
                except ValueError:
                    pass
                
                attribute_values[self.attributes[attribute].name] = attribute_value
               
        return attribute_values
    
    def se_exists(self):
        """Returns true if SE values exist for any samples, otherwise false"""
        
        return any(sample.variance for sample in self.sample_list)


def natural_sort_key(x):
    """Get expected results when using as a key for sort - ints or strings are sorted properly"""
    
    try:
        x = int(x)
    except ValueError:
        pass
    return x