-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBB_TranscriptToGene.py
More file actions
executable file
·161 lines (135 loc) · 3.39 KB
/
Copy pathBB_TranscriptToGene.py
File metadata and controls
executable file
·161 lines (135 loc) · 3.39 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
#! /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_TranscriptToGene.py in.bed out.bed"
print ""
print "\033[1;31;38m"
print "="*50
print "\033[0m"
os._exit(0)
##run##
print "The program is running!!!"
file_pirn_tran=open(sys.argv[1],"r")
file_pirn_gene=open(sys.argv[2],"w")
list_pirn_tran=read_file_into_list(file_pirn_tran)
dict_pirn_gene={}
for i in list_pirn_tran:
if not dict_pirn_gene.has_key(i[7]):
dict_pirn_gene[i[7]]=[i[0],i[1],i[2],i[11],i[5]]
else:
if int(i[1])<int(dict_pirn_gene[i[7]][1]):
dict_pirn_gene[i[7]][1]=i[1]
if int(i[2])>int(dict_pirn_gene[i[7]][2]):
dict_pirn_gene[i[7]][2]=i[2]
for key in dict_pirn_gene.keys():
if re.search("^Mir",key):
continue
if re.search("rRNA$",key):
continue
if re.search("^U\d+",key):
continue
if re.search("^SNO",key):
continue
if re.search("Y_RNA$",key):
continue
if re.search("^sno",key):
continue
if int(dict_pirn_gene[key][2])-int(dict_pirn_gene[key][1])<1000:
continue
file_pirn_gene.write("%s\t%s\t%s\t%s\t%s\t%s\n"%(dict_pirn_gene[key][0],dict_pirn_gene[key][1],dict_pirn_gene[key][2],key,dict_pirn_gene[key][3],dict_pirn_gene[key][4]))
file_pirn_gene.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"