aboutsummaryrefslogtreecommitdiff
path: root/gn2/base/data_set/genotypedataset.py
blob: a5b6577280933b1637eb63f3988edacd91b59ae1 (about) (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
"GenotypeDataSet class ..."

from .dataset import DataSet
from gn2.utility import webqtlUtil
from gn2.utility.tools import get_setting
from gn2.db import webqtlDatabaseFunction
from .utils import geno_mrna_confidentiality
from gn2.wqflask.database import database_connection

class GenotypeDataSet(DataSet):

    def setup(self):
        # Fields in the database table
        self.search_fields = ['Name',
                              'Chr']

        # Find out what display_fields is
        self.display_fields = ['name',
                               'chr',
                               'mb',
                               'source2',
                               'sequence']

        # Fields displayed in the search results table header
        self.header_fields = ['Index',
                              'ID',
                              'Location']

        # Todo: Obsolete or rename this field
        self.type = 'Geno'
        self.query_for_group = """
SELECT InbredSet.Name, InbredSet.Id, InbredSet.GeneticType, InbredSet.InbredSetCode
FROM InbredSet, GenoFreeze WHERE GenoFreeze.InbredSetId = InbredSet.Id AND
GenoFreeze.Name = %s"""

    def check_confidentiality(self):
        return geno_mrna_confidentiality(self)

    def get_trait_info(self, trait_list, species=None):
        for this_trait in trait_list:
            if not this_trait.haveinfo:
                this_trait.retrieveInfo()

            if this_trait.chr and this_trait.mb:
                this_trait.location_repr = 'Chr%s: %.6f' % (
                    this_trait.chr, float(this_trait.mb))

    def retrieve_sample_data(self, trait):
        results = []
        with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor:
            cursor.execute(
                "SELECT Strain.Name, GenoData.value, "
                "GenoSE.error, NULL, Strain.Name2 "
                "FROM (GenoData, GenoFreeze, Strain, Geno, "
                "GenoXRef) LEFT JOIN GenoSE ON "
                "(GenoSE.DataId = GenoData.Id AND "
                "GenoSE.StrainId = GenoData.StrainId) "
                "WHERE Geno.SpeciesId = %s AND "
                "Geno.Name = %s AND GenoXRef.GenoId = Geno.Id "
                "AND GenoXRef.GenoFreezeId = GenoFreeze.Id "
                "AND GenoFreeze.Name = %s AND "
                "GenoXRef.DataId = GenoData.Id "
                "AND GenoData.StrainId = Strain.Id "
                "ORDER BY Strain.Name",
                (webqtlDatabaseFunction.retrieve_species_id(self.group.name),
                 trait, self.name,))
            results = list(cursor.fetchall())

        if self.group.name in webqtlUtil.ParInfo:
            f1_1, f1_2, ref, nonref = webqtlUtil.ParInfo[self.group.name]
            results.append([f1_1, 0, None, None, f1_1])
            results.append([f1_2, 0, None, None, f1_2])
            results.append([ref, -1, None, None, ref])
            results.append([nonref, 1, None, None, nonref])

        return results