aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/search/SearchResultPage.py
blob: d62bb4498db097ef2ba518731f627ab731d732de (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
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
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
import string
import os
import cPickle
import re
from math import *
import time
import pyXLWriter as xl
import pp
import math
import datetime

from htmlgen import HTMLgen2 as HT

from base import webqtlConfig
from utility.THCell import THCell
from utility.TDCell import TDCell
from base.data_set import DataSet
from base.webqtlTrait import webqtlTrait
from base.templatePage import templatePage
from utility import webqtlUtil
from dbFunction import webqtlDatabaseFunction

import PubmedSearch

import logging
logging.basicConfig(filename="/tmp/gn_log_leiyan", level=logging.INFO)
_log = logging.getLogger("search")

class SearchResultPage(templatePage):

	maxReturn = 3000
#	NPerPage = 100
	nkeywords = 0

	def __init__(self, fd):

		templatePage.__init__(self, fd)

		if not self.openMysql():
			return

		self.dict['title'] = 'Search Results'
		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee',valign="top")
		self.database = fd.formdata.getfirst('database', '')
		if not self.database or self.database == 'spacer':
			#Error, No database selected
			heading = "Search Result"
			detail = ['''No database was selected for this search, please
				go back and SELECT at least one database.''']
			self.error(heading=heading,detail=detail,error="No Database Selected")
			return
		elif type(self.database) == type(""):
			#convert database into a database list
			#was used for multiple databases search, this
			#feature has been abandoned,
			self.database = string.split(self.database,',')
		else:
			pass

		###########################################
		#   Names and IDs of RISet / F2 set
		###########################################
		if self.database == ['_allPublish']:
			self.cursor.execute("""select PublishFreeze.Name, InbredSet.Name, InbredSet.Id from PublishFreeze,
				InbredSet where PublishFreeze.Name not like 'BXD300%' and InbredSet.Id =
				PublishFreeze.InbredSetId""")
			results = self.cursor.fetchall()
			self.database = map(lambda x: DataSet(x[0], self.cursor), results)
			self.databaseCrosses = map(lambda x: x[1], results)
			self.databaseCrossIds = map(lambda x: x[2], results)
			self.singleCross = False
		else:
			self.database = map(lambda x: DataSet(x, self.cursor), self.database)
			#currently, webqtl wouldn't allow multiple crosses
			#for other than multiple publish db search
			#so we can use the first database as example
			if self.database[0].type=="Publish":
				pass
			elif self.database[0].type in ("Geno", "ProbeSet"):

				#userExist = None

				for individualDB in self.database:
					self.cursor.execute('SELECT Id, Name, FullName, confidentiality, AuthorisedUsers FROM %sFreeze WHERE Name = "%s"' %  (self.database[0].type, individualDB))
					indId, indName, indFullName, confidential, AuthorisedUsers = self.cursor.fetchall()[0]
				
					if confidential == 1:
						access_to_confidential_dataset = 0

						#for the dataset that confidentiality is 1
						#1. 'admin' and 'root' can see all of the dataset
						#2. 'user' can see the dataset that AuthorisedUsers contains his id(stored in the Id field of User table)
						if webqtlConfig.USERDICT[self.privilege] > webqtlConfig.USERDICT['user']:
							access_to_confidential_dataset = 1
						else:
							AuthorisedUsersList=AuthorisedUsers.split(',')
							if AuthorisedUsersList.__contains__(self.userName):
								access_to_confidential_dataset = 1

						if not access_to_confidential_dataset:
							#Error, No database selected
							heading = "Search Result"
							detail = ["The %s database you selected is not open to the public at this time, please go back and SELECT other database." % indFullName]
							self.error(heading=heading,detail=detail,error="Confidential Database")
							return
			else:
				heading = "Search Result"
				detail = ['''The database has not been established yet, please
					go back and SELECT at least one database.''']
				self.error(heading=heading,detail=detail,error="No Database Selected")
				return

			self.database[0].getRISet()
			self.databaseCrosses = [self.database[0].riset]
			self.databaseCrossIds = [self.database[0].risetid]
			self.singleCross = True
			#XZ, August 24,2010: Since self.singleCross = True, it's safe to assign one species Id.
			self.speciesId = webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.database[0].riset)

		###########################################
		#    make sure search from same type of databases
		###########################################
		dbTypes = map(lambda X: X.type, self.database)
		self.dbType = dbTypes[0]
		for item in dbTypes:
			if item != self.dbType:
				heading = "Search Result"
				detail = ["Search can only be performed among the same type of databases"]
				self.error(heading=heading,detail=detail,error="Error")
				return
		if self.dbType == "Publish":
			self.searchField = ['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']

		elif self.dbType == "ProbeSet":
			self.searchField = ['Name','Description','Probe_Target_Description','Symbol','Alias','GenbankId', 'UniGeneId','RefSeq_TranscriptId']
		elif self.dbType == "Geno":
			self.searchField = ['Name','Chr']

		###########################################
		#	Search Options
		###########################################
		self.matchwhole = fd.formdata.getfirst('matchwhole')
		#split result into pages
		self.pageNumber = fd.formdata.getfirst('pageno', '0')
		try:
			self.pageNumber = int(self.pageNumber)
		except:
			self.pageNumber = 0


		###########################################
		#       Generate Mysql Query
		###########################################
		geneIdListQuery = fd.formdata.getfirst('geneId', '')
		if geneIdListQuery:
			geneIdListQuery = string.replace(geneIdListQuery, ",", " ")
			geneIdListQuery = " geneId=%s" % string.join(string.split(geneIdListQuery), "-")

		self.ANDkeyword = fd.formdata.getfirst('ANDkeyword', "")
		_log.info("self.ANDkeyword[1]: " + self.ANDkeyword)
		pubmedSearchObject = PubmedSearch.PubmedSearch(self.ANDkeyword, self.database[0].id)
		self.ANDkeyword = pubmedSearchObject.getNewS()
		_log.info("self.ANDkeyword[2]: " + self.ANDkeyword)

		self.ORkeyword = fd.formdata.getfirst('ORkeyword', "")
		_log.info("self.ORkeyword[1]: " + self.ORkeyword)
		pubmedSearchObject = PubmedSearch.PubmedSearch(self.ORkeyword, self.database[0].id)
		self.ORkeyword = pubmedSearchObject.getNewS()
		_log.info("self.ORkeyword[2]: "+self.ORkeyword)

		self.ORkeyword += geneIdListQuery

		self.ANDkeyword = self.ANDkeyword.replace("\\", "").strip()
		self.ORkeyword = self.ORkeyword.replace("\\", "").strip()
		#user defined sort option
		self.orderByUserInput = fd.formdata.getfirst('orderByUserInput', "").strip()
		#default sort option if user have not defined
		self.orderByDefalut = ""

		#XZ, Dec/16/2010: I add examples to help understand this block of code. See details in function pattersearch.

		#XZ: self._1mPattern examples: WIKI=xxx, RIF=xxx, GO:0045202
		self._1mPattern = re.compile('\s*(\S+)\s*[:=]\s*([a-zA-Z-\+\d\.]+)\s*')

		#XZ: self._2mPattern examples: Mean=(15.0 16.0), Range=(10 100), LRS=(Low_LRS_limit, High_LRS_limit), pvalue=(Low_limit, High_limit), Range=(10 100) 
		self._2mPattern = re.compile('\s*(\S+)\s*[=in]{1,2}\s*\(\s*([-\d\.]+)[, \t]+([-\d\.]+)[, \t]*([-\d\.]*)\s*\)')

		#XZ: self._3mPattern examples: Position=(Chr1 98 104), Pos=(Chr1 98 104), Mb=(Chr1 98 104), CisLRS=(Low_LRS_limit, High_LRS_limit, Mb_buffer), TransLRS=(Low_LRS_limit, High_LRS_limit, Mb_buffer)
		self._3mPattern = re.compile('\s*(\S+)\s*[=in]{1,2}\s*\(\s*[Cc][Hh][Rr]([^, \t]+)[, \t]+([-\d\.]+)[, \t]+([-\d\.]+)\s*\)')

		#XZ: self._5mPattern examples: LRS=(Low_LRS_limit, High_LRS_limit, ChrNN, Mb_Low_Limit, Mb_High_Limit)
		self._5mPattern = re.compile('\s*(\S+)\s*[=in]{1,2}\s*\(\s*([-\d\.]+)[, \t]+([-\d\.]+)[, \t]+[Cc][Hh][Rr]([^, \t]+)[, \t]+([-\d\.]+)[, \t]+([-\d\.]+)\s*\)')

		#Error, No keyword input
		if not (self.ORkeyword or self.ANDkeyword):
			heading = "Search Result"
			detail = ["Please make sure to enter either your search terms (genes, traits, markers), or advanced search commands."]
			self.error(heading=heading,detail=detail,error="No search terms were entered")
			return

		#query clauses
		self.ANDQuery = []
		self.ORQuery = []
		#descriptions, one for OR search, one for AND search
		self.ANDDescriptionText = []
		self.ORDescriptionText = []

		if not self.normalSearch():
			return
		if not self.patternSearch():
			return
		if not self.assembleQuery():
			return
		self.nresults = self.executeQuery()

		if len(self.database) > 1:
			dbUrl =  "Multiple phenotype databases"
			dbUrlLink = " were"
		else:
			dbUrl =  self.database[0].genHTML()
			dbUrlLink = " was"

		SearchText = HT.Blockquote('GeneNetwork searched the ', dbUrl, ' for all records ')
		if self.ORkeyword2:
			NNN = len(self.ORkeyword2)
			if NNN > 1:
				SearchText.append(' that match the terms ')
			else:
				SearchText.append(' that match the term ')
			for j, term in enumerate(self.ORkeyword2):
				SearchText.append(HT.U(term))
				if NNN > 1 and j < NNN-2:
					SearchText.append(", ")
				elif j == NNN-2:
					SearchText.append(", or ")
				else:
					pass
		if self.ORDescriptionText:
			if self.ORkeyword2:
				SearchText.append("; ")
			else:
				SearchText.append(" ")
			for j, item in enumerate(self.ORDescriptionText):
				SearchText.append(item)
				if j < len(self.ORDescriptionText) -1:
					SearchText.append(";")

		if (self.ORkeyword2 or self.ORDescriptionText) and (self.ANDkeyword2 or self.ANDDescriptionText):
			SearchText.append("; ")
		if self.ANDkeyword2:
			if (self.ORkeyword2 or self.ORDescriptionText):
				SearchText.append(' records')
			NNN = len(self.ANDkeyword2)
			if NNN > 1:
				SearchText.append(' that match the terms ')
			else:
				SearchText.append(' that match the term ')
			for j, term in enumerate(self.ANDkeyword2):
				SearchText.append(HT.U(term))
				if NNN > 1 and j < NNN-2:
					SearchText.append(", ")
				elif j == NNN-2:
					SearchText.append(", and ")
				else:
					pass
		if self.ANDDescriptionText:
			if self.ANDkeyword2:
				SearchText.append(" and ")
			else:
				SearchText.append(" ")
			for j, item in enumerate(self.ANDDescriptionText):
				SearchText.append(item)
				if j < len(self.ANDDescriptionText) -1:
					SearchText.append(" and ")

		SearchText.append(". ")
		if self.nresults == 0:
			heading = "Search Result"
			detail = ["Sorry, GeneNetwork did not find any records matching your request. Please check the syntax or try the ANY rather than the ALL field."]
			self.error(heading=heading,intro = SearchText.contents,detail=detail,error="Not Found")
			return
		elif self.nresults == 1:
			SearchText.append(HT.P(), 'GeneNetwork found one record that matches your request. To study this record, click on its text below. To add this record to your Selection window, use the checkbox and then click the ', HT.Strong('Add to Collection'),' button. ')
		elif self.nresults >= 1 and self.nresults <= self.maxReturn:
			SearchText.append(HT.P(), 'GeneNetwork found a total of ', HT.Span(self.nresults, Class='fwb cr'), ' records. To study any one of these records, click on its ID below. To add one or more records to your Selection window, use the checkbox and then click the ' , HT.Strong('Add to Collection'),' button. ')
		else:
			SearchText.append(' A total of ',HT.Span(self.nresults, Class='fwb cr'), ' records were found.')
			heading = "Search Result"
			# Modified by Hongqiang Li
			# detail = ["The terms you entered match %d records. Please modify your search to generate %d or fewer matches, or review  " % (self.nresults, self.maxReturn), HT.Href(text='Search Help', target='_blank',  url='http://web2qtl.utmem.edu/searchHelp.html', Class='fs14'), " to learn more about syntax and the use of wildcard characters."]
			detail = ["The terms you entered match %d records. Please modify your search to generate %d or fewer matches, or review  " % (self.nresults, self.maxReturn), HT.Href(text='Search Help', target='_blank',  url='%s/searchHelp.html' % webqtlConfig.PORTADDR, Class='fs14'), " to learn more about syntax and the use of wildcard characters."]
			#
			self.error(heading=heading,intro = SearchText.contents,detail=detail,error="Over %d" % self.maxReturn)
			return


		TD_LR.append(HT.Paragraph('Search Results', Class="title"), SearchText)
		self.genSearchResultTable(TD_LR)
		self.dict['body'] = str(TD_LR)
            	self.dict['js1'] = ''
            	self.dict['js2'] = 'onLoad="pageOffset()"'
		self.dict['layer'] = self.generateWarningLayer()

	def genSearchResultTable(self, TD_LR):
		
		pageTable = HT.TableLite(cellSpacing=2,cellPadding=0,width="100%",border=0)
	
		lastone = False
		for i, item in enumerate(self.results):
			if not item:
				continue
			lastone = False

			traitList = []
			for k, item2 in enumerate(item):
				j, ProbeSetID = item2[:2]
				thisTrait = webqtlTrait(db=self.database[j], name=ProbeSetID, cursor=self.cursor)
				traitList.append(thisTrait)				
			
				##############
        		# Excel file #
        		##############	
        		filename= webqtlUtil.genRandStr("Search_")
        		xlsUrl = HT.Input(type='button', value = 'Download Table', onClick= "location.href='/tmp/%s.xls'" % filename, Class='button')
        		# Create a new Excel workbook
        		workbook = xl.Writer('%s.xls' % (webqtlConfig.TMPDIR+filename))
        		headingStyle = workbook.add_format(align = 'center', bold = 1, border = 1, size=13, fg_color = 0x1E, color="white")

        		#XZ, 3/18/2010: pay attention to the line number of header in this file. As of today, there are 7 lines.
        		worksheet = self.createExcelFileWithTitleAndFooter(workbook=workbook, db=thisTrait.db, returnNumber=len(traitList))
        		newrow = 7
		
			tbl = HT.TableLite(cellSpacing=2,cellPadding=0,width="90%",border=0)
			#seq = self.pageNumber*self.NPerPage+1  //Edited out because we show all results in one page now - Zach 2/22/11
			seq = 1
			RISet = self.databaseCrosses[i]
			thisFormName = 'showDatabase'+RISet
			selectall = HT.Href(url="#", onClick="checkAll(document.getElementsByName('%s')[0]);" % thisFormName)
        		selectall_img = HT.Image("/images/select_all2_final.jpg", name="selectall", alt="Select All", title="Select All", style="border:none;")
        		selectall.append(selectall_img)
			reset = HT.Href(url="#", onClick="checkNone(document.getElementsByName('%s')[0]);" % thisFormName)
        		reset_img = HT.Image("/images/select_none2_final.jpg", alt="Select None", title="Select None", style="border:none;")
        		reset.append(reset_img)        		        					
        		selectinvert = HT.Href(url="#", onClick="checkInvert(document.getElementsByName('%s')[0]);" % thisFormName)
        		selectinvert_img = HT.Image("/images/invert_selection2_final.jpg", name="selectinvert", alt="Invert Selection", title="Invert Selection", style="border:none;")
        		selectinvert.append(selectinvert_img)
			addselect = HT.Href(url="#")
        		addselect_img = HT.Image("/images/add_collection1_final.jpg", name="addselect", alt="Add To Collection", title="Add To Collection", style="border:none;")
        		addselect.append(addselect_img)
			
			optionsTable = HT.TableLite(cellSpacing=2,cellPadding=0,width="20%",border=0)
			optionsRow = HT.TR(HT.TD(selectall, width="25%"), HT.TD(reset, width="25%"), HT.TD(selectinvert, width="25%"), HT.TD(addselect, width="25%"))
			labelsRow = HT.TR(HT.TD("&nbsp;"*2,"Select", width="25%"), HT.TD("&nbsp;","Deselect", width="255"), HT.TD("&nbsp;"*3,"Invert", width="25%"), HT.TD("&nbsp;"*4,"Add", width="25%"))
			optionsTable.append(optionsRow, labelsRow)

			pageTable.append(HT.TR(HT.TD(optionsTable)), HT.TR(HT.TD(xlsUrl, height=40)))		

			tblobj = {}
			mainfmName = thisFormName
			species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=RISet)

			if thisTrait.db.type=="Geno":
				tblobj['header'] = self.getTableHeaderForGeno(worksheet=worksheet, newrow=newrow, headingStyle=headingStyle)
				
				newrow += 1

				sortby = self.getSortByValue(datasetType="Geno")

				tblobj['body'] = self.getTableBodyForGeno(traitList=traitList, formName=mainfmName, worksheet=worksheet, newrow=newrow)
				
				workbook.close()
            			objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb')
            			cPickle.dump(tblobj, objfile)
            			objfile.close()

				div = HT.Div(webqtlUtil.genTableObj(tblobj, filename, sortby), Id="sortable")
				
				pageTable.append(HT.TR(HT.TD(div)))

			elif thisTrait.db.type=="Publish":
				tblobj['header'] = self.getTableHeaderForPublish(worksheet=worksheet, newrow=newrow, headingStyle=headingStyle)
				
				newrow += 1

				sortby = self.getSortByValue(datasetType="Publish")

				tblobj['body'] = self.getTableBodyForPublish(traitList=traitList, formName=mainfmName, worksheet=worksheet, newrow=newrow, species=species)
				
				workbook.close()
            			objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb')
            			cPickle.dump(tblobj, objfile)
            			objfile.close()

				div = HT.Div(webqtlUtil.genTableObj(tblobj, filename, sortby), Id="sortable")
				
				pageTable.append(HT.TR(HT.TD(div)))

			elif thisTrait.db.type=="ProbeSet":
				tblobj['header'] = self.getTableHeaderForProbeSet(worksheet=worksheet, newrow=newrow, headingStyle=headingStyle)
				
				newrow += 1

				sortby = self.getSortByValue(datasetType="ProbeSet")

				tblobj['body'] = self.getTableBodyForProbeSet(traitList=traitList, formName=mainfmName, worksheet=worksheet, newrow=newrow, species=species)
				
				workbook.close()
            			objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb')
            			cPickle.dump(tblobj, objfile)
            			objfile.close()

				div = HT.Div(webqtlUtil.genTableObj(tblobj, filename, sortby), Id="sortable")
				
				pageTable.append(HT.TR(HT.TD(div)))

			
			traitForm = HT.Form(cgi= os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), enctype='multipart/form-data', name=thisFormName, submit=HT.Input(type='hidden'))
			hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':'_','CellID':'_','RISet':RISet}
			hddn['incparentsf1']='ON'
			for key in hddn.keys():
				traitForm.append(HT.Input(name=key, value=hddn[key], type='hidden'))

			traitForm.append(HT.P(),pageTable)

			TD_LR.append(traitForm)
			if len(self.results) > 1 and i < len(self.results) - 1:
				lastone = True
		if lastone:
			TD_LR.contents.pop()

	def executeQuery(self):

		##construct sorting
		if self.dbType == "Publish":
			sortQuery = " order by Publication_PubMed_ID desc, Phenotype_Name, thistable"
		elif self.dbType == "Geno":
			if not self.orderByUserInput:
				if self.orderByDefalut:
					self.orderByUserInput = self.orderByDefalut
				else:
					self.orderByUserInput = "POSITION"
				if self.orderByUserInput.upper() in ["POS", "POSITION", "MB"]:
					self.orderByUserInput = "POSITION"
				else:
					pass
			self.orderByUserInput = self.orderByUserInput.upper()
			self.orderByUserInputOrig = self.orderByUserInput[:]
			if self.orderByUserInput == "NAME":
				sortQuery = " order by Geno_Name, Geno_chr_num, Geno_Mb"
			elif self.orderByUserInput == "SOURCE":
				sortQuery = " order by Geno_Source2, Geno_chr_num, Geno_Mb"
			else:
				sortQuery = " order by Geno_chr_num, Geno_Mb"
		#ProbeSet
		else:
			if not self.orderByUserInput:
				if self.orderByDefalut:
					self.orderByUserInput = self.orderByDefalut
				else:
					self.orderByUserInput = "POSITION"

			self.orderByUserInput = self.orderByUserInput.upper()
			self.orderByUserInputOrig = self.orderByUserInput[:]
			#XZ: 8/18/2009: "POSITION-"
			if self.orderByUserInput[-1] == '-':
				self.orderByUserInput = self.orderByUserInput[:-1]
				sortDesc = 'desc'
			else:
				sortDesc = ''

			if self.orderByUserInput in  ["MEAN", "LRS", "PVALUE"]:
				#sortQuery = " order by T%s %s, TNAME, thistable desc" % (self.orderByUserInput, sortDesc)
				sortQuery = " order by T%s desc, TNAME, thistable desc" % self.orderByUserInput
			elif self.orderByUserInput in ["POS", "POSITION", "MB"]:
				sortQuery = " order by TCHR_NUM %s, TMB %s, TNAME, thistable desc" % (sortDesc, sortDesc)
			elif self.orderByUserInput == 'SYMBOL':
				sortQuery = " order by TSYMBOL, thistable desc"
			else:
				sortQuery = " order by TNAME_NUM, thistable desc"

		if self.singleCross:
			if len(self.query) > 1:
				searchQuery = map(lambda X:'(%s)' % X, self.query)
				searchQuery = string.join(searchQuery, '  UNION ALL ')
			else:
				searchQuery = self.query[0]
			searchQuery += sortQuery
			#searchCountQuery retrieve all the results
			searchCountQuery = [searchQuery]
			#searchQuery = searchQuery + " limit %d,%d" % (self.pageNumber*self.NPerPage, self.NPerPage) // We removed the page limit - Zach 2/22/11
			searchQuery = [searchQuery]
		else:
			searchCountQuery = searchQuery = map(lambda X: X+sortQuery, self.query)

		allResults = []
		self.results = []
		for item in searchCountQuery:
		    start_time = datetime.datetime.now()
		    _log.info("111 Executing query: %s"%(item))
		    self.cursor.execute(item)
		    allResults.append(self.cursor.fetchall())
		    end_time = datetime.datetime.now()
		    _log.info("Total time: %s"%(end_time-start_time))

		_log.info("Done executing queries")


		#searchCountQuery retrieve all the results, for counting use only
		if searchCountQuery != searchQuery:
			for item in searchQuery:
				_log.info("222 Executing query: %s"%(item))
				self.cursor.execute(item)
				self.results.append(self.cursor.fetchall())
		else:
			self.results = allResults

		nresults = reduce(lambda Y,X:len(X)+Y, allResults, 0)
		return nresults



	def assembleQuery(self):
		self.query = []
		if self.ANDQuery or self.ORQuery:
			clause = self.ORQuery[:]

			for j, database in enumerate(self.database):
				if self.ANDQuery:
					clause.append(" (%s) " % string.join(self.ANDQuery, " AND "))

				newclause = []

				for item in clause:
					##need to retrieve additional field which won't be used
					##in the future, for sorting purpose only
					if self.dbType == "Publish":
						if item.find("Geno.name") < 0:
							incGenoTbl = ""
						else:
							incGenoTbl = " Geno, "
						newclause.append("SELECT %d, PublishXRef.Id, PublishFreeze.createtime as thistable, Publication.PubMed_ID as Publication_PubMed_ID, Phenotype.Post_publication_description as Phenotype_Name FROM %s PublishFreeze, Publication, PublishXRef, Phenotype WHERE PublishXRef.InbredSetId = %d and %s and PublishXRef.PhenotypeId = Phenotype.Id and PublishXRef.PublicationId = Publication.Id and PublishFreeze.Id = %d" % (j, incGenoTbl, self.databaseCrossIds[j], item, database.id))
					elif self.dbType == "ProbeSet":
						if item.find("GOgene") < 0:
							incGoTbl = ""
						else:
							incGoTbl = " ,db_GeneOntology.term as GOterm, db_GeneOntology.association as GOassociation, db_GeneOntology.gene_product as GOgene_product "
						if item.find("Geno.name") < 0:
							incGenoTbl = ""
						else:
							incGenoTbl = " Geno, "
						if item.find("GeneRIF_BASIC.") < 0:
							incGeneRIFTbl = ""
						else:
							incGeneRIFTbl = " GeneRIF_BASIC, "
						if item.find("GeneRIF.") < 0:
							incGeneRIFTbl += ""
						else:
							incGeneRIFTbl += " GeneRIF, "
						newclause.append("""SELECT distinct %d, ProbeSet.Name as TNAME, 0 as thistable,
						ProbeSetXRef.Mean as TMEAN, ProbeSetXRef.LRS as TLRS, ProbeSetXRef.PVALUE as TPVALUE,
						ProbeSet.Chr_num as TCHR_NUM, ProbeSet.Mb as TMB,  ProbeSet.Symbol as TSYMBOL,
						ProbeSet.name_num as TNAME_NUM  FROM %s%s ProbeSetXRef, ProbeSet %s
						WHERE %s and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSetXRef.ProbeSetFreezeId = %d
						""" % (j, incGeneRIFTbl, incGenoTbl, incGoTbl, item, database.id))
					elif self.dbType == "Geno":
						newclause.append("SELECT %d, Geno.Name, GenoFreeze.createtime as thistable, Geno.Name as Geno_Name, Geno.Source2 as Geno_Source2, Geno.chr_num as Geno_chr_num, Geno.Mb as Geno_Mb FROM GenoXRef, GenoFreeze, Geno WHERE %s and Geno.Id = GenoXRef.GenoId and GenoXRef.GenoFreezeId = GenoFreeze.Id and GenoFreeze.Id = %d"% (j, item, database.id))
					else:
						pass

				searchQuery = map(lambda X:'(%s)' % X, newclause)
				searchQuery = string.join(searchQuery, '  UNION ')
				self.query.append(searchQuery)
			return 1
		else:
			heading = "Search Result"
			detail = ["No keyword was entered for this search, please go back and enter your keyword."]
			self.error(heading=heading,detail=detail,error="No Keyword")
			return 0



	def normalSearch(self):
		self.ANDkeyword2 = re.sub(self._1mPattern, '', self.ANDkeyword)
		self.ANDkeyword2 = re.sub(self._2mPattern, '', self.ANDkeyword2)
		self.ANDkeyword2 = re.sub(self._3mPattern, '', self.ANDkeyword2)
		self.ANDkeyword2 = re.sub(self._5mPattern, '', self.ANDkeyword2)
		##remove remain parethesis, could be input with  syntax error
		self.ANDkeyword2 = re.sub(re.compile('\s*\([\s\S]*\)'), '', self.ANDkeyword2)
		self.ANDkeyword2 = self.encregexp(self.ANDkeyword2)

		self.ORkeyword2 = re.sub(self._1mPattern, '', self.ORkeyword)
		self.ORkeyword2 = re.sub(self._2mPattern, '', self.ORkeyword2)
		self.ORkeyword2 = re.sub(self._3mPattern, '', self.ORkeyword2)
		self.ORkeyword2 = re.sub(self._5mPattern, '', self.ORkeyword2)
		##remove remain parethesis, could be input with  syntax error
		self.ORkeyword2 = re.sub(re.compile('\s*\([\s\S]*\)'), '', self.ORkeyword2)
		self.ORkeyword2 = self.encregexp(self.ORkeyword2)

		if self.ORkeyword2 or self.ANDkeyword2:
			ANDFulltext = []
			ORFulltext = []
			for k, item in enumerate(self.ORkeyword2 + self.ANDkeyword2):
				self.nkeywords += 1
				if k >=len(self.ORkeyword2):
					query = self.ANDQuery
					DescriptionText = self.ANDDescriptionText
					clausejoin = ' OR '
					fulltext = ANDFulltext
				else:
					query = self.ORQuery
					DescriptionText = self.ORDescriptionText
					clausejoin = ' OR '
					fulltext = ORFulltext

				if self.dbType == "ProbeSet" and item.find('.') < 0 and item.find('\'') < 0:
					fulltext.append(item)
				else:
					if self.matchwhole and item.find("'") < 0:
						item = "[[:<:]]"+ item+"[[:>:]]"
					clause2 = []
					for field in self.searchField:
						if self.dbType == "Publish":
							clause2.append("%s REGEXP \"%s\"" % (field,item))
						else:
							clause2.append("%s REGEXP \"%s\"" % ("%s.%s" % (self.dbType,field),item))
					clauseItem = "(%s)" % string.join(clause2, clausejoin)
					query.append(" (%s) " % clauseItem)
			if ANDFulltext:
				clauseItem = " MATCH (ProbeSet.Name,ProbeSet.description,ProbeSet.symbol,alias,GenbankId, UniGeneId, Probe_Target_Description) AGAINST ('+%s' IN BOOLEAN MODE) " % string.join(ANDFulltext, " +")
				self.ANDQuery.append(" (%s) " % clauseItem)
			if ORFulltext:
				clauseItem = " MATCH (ProbeSet.Name,ProbeSet.description,ProbeSet.symbol,alias,GenbankId, UniGeneId, Probe_Target_Description) AGAINST ('%s' IN BOOLEAN MODE) " % string.join(ORFulltext, " ")
				self.ORQuery.append(" (%s) " % clauseItem)
		else:
			pass
		return 1



	def encregexp(self,str):
		if not str:
			return []
		else:
			wildcardkeyword = str.strip()
			wildcardkeyword = string.replace(wildcardkeyword,',',' ')
			wildcardkeyword = string.replace(wildcardkeyword,';',' ')
			wildcardkeyword = wildcardkeyword.split()
		NNN = len(wildcardkeyword)
		for i in range(NNN):
			keyword = wildcardkeyword[i]
			keyword = string.replace(keyword,"*",".*")
			keyword = string.replace(keyword,"?",".")
			wildcardkeyword[i] = keyword#'[[:<:]]'+ keyword+'[[:>:]]'
		return wildcardkeyword



	def patternSearch(self):
		# Lei Yan
		##Process Inputs
		m1_AND = self._1mPattern.findall(self.ANDkeyword)
		m2_AND = self._2mPattern.findall(self.ANDkeyword)
		m3_AND = self._3mPattern.findall(self.ANDkeyword)
		m5_AND = self._5mPattern.findall(self.ANDkeyword)
		m1_OR = self._1mPattern.findall(self.ORkeyword)
		m2_OR = self._2mPattern.findall(self.ORkeyword)
		m3_OR = self._3mPattern.findall(self.ORkeyword)
		m5_OR = self._5mPattern.findall(self.ORkeyword)

		#pattern search
		if m1_AND or m1_OR or m2_AND or m2_OR or m3_AND or m3_OR or m5_AND or m5_OR:

			self.orderByDefalut = 'PROBESETID'

			_1Cmds = map(string.upper, map(lambda x:x[0], m1_AND + m1_OR))
			_2Cmds = map(string.upper, map(lambda x:x[0], m2_AND + m2_OR))
			_3Cmds = map(string.upper, map(lambda x:x[0], m3_AND + m3_OR))
			_5Cmds = map(string.upper, map(lambda x:x[0], m5_AND + m5_OR))

			self.nkeywords += len(_1Cmds) + len(_2Cmds) + len(_3Cmds)

			if self.dbType == "Publish" and \
				( (_2Cmds and reduce(lambda x, y: (y not in ["LRS"]) or x, _2Cmds, False))\
				or (_5Cmds and reduce(lambda x, y: (y not in ["LRS"]) or x, _5Cmds, False)) ):
				heading = "Search Result"
				detail = ["Pattern search is not available for phenotype databases at this time."]
				self.error(heading=heading,detail=detail,error="Error")
				return 0
			elif self.dbType == "ProbeSet" and \
				((_2Cmds and reduce(lambda x, y: (y not in ["MEAN", "LRS", "PVALUE", "TRANSLRS", "CISLRS", "RANGE", "H2"]) or x, _2Cmds, False))\
				or (_3Cmds and reduce(lambda x, y: (y not in ["POS", "POSITION", "MB"]) or x, _3Cmds, False))\
				or (_5Cmds and reduce(lambda x, y: (y not in ["LRS"]) or x, _5Cmds, False))\
				or (_1Cmds and reduce(lambda x, y: (y not in ["FLAG", "STRAND_PROBE", "STRAND_GENE", "GO", "WIKI", "RIF", "GENEID"]) or x, _1Cmds, False))):
				heading = "Search Result"
				detail = ["You entered at least one incorrect search command."]
				self.error(heading=heading,detail=detail,error="Error")
				return 0
			elif self.dbType == "Geno" and (_1Cmds or _2Cmds or _5Cmds or (_3Cmds and reduce(lambda x, y: (y not in ["POS", "POSITION", "MB"]) or x, _3Cmds, False)) ):
				heading = "Search Result"
				detail = ["You entered at least one incorrect search command."]
				self.error(heading=heading,detail=detail,error="Error")
				return 0
			else:
				for k, item in enumerate(m1_OR+m1_AND):
					if k >=len(m1_OR):
						query = self.ANDQuery
						DescriptionText = self.ANDDescriptionText
					else:
						query = self.ORQuery
						DescriptionText = self.ORDescriptionText

					if item[1] == '-':
						strandName = 'minus'
					elif item[1] == '+':
						strandName = 'plus'
					else:
						strandName = item[1]

					if item[0].upper() in ("FLAG"):
						clauseItem = " %s.%s = %s " % (self.dbType, item[0], item[1])
						DescriptionText.append(HT.Span(' with ', HT.U('FLAG'), ' equal to ', item[1]))
					elif item[0].upper() in ("WIKI"):
						clauseItem = " %s.symbol = GeneRIF.symbol and GeneRIF.versionId=0 and GeneRIF.display>0 and (GeneRIF.comment REGEXP \"%s\" or GeneRIF.initial = \"%s\") " % (self.dbType, "[[:<:]]"+ item[1]+"[[:>:]]", item[1])
						DescriptionText.append(HT.Span(' with GeneWiki contains ', HT.U(item[1])))
					elif item[0].upper() in ("RIF"):
						clauseItem = " %s.symbol = GeneRIF_BASIC.symbol and MATCH (GeneRIF_BASIC.comment) AGAINST ('+%s' IN BOOLEAN MODE) " % (self.dbType, item[1])
						DescriptionText.append(HT.Span(' with GeneRIF contains ', HT.U(item[1])))
					elif item[0].upper() in ("GENEID"):
						clauseItem = " %s.GeneId in ( %s ) " % (self.dbType, string.replace(item[1], '-', ', '))
						DescriptionText.append(HT.Span(' with Entrez Gene ID in  ', HT.U(string.replace(item[1], '-', ', '))))
					elif item[0].upper() in ("GO"):
						Field = 'GOterm.acc'
						Id = 'GO:'+('0000000'+item[1])[-7:]
						Statements = '%s.symbol=GOgene_product.symbol and GOassociation.gene_product_id=GOgene_product.id and GOterm.id=GOassociation.term_id' % (self.dbType);
						clauseItem = " %s = '%s' and %s " % (Field, Id, Statements)
						#self.incGoTbl = " ,db_GeneOntology.term as GOterm, db_GeneOntology.association as GOassociation, db_GeneOntology.gene_product as GOgene_product "
						DescriptionText.append(HT.Span(' with ', HT.U('GO'), ' ID equal to ', Id))
					else:
						clauseItem = " %s.%s = '%s' " % (self.dbType, item[0], item[1])
						if item[0].upper() in ["STRAND_PROBE"]:
							DescriptionText.append(' with probe on the %s strand' % strandName)
						elif item[0].upper() in ["STRAND_GENE"]:
							DescriptionText.append(' with gene on the %s strand' % strandName)
						else:
							pass
					query.append(" (%s) " % clauseItem)

				for k, item in enumerate(m2_OR+m2_AND):
					if k >=len(m2_OR):
						query = self.ANDQuery
						DescriptionText = self.ANDDescriptionText
					else:
						query = self.ORQuery
						DescriptionText = self.ORDescriptionText

					itemCmd = item[0]
					lowerLimit = float(item[1])
					upperLimit = float(item[2])

					if itemCmd.upper() in ("TRANSLRS", "CISLRS"):
						if item[3]:
							mthresh = float(item[3])
							clauseItem = " %sXRef.LRS > %2.7f and %sXRef.LRS < %2.7f " % \
								(self.dbType, min(lowerLimit, upperLimit), self.dbType, max(lowerLimit, upperLimit))
							if itemCmd.upper() == "CISLRS":
								clauseItem += """ and  %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and %s.Chr = Geno.Chr and ABS(%s.Mb-Geno.Mb) < %2.7f """ % (self.dbType, self.speciesId, self.dbType, self.dbType, mthresh)
								DescriptionText.append(HT.Span(' with a ', HT.U('cis-QTL'), ' having an LRS between %g and %g using a %g Mb exclusion buffer'  % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit),  mthresh)))
							else:
								clauseItem += """ and  %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and (%s.Chr != Geno.Chr or (%s.Chr != Geno.Chr and ABS(%s.Mb-Geno.Mb) > %2.7f)) """ % (self.dbType, self.speciesId, self.dbType, self.dbType, self.dbType, mthresh)
								DescriptionText.append(HT.Span(' with a ', HT.U('trans-QTL'), ' having an LRS between %g and %g using a %g Mb exclusion buffer'  % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit),  mthresh)))
							query.append(" (%s) " % clauseItem)
							self.orderByDefalut = "LRS"
						else:
							pass
					elif itemCmd.upper() in ("RANGE"):
						#XZ, 03/05/2009: Xiaodong changed Data to ProbeSetData
						clauseItem = " (select Pow(2, max(value) -min(value)) from ProbeSetData where Id = ProbeSetXRef.dataId) > %2.7f and (select Pow(2, max(value) -min(value)) from ProbeSetData where Id = ProbeSetXRef.dataId) < %2.7f " % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit))
						query.append(" (%s) " % clauseItem)
						DescriptionText.append(HT.Span(' with a range of expression that varied between %g and %g' % (min(lowerLimit, upperLimit),  max(lowerLimit, upperLimit)), "  (fold difference)"))
					else:
						clauseItem = " %sXRef.%s > %2.7f and %sXRef.%s < %2.7f " % \
							(self.dbType, itemCmd, min(lowerLimit, upperLimit), self.dbType, itemCmd, max(lowerLimit, upperLimit))
						query.append(" (%s) " % clauseItem)
						self.orderByDefalut = itemCmd
						DescriptionText.append(HT.Span(' with ', HT.U(itemCmd), ' between %g and %g' % (min(lowerLimit, upperLimit),  max(lowerLimit, upperLimit))))

				for k, item in enumerate(m3_OR+m3_AND):
					if k >=len(m3_OR):
						query = self.ANDQuery
						DescriptionText = self.ANDDescriptionText
					else:
						query = self.ORQuery
						DescriptionText = self.ORDescriptionText
					itemCmd = item[0]
					chrsch = item[1]
					lowerLimit = float(item[2])
					upperLimit = float(item[3])
					fname = 'target genes'
					if self.dbType == "ProbeSet":
						clauseItem = " %s.Chr = '%s' and %s.Mb > %2.7f and %s.Mb < %2.7f " % \
							(self.dbType, chrsch, self.dbType, min(lowerLimit, upperLimit), self.dbType, max(lowerLimit, upperLimit))
					elif self.dbType == "Geno":
						fname = 'loci'
						clauseItem = " %s.Chr = '%s' and %s.Mb > %2.7f and %s.Mb < %2.7f " % \
							(self.dbType, chrsch, self.dbType, min(lowerLimit, upperLimit), self.dbType, max(lowerLimit, upperLimit))
					else:
						continue
					query.append(" (%s) " % clauseItem)
					self.orderByDefalut = itemCmd
					DescriptionText.append(HT.Span(' with ', HT.U('target genes'), ' on chromosome %s between %g and %g Mb' % \
						(chrsch, min(lowerLimit, upperLimit), max(lowerLimit, upperLimit))))

				for k, item in enumerate(m5_OR+m5_AND):
					if k >=len(m5_OR):
						query = self.ANDQuery
						DescriptionText = self.ANDDescriptionText
					else:
						query = self.ORQuery
						DescriptionText = self.ORDescriptionText
					itemCmd = item[0]
					lowerLimit = float(item[1])
					upperLimit = float(item[2])
					chrsch = item[3]
					MblowerLimit = float(item[4])
					MbupperLimit = float(item[5])
					if self.dbType == "ProbeSet" or self.dbType == "Publish":
						clauseItem = " %sXRef.LRS > %2.7f and %sXRef.LRS < %2.7f " % \
							(self.dbType, min(lowerLimit, upperLimit), self.dbType, max(lowerLimit, upperLimit))
						clauseItem += " and  %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and Geno.Chr = '%s' and Geno.Mb > %2.7f and Geno.Mb < %2.7f" \
							% (self.dbType, self.speciesId, chrsch, min(MblowerLimit, MbupperLimit),  max(MblowerLimit, MbupperLimit))
						query.append(" (%s) " % clauseItem)
						self.orderByDefalut = "MB"
						DescriptionText.append(HT.Span(' with ', HT.U('LRS'), ' between %g and %g' % \
							(min(lowerLimit, upperLimit),  max(lowerLimit, upperLimit)), \
							' on chromosome %s between %g and %g Mb' % \
							(chrsch, min(MblowerLimit, MbupperLimit),  max(MblowerLimit, MbupperLimit))))
			pass

		return 1

    	def generateWarningLayer(self):

        	layerString = """
		<!-- BEGIN FLOATING LAYER CODE //-->
		<div id="warningLayer" style="padding:3px; border: 1px solid #222;
  		background-color: #fff; position:absolute;width:250px;left:100;top:100;visibility:hidden">			
			<table border="0" width="250" class="cbrb" cellspacing="0" cellpadding="5">
				<tr>
					<td width="100%">
  						<table border="0" width="100%" cellspacing="0" cellpadding="0" height="36">
  							<tr>
  								<td class="cbrb cw ff15 fwb" align="Center" width="100%" style="padding:4px">
        								Sort Table
  								</td>
  							</tr>
  							<tr>
  								<td width="100%" bgcolor="#eeeeee" align="Center" style="padding:4px">
									<!-- PLACE YOUR CONTENT HERE //-->
									Resorting this table <br>
									<!-- END OF CONTENT AREA //-->
  								</td>
  							</tr>
  						</table>
					</td>
				</tr>
			</table>
		</div>
		<!-- END FLOATING LAYER CODE //-->

            	"""

		return layerString

	def getTableHeaderForGeno(self, worksheet=None, newrow=None, headingStyle=None):

		tblobj_header = []

		className = "fs13 fwb ffl b1 cw cbrb"

		tblobj_header = [[THCell(HT.TD(' ', Class=className), sort=0),
			THCell(HT.TD('Record', HT.BR(), 'ID', HT.BR(), Class=className), text='record_id', idx=1),
			THCell(HT.TD('Location', HT.BR(), 'Chr and Mb', HT.BR(), Class=className), text='location', idx=2)]]

		for ncol, item in enumerate(['Record ID', 'Location (Chr, Mb)']):
			worksheet.write([newrow, ncol], item, headingStyle)
			worksheet.set_column([ncol, ncol], 2*len(item))

		return tblobj_header


	def getTableBodyForGeno(self, traitList, formName=None, worksheet=None, newrow=None):

		tblobj_body = []

		className = "fs12 fwn ffl b1 c222"

		for thisTrait in traitList:
			tr = []

			if not thisTrait.haveinfo:
				thisTrait.retrieveInfo()

			trId = str(thisTrait)
                
			tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class="checkbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", Class=className), text=trId))

			tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.name,url="javascript:showDatabase3('%s','%s','%s','')" % (formName, thisTrait.db.name, thisTrait.name), Class="fs12 fwn ffl"),align="left", Class=className), text=thisTrait.name, val=thisTrait.name.upper()))

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

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

				trait_location_repr = 'Chr%s: %.6f' % (thisTrait.chr, float(thisTrait.mb) )

			tr.append(TDCell(HT.TD(trait_location_repr, Class="fs12 fwn b1 c222", nowrap="on"), trait_location_repr, trait_location_value))

			tblobj_body.append(tr)

			for ncol, item in enumerate([thisTrait.name, trait_location_repr]):
				worksheet.write([newrow, ncol], item)

			newrow += 1

		return tblobj_body

	def getTableHeaderForPublish(self, worksheet=None, newrow=None, headingStyle=None):

		tblobj_header = []

		className = "fs13 fwb ffl b1 cw cbrb"

		tblobj_header = [[THCell(HT.TD(' ', Class=className, nowrap="on"), sort=0), 
			THCell(HT.TD('Record',HT.BR(), 'ID',HT.BR(), Class=className, nowrap="on"), text="recond_id", idx=1),
			THCell(HT.TD('Phenotype',HT.BR(),HT.BR(), Class=className, nowrap="on"), text="pheno", idx=2),
			THCell(HT.TD('Authors',HT.BR(),HT.BR(), Class=className, nowrap="on"), text="auth", idx=3),
			THCell(HT.TD('Year',HT.BR(),HT.BR(), Class=className, nowrap="on"), text="year", idx=4),
			THCell(HT.TD('Max',HT.BR(), 'LRS', HT.BR(), Class="fs13 fwb ffl b1 cw cbrb", nowrap="on"), text="lrs", idx=5),
			THCell(HT.TD('Max LRS Location',HT.BR(),'Chr and Mb',HT.BR(), Class="fs13 fwb ffl b1 cw cbrb", nowrap="on"), text="lrs_location", idx=6)]]

		for ncol, item in enumerate(["Record", "Phenotype", "Authors", "Year", "Pubmed Id", "Max LRS", "Max LRS Location (Chr: Mb)"]):
			worksheet.write([newrow, ncol], item, headingStyle)
			worksheet.set_column([ncol, ncol], 2*len(item))

		return tblobj_header

	def getTableBodyForPublish(self, traitList, formName=None, worksheet=None, newrow=None, species=''):

		tblobj_body = []

		className = "fs12 fwn b1 c222"

		for thisTrait in traitList:
			tr = []

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

			trId = str(thisTrait)

			tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class="checkbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", Class=className), text=trId))

			tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.name,url="javascript:showDatabase3('%s','%s','%s','')" % (formName, thisTrait.db.name, thisTrait.name), Class="fs12 fwn"), nowrap="yes",align="center", Class=className),str(thisTrait.name), thisTrait.name))

			PhenotypeString = thisTrait.post_publication_description
			if thisTrait.confidential:
				if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=thisTrait.authorized_users):
					PhenotypeString = thisTrait.pre_publication_description
			tr.append(TDCell(HT.TD(PhenotypeString, Class=className), PhenotypeString, PhenotypeString.upper()))

			tr.append(TDCell(HT.TD(thisTrait.authors, Class="fs12 fwn b1 c222 fsI"),thisTrait.authors, thisTrait.authors.strip().upper()))

			try:
				PubMedLinkText = myear = repr = int(thisTrait.year)
			except:
                		PubMedLinkText = repr = "N/A"
				myear = 0

			if thisTrait.pubmed_id:
				PubMedLink = HT.Href(text= repr,url= webqtlConfig.PUBMEDLINK_URL % thisTrait.pubmed_id,target='_blank', Class="fs12 fwn")
			else:
				PubMedLink = repr

			tr.append(TDCell(HT.TD(PubMedLink, Class=className, align='center'), repr, myear))

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


			if thisTrait.lrs:
                        	LRS_score_repr = '%3.1f' % thisTrait.lrs
                        	LRS_score_value = thisTrait.lrs
                		tr.append(TDCell(HT.TD(LRS_score_repr, Class=className), LRS_score_repr, LRS_score_value))

				self.cursor.execute("""
					select Geno.Chr, Geno.Mb from Geno, Species
					where Species.Name = '%s' and
						Geno.Name = '%s' and
						Geno.SpeciesId = Species.Id
				""" % (species, thisTrait.locus))
				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)

                        			LRS_location_repr = 'Chr%s: %.6f' % (LRS_Chr, float(LRS_Mb) )
                        			LRS_flag = 0

                		tr.append(TDCell(HT.TD(LRS_location_repr, Class=className, nowrap="on"), LRS_location_repr, LRS_location_value))

			else:
				tr.append(TDCell(HT.TD("N/A", Class=className), "N/A", "N/A"))
            			tr.append(TDCell(HT.TD("N/A", Class=className), "N/A", "N/A"))

			tblobj_body.append(tr)

			for ncol, item in enumerate([thisTrait.name, PhenotypeString, thisTrait.authors, thisTrait.year, thisTrait.pubmed_id, LRS_score_repr, LRS_location_repr]):
				worksheet.write([newrow, ncol], item)

			newrow += 1

		return tblobj_body

	def getTableHeaderForProbeSet(self, worksheet=None, newrow=None, headingStyle=None):

		tblobj_header = []

		className = "fs13 fwb ffl b1 cw cbrb"
	
		tblobj_header = [[THCell(HT.TD(' ', Class="fs13 fwb ffl b1 cw cbrb",nowrap='ON'), sort=0),
					  	THCell(HT.TD('Record',HT.BR(), 'ID',HT.BR(), Class="fs13 fwb ffl b1 cw cbrb"), text="record_id", idx=1),
					 	 THCell(HT.TD('Symbol',HT.BR(),HT.BR(), Class="fs13 fwb ffl b1 cw cbrb"), text="symbol", idx=2),
					 	 THCell(HT.TD('Description',HT.BR(),HT.BR(), Class="fs13 fwb ffl b1 cw cbrb"), text="desc", idx=3),
					 	 THCell(HT.TD('Location',HT.BR(), 'Chr and Mb', HT.BR(), Class="fs13 fwb ffl b1 cw cbrb"), text="location", idx=4),
					 	 THCell(HT.TD('Mean',HT.BR(),'Expr',HT.BR(), Class="fs13 fwb ffl b1 cw cbrb"), text="mean", idx=5),
					 	 THCell(HT.TD('Max',HT.BR(),'LRS',HT.BR(), Class="fs13 fwb ffl b1 cw cbrb", nowrap='ON'), text="lrs", idx=6),
					  	THCell(HT.TD('Max LRS Location',HT.BR(),'Chr and Mb',HT.BR(), Class="fs13 fwb ffl b1 cw cbrb", nowrap='ON'), text="lrs_location", idx=7)]]

		for ncol, item in enumerate(['Record', 'Gene ID', 'Homologene ID', 'Symbol', 'Description', 'Location (Chr, Mb)', 'Mean Expr', 'Max LRS', 'Max LRS Location (Chr: Mb)']):
			worksheet.write([newrow, ncol], item, headingStyle)
			worksheet.set_column([ncol, ncol], 2*len(item))

		return tblobj_header

	def getTableBodyForProbeSet(self, traitList=[], primaryTrait=None, formName=None, worksheet=None, newrow=None, species=''):

		tblobj_body = []

		className = "fs12 fwn b1 c222"

		for thisTrait in traitList:

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

			if thisTrait.symbol:
                		pass
            		else:
                		thisTrait.symbol = "N/A"

            		tr = []

            		trId = str(thisTrait)

            		#XZ, 12/08/2008: checkbox
            		tr.append(TDCell(HT.TD(HT.Input(type="checkbox", Class="checkbox", name="searchResult",value=trId, onClick="highlight(this)"), nowrap="on", Class="fs12 fwn ffl b1 c222"), text=trId))

            		#XZ, 12/08/2008: probeset name
			if thisTrait.cellid:
            			tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.name, url="javascript:showDatabase3('%s','%s','%s','%s')" % (formName, thisTrait.db.name,thisTrait.name,thisTrait.cellid), Class="fs12 fwn"), Class=className), thisTrait.name, thisTrait.name.upper()))
			else:
				tr.append(TDCell(HT.TD(HT.Href(text=thisTrait.name, url="javascript:showDatabase3('%s','%s','%s','')" % (formName, thisTrait.db.name,thisTrait.name), Class="fs12 fwn"), Class=className), thisTrait.name, thisTrait.name.upper()))            	

           		if thisTrait.geneid:
                        	symbolurl = HT.Href(text=thisTrait.symbol,target='_blank',url="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids=%s" % thisTrait.geneid, Class="font_black fs12 fwn")
            		else:
                        	symbolurl = HT.Href(text=thisTrait.symbol,target='_blank',url="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?CMD=search&DB=gene&term=%s" % thisTrait.symbol, Class="font_black fs12 fwn")

            		#XZ, 12/08/2008: gene symbol
            		tr.append(TDCell(HT.TD(symbolurl, Class="fs12 fwn b1 c222 fsI"),thisTrait.symbol, thisTrait.symbol.upper()))

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

            		description_display = ''

            		if len(description_string) > 1 and description_string != 'None':
            	    		description_display = description_string
           	 	else:
           		     	description_display = thisTrait.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()

            		tr.append(TDCell(HT.TD(description_display, Class=className), description_display, description_display))

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

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

                		trait_location_repr = 'Chr%s: %.6f' % (thisTrait.chr, float(thisTrait.mb) )

          	  	tr.append(TDCell(HT.TD(trait_location_repr, Class=className, nowrap="on"), trait_location_repr, trait_location_value))

            		#XZ, 01/12/08: This SQL query is much faster.
           	 	self.cursor.execute("""
                   	 	select ProbeSetXRef.mean from ProbeSetXRef, ProbeSet
                   	 	where ProbeSetXRef.ProbeSetFreezeId = %d and
                        	  	ProbeSet.Id = ProbeSetXRef.ProbeSetId and
                        	  	ProbeSet.Name = '%s'
            		""" % (thisTrait.db.id, thisTrait.name))
            		result = self.cursor.fetchone()
            		if result:
                		if result[0]:
                    			mean = result[0]
                		else:
                    			mean=0
            		else:
                		mean = 0

			#XZ, 06/05/2009: It is neccessary to turn on nowrap
            		repr = "%2.3f" % mean
            		tr.append(TDCell(HT.TD(repr, Class=className, align='right', nowrap='ON'),repr, mean))

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

			#Max LRS and its Locus location
			if thisTrait.lrs and thisTrait.locus:
				self.cursor.execute("""
					select Geno.Chr, Geno.Mb from Geno, Species
					where Species.Name = '%s' and
						Geno.Name = '%s' and
						Geno.SpeciesId = Species.Id
				""" % (species, thisTrait.locus))
				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)

                        			LRS_score_repr = '%3.1f' % thisTrait.lrs
                        			LRS_score_value = thisTrait.lrs
                        			LRS_location_repr = 'Chr%s: %.6f' % (LRS_Chr, float(LRS_Mb) )
                        			LRS_flag = 0

                        			#tr.append(TDCell(HT.TD(HT.Href(text=LRS_score_repr,url="javascript:showIntervalMapping('%s', '%s : %s')" % (formName, thisTrait.db.shortname, thisTrait.name), Class="fs12 fwn"), Class=className, align='right', nowrap="on"),LRS_score_repr, LRS_score_value))
                        			tr.append(TDCell(HT.TD(LRS_score_repr, Class=className, align='right', nowrap="on"), LRS_score_repr, LRS_score_value))
                        			tr.append(TDCell(HT.TD(LRS_location_repr, Class=className, nowrap="on"), LRS_location_repr, LRS_location_value))

          			if LRS_flag:
                			tr.append(TDCell(HT.TD(LRS_score_repr, Class=className), LRS_score_repr, LRS_score_value))
                			tr.append(TDCell(HT.TD(LRS_location_repr, Class=className), LRS_location_repr, LRS_location_value))
		
			else:
				tr.append(TDCell(HT.TD("N/A", Class=className), "N/A", "N/A"))
				tr.append(TDCell(HT.TD("N/A", Class=className), "N/A", "N/A"))

			tblobj_body.append(tr)

			for ncol, item in enumerate([thisTrait.name, thisTrait.geneid, thisTrait.homologeneid, thisTrait.symbol, description_display, trait_location_repr, mean, LRS_score_repr, LRS_location_repr]):
      				worksheet.write([newrow, ncol], item)
				

            		newrow += 1

		return tblobj_body

	def createExcelFileWithTitleAndFooter(self, workbook=None, identification=None, db=None, returnNumber=None):

		worksheet = workbook.add_worksheet()

		titleStyle = workbook.add_format(align = 'left', bold = 0, size=14, border = 1, border_color="gray")

		##Write title Info
		# Modified by Hongqiang Li 
		worksheet.write([1, 0], "Citations: Please see %s/reference.html" % webqtlConfig.PORTADDR, titleStyle)
		worksheet.write([1, 0], "Citations: Please see %s/reference.html" % webqtlConfig.PORTADDR, titleStyle)
		worksheet.write([2, 0], "Trait : %s" % identification, titleStyle)
		worksheet.write([3, 0], "Database : %s" % db.fullname, titleStyle)
		worksheet.write([4, 0], "Date : %s" % time.strftime("%B %d, %Y", time.gmtime()), titleStyle)
		worksheet.write([5, 0], "Time : %s GMT" % time.strftime("%H:%M ", time.gmtime()), titleStyle)
		worksheet.write([6, 0], "Status of data ownership: Possibly unpublished data; please see %s/statusandContact.html for details on sources, ownership, and usage of these data." % webqtlConfig.PORTADDR, titleStyle)
		#Write footer info
		worksheet.write([9 + returnNumber, 0], "Funding for The GeneNetwork: NIAAA (U01AA13499, U24AA13513), NIDA, NIMH, and NIAAA (P20-DA21131), NCI MMHCC (U01CA105417), and NCRR (U01NR 105417)", titleStyle)
		worksheet.write([10 + returnNumber, 0], "PLEASE RETAIN DATA SOURCE INFORMATION WHENEVER POSSIBLE", titleStyle)

		return worksheet

	def getSortByValue(self, datasetType=''):

		if datasetType == 'Geno':
			sortby = ("location", "up")
		elif datasetType == 'ProbeSet':
			sortby = ("symbol", "up")
		else: #Phenotype
			sortby = ("record_id", "down")

		return sortby