[go: up one dir, main page]

Menu

[r3]: / reditools / TableToGFF.py  Maximize  Restore  History

Download this file

180 lines (165 with data), 5.8 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/home/epicardi/bin/python27/bin/python
# Copyright (c) 2013-2014 Ernesto Picardi <ernesto.picardi@uniba.it>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sys, os, getopt, time, random, heapq, gzip
from tempfile import gettempdir
from itertools import islice, cycle
from collections import namedtuple
from operator import itemgetter
version='1.0'
pid=str(os.getpid()+random.randint(0,999999999))
def usage():
print """
USAGE: python TableToGFF.py [options]
Options:
-i Table file from REDItools
-s Sort output GFF
-t Tabix output GFF (requires Pysam module)
-b Buffer size (as number of lines) [32000] (requires -s)
-T Temporary directory (requires -s)
-o Outfile [outTable_%s.gff]
-h Print this help
"""%(pid)
try:
opts, args = getopt.getopt(sys.argv[1:], "i:o:sthT:b:",["help"])
if len(opts)==0:
usage()
sys.exit(2)
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
tablefile=''
outfile='outTable_%s.gff' %(pid)
sort=0
tabix=0
buffer_size=32000
tempdirs=[]
for o, a in opts:
if o in ("-h","--help"):
usage()
sys.exit()
elif o == "-i":
tablefile=a
if not os.path.exists(tablefile):
usage()
sys.exit('Table file not found')
elif o == "-o": outfile=a
elif o == "-s": sort=1
elif o == "-t": tabix=1
elif o == "-b": buffer_size=int(a)
elif o == "-T": tempdirs.append(a)
else:
assert False, "Unhandled Option"
#Sorting code from SortGFF.py
Keyed = namedtuple("Keyed", ["key", "obj"])
key_=eval('lambda line : (%s)' %('line[:]'))
def gk(key,obj):
ik=itemgetter(0,3,4)(obj.split('\t'))
return key((ik[0],int(ik[1]),int(ik[2])))
def merge(key=None, *iterables):
# based on code posted by Scott David Daniels in c.l.p.
# http://groups.google.com/group/comp.lang.python/msg/484f01f1ea3c832d
#print iterables
if key is None:
keyed_iterables = iterables
else:
keyed_iterables = [(Keyed(gk(key,obj), obj) for obj in iterable) for iterable in iterables]
#print keyed_iterables
for element in heapq.merge(*keyed_iterables):
yield element.obj
def batch_sort(input, output, key=None, buffer_size=32000, tempdirs=None):
if tempdirs is None:
tempdirs = []
if not tempdirs:
tempdirs.append(gettempdir())
chunks = []
xx=0
try:
with open(input,'rb',64*1024) as input_file:
input_iterator = iter(input_file)
for tempdir in cycle(tempdirs):
current_chunk2=[]
for j in islice(input_iterator,buffer_size):
l=(j.strip()).split('\t')
l[3]=int(l[3])
l[4]=int(l[4])
current_chunk2.append(l)
current_chunk3=[]
for j in sorted(current_chunk2, key=itemgetter(0,3,4)):
j[3]=str(j[3])
j[4]=str(j[4])
current_chunk3.append('\t'.join(j)+'\n')
xx+=len(current_chunk3)
if not current_chunk3: break
sys.stdout.write("Loaded and sorted %i lines.\n"%(xx))
output_chunk = open(os.path.join(tempdir,'%06i_%s'%(len(chunks),pid)),'w+b',64*1024)
chunks.append(output_chunk)
output_chunk.writelines(current_chunk3)
output_chunk.flush()
output_chunk.seek(0)
sys.stdout.write("Merging from %i files.\n"%(len(chunks)))
with open(output,'wb',64*1024) as output_file:
output_file.writelines(merge(key, *chunks))
finally:
for chunk in chunks:
try:
chunk.close()
os.remove(chunk.name)
except Exception:
pass
#END sorting code
script_time=time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
sys.stdout.write("Script time --> START: %s\n"%(script_time))
sys.stdout.write("Reading table...\n")
if tablefile.endswith('.gz'): f=gzip.open(tablefile,'rb')
else: f=open(tablefile)
o=open(outfile,'w')
xx=0
#chr21 10205589 C 0 12 34.75 [0, 3, 0, 9] CT 0.75 16 28.56 [0, 16, 0, 0] - 0.00 -
for i in f:
if i.startswith('Region'): continue
if i.strip()=='': continue
l=(i.strip()).split('\t')
strand='+'
if l[3]=='0': strand='-'
gffLine=[l[0],'reditoolTable','pos',l[1],l[1],'.',strand,'.',l[0]+'-'+l[1]]
o.write('\t'.join(gffLine)+'\n')
xx+=1
f.close()
o.close()
sys.stdout.write("Converted %i lines.\n"%(xx))
sys.stdout.write("GFF saved on %s\n"%(outfile))
if sort:
sys.stdout.write("Sorting GFF file...\n")
outfileS='.'.join(outfile.split('.')[:-1])+'.sorted.gff'
batch_sort(outfile,outfileS,key_,buffer_size,tempdirs)
outfile=outfileS
sys.stdout.write("Sorted GFF saved on %s\n"%(outfileS))
if tabix:
try:
import pysam
sys.stdout.write("Indexing GFF file...\n")
outfileS=pysam.tabix_index(outfile, preset='gff')
sys.stdout.write("Tabix file saved on %s.\n" %(outfileS))
sys.stdout.write("Indices saved on %s.tbi.\n" %(outfileS))
except: sys.exit('Pysam module not found.\nTabix indexing not available.')
script_time=time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
sys.stdout.write("Script time --> END: %s\n"%(script_time))