aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/textUI/cmdMap.py
blob: 1fbff5a5bfb94a733696727b111a70e0b2d4efc9 (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
# 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 os

import reaper

from base import webqtlConfig
from cmdClass import cmdClass


#########################################
#      Mapping Class
#########################################
class cmdMap(cmdClass):

	def __init__(self,fd=None):

		cmdClass.__init__(self,fd)

		if not webqtlConfig.TEXTUI:
			self.contents.append("Please send your request to http://robot.genenetwork.org")
			return

		self.example = '###Example : <a href="%s%s?cmd=%s&probeset=100001_at&probe=136415&db=bra03-03Mas5&sort=lrs&return=20">%s%s?cmd=%s&probeset=100001_at&probe=136415&db=bra03-03Mas5&sort=lrs&return=20</a>' % (webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE, self.cmdID, webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE, self.cmdID)
		if self.accessError:
			return
		try:
			self.returnnumber = int(self.data.getvalue('return'))
		except:
			self.returnnumber = None
		
		if self.error:
			self.contents.append(self.example)
			self.contents.append(self.accessCode)
		else:
			self.sort = self.data.getvalue('sort')
			self.readDB()
		
	def  readDB(self):
		prefix, dbId = self.getDBId(self.database)
		if not prefix or not dbId or (self.probe and string.lower(self.probe) in ("all","mm","pm")):
			self.contents.append("###Error: source trait doesn't exist or SELECT more than one trait.")
			self.contents.append(self.example)
			self.contents.append(self.accessCode)
			return
		RISet = self.getRISet(prefix, dbId)
		traitdata, heads = self.getTraitData(prefix, dbId, self.probeset, self.probe)
		if not traitdata:
			self.contents.append("###Error: source trait doesn't exist or SELECT more than one trait.")
			self.contents.append(self.example)
			self.contents.append(self.accessCode)
			return
			
		dataset = reaper.Dataset()
		dataset.read(os.path.join(webqtlConfig.GENODIR, RISet + '.geno'))
		strainList = list(dataset.prgy)
		
		strains = []
		trait = []
		_prgy = dataset.prgy
		for item in traitdata:
			if item[0] in _prgy:
				strains.append(item[0])	
				trait.append(item[1])
		
		qtlscan = dataset.regression(strains, trait)
		LRS = dataset.permutation(strains, trait)
		nperm = len(LRS)	
		if qtlscan:
			self.contents.append("Locus\tLRS\tChr\tAdditive\tp-value") 
			qtlresult = []
			if self.returnnumber:
				self.returnnumber = min(self.returnnumber,len(qtlscan))
				if self.sort == 'lrs':
					qtlscan.sort(self.cmpLRS)
					for item in qtlscan[:self.returnnumber]:
						p_value = reaper.pvalue(item.lrs,LRS)	
						qtlresult.append((item.locus.name,item.lrs,item.locus.chr,item.additive,p_value))
				else:#sort by position
					qtlscan2 = qtlscan[:]
					qtlscan2.sort(self.cmpLRS)
					LRSthresh = qtlscan2[self.returnnumber].lrs
					for item in qtlscan:
						if item.lrs >= LRSthresh:
							p_value = reaper.pvalue(item.lrs,LRS)	
							qtlresult.append((item.locus.name,item.lrs,item.locus.chr,item.additive,p_value))
			else:
				for item in qtlscan:
					p_value = reaper.pvalue(item.lrs,LRS)
					qtlresult.append((item.locus.name,item.lrs,item.locus.chr,item.additive,p_value))
				if self.sort == 'lrs':
					qtlresult.sort(self.cmpLRS2)
			for item in qtlresult:
				self.contents.append("%s\t%2.5f\t%s\t%2.5f\t%1.5f" % item)
		else:
			self.contents.append("###Error: Error occurs while regression.")
			return
			
	def cmpLRS(self,A,B):
		try:
			if A.lrs < B.lrs:
				return 1
			elif A.lrs == B.lrs:
				return 0
			else:
				return -1	
		except:
			return 0

	def cmpLRS2(self,A,B):
		try:
			if A[1] < B[1]:
				return 1
			elif A[1] == B[1]:
				return 0
			else:
				return -1	
		except:
			return 0