aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/pairScan/CategoryGraphPage.py
blob: 696c05ce62794e38216fb91ab4ad4e75dd3203e6 (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
# 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 string
import piddle as pid
from htmlgen import HTMLgen2 as HT

from utility import Plot
from base.templatePage import templatePage
from utility import webqtlUtil
from base import webqtlConfig

#########################################
#      Category Graph Page
#########################################
class CategoryGraphPage(templatePage):
	def __init__(self, fd):

		LRSFullThresh = 30
		LRSInteractThresh = 25
		maxPlotSize = 800
		mainfmName = webqtlUtil.genRandStr("fm_")

		templatePage.__init__(self, fd)

		if not fd.genotype:
			fd.readData()

		##Remove F1 and Parents
		fd.genotype = fd.genotype_1

		plotType = fd.formdata.getvalue('plotType')
		self.dict['title'] = '%s Plot' % plotType
		main_title = HT.Paragraph("%s Plot" % plotType)
		main_title.__setattr__("class","title")

		interval1 = fd.formdata.getvalue('interval1')
		interval2 = fd.formdata.getvalue('interval2')

		flanka1, flanka2, chram = string.split(interval1)
		flankb1, flankb2, chrbm = string.split(interval2)

		traitValues = string.split(fd.formdata.getvalue('traitValues'), ',')
		traitValues = map(webqtlUtil.StringAsFloat, traitValues)
		traitStrains = string.split(fd.formdata.getvalue('traitStrains'), ',')

		flankaGeno = []
		flankbGeno = []

		for chr in fd.genotype:
			for locus in chr:
				if locus.name in (flanka1, flankb1):
					if locus.name == flanka1:
						flankaGeno = locus.genotype[:]
					else:
						flankbGeno = locus.genotype[:]
			if flankaGeno and flankbGeno:
				break

		flankaDict = {}
		flankbDict = {}
		for i in range(len(fd.genotype.prgy)):
			flankaDict[fd.genotype.prgy[i]] = flankaGeno[i]
			flankbDict[fd.genotype.prgy[i]] = flankbGeno[i]

		BB = []
		BD = []
		DB = []
		DD = []

		iValues = []
		for i in range(len(traitValues)):
			if traitValues[i] != None:
				iValues.append(traitValues[i])
				thisstrain = traitStrains[i]
				try:
					a1 = flankaDict[thisstrain]
					b1 = flankbDict[thisstrain]
				except:
					continue
				if a1 == -1.0:
					if b1 == -1.0:
						BB.append((thisstrain, traitValues[i]))
					elif b1 == 1.0:
						BD.append((thisstrain, traitValues[i]))
				elif a1 == 1.0:
					if b1 == -1.0:
						DB.append((thisstrain, traitValues[i]))
					elif b1 == 1.0:
						DD.append((thisstrain, traitValues[i]))
				else:
					pass

		#print BB, BD, DB, DD, max(iValues), min(iValues)

		plotHeight = 400
		plotWidth = 600
		xLeftOffset = 60
		xRightOffset = 40
		yTopOffset = 40
		yBottomOffset = 60

		canvasHeight = plotHeight + yTopOffset + yBottomOffset
		canvasWidth = plotWidth + xLeftOffset + xRightOffset
		canvas = pid.PILCanvas(size=(canvasWidth,canvasHeight))
		XXX = [('Mat/Mat', BB), ('Mat/Pat', BD), ('Pat/Mat', DB), ('Pat/Pat', DD)]
		XLabel = "Interval 1 / Interval 2"

		if plotType == "Box":
			Plot.plotBoxPlot(canvas, XXX, offset=(xLeftOffset, xRightOffset, yTopOffset, yBottomOffset), XLabel = XLabel)
		else:
			#Could be a separate function, but seems no other uses
			max_Y = max(iValues)
			min_Y = min(iValues)
			scaleY = Plot.detScale(min_Y, max_Y)
			Yll = scaleY[0]
			Yur = scaleY[1]
			nStep = scaleY[2]
			stepY = (Yur - Yll)/nStep
			stepYPixel = plotHeight/(nStep)
			canvas.drawRect(plotWidth+xLeftOffset, plotHeight + yTopOffset, xLeftOffset, yTopOffset)

			##draw Y Scale
			YYY = Yll
			YCoord = plotHeight + yTopOffset
			scaleFont=pid.Font(ttf="cour",size=11,bold=1)
			for i in range(nStep+1):
				strY = Plot.cformat(d=YYY, rank=0)
				YCoord = max(YCoord, yTopOffset)
				canvas.drawLine(xLeftOffset,YCoord,xLeftOffset-5,YCoord)
				canvas.drawString(strY,	xLeftOffset -30,YCoord +5,font=scaleFont)
				YYY += stepY
				YCoord -= stepYPixel


			##draw X Scale
			stepX = plotWidth/len(XXX)
			XCoord = xLeftOffset + 0.5*stepX
			YCoord = plotHeight + yTopOffset
			scaleFont = pid.Font(ttf="tahoma",size=12,bold=0)
			labelFont = pid.Font(ttf="tahoma",size=13,bold=0)
			for item in XXX:
				itemname, itemvalue = item
				canvas.drawLine(XCoord, YCoord,XCoord, YCoord+5, color=pid.black)
				canvas.drawString(itemname, XCoord - canvas.stringWidth(itemname,font=labelFont)/2.0,YCoord +20,font=labelFont)
				itemvalue.sort(webqtlUtil.cmpOrder2)
				j = 0
				for item2 in itemvalue:
					tstrain, tvalue = item2
					canvas.drawCross(XCoord, plotHeight + yTopOffset - (tvalue-Yll)*plotHeight/(Yur - Yll), color=pid.red,size=5)
					if j % 2 == 0:
						canvas.drawString(tstrain, XCoord+5, plotHeight + yTopOffset - \
						(tvalue-Yll)*plotHeight/(Yur - Yll) +5, font=scaleFont, color=pid.blue)
					else:
						canvas.drawString(tstrain, XCoord-canvas.stringWidth(tstrain,font=scaleFont)-5, \
						plotHeight + yTopOffset - (tvalue-Yll)*plotHeight/(Yur - Yll) +5, font=scaleFont, color=pid.blue)
					j += 1
				XCoord += stepX


			labelFont=pid.Font(ttf="verdana",size=18,bold=0)
			canvas.drawString(XLabel, xLeftOffset + (plotWidth -canvas.stringWidth(XLabel,font=labelFont))/2.0, YCoord +40, font=labelFont)
			canvas.drawString("Value",xLeftOffset-40,  YCoord-(plotHeight -canvas.stringWidth("Value",font=labelFont))/2.0, font=labelFont, angle =90)


		filename= webqtlUtil.genRandStr("Cate_")
		canvas.save(webqtlConfig.IMGDIR+filename, format='gif')
		img=HT.Image('/image/'+filename+'.gif',border=0)

		TD_LR = HT.TD(height=200,width="100%",bgColor='#eeeeee',valign='top')
		TD_LR.append(main_title, HT.Center(img))#, traitValues , len(traitValues), traitStrains, len(traitStrains), len(fd.genotype.prgy))
		#TD_LR.append(main_title, HT.BR(), flanka1, flanka2, chram, HT.BR(), flankb1, flankb2, chrbm)
		self.dict['body'] = str(TD_LR)