aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/showTrait/ShowTraitPage.py
blob: 82511228aaa3e54ac6bd176badfa1d853196be9c (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
# 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)
#
# Created by GeneNetwork Core Team 2010/08/10
#
# Last updated by GeneNetwork Core Team 2010/10/20

from htmlgen import HTMLgen2 as HT

from base import webqtlConfig
from utility import webqtlUtil
from base.webqtlTrait import webqtlTrait
from base.templatePage import templatePage
from DataEditingPage import DataEditingPage



class ShowTraitPage(DataEditingPage):

	def __init__(self, fd, traitInfos = []):

		templatePage.__init__(self, fd)

		if not self.openMysql():
			return
			
		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee')
		
		if traitInfos:
			database,ProbeSetID,CellID = traitInfos
		else:
			database = fd.formdata.getfirst('database')
			ProbeSetID = fd.formdata.getfirst('ProbeSetID')
			CellID = fd.formdata.getfirst('CellID')
		try:
			thisTrait = webqtlTrait(db=database, name=ProbeSetID, cellid= CellID, cursor=self.cursor)
		except:
			heading = "Trait Data and Analysis Form"
			detail = ["The trait isn't available currently."]
			self.error(heading=heading,detail=detail,error="Error")
			return

		if thisTrait.db.type == "ProbeSet":

			self.cursor.execute('''SELECT Id, Name, FullName, confidentiality, AuthorisedUsers 
						FROM ProbeSetFreeze WHERE Name = "%s"''' %  database)

			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, Confidential Database
					heading = "Show Database"
					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
					
		user_ip = fd.remote_ip
		query = "SELECT count(id) FROM AccessLog WHERE ip_address = %s and \
				UNIX_TIMESTAMP()-UNIX_TIMESTAMP(accesstime)<86400"
		self.cursor.execute(query,user_ip)
		daycount = self.cursor.fetchall()
		if daycount:
			daycount = daycount[0][0]
			if daycount > webqtlConfig.DAILYMAXIMUM:
				heading = "Retrieve Data"
				detail = ['For security reasons, the maximum access to a database is \
				%d times per day per ip address. You have reached the limit, please \
				try it again tomorrow.' % webqtlConfig.DAILYMAXIMUM]
				self.error(heading=heading,detail=detail)
				return
			else:
				pass
		else:
			pass
		
		if thisTrait.db.type != 'ProbeSet' and thisTrait.cellid:
				heading = "Retrieve Data"
				detail = ['The Record you requested doesn\'t exist!']
				self.error(heading=heading,detail=detail)
				return

		#XZ: Aug 23, 2010: I commented out this block because this feature is not used anymore		
		# check if animal information are available
		"""
		self.cursor.execute('''
						SELECT 
							SampleXRef.ProbeFreezeId 
						FROM 
							SampleXRef, ProbeSetFreeze 
						WHERE 
							SampleXRef.ProbeFreezeId = ProbeSetFreeze.ProbeFreezeId AND
							ProbeSetFreeze.Name = "%s"
					 ''' % thisTrait.db.name)
		
		sampleId = self.cursor.fetchall()
		if sampleId:
			thisTrait.strainInfo = 1
		else:
			thisTrait.strainInfo = None
		"""

		##identification, etc.
		fd.identification = '%s : %s'%(thisTrait.db.shortname,ProbeSetID)
		thisTrait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\
			&ProbeSetID=%s&RISet=%s&parentsf1=on' %(database,ProbeSetID,fd.RISet)
		
		if CellID:
			fd.identification = '%s/%s'%(fd.identification, CellID)
			thisTrait.returnURL = '%s&CellID=%s' % (thisTrait.returnURL, CellID)
		
		#retrieve trait information		
		try:
			thisTrait.retrieveInfo()
			thisTrait.retrieveData()
			self.updMysql()
			self.cursor.execute("insert into AccessLog(accesstime,ip_address) values(Now(),%s)" ,user_ip)
			self.openMysql()
		except:
			heading = "Retrieve Data"
			detail = ["The information you requested is not avaiable at this time."]
			self.error(heading=heading,detail=detail)
			return
			
		##read genotype file
		fd.RISet = thisTrait.riset
		fd.readGenotype()
		
		if webqtlUtil.ListNotNull(map(lambda x:x.var, thisTrait.data.values())):
			fd.displayVariance = 1
			fd.varianceDispName = 'SE'
			fd.formID = 'varianceChoice'
		
		self.dict['body']= thisTrait
		DataEditingPage.__init__(self, fd, thisTrait)
		self.dict['title'] = '%s: Display Trait' % fd.identification