-
Notifications
You must be signed in to change notification settings - Fork 1
/
BB_GetBinFromGenome.py
executable file
·149 lines (127 loc) · 2.98 KB
/
BB_GetBinFromGenome.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#########readme##############
#########module##############
import os,sys,time,re
#########information#########
pro_name=sys.argv[0]
pro_author="Tianxiong Yu"
pro_date="Oct 22, 2015"
pro_purpose=""
pro_begin_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
begin_time=time.clock()
# print "\033[1;31;38m"
# print "*"*50
# print "ProgramName:\t"+pro_name
# print "Author:\t"+pro_author
# print "ProgramDate:\t"+pro_date
# print "Purpose:\t"+pro_purpose
# print "Begin time:\t"+pro_begin_time
# print "="*50
# print "\033[0m"
#########prepare#############
#os.chdir("/Users/duan/Desktop")
##set your work dir
#########function############
def input_file(input,n):
file_list=[]
if len(input)!=n+1:
print "The input file's number is wrong!!!"
return
else:
for i in range(1,len(input)):
file_list.append(open("%s"%input[i],"r"))
return file_list
def delete(li,index):
li=li[:index]+li[index+1:]
return li
def unique_list(li):
new_li=list(set(li))
new_li.sort(li.index)
return new_li
def read_file_into_list(file_in):
l=[]
while True:
line=file_in.readline().split()
if not line:
break
l.append(line)
return l
def read_file_into_list_split_tab(file_in):
l=[]
while True:
line=file_in.readline().split("\t")
if not line:
break
l.append(line)
return l
def read_file_into_dict(file_in,key_num):
###key_num is start with 0
d={}
while True:
line=file_in.readline().split()
if not line:
break
key=line[key_num]
value=delete(line,key_num)
d[key]=value
return d
def write_list_into_file(li,file_out):
###the value in li must be string not number
if len(li)==1:
content=li[0]
file_out.write(content)
if len(li)>1:
if isinstance(li[0],list):
for i in range(len(li)):
li[i]="\t".join(li[i])
content="\n".join(li)
file_out.write(content)
else:
content="\n".join(li)
file_out.write(content)
file_out.close()
#########code################
##help document##
if len(sys.argv)<2:
print "usage:"
print "BB_Get5kbbin.py in.chromsize out.bed window_size shifting_size"
print "\033[1;31;38m"
print "="*50
print "\033[0m"
os._exit(0)
##run##
f_genome=open(sys.argv[1],"r")
f_bin=open(sys.argv[2],"w")
window=int(sys.argv[3])
shift=int(sys.argv[4])
#read chr length
Chr_Len={}
while True:
l=f_genome.readline().split()
if not l:
break
Chr_Len[l[0]]=int(l[1])
#output bed file for each 1000bp bin
for i in Chr_Len.keys():
START=1
END=window
while START<Chr_Len[i]-window+shift:
if END>Chr_Len[i]:
f_bin.write("%s\t%s\t%s\t0\t0\t+\n"%(i,START,Chr_Len[i]))
else:
f_bin.write("%s\t%s\t%s\t0\t0\t+\n"%(i,START,END))
START+=shift
END+=shift
f_bin.close()
#########end#################
pro_end_time=time.strftime('%Y-%m-%d %H:%M:%S',\
time.localtime(time.time()))
end_time=time.clock()
pro_time=end_time-begin_time
# print "\033[1;31;38m"
# print "="*50
# print "End time:\t"+pro_end_time
# print "Program run time:\t%.03f seconds"%pro_time
# print "*"*50
# print "\033[0m"