-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_codeml_clade.py
More file actions
176 lines (157 loc) · 5.05 KB
/
read_codeml_clade.py
File metadata and controls
176 lines (157 loc) · 5.05 KB
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
import os
import re
import sys
# pattern search for alt file: w (dN/dS) for branches: 0.01893 0.03429
# lnL(ntime: 24 np: 27): -20580.011581
# pattern search for null file: omega (dN/dS) = 0.01963
# lnL(ntime: 24 np: 26): -20583.373058
def read_null(filename):
infile = open(filename, 'r')
text = infile.read()
lnl = "NA"
dnds = "NA"
#Find lnL value
if re.search("lnL",text):
lnlpos = re.search("lnL",text).start()
line = text[lnlpos:lnlpos+55] #45 is an estimate for the line length. Can be changed.
if re.search("\):",line):
colonpos = re.search("\):",line).end()
pluspos = re.search("\+",line).end()
lnl = line[colonpos:pluspos-1]
else:
lnl = "NA"
#Find dN/dS values
if re.search("omega \(dN\/dS\) = ", text):
omega = re.search("omega \(dN\/dS\) = ", text).end()
dline = text[omega:omega+10]
dnds = dline.strip()
else:
dnds = "NA"
infile.close()
return lnl,dnds; #this is tuple and should be stored as such
def read_alt(filename):
infile = open(filename, 'r')
text = infile.read()
lnl = "NA"
#Find lnL value
if re.search("lnL",text):
lnlpos = re.search("lnL",text).start()
line = text[lnlpos:lnlpos+55]
if re.search("\):",line):
colonpos = re.search("\):",line).end()
pluspos = re.search("\+",line).end()
lnl = line[colonpos:pluspos-1]
lnl = lnl.strip()
else:
print("alt lnl not found")
lnl = "NA"
#Find background omegas
if re.search("branch type 0",text):
w = re.search("branch type 0", text).end()
dline = text[w+1:w+30]
dline = dline.strip()
dline = dline.split()
bg0 = dline[0]
bg1 = dline[1]
bg2 = dline[2]
else:
bg0 = 'NA'
bg1 = 'NA'
bg2 = 'NA'
#Find foreground omegas
if re.search("branch type 1",text):
fw = re.search("branch type 1",text).end()
dline = text[fw+1:fw+30]
dline = dline.strip()
dline = dline.split()
fg0 = dline[0]
fg1 = dline[1]
fg2 = dline[2]
else:
fg0 = 'NA'
fg1 = 'NA'
fg2 = 'NA'
#Find BEB Values
if re.search("Bayes Empirical Bayes",text):
ps = re.search("Bayes Empirical Bayes",text).end()
if re.search("The grid",text):
gr = re.search("The grid", text).start()
site = text[ps+124:gr-1]
site = site.strip()
site = site.split("\n")
else:
site = 'NA'
infile.close()
return lnl,bg0,bg1,bg2,fg0,fg1,fg2,site;
#initializes the tuples to store output from function
nullvalues = ()
altvalues = ()
sites = ""
#Value will be set for true if an outfile is found.
nullflag = False
altflag = False
groupname = ""
unfinishednull = []
unfinishedalt = []
filedir = sys.argv[1]
outputname = sys.argv[2]
outfile = open(outputname,'w+')
b_name = filedir+"BEB_output.txt"
out2 = open(b_name, 'w+')
#write the headers for outfile
outfile.write("Group\tNull lnL\t\tAlt lnL\tNull w0\tBackground w0\tBackground w1\tBackground w2\tAlt w0\tAlt w1\tAlt w2\n")
out2.write("Group\tBEB Values\n")
# Create a section to loop through a directory of group names that are subdirectories.
# Null and Alt out files will be within the subdirectories
#Loops through every files in the given directory
for file in os.listdir(filedir):
subdir = os.path.join(filedir,file)
#Check if the file is subdirectory
if os.path.isdir(subdir):
#subdir is the path to the subdirectory WITHOUT THE FILE SLASH
#Loops through the subdirectory to look for out files
for subfile in os.listdir(subdir+"/"):
fullpathsubfile = os.path.join(subdir+"/"+subfile)
if os.path.isfile(fullpathsubfile):
if re.search(".treefile",subfile):
groupname = subfile[4:-23]
if re.search("_null_clade.out",subfile):
nullflag = True
nullfile = fullpathsubfile
nullvalues = read_null(nullfile)
if re.search("_alt_clade.out",subfile):
altflag = True
altfile = fullpathsubfile
altvalues = read_alt(altfile)
sites = "\t".join(altvalues[7])
if nullflag == True and altflag == True:
outfile.write(groupname+"\t"+nullvalues[0]+"\t"+altvalues[0]+"\t"+nullvalues[1]+"\t"+altvalues[1]+"\t"+altvalues[2]+"\t"+altvalues[3]+"\t"+altvalues[4]+"\t"+altvalues[5])
outfile.write("\t"+altvalues[6]+"\t"+"\n")
out2.write(groupname+"\t"+sites+"\n")
elif nullflag == True and altflag == False:
outfile.write(groupname+"\t"+nullvalues[0]+"\t"+"NA\t"+nullvalues[1]+"\tNA\tNA\tNA\tNA\tNA\tNA\n")
elif nullflag == False and altflag == True:
outfile.write(groupname+"\tNA"+altvalues[0]+"\tNA\t"+altvalues[1]+"\t"+altvalues[2]+"\t"+altvalues[3]+"\t"+altvalues[4]+"\t"+altvalues[5])
outfile.write("\t"+altvalues[6]+"\t"+"\n")
out2.write(groupname+"\t"+sites+"\n")
#Done with subdirectory and flagging if there were not outfiles
if nullflag == False:
print("No null file for"+subdir)
unfinishednull.append(groupname)
if altflag == False:
print("No alt file for"+subdir)
unfinishedalt.append(groupname)
nullflag = False
altflag = False
groupname = ""
altvalues = ()
nullvalues = ()
name = "undonelist.txt"
todolist = open(name,"w+")
for items in unfinishednull:
todolist.write("qsub "+items+"_null_runcod.sh \n")
for items in unfinishedalt:
todolist.write("qsub "+items+"_alt_runcod.sh \n")
todolist.close()
outfile.close()
out2.close()