-
Notifications
You must be signed in to change notification settings - Fork 1
/
BB_GtfToBed6.py
executable file
·151 lines (127 loc) · 3.02 KB
/
BB_GtfToBed6.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
#! /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 "python BB_GtfToBed6.py in.gtf out.bed id/name"
print "\033[1;31;38m"
print "="*50
print "\033[0m"
os._exit(0)
##run##
f_gtf=open(sys.argv[1],"r")
f_bed=open(sys.argv[2],"w")
while True:
l=f_gtf.readline().split("\t")
if not l[0]:
break
if l[0][0]=="#":
continue
gene_Id=re.findall(r'gene_id \"([\w\.\-\_]+)\"',l[8])
gene_name=re.findall(r'gene_name \"([\w\.\-\_]+)\"',l[8])
trans_id=re.findall(r'transcript_id \"([\w\.\-\_]+)\"',l[8])
if len(trans_id)==0:
trans_id=["NONE"]
if len(gene_name)==0:
gene_name=gene_Id
if sys.argv[3]=="id":
gn=gene_id[0]
elif sys.argv[3]=="name":
gn=gene_name[0]
tn=trans_id[0]
if l[2]=="exon":
f_bed.write("%s\t%s\t%s\t%s\t%s\t%s\n"%(l[0],l[3],l[4],gn,tn,l[6]))
f_bed.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"