-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTestLoop.py
302 lines (256 loc) · 9.16 KB
/
TestLoop.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import ftplib
import sys
import json
import logging
import psycopg2
import os
import gc
import shutil
import time
from MzIdParser import MzIdParser
from NumpyEncoder import NumpyEncoder
import PostgreSQL as db
class TestLoop:
def __init__(self):
# count parsed id files
self.mzId_count = 0
# logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(message)s')
self.logger = logging.getLogger(__name__)
# config
self.ip = "193.62.192.9"
self.base = "pride/data/archive"
self.unimod_path = 'obo/unimod.obo'
self.temp_dir = os.path.expanduser('~') + "/parser_temp/"
# connect to DB
# try:
# con = db.connect('')
# except db.DBException as e:
# self.logger.error(e)
# print(e)
# sys.exit(1)
def all_years(self):
files = self.get_ftp_file_list(self.base)
for f in files:
self.year(f)
def year(self, year):
target_dir = self.base + '/' + year
files = self.get_ftp_file_list(target_dir)
for f in files:
self.month(year + '/' + f)
def month(self, year_month):
target_dir = self.base + '/' + year_month
files = self.get_ftp_file_list(target_dir)
for f in files:
ymp = year_month + '/' + f
self.project(ymp)
def project(self, year_month_project):
target_dir = self.base + '/' + year_month_project
files = self.get_ftp_file_list(target_dir)
print ('>> ' + year_month_project)
for f in files:
if f.lower().endswith('mzid') or f.lower().endswith('mzid.gz'):
print(f)
self.file(year_month_project, f)
break
def file(self, ymp, file_name):
# make temp dir, it is entirely removed again at end of this function
try:
os.mkdir(self.temp_dir)
except OSError:
pass
path = self.temp_dir + file_name
target_dir = '/' + self.base + '/' + ymp
ftp = self.get_ftp_login()
# fetch mzId file from pride
try:
ftp.cwd(target_dir)
ftp.retrbinary("RETR " + file_name, open(path, 'wb').write)
except ftplib.error_perm as e:
ftp.quit()
error_msg = "%s: %s" % (file_name, e.args[0])
self.logger.error(error_msg)
raise e
ftp.quit()
mzid_parser = MzIdParser(path, self.temp_dir, self.temp_dir, db, self.logger, 0, origin=ymp)
# init parser
try:
mzid_parser.initialise_mzid_reader()
mzid_parser.upload_info()
mzid_parser.check_all_spectra_data_validity()
peak_files = mzid_parser.get_supported_peak_list_file_names()
except Exception as mzId_error:
self.logger.exception(mzId_error)
error = json.dumps(mzId_error.args, cls=NumpyEncoder)
con = db.connect('')
cur = con.cursor()
db.write_error(mzid_parser.upload_id, type(mzId_error).__name__, error, cur, con)
con.close()
return
# fetch peak list files from pride
for peak_file in peak_files:
# don't download raw files, neater to download everything else even if not supported peak list format
# if not peak_file.endswith('raw'):
ftp = self.get_ftp_login()
try:
ftp.cwd(target_dir)
print('getting ' + peak_file)
ftp.retrbinary("RETR " + peak_file,
open(self.temp_dir + peak_file, 'wb').write)
except ftplib.error_perm as e:
print('missing file: ' + peak_file + " (checking for .gz)")
# check for gzipped
try:
os.remove(self.temp_dir + peak_file)
print('getting ' + peak_file + '.gz')
# ftp.cwd(target_dir + '/generated/')
ftp.retrbinary("RETR " + peak_file + '.gz',
open(self.temp_dir + '/' + peak_file + '.gz', 'wb').write)
except ftplib.error_perm as e:
print('missing file: ' + peak_file + '.gz')
warnings = json.dumps(mzid_parser.warnings, cls=NumpyEncoder)
con = db.connect('')
cur = con.cursor()
try:
cur.execute("""
UPDATE uploads SET
error_type=%s,
upload_error=%s,
upload_warnings=%s
WHERE id = %s""", ["Missing file?", peak_file, warnings, mzid_parser.upload_id])
con.commit()
except psycopg2.Error as e:
raise db.DBException(e.message)
con.close()
return
ftp.close()
# actually parse
try:
mzid_parser.parse()
except Exception as mzid_error:
self.logger.exception(mzid_error)
error = json.dumps(mzid_error.args, cls=NumpyEncoder)
con = db.connect('')
cur = con.cursor()
db.write_error(mzid_parser.upload_id, type(mzid_error).__name__, error, cur, con)
con.close()
try:
shutil.rmtree(self.temp_dir)
except OSError:
pass
self.mzId_count = self.mzId_count + 1
mzid_parser = None
gc.collect()
def get_ftp_login(self):
time.sleep(10)
while True:
try:
ftp = ftplib.FTP(self.ip)
ftp.login() # Uses password: anonymous@
return ftp
except:
print('FTP fail at '+time.strftime("%c")+'... waiting an hour')
time.sleep(60 * 60)
def get_ftp_file_list (self, dir):
ftp = self.get_ftp_login()
try:
ftp.cwd(dir)
except ftplib.error_perm as e:
error_msg = "%s: %s" % (dir, e.args[0])
print error_msg
ftp.quit()
return []
files = []
try:
files = ftp.nlst()
except ftplib.error_perm, resp:
if str(resp) == "550 No files found":
print "No files in this directory"
else:
error_msg = "%s: %s" % (dir, ftplib.error_perm.args[0])
print error_msg
ftp.close()
files.reverse()
return files
test_loop = TestLoop()
# test_loop.year('2018')
# test_loop.year('2017')
# test_loop.year('2016')
test_loop.month('2016/08')
test_loop.month('2016/07')
test_loop.month('2016/06')
test_loop.month('2016/05')
test_loop.month('2016/04')
test_loop.month('2016/03')
test_loop.month('2016/02')
test_loop.month('2016/01')
test_loop.year('2015')
test_loop.year('2014')
test_loop.year('2013')
test_loop.month('2012/12')
# test_loop.project("2018/05/PXD005015") # no attribute 'tag', problems is with attributes containing single quote mark
# test_loop.project("2018/07/PXD007714") # no attribute 'tag', also 2018/09/PXD009640
# test_loop.project("2018/06/PXD009747") # odd missing file # compare 2018/07/PXD009603
# test_loop.project("2016/08/PXD004741") # zip archive error
# test_loop.project("2018/04/PXD008493") # massive 2.9Gb mzML, very slow, takes days
# test_loop.project("2018/06/PXD010000")
# test_loop.project("2018/11/PXD009966")
# test_loop.project("2018/10/PXD010121") # good one, raw file with MGF accession number
# mzML
# test_loop.project("2017/11/PXD007748")
# test_loop.project("2016/11/PXD004785")
# test_loop.project("2016/05/PXD002967")
# test_loop.project("2016/09/PXD004499")
# test_loop.project("2015/06/PXD002045")
# test_loop.project("2017/08/PXD007149")
# test_loop.project("2015/06/PXD002048")
# test_loop.project("2015/06/PXD002047")
# test_loop.project("2014/11/PXD001267")
# 2015/06/PXD002046
# 2014/09/PXD001006
# 2014/09/PXD001000
# 2016/09/PXD002317
# 2014/09/PXD000966
# 2015/06/PXD002044
# 2015/06/PXD002043
# 2015/06/PXD002042
# 2015/06/PXD002041
# 2016/06/PXD004163
# 2015/05/PXD002161
# 2018/01/PXD007913
# 2017/11/PXD006204
# 2015/07/PXD002089
# 2015/07/PXD002088
# 2015/07/PXD002087
# 2015/07/PXD002086
# 2017/07/PXD002901
# 2015/07/PXD002085
# 2017/11/PXD007689
# 2015/07/PXD002084
# 2015/05/PXD002161
# 2015/05/PXD002161
# 2015/07/PXD002083
# 2015/07/PXD002082
# 2015/07/PXD002081
# 2015/07/PXD002080
# 2015/06/PXD002050
# 2015/06/PXD002049
#sim-xl
# test_loop.project("2017/05/PXD006574")
# test_loop.project("2015/02/PXD001677")
#missing file
# test_loop.project("2013/09/PXD000443")
#prob
#test_loop.project("2014/04/PXD000579") # missing file name
print("mzId count:" + str(test_loop.mzId_count))
# @staticmethod
# def get_pride_info (pxd):
# time.sleep(1)
# try:
# prideAPI = urllib.urlopen('https://www.ebi.ac.uk:443/pride/ws/archive/project/' + pxd).read()
# pride = json.loads(prideAPI)
# return pride
# except Exception:
# print ("failed to get " + pxd + "from pride api. Will try again in 5 secs.")
# time.sleep(5)
# return TestLoop.get_pride_info(pxd)