-
Notifications
You must be signed in to change notification settings - Fork 1
/
baseline.py
executable file
·253 lines (198 loc) · 6.67 KB
/
baseline.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
import os
import sys
import fnmatch
import shlex
import subprocess
import shutil
import math
from optparse import OptionParser
from scripts.split_chromosome import splitByChromosome3 as splitByChromosome
from scripts.get_chr_length import *
from scripts.convert_targeted_regions import *
from scripts.convert_gene_coordinate import convertGeneCoordinate2 as convertGeneCoordinate
from multiprocessing import Pool
class Params:
"""
Class for top-level system parameters:
"""
def __init__(self):
def mult_files(option, opt_str, value, parser):
args=[]
for arg in parser.rargs:
if arg[0] != "-":
args.append(arg)
else:
del parser.rargs[:len(args)]
break
if getattr(parser.values, option.dest):
args.extend(getattr(parser.values, option.dest))
setattr(parser.values, option.dest, args)
#command-line option definition
self.parser = OptionParser()
self.parser.add_option("-t", "--target",
help="Target region definition file [REQUIRED] [BED Format]",
action="store", type="string", dest="target")
self.parser.add_option("-f", "--files",
help="Files to be converted to baselines [REQUIRED] [BAM]",
action="callback", callback=mult_files, dest="files")
self.parser.add_option("-o", "--output",
help="Output folder [REQUIRED]",
action="store", type="string", dest="output")
self.parser.add_option("-c","--trim",
help="Portion of outliers to be removed before calculating average [Default: 0.2]",
action="store", dest="trim", default="0.2")
self.parser.add_option("-n","--name",
help="Output baseline name [Default: baseline]",
action="store", dest="name", default="baseline")
# required parameters list:
self.ERRORLIST = []
#change system parameters based on any command line arguments
(options, args) = self.parser.parse_args()
if options.target:
self.TARGET=options.target
else:
self.ERRORLIST.append("target")
if options.files:
self.FILES=options.files
else:
self.ERRORLIST.append("files")
if options.output:
self.OUTPUT=options.output
else:
self.ERRORLIST.append("output")
if options.trim:
self.TRIM=float(options.trim)
if options.name:
self.NAME=options.name
if len(self.ERRORLIST) != 0:
self.parser.print_help()
self.parser.error("Missing required parameters")
def repeat(self):
#params test
print "target :", self.TARGET
print "files :", self.FILES
print "output :", self.OUTPUT
print "trim :", self.TRIM
print "name :", self.NAME
# option handling
params = Params()
#params.repeat()
targetFile = params.TARGET
infiles = params.FILES
output_dir = params.OUTPUT
#Debug
print " ------ baseline.py ------- "
print "Target:", targetFile
for files in infiles:
print "File:", files
print "Output Directory: ", output_dir
def make_new_directory(outdir):
print outdir
if not os.path.exists(outdir):
os.mkdir(outdir)
print " ----- creating output directory -----"
make_new_directory(output_dir)
outdir = os.path.join(output_dir, "buf")
make_new_directory(outdir)
targetFile2 = os.path.join(outdir, os.path.basename(targetFile)+".sorted")
os.system("sort -k1,1 -k2n %s > %s" %(targetFile,targetFile2))
def processInFile(infile):
infilename = os.path.basename(infile)
s_outdir = os.path.join(outdir, infilename)
make_new_directory(s_outdir)
genomeFile = os.path.join(s_outdir,infilename +'.chrsummary')
get_genome(infile, genomeFile)
bedgraph = os.path.join(s_outdir, infilename + ".BEDGRAPH")
args = shlex.split("genomeCoverageBed -ibam %s -bga -g %s" %(infile, genomeFile))
iOutFile = open(bedgraph, "w")
output = subprocess.Popen(args, stdout = iOutFile).wait()
iOutFile.close()
targetList = convertTarget(targetFile2)
splitByChromosome(bedgraph)
convertGeneCoordinate(targetList, os.path.dirname(bedgraph)+"/")
bedgraph_tgtonly = bedgraph+".TARGETONLY"
bedgraph_tgtonly_avg = bedgraph+".TARGETONLY.AVERAGE"
os.rename(os.path.join(os.path.dirname(bedgraph),"geneRefCoverage.txt"),bedgraph_tgtonly)
os.rename(os.path.join(os.path.dirname(bedgraph),"geneRefCoverage_targetAverage.txt"),bedgraph_tgtonly_avg)
shutil.copy(bedgraph_tgtonly,outdir)
shutil.copy(bedgraph_tgtonly_avg,outdir)
print "----- Processing Files -----"
pool = Pool(5)
pool.map(processInFile,infiles)
# STEP 2 -> Create Union BedGraph
allfiles_names = [x for x in os.listdir(outdir) if x.endswith("TARGETONLY")]
allfiles_path = [os.path.join(outdir,x) for x in allfiles_names]
args=["unionBedGraphs","-header","-i"]+allfiles_path+["-names"]+allfiles_names
print (str(args))
fo = os.path.join(outdir,"TARGETONLY.union.txt")
foh = open(fo,"w")
subprocess.Popen(args,stdout=foh).wait()
foh.close()
# STEP 3 -> POOL METHOD
TRIM = params.TRIM #TRIM = 0.2
f = fo
fh = open(f)
fo=os.path.join(output_dir, params.NAME+".pooled2_TRIM"+str(TRIM)+".txt")
fo_libsize=fo+".LIBSIZE"
foh=open(fo,'w')
fo_libsize_h=open(fo_libsize,'w')
fh.readline()
Ns = None
nSamples = None
lineCount=0
for line in fh:
lineCount+=1
line_elems=line.rstrip().split("\t")
rangeLen=int(line_elems[2])-int(line_elems[1])
if not Ns:
nSamples=len(line_elems)-3
Ns = [0 for x in range(nSamples)]
for i in range(nSamples):
ind=i+3
Ns[i] += int(line_elems[ind])*rangeLen
fh.close()
fh=open(f)
fh.readline()
lineCount=0
nSamples_exclude = int(math.floor(TRIM*nSamples))
def gm_mean(xs):
tmpprod = 1
p = 1.0/len(xs)
for x in xs:
tmpprod = tmpprod * math.pow(x,p)
return tmpprod
Ns_gmean = gm_mean(Ns)
def meanstdv(x):
from math import sqrt
n, mean, std = len(x), 0, 0
for a in x:
mean = mean+a
mean = mean / float(n)
for a in x:
std = std+(a-mean)**2
std=sqrt(std/float(n-1))
return mean, std
libsize = 0
for line in fh:
lineCount+=1
line_elems=line.rstrip().split("\t")
rangeLen=int(line_elems[2])-int(line_elems[1])
xs_tmp=[int(x) for x in line_elems[3:]]
xs = [float(xs_tmp[i])*Ns_gmean/Ns[i] for i in range(len(xs_tmp))]
xs.sort()
xs_trimmed=xs[nSamples_exclude:(nSamples-nSamples_exclude)]
#trimmed_mean=sum(xs_trimmed)/float(len(xs_trimmed))
trimmed_mean,std=meanstdv(xs_trimmed)
libsize+=rangeLen*trimmed_mean
foh.write("\t".join(line_elems[0:3]+[str(trimmed_mean),str(std)])+"\n")
fo_libsize_h.write(str(int(round(libsize))))
fh.close()
foh.close()
fo_libsize_h.close()
def removeTempFolder(tempFolderPath):
import shutil
shutil.rmtree(tempFolderPath)
print "Temp Folder Removed"
# Removed Temp Folder
#removeTempFolder(outdir)