annotate ttest/stats.py @ 0:12bb38e187b9

Uploaded, initial check-in
author melissacline
date Mon, 28 Jul 2014 20:12:17 -0400
parents
children f1929875b1b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
1 #!/usr/bin/env python
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
2
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
3
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
4
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
5 import argparse
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
6 import math
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
7 import re
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
8 import sys
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
9 try:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
10 import numpy
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
11 except:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
12 from galaxy import eggs
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
13 eggs.require("numpy")
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
14 import numpy
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
15 try:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
16 import scipy
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
17 except:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
18 from galaxy import eggs
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
19 eggs.require( "scipy" )
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
20 import scipy
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
21 import scipy.stats
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
22
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
23 def main():
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
24 parser = argparse.ArgumentParser()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
25 parser.add_argument("genomicData", type=str)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
26 parser.add_argument("clinicalVector", type=str)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
27 parser.add_argument("outfile", type=str)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
28 parser.add_argument('-a', type=str, default=None)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
29 parser.add_argument('-b', type=str, default=None)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
30 args = parser.parse_args()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
31 clinicalVector = readClinicalFile(args.clinicalVector)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
32 genomicFp = open(args.genomicData)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
33 headerTokens = genomicFp.readline().rstrip().split("\t")
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
34 (columnsGroupA, columnsGroupB) = headerLineToGroups(headerTokens,
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
35 clinicalVector,
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
36 args.a, args.b)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
37 print columnsGroupA, columnsGroupB
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
38 outfile = open(args.outfile, "w")
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
39 outfile.write("%s\tStatistic\tpValue\tMedian1\tMedian2\tDelta\n" % (headerTokens[0]))
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
40 for row in genomicFp:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
41 tokens = row.rstrip().split("\t")
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
42 symbol = tokens[0]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
43 groupATokens = [tokens[ii] for ii in columnsGroupA]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
44 groupBTokens = [tokens[ii] for ii in columnsGroupB]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
45 groupAValues = [float(xx) for xx in groupATokens if xx != "NA"]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
46 groupBValues = [float(xx) for xx in groupBTokens if xx != "NA"]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
47 (ttest, pvalue) = performTTest(groupAValues, groupBValues)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
48 if ttest == None or pvalue == None:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
49 outputLine = "%s\tNA\tNA" % (symbol)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
50 else:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
51 outputLine= "%s\t%f\t%f" % (symbol, ttest, pvalue)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
52 if len(groupAValues) == 0:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
53 outputLine = outputLine + "\tNA"
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
54 else:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
55 medianA = numpy.median(groupAValues)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
56 outputLine = "%s\t%f" % (outputLine, medianA)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
57 if len(groupBValues) == 0:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
58 outputLine = outputLine + "\tNA"
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
59 else:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
60 medianB = numpy.median(groupBValues)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
61 outputLine = "%s\t%f" % (outputLine, medianB)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
62 if len(groupAValues) == 0 or len(groupBValues) == 0:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
63 outputLine = outputLine + "\tNA"
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
64 else:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
65 outputLine = "%s\t%f" % (outputLine, medianA - medianB)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
66 outfile.write("%s\n" % (outputLine))
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
67 outfile.close()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
68
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
69 def readClinicalFile(clinicalFile):
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
70 clinicalVector = dict()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
71 fp = open(clinicalFile)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
72 fp.readline()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
73 for row in fp:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
74 tokens = row.rstrip().split("\t")
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
75 clinicalVector[tokens[0]] = tokens[1]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
76 fp.close()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
77 return(clinicalVector)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
78
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
79 def headerLineToGroups(headerTokens, clinicalVector, optionA, optionB):
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
80 columnsGroupA = list()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
81 columnsGroupB = list()
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
82 for ii in range(len(headerTokens)):
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
83 id = headerTokens[ii]
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
84 if clinicalVector.has_key(id):
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
85 if clinicalVector[id] == optionA:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
86 columnsGroupA.append(ii)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
87 elif clinicalVector[id] == optionB:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
88 columnsGroupB.append(ii)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
89 return((columnsGroupA, columnsGroupB))
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
90
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
91 def performTTest(groupA, groupB):
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
92 from scipy import special
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
93
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
94 epsilon = 1E-06
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
95 countA = len(groupA)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
96 countB = len(groupB)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
97 meanA = numpy.mean(groupA)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
98 meanB = numpy.mean(groupB)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
99 varA = numpy.var(groupA)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
100 varB = numpy.var(groupB)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
101 degreesOfFreedom = countA + countB - 2
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
102 sVar = ((countA - 1) * varA + (countB - 1) * varB) / degreesOfFreedom
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
103 if sVar == 0:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
104 tStat = None
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
105 prob = None
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
106 else:
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
107 tStat = (meanA - meanB)/math.sqrt(sVar * (1.0/(countA + epsilon)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
108 + 1.0/(countB + epsilon)))
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
109 bottom = degreesOfFreedom + tStat * tStat
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
110 prob = special.betainc(0.5*degreesOfFreedom, 0.5,
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
111 degreesOfFreedom/bottom)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
112 return(tStat, prob)
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
113
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
114
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
115
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
116
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
117 if __name__ == "__main__":
12bb38e187b9 Uploaded, initial check-in
melissacline
parents:
diff changeset
118 main()