aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/compareCorrelates/htmlModule.py
blob: ebba3b8617199987b90057a43e357aa7678d55b0 (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
# 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

import sys
import string
import os
import MySQLdb
import cgi

from htmlgen import HTMLgen2 as HT

from base import webqtlConfig


# XZ 08/14/2008: When I tried to replace 'from webqtlConfig import *' with 'import webqtlConfig'
# XZ 08/14/2008: I found some problems. I discussed with Hongqiang and the below is conclusion.
# XZ 08/14/2008: The program uses webqtlConfig.DB_NAME, webqtlConfig.MYSQL_SERVER and so on
# XZ 08/14/2008: without 'import webqtlConfig'. This program will not work.
# XZ 08/14/2008: CONFIG_htmlpath doesn't exist in webqtlConfig.py
# XZ 08/14/2008: Hongqian said this was done by Fan Zhang, and this program was not tested.
# XZ 08/14/2008: So nobody realize these bugs.

# XZ, 09/09/2008: This function is not called any where. 
# XZ, 09/09/2008: Actually, I don't think this function works.
def genHeaderFooter(i=1,title='',basehref='',js1='',js2='',layer='',body=''):
	"""
	generate footer and header HTML code
	default is header
	i = 0 is footer+header
	i = 1 is header
	i = 2 is footer	
	"""
	try:
                temp_file = CONFIG_htmlpath + 'beta-template.html'
		fp = open(temp_file, 'rb')
		template = fp.read()
		fp.close()
		template = template % (title,basehref,js1,js2,layer,body, "")
		header,footer = string.split(template,'<!-- split from Here -->')
		if i == 0:
			return header + footer
		elif i == 1:
			return header
		elif i == 2:
			return footer
		else:
			return ""
	except:
		if i == 0:
			return "header + footer"
		elif i == 1:
			return "header"
		elif i == 2:
			return "footer"
		else:
			return ""

# XZ, 09/09/2008: This function is only used in multitrait.py where it is called with value assigned to db.
# XZ, 09/09/2008: So the try-except block is not executed.
# XZ, 09/09/2008: This explains why no error was generated even without 'import webqtlConfig'
def genDatabaseMenu(db = None, public =1, RISetgp = 'BXD', selectname = 'database', selected = ""):
	"""
	generate database Menu
	public = 0 : search3.html databases Menu
	public = 1 : search.html databases Menu
	"""
	if not db:
		try:
			# import MySQLdb
			# con = MySQLdb.Connect(db='db_webqtl')
			# Modified by Fan Zhang
			con = MySQLdb.Connect(db=webqtlConfig.DB_NAME,host=webqtlConfig.MYSQL_SERVER, user=webqtlConfig.DB_USER,passwd=webqtlConfig.DB_PASSWD)
			db = con.cursor()
		except:
			return "Connect MySQL Server Error"
	else:
		pass
	
	databaseMenu = HT.Select(name=selectname)
	nmenu = 0

	# here's a hack: bxd and bxd300 can be correlated against each other
	# if either of those are the group, we put in special SQL that pulls both
	if RISetgp in ("BXD", "BXD300"):
		ibsNameQry = '(InbredSet.Name = "BXD" OR InbredSet.Name = "BXD300")'
	else:
		ibsNameQry = 'InbredSet.Name = "%s"' % RISetgp
	
	#Publish Database
	db.execute('''
		   SelecT
		     PublishFreeze.FullName,
		     PublishFreeze.Name
		   from
		     PublishFreeze,
		     InbredSet
		   where
		     PublishFreeze.InbredSetId = InbredSet.Id and
		     %s
		   ''' % ibsNameQry)
	for item in db.fetchall():
		databaseMenu.append(item)
		nmenu += 1
	
	#Genome Database
	db.execute('''
		   SelecT
		     GenoFreeze.FullName,
		     GenoFreeze.Name
		   from
		     GenoFreeze,InbredSet
		   where
		     GenoFreeze.InbredSetId = InbredSet.Id and
		     %s
		   ''' % ibsNameQry)
	for item in db.fetchall():
		databaseMenu.append(item)
		nmenu += 1
	
	#Microarray Database
	db.execute('SelecT Id, Name from Tissue')
	for item in db.fetchall():
		TId, TName = item
		databaseMenuSub = HT.Optgroup(label = '%s ------' % TName)
		db.execute('''
			   SelecT
			     ProbeSetFreeze.FullName,
			     ProbeSetFreeze.Name
			   from
			     ProbeSetFreeze,
			     ProbeFreeze,
			     InbredSet
			   where
			     ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id and
			     ProbeFreeze.TissueId = %d and
			     ProbeSetFreeze.public > %d and
			     ProbeFreeze.InbredSetId = InbredSet.Id and
			     %s
			   order by
			     ProbeSetFreeze.CreateTime desc,
			     ProbeSetFreeze.AvgId
			   '''  % (TId,public,ibsNameQry))
		for item2 in db.fetchall():
			databaseMenuSub.append(item2)
			nmenu += 1
		databaseMenu.append(databaseMenuSub)
	
	if nmenu:
		if selected:
			databaseMenu.selected.append(selected)
		return str(databaseMenu)
	else:
		return ''


# XZ, 09/09/2008: This function is not called any where. 
# XZ, 09/09/2008: Actually, I don't think this function works.
# XZ, 09/09/2008: There is no 'DataForm' file now. It should be webqtlForm.py
def genRISample():
	import glob
	import reaper
	import random
	import math
	import webqtlUtil
	risets = filter(lambda X:X.find('F2')<0, map(os.path.basename, glob.glob(os.path.join(CONFIG_genodir, "*.geno"))))
	risets = map(lambda X:X.split('.')[0], risets)
	risets.remove("BayXSha")
	risets.sort()
	body = HT.Blockquote()
	NPerRow = 6
	for item in risets:
		values = []
		if item == 'AXBXA': item2='AXB/BXA'
		elif item == 'HXBBXH': item2='HXB/BXH'
		else: item2=item
		body.append(HT.Paragraph(item2, Class='subtitle'))
		tbl = HT.TableLite(Class="collap")
		dataset = reaper.Dataset()
		dataset.read(os.path.join(CONFIG_genodir, "%s.geno"%item))
		prgy = webqtlUtil.ParInfo[item] + list(dataset.prgy)
		
		mean = random.random()*100
		variance = random.random()*500
		variables = []
		while len(variables) < len(prgy):
			S = 2
			while (S>=1):
				U1= random.random()
				U2= random.random()
				V1= 2*U1-1.0
				V2= 2*U2-1.0
				S=V1*V1+V2*V2
			X= math.sqrt(-2 *  math.log(S) / S) * V1
			Y= math.sqrt(-2 *  math.log(S) / S) * V2	
			variables.append(mean + math.sqrt(variance) * X)
			variables.append(mean + math.sqrt(variance) * Y) 
		
		tempTR = HT.TR()
		for i, strain in enumerate(prgy):
			if i and i%NPerRow==0:
				tbl.append(tempTR)
				tempTR = HT.TR()
			if random.random() < 0.2:
				variable = 'X'
			else:
				variable = "%2.3f" % variables[i]
			
			tempTR.append(HT.TD(strain, Class="strains", width=80))
			tempTR.append(HT.TD(variable, Class="values", width=60))
			values.append(variable)
		
		for j in range(NPerRow-i%NPerRow-1):
			tempTR.append(HT.TD())
		tbl.append(tempTR)	
		body.append(tbl)
		body.append(HT.Paragraph("Copy the following line to paste into the GeneNetwork entry box:"))
		body.append(HT.Code(string.join(values, " ")))
		body.append(HT.HR(width="90%"))
	return body
		
if __name__ == "__main__":
	if os.environ.has_key('SCRIPT_FILENAME'):
		script_filename = os.environ['SCRIPT_FILENAME']
	else:
		script_filename = ''
	#Used as cgi script
	if script_filename and script_filename[-2:] == 'py':
		print 'Content-type: text/html\n'
		formdata = cgi.FieldStorage()
		sys.stderr = sys.stdout
		try:
			getID = string.lower(formdata.getvalue('get'))
		except:
			getID = ''
	#Used as command
	else:
		if len(sys.argv) >= 2:
			getID = string.lower(sys.argv[1])
		else:
			getID = ''
	
	if getID == 'headerfooter':
		print genHeaderFooter(0)
	elif getID == 'header':
		print genHeaderFooter(1)
	elif getID == 'footer':
		print genHeaderFooter(2)
	elif getID == 'databasemenu':
		print genDatabaseMenu(public=0)
	elif getID == 'datasample':
		print genRISample()
	else:
		print genHeaderFooter(0)
else:
	pass