-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathppnb.py
executable file
·140 lines (126 loc) · 4.78 KB
/
ppnb.py
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
#!/usr/bin/env python
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2018 William H. Majoros ([email protected])
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
# The above imports should allow this program to run in both Python 2 and
# Python 3. You might need to update your version of module "future".
import sys
import os
import math
import ProgramName
from Rex import Rex
rex=Rex()
import TempFilename
import getopt
from StanParser import StanParser
from PooledParser import PooledParser
from Stan import Stan
DEBUG=False
WARMUP=1000
MU=1 # 1
SIGMA2=100 # 1
ALPHA=1 # 1
BETA=0.05 # 0.05
STDERR=TempFilename.generate(".stderr")
INPUT_FILE=TempFilename.generate(".staninputs")
INIT_FILE=TempFilename.generate(".staninit")
OUTPUT_TEMP=TempFilename.generate(".stanoutputs")
def printFields(fields,hFile):
numFields=len(fields)
for i in range(7,numFields):
print(i-6,"=",fields[i],sep="",end="",file=hFile)
if(i<numFields-1): print("\t",end="",file=hFile)
print(file=hFile)
def getFieldIndex(label,fields):
numFields=len(fields)
index=None
for i in range(7,numFields):
if(fields[i]==label): index=i
return index
def writeToFile(fields,OUT):
numFields=len(fields)
for i in range(7,numFields):
print(fields[i],end="",file=OUT)
if(i<numFields-1): print("\t",end="",file=OUT)
print(file=OUT)
def writeInitializationFile(stan,filename):
OUT=open(filename,"wt")
print("theta <- 1",file=OUT)
print("r_ref <- 1",file=OUT)
print("lambda_ref <- 5",file=OUT)
print("lambda_alt <- 5",file=OUT)
OUT.close()
def writeInputsFile(stan,dna_ref,dna_alt,rna_ref,rna_alt,filename):
OUT=open(filename,"wt")
print("dna_ref <-",dna_ref,file=OUT)
print("dna_alt <-",dna_alt,file=OUT)
print("rna_ref <-",rna_ref,file=OUT)
print("rna_alt <-",rna_alt,file=OUT)
print("mu <-",MU,file=OUT)
print("sigma2 <-",SIGMA2,file=OUT)
print("alpha <-",ALPHA,file=OUT)
print("beta <-",BETA,file=OUT)
OUT.close()
def runVariant(stan,dna_ref,dna_alt,rna_ref,rna_alt,numSamples):
# Write inputs file for STAN
writeInputsFile(stan,dna_ref,dna_alt,rna_ref,rna_alt,INPUT_FILE)
writeInitializationFile(stan,INIT_FILE)
# Run STAN model
cmd=stan.getCmd(WARMUP,numSamples,INPUT_FILE,OUTPUT_TEMP,STDERR,INIT_FILE)
if(DEBUG):
print(cmd)
exit()
os.system(cmd)
# Parse MCMC output
parser=StanParser(OUTPUT_TEMP)
thetas=parser.getVariable("theta")
return (thetas,parser)
def summarize(parser,thetas,ID,minRight):
(median,CI_left,CI_right)=parser.getMedianAndCI(0.95,"theta")
maxLeft=1.0/minRight
leftP=parser.getLeftTail("theta",maxLeft)
rightP=parser.getRightTail("theta",minRight)
Preg=leftP if leftP>rightP else rightP
medianRatio=parser.getMedianAndCI(0.95,"r_ref")[0]
#print(ID,round(median,3),round(CI_left,3),round(CI_right,3),
# round(Preg,3),sep="\t")
print(ID,round(median,3),round(CI_left,3),round(CI_right,3),
round(Preg,3),round(medianRatio,3),sep="\t")
#=========================================================================
# main()
#=========================================================================
(options,args)=getopt.getopt(sys.argv[1:],"s:t:")
if(len(args)!=7):
exit(ProgramName.get()+" [-s stanfile] [-t thetafile] <model> <min-effect> <#MCMC-samples> <dna_ref> <dna_alt> <rna_ref> <rna_alt>\n -s = save raw STAN file\n -t = save theta samples\n min-effect (lambda) must be >= 1\n")
(model,minEffect,numSamples,dna_ref,dna_alt,rna_ref,rna_alt)=args
stanFile=None
thetaFile=None
for pair in options:
(key,value)=pair
if(key=="-s"): stanFile=value
if(key=="-t"): thetaFile=value
minEffect=float(minEffect)
dna_ref=int(dna_ref); dna_alt=int(dna_alt)
rna_ref=int(rna_ref); rna_alt=int(rna_alt)
if(minEffect<1): raise Exception("Min-effect must be >= 1")
THETA=None
if(thetaFile is not None): THETA=open(thetaFile,"wt")
stan=Stan(model)
(thetas,stanParser)=runVariant(stan,dna_ref,dna_alt,rna_ref,rna_alt,numSamples)
#summarize(stanParser,thetas,variant.ID,minEffect)
if(THETA is not None):
for i in range(len(thetas)):
print(thetas[i],file=THETA)
os.remove(STDERR)
os.remove(INPUT_FILE)
if(stanFile is None):
os.remove(OUTPUT_TEMP)
else:
os.system("cp "+OUTPUT_TEMP+" "+stanFile)
if(THETA is not None): THETA.close()