annotate SMART/Java/Python/misc/Utils.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 """Some useful functions"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import sys, os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import subprocess
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 def writeFile(fileName, content):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 Write the content of a file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 handle = open(fileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 handle.write(content)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def sumOfLists(list1, list2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 Element by element sum
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 if len(list1) != len(list2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 sys.exit("Cannot sum list whose sizes are different!")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 return [list1[i] + list2[i] for i in range(len(list1))]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 def protectBackslashes(string):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 Protect the backslashes in a path by adding another backslash
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 return string.replace("\\", "\\\\")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def getHammingDistance(string1, string2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 Compute Hamming distance between two strings
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 if len(string1) != len(string2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 raise Exception("Error, size of %s and %s differ" % (string1, string2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 return sum(ch1 != ch2 for ch1, ch2 in zip(string1, string2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 def getLevenshteinDistance(string1, string2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 Compute Levenshtein distance between two strings
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 if len(string1) < len(string2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 return getLevenshteinDistance(string2, string1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 if not string1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 return len(string2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 previousRow = xrange(len(string2) + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 for i, c1 in enumerate(string1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 currentRow = [i + 1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 for j, c2 in enumerate(string2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 insertions = previousRow[j + 1] + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 deletions = currentRow[j] + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 substitutions = previousRow[j] + (c1 != c2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 currentRow.append(min(insertions, deletions, substitutions))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 previousRow = currentRow
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 return previousRow[-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 def getMinAvgMedMax(values):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 Get some stats about a dict
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 @param values: a distribution (the value being the number of occurrences of the key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 @type values: dict int to int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 @return: a tuple
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 minValues = min(values.keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 maxValues = max(values.keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 sumValues = sum([value * values[value] for value in values])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 nbValues = sum(values.values())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 allValues = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 for key in values:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 for i in range(values[key]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 allValues.append(key)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 sortedValues = sorted(allValues)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 sorted(values.values())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 if (nbValues % 2 == 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 medValues = (sortedValues[nbValues / 2 - 1] + sortedValues[nbValues / 2]) / 2.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 medValues = sortedValues[(nbValues + 1) / 2 - 1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 return (minValues, float(sumValues) / nbValues, medValues, maxValues)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 def xor(value1, value2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 Logical xor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 @param value1: a value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 @type value1: anything
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 @param value2: a value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 @type value2: anything
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 return bool(value1) != bool(value2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 def diff(fileName1, fileName2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 Compare two files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 @param fileName1: a file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 @type fileName1: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 @param fileName2: another file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 @type fileName2: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 @return: None if the files are the same, a string otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 handle1 = open(fileName1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 lines1 = handle1.readlines()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 handle2 = open(fileName2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 lines2 = handle2.readlines()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 if len(lines1) != len(lines2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 print "Sizes of files differ (%d != %d)" % (len(lines1), len(lines2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 for i in xrange(len(lines1)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 if lines1[i] != lines2[i]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 print "Line %d differ ('%s' != '%s')" % (i, lines1[i].strip(), lines2[i].strip())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 def binomialCoefficient(a, b):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 Compute cumulated product from a to b
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 @param a: a value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 @type a: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 @param b: a value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 @type b: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 if a > b / 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 a = b-a
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 p = 1.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 for i in range(b-a+1, b+1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 p *= i
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 q = 1.0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 for i in range(1, a+1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 q *= i
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 return p / q
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 memory = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 # def fisherExactPValue(a, b, c, d):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 # """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 # P-value of Fisher exact test for 2x2 contingency table
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 # """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 # if (a, b, c, d) in memory:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 # return memory[(a, b, c, d)]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 # n = a + b + c + d
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 # i1 = binomialCoefficient(a, a+b)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 # i2 = binomialCoefficient(c, a+c)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 # i3 = binomialCoefficient(c+d, n)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 # pValue = i1 * i2 / i3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 # memory[(a, b, c, d)] = pValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 # return pValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 def fisherExactPValue(a, b, c, d):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 if (a, b, c, d) in memory:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 return memory[(a, b, c, d)]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 scriptFileName = "tmpScript-%d.R" % (random.randint(0, 10000))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 rScript = open(scriptFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 rScript.write("data = matrix(c(%d, %d, %d, %d), nr=2)\n" % (a, b, c, d))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 rScript.write("fisher.test(data)\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 #rScript.write("chisq.test(data)\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 rScript.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 rCommand = "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 if "SMARTRPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 rCommand = os.environ["SMARTRPATH"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 command = "\"%s\" CMD BATCH %s" % (rCommand, scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 status = subprocess.call(command, shell=True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 if status != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 sys.exit("Problem with the execution of script file %s, status is: %s" % (scriptFileName, status))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 outputRFileName = "%sout" % (scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 outputRFile = open(outputRFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 pValue = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 pValueTag = "p-value "
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 for line in outputRFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 if line == "": continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 for splittedLine in line.split(","):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 splittedLine = splittedLine.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 if splittedLine.startswith(pValueTag):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 pValue = float(splittedLine.split()[-1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 if pValue == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 sys.exit("Problem with the cannot find p-value! File %s, values are: %d, %d, %d, %d" % (scriptFileName, a, b, c, d))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 os.remove(scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 os.remove(outputRFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 memory[(a, b, c, d)] = pValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 return pValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 def fisherExactPValueBulk(list):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 scriptFileName = "tmpScript-%d.R" % (random.randint(0, 10000))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 rScript = open(scriptFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 for element in list:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 rScript.write("fisher.test(matrix(c(%d, %d, %d, %d), nr=2))$p.value\n" % (int(element[0]), int(element[1]), int(element[2]), int(element[3])))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 rScript.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 rCommand = "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 if "SMARTRPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 rCommand = os.environ["SMARTRPATH"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 command = "\"%s\" CMD BATCH %s" % (rCommand, scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 status = subprocess.call(command, shell=True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if status != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 sys.exit("Problem with the execution of script file %s, status is: %s" % (scriptFileName, status))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 outputRFileName = "%sout" % (scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 outputRFile = open(outputRFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 pValue = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 pValueTag = "[1] "
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 results = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 cpt = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 for line in outputRFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 if line == "": continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 if line.startswith(pValueTag):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 pValue = float(line.split()[-1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 results[list[cpt][0:2]] = pValue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 cpt += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 if pValue == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 sys.exit("Problem with the cannot find p-value!")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 if cpt != len(list):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 sys.exit("Error in the number of p-values computed by R in file '%s'!" % (scriptFileName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 os.remove(scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 os.remove(outputRFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 return results
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271