-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_weighted_singletons_from_peaks.py
177 lines (144 loc) · 7.25 KB
/
create_weighted_singletons_from_peaks.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
import sys, os
from scipy.stats import stats as st
from optparse import OptionParser , IndentedHelpFormatter
import pybedtools
allInfo = {}
def process_file(filename,outfile,sg07,options):
input = open(filename,'rt')
fwtS = open(outfile,'wb')
region = options.ex/2
posS = ""
negS = ""
posnoS = ""
negnoS = ""
allS = ""
countS = 0
countnoS = 0
tagS = []
tagnoS = []
std = []
for line in input:
if line.startswith("#"):
fwtS.write(line)
continue
if(len(line.split("\t")) == 9):
chrom,junk,junk,start,end,tag,strand,junk,attr = line.split("\t")
if float(get_std(attr.rstrip())) == 0:
tagS.append(int(tag))
if strand == "+":
posS = posS+line
else:
negS = negS+line
else:
countnoS +=1
std.append(float(get_std(attr.rstrip())))
tagnoS.append(int(tag))
allS = allS+line
if strand == "+":
posnoS = posnoS+line
else:
negnoS = negnoS+line
#posS_int_negnoS = pybedtools.BedTool(posS,from_string=True).slop(g=sg07,l=10,r=10).intersect(pybedtools.BedTool(negnoS,from_string=True),u=True) # add half the exclusion zone to start and end coordinate
#negS_int_posnoS = pybedtools.BedTool(negS,from_string=True).slop(g=sg07,l=10,r=10).intersect(pybedtools.BedTool(posnoS,from_string=True),u=True)
try:
posS_int_negnoS = pybedtools.BedTool(posS,from_string=True).intersect(pybedtools.BedTool(negnoS,from_string=True).slop(g=sg07,l=region,r=region),u=True) # add half the exclusion zone to start and end coordinate
negS_int_posnoS = pybedtools.BedTool(negS,from_string=True).intersect(pybedtools.BedTool(posnoS,from_string=True).slop(g=sg07,l=region,r=region),u=True)
fullFile = allS+str(posS_int_negnoS)+str(negS_int_posnoS) # merging singletons and non singletons together
countS = posS_int_negnoS.count() + negS_int_posnoS.count() # Couting all the singletongs
fwtS.write(str(pybedtools.BedTool(fullFile,from_string=True).sort()))
except pybedtools.helpers.BEDToolsError:
# if the peak call file had no singletons in it.
print "The file had no singletons"
countS = 0
fullFile = allS
fwtS.write(str(pybedtools.BedTool(fullFile,from_string=True).sort()))
ratio = signal2noise(tagnoS,tagS) # calculating the signal to noise ratio.
#fullFile = allS+str(posS_int_negnoS)+str(negS_int_posnoS) # merging singletons and non singletons together
#countS = posS_int_negnoS.count() + negS_int_posnoS.count() # Couting all the singletongs
#fwtS.write(str(pybedtools.BedTool(fullFile,from_string=True).sort()))
#ratio = signal2noise(tagnoS,tagS) # calculating the signal to noise ratio.
allInfo[os.path.basename(filename)] = ".\t.\t"+str(countnoS)+"\t"+str(countS)+"\t"+str(st.nanmedian(tagnoS))+"\t"+str(st.nanmean(tagnoS))+"\t"+str(st.nanmedian(std))+"\t"+str(st.nanmean(std))+"\t"+str(ratio)
def get_std(attr):
if not ";" in attr:
return(attr.split("=")[1])
else:
list1 = attr.split(";")
for at in list1:
if at.split("=")[0] == "stddev":
return(at.split("=")[1])
def print_stats(stats):
fstats = open(stats,'wb')
header = "Filename\tMapped_reads\tUniquely_mapped_reads\tPeaks\tSingletons\tPeak_median_excluding_singletons\tPeak_mean_exclusing_singletons\tMedian_std_excluding_singeltons\tMean_std_excluding_singletons\tSignal2Nose"
fstats.write(header)
fstats.write("\n")
for k,v in allInfo.items():
fstats.write(k+"\t"+v)
fstats.write("\n")
def signal2noise(tags,Stags):
if sum(Stags) == 0:
return(1) # instead of returning Nan I want to see those values with no singletons, hence the value 1
else:
return(float(sum(tags)/sum(Stags)))
usage = '''
input_paths may be:
- a directory to run on all files in them
- a single file.
REQUIREMENT:
1) a file with chromosome lengths in it, in the format: chr1 1 1000000.
This file should be in the same folder as the script. Do not remove it. The
file name is sg07.txt. Change it for other organism.
example usages:
python create_weighted_singletons_from_peaks.py /usr/local/peaks/genetrack_peaks.gff
python create_weighted_singletons_from_peaks.py /usr/local/peaks/genetrack_s5e10/
'''.lstrip()
# We must override the help formatter to force it to obey our newlines in our custom description
class CustomHelpFormatter(IndentedHelpFormatter):
def format_description(self, description):
return description
def run():
parser = OptionParser(usage='%prog [options] input_paths', description=usage, formatter=CustomHelpFormatter())
parser.add_option('-e', action='store', type='int', dest='ex',default=20,
help='Exclusion zone used during peak calling.Default 20')
#parser.add_option('-d', action='store', type='int', dest='down_distance',default=50,
# help='Downstream distance to go from the peak-pair mid_point, Default=50')
#parser.add_option('-i', action='store', type='int', dest='iter',default=100,
# help='Number of iterations to do for shuffling')
(options, args) = parser.parse_args()
# Check if all the required arguments are provided, else exit
if not args:
parser.print_help()
sys.exit(1)
try:
from scipy.stats import stats
import pybedtools
except ImportError:
print "You need to install Scipy(http://www.scipy.org/Download) before you can run this script."
print "If you have Scipy installed, then you should check if you have pybedtools installed"
sys.exit(1)
sg07 = os.path.join(os.path.dirname(os.path.abspath(__file__)),"sg07.txt") # location of the chromosome length file. Located with the scripts. DO NOT change
if not os.path.isdir(args[0]):
# Create dir names for wtS folders
outdir_wtS = os.path.join(os.path.dirname(args[0]),"_wtS")
# Check if wtS dir exists else create them
if not os.path.exists(outdir_wtS) :
os.mkdir(outdir_wtS)
outfile_wtS = os.path.join(outdir_wtS,os.path.splitext(os.path.basename(args[0]))[0]+"wtS.gff")
stats = os.path.join(outdir_wtS,"peak_stats.txt")
process_file(args[0],outfile_wtS,sg07,options)
print_stats(stats)
else:
if not os.path.exists(args[0]):
parser.error('Path %s does not exist.' % args[0])
# Create dir names for noS and onlyS folders
outdir_wtS = os.path.join(args[0],"_wtS")
if not os.path.exists(outdir_wtS):
os.mkdir(outdir_wtS)
stats = os.path.join(outdir_wtS,"peak_stats.txt")
for name in os.listdir(args[0]):
if name.endswith('.gff'):
fname = os.path.join(args[0], name)
outfile_wtS = os.path.join(outdir_wtS,os.path.splitext(name)[0]+"wtS.gff")
process_file(fname,outfile_wtS,sg07,options)
print_stats(stats)
if __name__ == "__main__":
run()