-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLAT_gros.py
executable file
·160 lines (141 loc) · 5.13 KB
/
BLAT_gros.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
#!/usr/bin/env python
# -*- coding:Utf-8 -*-
#
# Copyright 2014 CIRAD
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/> or
# write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
#!usr/bin/python
import optparse, os, shutil, subprocess, sys, tempfile, fileinput, ConfigParser, operator, time, multiprocessing
def stop_err( msg ):
sys.stderr.write( "%s\n" % msg )
sys.exit()
def run_job (cmd_line, ERROR):
# print cmd_line
try:
tmp = tempfile.NamedTemporaryFile().name
# print tmp
error = open(tmp, 'w')
proc = subprocess.Popen( args=cmd_line, shell=True, stderr=error)
returncode = proc.wait()
error.close()
error = open( tmp, 'rb' )
stderr = ''
buffsize = 1048576
try:
while True:
stderr += error.read( buffsize )
if not stderr or len( stderr ) % buffsize != 0:
break
except OverflowError:
pass
error.close()
os.remove(tmp)
if returncode != 0:
raise Exception, stderr
except Exception, e:
stop_err( ERROR + str( e ) )
def __main__():
#Parse Command Line
parser = optparse.OptionParser()
# Wrapper options.
parser.add_option( '', '--blast', dest='blast', default='not_filled', help='The blast file default output format')
parser.add_option( '', '--ident', dest='ident', default='90', help='The minimal identity percentage, [default: %default]')
parser.add_option( '', '--max_hit', dest='max_hit', default='1', help='The maximal hit number, [default: %default]')
parser.add_option( '', '--seq', dest='seq', default='100', help='The number of sequence to search simultaneously, [default: %default]')
parser.add_option( '', '--thread', dest='thread', default='1', help='The thread number used for mapping (integer), [default: %default]')
parser.add_option( '', '--out', dest='out', default='not_filled', help='An id for intermediate output')
(options, args) = parser.parse_args()
ScriptPath = os.path.dirname(sys.argv[0])
loca_programs = ConfigParser.RawConfigParser()
loca_programs.read(ScriptPath+'/loca_programs.conf')
proc = int(options.thread)
if options.blast == 'not_filled':
mot = 'Please provide an argument for --blast'
sys.exit(mot)
if options.out == 'not_filled':
mot = 'Please provide an argument for --out'
sys.exit(mot)
file = open (options.blast)
j = 0
liste_job = []
liste_temp = []
for line in file:
if line.split() != []:
if line.split()[0] == 'BLASTN':
if j == 0:
outfile = open(options.out+'_File'+str(j)+'.temp','w')
outfile.write(line)
liste_job.append('%s %s/blat_results_analyzer_v3.pl --blat %s --identity %s --nombre %s > %s' % (loca_programs.get('Programs','perl'), ScriptPath, options.out+'_File'+str(j)+'.temp', options.ident, options.max_hit, options.out+'.bra'+str(j)))
liste_temp.append(str(j))
elif j%int(options.seq) == 0:
outfile.close()
liste_job.append('%s %s/blat_results_analyzer_v3.pl --blat %s --identity %s --nombre %s > %s' % (loca_programs.get('Programs','perl'), ScriptPath, options.out+'_File'+str(j)+'.temp', options.ident, options.max_hit, options.out+'.bra'+str(j)))
liste_temp.append(str(j))
outfile = open(options.out+'_File'+str(j)+'.temp','w')
outfile.write(line)
else:
outfile.write(line)
j += 1
else:
outfile.write(line)
else:
outfile.write(line)
outfile.close()
liste_process = []
for n in liste_job:
t = multiprocessing.Process(target=run_job, args=(n, 'Bug lauching blat_results_analyzer_v3.pl',))
liste_process.append(t)
if len(liste_process) == proc:
# Starts threads
for process in liste_process:
process.start()
# In this blocks the calling thread until the thread whose join() method is called is terminated.
for process in liste_process:
process.join()
#the processes are done
liste_process = []
if liste_process:
# Starts threads
for process in liste_process:
process.start()
# This blocks the calling thread until the thread whose join() method is called is terminated.
for process in liste_process:
process.join()
#the processes are done
liste_process = []
os.system('cat '+options.out+'.bra* > '+options.out+'_final')
for n in liste_temp:
os.remove(options.out+'_File'+n+'.temp')
os.remove(options.out+'.bra'+n)
doublon = set()
dico = {}
file = open(options.out+'_final')
for line in file:
data = line.split()
if data:
if data[0] in dico:
doublon.add(data[0])
else:
dico[data[0]] = line
file.close()
for n in dico:
if not(n in doublon):
print('\t'.join(dico[n].split()))
os.remove(options.out+'_final')
if __name__ == "__main__": __main__()