aboutsummaryrefslogtreecommitdiff
path: root/wqflask/base/data_set.py
blob: 16f9da5d8b64b92cbab47ad5f7d877e12823f026 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
#
# This module is used by GeneNetwork project (www.genenetwork.org)

from __future__ import absolute_import, print_function, division
import os
import math
import string
import collections

import json
import gzip
import cPickle as pickle
import itertools

from redis import Redis
Redis = Redis()

from flask import Flask, g

import reaper

from base import webqtlConfig
from base import species
from dbFunction import webqtlDatabaseFunction
from utility import webqtlUtil
from utility.benchmark import Bench
from wqflask.my_pylmm.pyLMM import chunks

from maintenance import get_group_samplelists

from MySQLdb import escape_string as escape
from pprint import pformat as pf

# Used by create_database to instantiate objects
# Each subclass will add to this
DS_NAME_MAP = {}

def create_dataset(dataset_name, dataset_type = None):
    if not dataset_type:
        dataset_type = Dataset_Getter(dataset_name)
        #dataset_type = get_dataset_type_from_json(dataset_name)

        print("dataset_type is:", dataset_type)
        #query = """
        #    SELECT DBType.Name
        #    FROM DBList, DBType
        #    WHERE DBList.Name = '{}' and
        #          DBType.Id = DBList.DBTypeId
        #    """.format(escape(dataset_name))
        #dataset_type = g.db.execute(query).fetchone().Name


    dataset_ob = DS_NAME_MAP[dataset_type]
    dataset_class = globals()[dataset_ob]
    return dataset_class(dataset_name)


#def get_dataset_type_from_json(dataset_name):
    
class Dataset_Types(object):
    
    def __init__(self):
        self.datasets = {}
        file_name = "wqflask/static/new/javascript/dataset_menu_structure.json"
        with open(file_name, 'r') as fh:
            data = json.load(fh)
        
        print("*" * 70)
        for species in data['datasets']:
            for group in data['datasets'][species]:
                for dataset_type in data['datasets'][species][group]:
                    for dataset in data['datasets'][species][group][dataset_type]:
                        print("dataset is:", dataset)
                        
                        short_dataset_name = dataset[0]
                        if dataset_type == "Phenotypes":
                            new_type = "Publish"
                        elif dataset_type == "Genotypes":
                            new_type = "Geno"
                        else:
                            new_type = "ProbeSet"
                        self.datasets[short_dataset_name] = new_type
                            
    def __call__(self, name):
        return self.datasets[name]
    
# Do the intensive work at startup one time only
Dataset_Getter = Dataset_Types()

#
#print("Running at startup:", get_dataset_type_from_json("HBTRC-MLPFC_0611"))
                    

def create_datasets_list():
    key = "all_datasets"
    result = Redis.get(key)
    
    if result:
        print("Cache hit!!!")
        datasets = pickle.loads(result)
        
    else:
        datasets = list()
        with Bench("Creating DataSets object"):
            type_dict = {'Publish': 'PublishFreeze',
                         'ProbeSet': 'ProbeSetFreeze',
                         'Geno': 'GenoFreeze'}
        
            for dataset_type in type_dict:
                query = "SELECT Name FROM {}".format(type_dict[dataset_type])
                for result in g.db.execute(query).fetchall():
                    #The query at the beginning of this function isn't necessary here, but still would
                    #rather just reuse it
                    #print("type: {}\tname: {}".format(dataset_type, result.Name))
                    dataset = create_dataset(result.Name, dataset_type)
                    datasets.append(dataset)
            
        Redis.set(key, pickle.dumps(datasets, pickle.HIGHEST_PROTOCOL))
        Redis.expire(key, 60*60)
    
    return datasets


def create_in_clause(items):
    """Create an in clause for mysql"""
    in_clause = ', '.join("'{}'".format(x) for x in mescape(*items))
    in_clause = '( {} )'.format(in_clause)
    return in_clause


def mescape(*items):
    """Multiple escape"""
    escaped = [escape(str(item)) for item in items]
    #print("escaped is:", escaped)
    return escaped


class Markers(object):
    """Todo: Build in cacheing so it saves us reading the same file more than once"""
    def __init__(self, name):
        json_data_fh = open(os.path.join(webqtlConfig.NEWGENODIR + name + '.json'))
        self.markers = json.load(json_data_fh)
    
    def add_pvalues(self, p_values):
        #print("length of self.markers:", len(self.markers))
        #print("length of p_values:", len(p_values))
        
        # THIS IS only needed for the case when we are limiting the number of p-values calculated
        if len(self.markers) < len(p_values):
            self.markers = self.markers[:len(p_values)]
        
        for marker, p_value in itertools.izip(self.markers, p_values):
            marker['p_value'] = p_value
            if marker['p_value'] == 0:
                marker['lod_score'] = 0
                marker['lrs_value'] = 0
            else:
                marker['lod_score'] = -math.log10(marker['p_value'])
                #Using -log(p) for the LRS; need to ask Rob how he wants to get LRS from p-values
                marker['lrs_value'] = -math.log10(marker['p_value']) * 4.61


class HumanMarkers(Markers):
    
    def __init__(self, name):
        marker_data_fh = open(os.path.join(webqtlConfig.PYLMM_PATH + name + '.bim'))
        self.markers = []
        for line in marker_data_fh:
            splat = line.strip().split()
            marker = {}
            marker['chr'] = int(splat[0])
            marker['name'] = splat[1]
            marker['Mb'] = float(splat[3]) / 1000000
            self.markers.append(marker)


    def add_pvalues(self, p_values):
        #for marker, p_value in itertools.izip(self.markers, p_values):
        #    if marker['Mb'] <= 0 and marker['chr'] == 0:
        #        continue
        #    marker['p_value'] = p_value
        #    print("p_value is:", marker['p_value'])
        #    marker['lod_score'] = -math.log10(marker['p_value'])
        #    #Using -log(p) for the LRS; need to ask Rob how he wants to get LRS from p-values
        #    marker['lrs_value'] = -math.log10(marker['p_value']) * 4.61
        
        super(HumanMarkers, self).add_pvalues(p_values)
        
        with Bench("deleting markers"):
            markers = []
            for marker in self.markers:
                if not marker['Mb'] <= 0 and not marker['chr'] == 0:
                    markers.append(marker)
            self.markers = markers
        
    

class DatasetGroup(object):
    """
    Each group has multiple datasets; each species has multiple groups.
    
    For example, Mouse has multiple groups (BXD, BXA, etc), and each group
    has multiple datasets associated with it.
    
    """
    def __init__(self, dataset):
        """This sets self.group and self.group_id"""
        self.name, self.id = g.db.execute(dataset.query_for_group).fetchone()
        if self.name == 'BXD300':
            self.name = "BXD"
        
        self.f1list = None
        self.parlist = None        
        self.get_f1_parent_strains()
        #print("parents/f1s: {}:{}".format(self.parlist, self.f1list))
        
        self.species = webqtlDatabaseFunction.retrieve_species(self.name)
        
        self.incparentsf1 = False
        self.allsamples = None


    def get_markers(self):
        #print("self.species is:", self.species)
        if self.species == "human":
            marker_class = HumanMarkers 
        else:
            marker_class = Markers

        self.markers = marker_class(self.name)


    def get_f1_parent_strains(self):
        try:
            # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py;
            f1, f12, maternal, paternal = webqtlUtil.ParInfo[self.name]
        except KeyError:
            f1 = f12 = maternal = paternal = None
            
        if f1 and f12:
            self.f1list = [f1, f12]
        if maternal and paternal:
            self.parlist = [maternal, paternal]

    def get_samplelist(self):
        key = "samplelist:v4:" + self.name
        print("key is:", key)
        with Bench("Loading cache"):
            result = Redis.get(key)

        if result:
            print("Sample List Cache hit!!!")
            print("Before unjsonifying {}: {}".format(type(result), result))
            self.samplelist = json.loads(result)
            print("  type: ", type(self.samplelist))
            print("  self.samplelist: ", self.samplelist)
        else:
            print("Cache not hit")
            try:
                self.samplelist = get_group_samplelists.get_samplelist(self.name + ".geno")
            except IOError:
                self.samplelist = None
            print("after get_samplelist")
            Redis.set(key, json.dumps(self.samplelist))
            Redis.expire(key, 60*5)

    def read_genotype_file(self):
        '''Read genotype from .geno file instead of database'''
        #if self.group == 'BXD300':
        #    self.group = 'BXD'
        #
        #assert self.group, "self.group needs to be set"

        #genotype_1 is Dataset Object without parents and f1
        #genotype_2 is Dataset Object with parents and f1 (not for intercross)

        genotype_1 = reaper.Dataset()

        # reaper barfs on unicode filenames, so here we ensure it's a string
        full_filename = str(os.path.join(webqtlConfig.GENODIR, self.name + '.geno'))
        if os.path.isfile(full_filename):
            print("Reading file: ", full_filename)
            genotype_1.read(full_filename)
            print("File read")
        else:
            try:
                full_filename = str(os.path.join(webqtlConfig.TMPDIR, self.name + '.geno'))
                #print("Reading file")
                genotype_1.read(full_filename)
                #print("File read")
            except IOError:
                print("File doesn't exist!")

        if genotype_1.type == "group" and self.parlist:
            genotype_2 = genotype_1.add(Mat=self.parlist[0], Pat=self.parlist[1])       #, F1=_f1)
        else:
            genotype_2 = genotype_1

        #determine default genotype object
        if self.incparentsf1 and genotype_1.type != "intercross":
            genotype = genotype_2
        else:
            self.incparentsf1 = 0
            genotype = genotype_1

        self.samplelist = list(genotype.prgy)


#class DataSets(object):
#    """Builds a list of DataSets"""
#    
#    def __init__(self):
#        self.datasets = list()
#        

        
        #query = """SELECT Name FROM ProbeSetFreeze
        #           UNION
        #           SELECT Name From PublishFreeze
        #           UNION
        #           SELECT Name From GenoFreeze"""
        #
        #for result in g.db.execute(query).fetchall():
        #    dataset = DataSet(result.Name)
        #    self.datasets.append(dataset)

#ds = DataSets()
#print("[orange] ds:", ds.datasets)

class DataSet(object):
    """
    DataSet class defines a dataset in webqtl, can be either Microarray,
    Published phenotype, genotype, or user input dataset(temp)

    """

    def __init__(self, name):

        assert name, "Need a name"
        self.name = name
        self.id = None
        self.shortname = None
        self.fullname = None
        self.type = None

        self.setup()

        self.check_confidentiality()

        self.retrieve_other_names()
        
        self.group = DatasetGroup(self)   # sets self.group and self.group_id and gets genotype
        self.group.get_samplelist()
        self.species = species.TheSpecies(self)


    def get_desc(self):
        """Gets overridden later, at least for Temp...used by trait's get_given_name"""
        return None
    
    #@staticmethod
    #def get_by_trait_id(trait_id):
    #    """Gets the dataset object given the trait id"""
    #    
    #    
    #
    #    name = g.db.execute(""" SELECT 
    #                        
    #                        """)
    #    
    #    return DataSet(name)

    # Delete this eventually
    @property
    def riset():
        Weve_Renamed_This_As_Group
        
        
    #@property
    #def group(self):
    #    if not self._group:
    #        self.get_group()
    #        
    #    return self._group


    def retrieve_other_names(self):
        """
        If the data set name parameter is not found in the 'Name' field of the data set table,
        check if it is actually the FullName or ShortName instead.

        This is not meant to retrieve the data set info if no name at all is passed.

        """

        query_args = tuple(escape(x) for x in (
            (self.type + "Freeze"),
            str(webqtlConfig.PUBLICTHRESH),
            self.name,
            self.name,
            self.name))
        print("query_args are:", query_args)

        #print("""
        #        SELECT Id, Name, FullName, ShortName
        #        FROM %s
        #        WHERE public > %s AND
        #             (Name = '%s' OR FullName = '%s' OR ShortName = '%s')
        #  """ % (query_args))
        
        try:
            self.id, self.name, self.fullname, self.shortname = g.db.execute("""
                    SELECT Id, Name, FullName, ShortName
                    FROM %s
                    WHERE public > %s AND
                         (Name = '%s' OR FullName = '%s' OR ShortName = '%s')
              """ % (query_args)).fetchone()
        except TypeError:
            print("Dataset {} is not yet available in GeneNetwork.".format(self.name))
            pass
        
    def get_trait_data(self):
        self.samplelist = self.group.samplelist + self.group.parlist + self.group.f1list
        query = """
            SELECT Strain.Name, Strain.Id FROM Strain, Species
            WHERE Strain.Name IN {}
            and Strain.SpeciesId=Species.Id
            and Species.name = '{}'
            """.format(create_in_clause(self.samplelist), *mescape(self.group.species))
        results = dict(g.db.execute(query).fetchall())
        sample_ids = [results[item] for item in self.samplelist]

        # MySQL limits the number of tables that can be used in a join to 61,
        # so we break the sample ids into smaller chunks
        # Postgres doesn't have that limit, so we can get rid of this after we transition
        chunk_size = 50
        number_chunks = int(math.ceil(len(sample_ids) / chunk_size))
        trait_sample_data = []
        for sample_ids_step in chunks.divide_into_chunks(sample_ids, number_chunks):

        #XZ, 09/24/2008: build one temporary table that only contains the records associated with the input GeneId 
        #tempTable = None
        #if GeneId and db.type == "ProbeSet": 
        #    if method == "3":
        #        tempTable = self.getTempLiteratureTable(species=species,
        #                                                input_species_geneid=GeneId,
        #                                                returnNumber=returnNumber)
        #
        #    if method == "4" or method == "5":
        #        tempTable = self.getTempTissueCorrTable(primaryTraitSymbol=GeneSymbol,
        #                                        TissueProbeSetFreezeId=tissueProbeSetFreezeId,
        #                                        method=method,
        #                                        returnNumber=returnNumber)
        
            if self.type == "Publish":
                dataset_type = "Phenotype"
            else:
                dataset_type = self.type
            temp = ['T%s.value' % item for item in sample_ids_step]
            if self.type == "Publish":
                query = "SELECT {}XRef.Id,".format(escape(self.type))
            else:
                query = "SELECT {}.Name,".format(escape(dataset_type))
            data_start_pos = 1
            query += string.join(temp, ', ')
            query += ' FROM ({}, {}XRef, {}Freeze) '.format(*mescape(dataset_type,
                                                                     self.type,
                                                                     self.type))

            for item in sample_ids_step:
                query += """
                        left join {}Data as T{} on T{}.Id = {}XRef.DataId
                        and T{}.StrainId={}\n
                        """.format(*mescape(self.type, item, item, self.type, item, item))
                        
            if self.type == "Publish":
                query += """
                        WHERE {}XRef.PublicationId = {}Freeze.Id
                        and {}Freeze.Name = '{}'
                        and {}.Id = {}XRef.{}Id
                        order by {}.Id
                        """.format(*mescape(self.type, self.type, self.type, self.type,
                                   self.name, dataset_type, self.type, self.type, dataset_type))
            else:
                query += """
                        WHERE {}XRef.{}FreezeId = {}Freeze.Id
                        and {}Freeze.Name = '{}'
                        and {}.Id = {}XRef.{}Id
                        order by {}.Id
                        """.format(*mescape(self.type, self.type, self.type, self.type,
                                   self.name, dataset_type, self.type, self.type, dataset_type))
            results = g.db.execute(query).fetchall()
            trait_sample_data.append(results)

        trait_count = len(trait_sample_data[0])
        self.trait_data = collections.defaultdict(list)
        
        # put all of the separate data together into a dictionary where the keys are
        # trait names and values are lists of sample values
        for trait_counter in range(trait_count):
            trait_name = trait_sample_data[0][trait_counter][0]
            for chunk_counter in range(int(number_chunks)):
                self.trait_data[trait_name] += (
                    trait_sample_data[chunk_counter][trait_counter][data_start_pos:])

class PhenotypeDataSet(DataSet):
    DS_NAME_MAP['Publish'] = 'PhenotypeDataSet'

    def setup(self):
        
        print("IS A PHENOTYPEDATASET")
        
        # Fields in the database table
        self.search_fields = ['Phenotype.Post_publication_description',
                            'Phenotype.Pre_publication_description',
                            'Phenotype.Pre_publication_abbreviation',
                            'Phenotype.Post_publication_abbreviation',
                            'Phenotype.Lab_code',
                            'Publication.PubMed_ID',
                            'Publication.Abstract',
                            'Publication.Title',
                            'Publication.Authors',
                            'PublishXRef.Id']

        # Figure out what display_fields is
        self.display_fields = ['name',
                               'pubmed_id',
                               'pre_publication_description',
                               'post_publication_description',
                               'original_description',
                               'pre_publication_abbreviation',
                               'post_publication_abbreviation',
                               'lab_code',
                               'submitter', 'owner',
                               'authorized_users',
                               'authors', 'title',
                               'abstract', 'journal',
                               'volume', 'pages',
                               'month', 'year',
                               'sequence', 'units', 'comments']

        # Fields displayed in the search results table header
        self.header_fields = ['',
                            'ID',
                            'Description',
                            'Authors',
                            'Year',
                            'Max LRS',
                            'Max LRS Location']

        self.type = 'Publish'

        self.query_for_group = '''
                            SELECT
                                    InbredSet.Name, InbredSet.Id
                            FROM
                                    InbredSet, PublishFreeze
                            WHERE
                                    PublishFreeze.InbredSetId = InbredSet.Id AND
                                    PublishFreeze.Name = "%s"
                    ''' % escape(self.name)

    def check_confidentiality(self):
        # (Urgently?) Need to write this
        pass

    def get_trait_list(self):
        query = """
            select PublishXRef.Id
            from PublishXRef, PublishFreeze
            where PublishFreeze.InbredSetId=PublishXRef.InbredSetId
            and PublishFreeze.Id = {}
            """.format(escape(str(self.id)))
        results = g.db.execute(query).fetchall()
        trait_data = {}
        for trait in results:
            trait_data[trait[0]] = self.retrieve_sample_data(trait[0])
        return trait_data

    def get_trait_info(self, trait_list, species = ''):
        for this_trait in trait_list:
            if not this_trait.haveinfo:
                this_trait.retrieve_info(get_qtl_info=True)

            description = this_trait.post_publication_description
            
            #If the dataset is confidential and the user has access to confidential
            #phenotype traits, then display the pre-publication description instead
            #of the post-publication description
            if this_trait.confidential:
                continue   # for now
            
                if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(
                        privilege=self.privilege,
                        userName=self.userName,
                        authorized_users=this_trait.authorized_users):
                        
                    description = this_trait.pre_publication_description
            
            this_trait.description_display = description

            if not this_trait.year.isdigit():
                this_trait.pubmed_text = "N/A"

            if this_trait.pubmed_id:
                this_trait.pubmed_link = webqtlConfig.PUBMEDLINK_URL % this_trait.pubmed_id

            #LRS and its location
            this_trait.LRS_score_repr = "N/A"
            this_trait.LRS_score_value = 0
            this_trait.LRS_location_repr = "N/A"
            this_trait.LRS_location_value = 1000000

            if this_trait.lrs:
                result = g.db.execute("""
                    select Geno.Chr, Geno.Mb from Geno, Species
                    where Species.Name = %s and
                        Geno.Name = %s and
                        Geno.SpeciesId = Species.Id
                """, (species, this_trait.locus)).fetchone()
                #result = self.cursor.fetchone()

                if result:
                    if result[0] and result[1]:
                        LRS_Chr = result[0]
                        LRS_Mb = result[1]

                        #XZ: LRS_location_value is used for sorting
                        try:
                            LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb)
                        except:
                            if LRS_Chr.upper() == 'X':
                                LRS_location_value = 20*1000 + float(LRS_Mb)
                            else:
                                LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb)

                        this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs
                        this_trait.LRS_score_value = LRS_score_value = this_trait.lrs
                        this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb))
                        
    def retrieve_sample_data(self, trait):
        query = """
                    SELECT
                            Strain.Name, PublishData.value, PublishSE.error, NStrain.count
                    FROM
                            (PublishData, Strain, PublishXRef, PublishFreeze)
                    left join PublishSE on
                            (PublishSE.DataId = PublishData.Id AND PublishSE.StrainId = PublishData.StrainId)
                    left join NStrain on
                            (NStrain.DataId = PublishData.Id AND
                            NStrain.StrainId = PublishData.StrainId)
                    WHERE
                            PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND
                            PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = %s AND
                            PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id
                    Order BY
                            Strain.Name
                    """ % (trait, self.id)
        results = g.db.execute(query).fetchall()
        return results


class GenotypeDataSet(DataSet):
    DS_NAME_MAP['Geno'] = 'GenotypeDataSet'

    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 = ['',
                              'ID',
                              'Location']

        # Todo: Obsolete or rename this field
        self.type = 'Geno'

        self.query_for_group = '''
                SELECT
                        InbredSet.Name, InbredSet.Id
                FROM
                        InbredSet, GenoFreeze
                WHERE
                        GenoFreeze.InbredSetId = InbredSet.Id AND
                        GenoFreeze.Name = "%s"
                ''' % escape(self.name)

    def check_confidentiality(self):
        return geno_mrna_confidentiality(self)
    
    def get_trait_list(self):
        query = """
            select Geno.Name
            from Geno, GenoXRef
            where GenoXRef.GenoId = Geno.Id
            and GenoFreezeId = {}
            """.format(escape(str(self.id)))
        results = g.db.execute(query).fetchall()
        trait_data = {}
        for trait in results:
            trait_data[trait[0]] = self.retrieve_sample_data(trait[0])
        return trait_data

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

            #XZ: trait_location_value is used for sorting
            trait_location_repr = 'N/A'
            trait_location_value = 1000000

            if this_trait.chr and this_trait.mb:
                try:
                    trait_location_value = int(this_trait.chr)*1000 + this_trait.mb
                except:
                    if this_trait.chr.upper() == 'X':
                        trait_location_value = 20*1000 + this_trait.mb
                    else:
                        trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb

                this_trait.location_repr = 'Chr%s: %.4f' % (this_trait.chr, float(this_trait.mb) )
                this_trait.location_value = trait_location_value
                
    def retrieve_sample_data(self, trait):
        query = """
                    SELECT
                            Strain.Name, GenoData.value, GenoSE.error, GenoData.Id
                    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 = g.db.execute(query).fetchall()
        return results


class MrnaAssayDataSet(DataSet):
    '''
    An mRNA Assay is a quantitative assessment (assay) associated with an mRNA trait

    This used to be called ProbeSet, but that term only refers specifically to the Affymetrix
    platform and is far too specific.

    '''
    DS_NAME_MAP['ProbeSet'] = 'MrnaAssayDataSet'

    def setup(self):
        # Fields in the database table
        self.search_fields = ['Name',
                              'Description',
                              'Probe_Target_Description',
                              'Symbol',
                              'Alias',
                              'GenbankId',
                              'UniGeneId',
                              'RefSeq_TranscriptId']

        # Find out what display_fields is
        self.display_fields = ['name', 'symbol',
                               'description', 'probe_target_description',
                               'chr', 'mb',
                               'alias', 'geneid',
                               'genbankid', 'unigeneid',
                               'omim', 'refseq_transcriptid',
                               'blatseq', 'targetseq',
                               'chipid', 'comments',
                               'strand_probe', 'strand_gene',
                               'probe_set_target_region',
                               'probe_set_specificity',
                               'probe_set_blat_score',
                               'probe_set_blat_mb_start',
                               'probe_set_blat_mb_end',
                               'probe_set_strand',
                               'probe_set_note_by_rw',
                               'flag']

        # Fields displayed in the search results table header
        self.header_fields = ['',
                             'ID',
                             'Symbol',
                             'Description',
                             'Location',
                             'Mean Expr',
                             'Max LRS',
                             'Max LRS Location']

        # Todo: Obsolete or rename this field
        self.type = 'ProbeSet'

        self.query_for_group = '''
                        SELECT
                                InbredSet.Name, InbredSet.Id
                        FROM
                                InbredSet, ProbeSetFreeze, ProbeFreeze
                        WHERE
                                ProbeFreeze.InbredSetId = InbredSet.Id AND
                                ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND
                                ProbeSetFreeze.Name = "%s"
                ''' % escape(self.name)


    def check_confidentiality(self):
        return geno_mrna_confidentiality(self)
        
    def get_trait_list_1(self):
        query = """
            select ProbeSet.Name
            from ProbeSet, ProbeSetXRef
            where ProbeSetXRef.ProbeSetId = ProbeSet.Id
            and ProbeSetFreezeId = {}
            """.format(escape(str(self.id)))
        results = g.db.execute(query).fetchall()
        #print("After get_trait_list query")
        trait_data = {}
        for trait in results:
            print("Retrieving sample_data for ", trait[0])
            trait_data[trait[0]] = self.retrieve_sample_data(trait[0])
        #print("After retrieve_sample_data")
        return trait_data
    
    #def get_trait_data(self):
    #    self.samplelist = self.group.samplelist + self.group.parlist + self.group.f1list
    #    query = """
    #        SELECT Strain.Name, Strain.Id FROM Strain, Species
    #        WHERE Strain.Name IN {}
    #        and Strain.SpeciesId=Species.Id
    #        and Species.name = '{}'
    #        """.format(create_in_clause(self.samplelist), *mescape(self.group.species))
    #    results = dict(g.db.execute(query).fetchall())
    #    sample_ids = [results[item] for item in self.samplelist]
    #
    #    # MySQL limits the number of tables that can be used in a join to 61,
    #    # so we break the sample ids into smaller chunks
    #    # Postgres doesn't have that limit, so we can get rid of this after we transition
    #    chunk_size = 50
    #    number_chunks = int(math.ceil(len(sample_ids) / chunk_size))
    #    trait_sample_data = []
    #    for sample_ids_step in chunks.divide_into_chunks(sample_ids, number_chunks):
    #
    #    #XZ, 09/24/2008: build one temporary table that only contains the records associated with the input GeneId 
    #    #tempTable = None
    #    #if GeneId and db.type == "ProbeSet": 
    #    #    if method == "3":
    #    #        tempTable = self.getTempLiteratureTable(species=species,
    #    #                                                input_species_geneid=GeneId,
    #    #                                                returnNumber=returnNumber)
    #    #
    #    #    if method == "4" or method == "5":
    #    #        tempTable = self.getTempTissueCorrTable(primaryTraitSymbol=GeneSymbol,
    #    #                                        TissueProbeSetFreezeId=tissueProbeSetFreezeId,
    #    #                                        method=method,
    #    #                                        returnNumber=returnNumber)
    #    
    #        temp = ['T%s.value' % item for item in sample_ids_step]
    #        query = "SELECT {}.Name,".format(escape(self.type))
    #        data_start_pos = 1
    #        query += string.join(temp, ', ')
    #        query += ' FROM ({}, {}XRef, {}Freeze) '.format(*mescape(self.type,
    #                                                                 self.type,
    #                                                                 self.type))
    #
    #        for item in sample_ids_step:
    #            query += """
    #                    left join {}Data as T{} on T{}.Id = {}XRef.DataId
    #                    and T{}.StrainId={}\n
    #                    """.format(*mescape(self.type, item, item, self.type, item, item))
    #                    
    #        query += """
    #                WHERE {}XRef.{}FreezeId = {}Freeze.Id
    #                and {}Freeze.Name = '{}'
    #                and {}.Id = {}XRef.{}Id
    #                order by {}.Id
    #                """.format(*mescape(self.type, self.type, self.type, self.type,
    #                           self.name, self.type, self.type, self.type, self.type))
    #        results = g.db.execute(query).fetchall()
    #        trait_sample_data.append(results)
    #
    #    trait_count = len(trait_sample_data[0])
    #    self.trait_data = collections.defaultdict(list)
    #    
    #    # put all of the separate data together into a dictionary where the keys are
    #    # trait names and values are lists of sample values
    #    for trait_counter in range(trait_count):
    #        trait_name = trait_sample_data[0][trait_counter][0]
    #        for chunk_counter in range(int(number_chunks)):
    #            self.trait_data[trait_name] += (
    #                trait_sample_data[chunk_counter][trait_counter][data_start_pos:])
    

    def get_trait_info(self, trait_list=None, species=''):

        #  Note: setting trait_list to [] is probably not a great idea. 
        if not trait_list:
            trait_list = []

        for this_trait in trait_list:

            if not this_trait.haveinfo:
                this_trait.retrieveInfo(QTL=1)

            if not this_trait.symbol:
                this_trait.symbol = "N/A"

            #XZ, 12/08/2008: description
            #XZ, 06/05/2009: Rob asked to add probe target description
            description_string = str(this_trait.description).strip()
            target_string = str(this_trait.probe_target_description).strip()

            if len(description_string) > 1 and description_string != 'None':
                description_display = description_string
            else:
                description_display = this_trait.symbol

            if (len(description_display) > 1 and description_display != 'N/A' and
                    len(target_string) > 1 and target_string != 'None'):
                description_display = description_display + '; ' + target_string.strip()

            # Save it for the jinja2 template
            this_trait.description_display = description_display

            #XZ: trait_location_value is used for sorting
            trait_location_repr = 'N/A'
            trait_location_value = 1000000

            if this_trait.chr and this_trait.mb:
                #Checks if the chromosome number can be cast to an int (i.e. isn't "X" or "Y")
                #This is so we can convert the location to a number used for sorting
                trait_location_value = self.convert_location_to_value(this_trait.chr, this_trait.mb)
                #try:
                #    trait_location_value = int(this_trait.chr)*1000 + this_trait.mb
                #except ValueError:
                #    if this_trait.chr.upper() == 'X':
                #        trait_location_value = 20*1000 + this_trait.mb
                #    else:
                #        trait_location_value = (ord(str(this_trait.chr).upper()[0])*1000 +
                #                               this_trait.mb)

                #ZS: Put this in function currently called "convert_location_to_value"
                this_trait.location_repr = 'Chr %s: %.4f Mb' % (this_trait.chr,
                                                                float(this_trait.mb))
                this_trait.location_value = trait_location_value

            #Get mean expression value
            query = (
            """select ProbeSetXRef.mean from ProbeSetXRef, ProbeSet
                where ProbeSetXRef.ProbeSetFreezeId = %s and
                ProbeSet.Id = ProbeSetXRef.ProbeSetId and
                ProbeSet.Name = '%s'
            """ % (escape(str(this_trait.dataset.id)),
                   escape(this_trait.name)))

            #print("query is:", pf(query))

            result = g.db.execute(query).fetchone()
            
            mean = result[0] if result else 0

            this_trait.mean = "%2.3f" % mean

            #LRS and its location
            this_trait.LRS_score_repr = 'N/A'
            this_trait.LRS_score_value = 0
            this_trait.LRS_location_repr = 'N/A'
            this_trait.LRS_location_value = 1000000

            #Max LRS and its Locus location
            if this_trait.lrs and this_trait.locus:
                query = """
                    select Geno.Chr, Geno.Mb from Geno, Species
                    where Species.Name = '{}' and
                        Geno.Name = '{}' and
                        Geno.SpeciesId = Species.Id
                """.format(species, this_trait.locus)
                result = g.db.execute(query).fetchone()
                
                if result:
                    #if result[0] and result[1]:
                    #    lrs_chr = result[0]
                    #    lrs_mb = result[1]
                    lrs_chr, lrs_mb = result
                    #XZ: LRS_location_value is used for sorting
                    lrs_location_value = self.convert_location_to_value(lrs_chr, lrs_mb)
                    
                    #try:
                    #    lrs_location_value = int(lrs_chr)*1000 + float(lrs_mb)
                    #except:
                    #    if lrs_chr.upper() == 'X':
                    #        lrs_location_value = 20*1000 + float(lrs_mb)
                    #    else:
                    #        lrs_location_value = (ord(str(LRS_chr).upper()[0])*1000 +
                    #                              float(lrs_mb))

                    this_trait.LRS_score_repr = '%3.1f' % this_trait.lrs
                    this_trait.LRS_score_value = this_trait.lrs
                    this_trait.LRS_location_repr = 'Chr %s: %.4f Mb' % (lrs_chr, float(lrs_mb))
      

    def convert_location_to_value(self, chromosome, mb):
        try:
            location_value = int(chromosome)*1000 + float(mb)
        except ValueError:
            if chromosome.upper() == 'X':
                location_value = 20*1000 + float(mb)
            else:
                location_value = (ord(str(chromosome).upper()[0])*1000 +
                                  float(mb))
        
        return location_value

    def get_sequence(self):
        query = """
                    SELECT
                            ProbeSet.BlatSeq
                    FROM
                            ProbeSet, ProbeSetFreeze, ProbeSetXRef
                    WHERE
                            ProbeSet.Id=ProbeSetXRef.ProbeSetId and
                            ProbeSetFreeze.Id = ProbeSetXRef.ProbSetFreezeId and
                            ProbeSet.Name = %s
                            ProbeSetFreeze.Name = %s
                """ % (escape(self.name), escape(self.dataset.name))
        results = g.db.execute(query).fetchone()
        return results[0]
   
    def retrieve_sample_data(self, trait):
        query = """
                    SELECT
                            Strain.Name, ProbeSetData.value, ProbeSetSE.error, ProbeSetData.Id
                    FROM
                            (ProbeSetData, ProbeSetFreeze, Strain, ProbeSet, ProbeSetXRef)
                    left join ProbeSetSE on
                            (ProbeSetSE.DataId = ProbeSetData.Id AND ProbeSetSE.StrainId = ProbeSetData.StrainId)
                    WHERE
                            ProbeSet.Name = '%s' AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND
                            ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND
                            ProbeSetFreeze.Name = '%s' AND
                            ProbeSetXRef.DataId = ProbeSetData.Id AND
                            ProbeSetData.StrainId = Strain.Id
                    Order BY
                            Strain.Name
                    """ % (escape(trait), escape(self.name))
        results = g.db.execute(query).fetchall()
        return results

    def retrieve_gene_symbols(self):
        query = """
                    select ProbeSet.Name, ProbeSet.Symbol, ProbeSet.GeneId
                    from ProbeSet,ProbeSetXRef
                    where ProbeSetXRef.ProbeSetFreezeId = %s and
                    ProbeSetXRef.ProbeSetId=ProbeSet.Id;
                """ % (self.id)
        results = g.db.execute(query).fetchall()
        symbol_dict = {}
        for item in results:
            symbol_dict[item[0]] = item[1]
        return symbol_dict
    
    def retrieve_gene_ids(self):
        query = """
                    select ProbeSet.Name, ProbeSet.GeneId
                    from ProbeSet,ProbeSetXRef
                    where ProbeSetXRef.ProbeSetFreezeId = %s and
                    ProbeSetXRef.ProbeSetId=ProbeSet.Id;
                """ % (self.id)
        results = g.db.execute(query).fetchall()
        symbol_dict = {}
        for item in results:
            symbol_dict[item[0]] = item[1]
        return symbol_dict


class TempDataSet(DataSet):
    '''Temporary user-generated data set'''

    def setup(self):
        self.search_fields = ['name',
                              'description']

        self.display_fields = ['name',
                               'description']

        self.header_fields = ['Name',
                              'Description']

        self.type = 'Temp'

        # Need to double check later how these are used
        self.id = 1
        self.fullname = 'Temporary Storage'
        self.shortname = 'Temp'
        
       
    @staticmethod
    def handle_pca(desc):
        if 'PCA' in desc:
            # Todo: Modernize below lines
            desc = desc[desc.rindex(':')+1:].strip()
        else:
            desc = desc[:desc.index('entered')].strip()
        return desc
        
    def get_desc(self):
        g.db.execute('SELECT description FROM Temp WHERE Name=%s', self.name)
        desc = g.db.fetchone()[0]
        desc = self.handle_pca(desc)
        return desc    
        
    def get_group(self):
        self.cursor.execute("""
                    SELECT
                            InbredSet.Name, InbredSet.Id
                    FROM
                            InbredSet, Temp
                    WHERE
                            Temp.InbredSetId = InbredSet.Id AND
                            Temp.Name = "%s"
            """, self.name)
        self.group, self.group_id = self.cursor.fetchone()
        #return self.group
        
    def retrieve_sample_data(self, trait):
        query = """
                SELECT
                        Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id
                FROM
                        TempData, Temp, Strain
                WHERE
                        TempData.StrainId = Strain.Id AND
                        TempData.Id = Temp.DataId AND
                        Temp.name = '%s'
                Order BY
                        Strain.Name
                """ % escape(trait.name)
                
        results = g.db.execute(query).fetchall()


def geno_mrna_confidentiality(ob):
    dataset_table = ob.type + "Freeze"
    #print("dataset_table [%s]: %s" % (type(dataset_table), dataset_table))

    query = '''SELECT Id, Name, FullName, confidentiality,
                        AuthorisedUsers FROM %s WHERE Name = %%s''' % (dataset_table)

    result = g.db.execute(query, ob.name)

    (dataset_id,
     name,
     full_name,
     confidential,
     authorized_users) = result.fetchall()[0]

    if confidential:
        return True