-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmirror.py
executable file
·286 lines (249 loc) · 10.4 KB
/
mirror.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
import os
import shutil
import urllib
import pathlib
import requests
import pandas as pd
from jug import bvalue
from math import isnan
from contextlib import closing
from os import path, makedirs
from config import ASPERA_BINARY, ASPERA_KEY
def mirror_path(mirror_basedir, ftp):
import urllib
import pathlib
url = urllib.parse.urlparse('http://' + ftp)
p = pathlib.PurePath(url.path)
target_dir = mirror_basedir / p.parent.relative_to('/')
return target_dir / p.name
def md5sum_file(ifile):
'''Computes MD5 sum of ifile'''
import hashlib
m = hashlib.md5()
BLOCK_SIZE = 8192
with open(ifile, 'rb') as ifile:
while True:
data = ifile.read(BLOCK_SIZE)
if not data:
return m.hexdigest()
m.update(data)
def http_download_file(url, ofile):
with closing(requests.get(url, stream=True)) as ifile, \
open(ofile, 'wb') as ofile:
for chunk in ifile.iter_content(8192):
ofile.write(chunk)
def wget_download_file(url, ofile):
'''Call wget on the command line to download `url` to `ofile`'''
import subprocess
cmdline = ['wget',
url,
'-O',
str(ofile)]
print('WGET_CMD', cmdline)
subprocess.run(cmdline, check=True)
def aspera_download_file_temp_dir(aspera_url, ofile):
'''Call ascp on the command line to download `aspera_url` to `ofile`
- Temporaily downloads to bork9 first and then copies to ofile due to
data fragmentation issies encountered on scb2.'''
import subprocess
temp_download_area = '/g/bork9/fullam/temp_downloads/'
temp_download_name = os.path.join(temp_download_area,
os.path.basename(ofile))
cmdline = [ASPERA_BINARY,
'-P33001', # Use special port
'-T', # No encryption
'-l', '300m',
'-i', ASPERA_KEY,
aspera_url,
str(temp_download_name)]
print('ASPERA_CMD', cmdline)
print('DESTINATION: ', ofile)
subprocess.run(cmdline, check=True)
if os.path.isfile(temp_download_name):
shutil.move(temp_download_name, ofile)
def aspera_download_file(aspera_url, ofile):
'''Call ascp on the command line to download `aspera_url` to `ofile`
- Temporaily downloads to bork9 first and then copies to ofile due to
data fragmentation issies encountered on scb2.'''
import subprocess
temp_download_area = '/g/bork9/fullam/temp_downloads/'
temp_download_name = os.path.join(temp_download_area,
os.path.basename(ofile))
cmdline = [ASPERA_BINARY,
'-P33001', # Use special port
'-T', # No encryption
'-l', '300m',
'-i', ASPERA_KEY,
aspera_url,
str(temp_download_name)]
print('ASPERA_CMD', cmdline)
subprocess.run(cmdline, check=True)
if os.path.isfile(temp_download_name):
shutil.move(temp_download_name, ofile)
def mirror_all_files(study_accession, filetable, mirror_basedir, *, progress=True, use='HTTP'):
n = len(filetable)
for i in range(n):
if progress:
print("Processing file {} of {}. Study: {}".format(i + 1, n, study_accession))
source = filetable.iloc[i]
if type(source.ftp) == float and isnan(source.ftp):
continue
urlraw = 'http://' + source.ftp
url = urllib.parse.urlparse(urlraw, allow_fragments=False)
p = pathlib.PurePath(url.path)
target_dir = mirror_basedir / p.parent.relative_to('/')
makedirs(target_dir, exist_ok=True)
ofile = target_dir / p.name
if path.exists(ofile):
if os.stat(ofile).st_size != int(source.bytes):
print("Existing output file has wrong size. Removing...")
os.unlink(ofile)
# elif md5sum_file(ofile) != source.md5:
# print("Existing output file has wrong hash. Removing...")
# os.unlink(ofile)
else:
print("Correct output file exists. Skipping...")
continue
if use == 'ASPERA':
aspera_url = '[email protected]:' + url.path
for attempt_number in range(3):
aspera_download_file(aspera_url, ofile)
if check_file(ofile, source):
break
elif use == 'WGET':
for attempt_number in range(3):
wget_download_file(source.ftp, ofile)
if check_file(ofile, source):
break
else:
for attempt_number in range(3):
http_download_file(urlraw, ofile)
if check_file(ofile, source):
break
def check_file(ofile, source):
print('Checking file...')
file_size = os.stat(ofile).st_size
print('File size: {0}'.format(file_size))
if file_size != int(source.bytes):
print("File has wrong size. {0} vs.{1}".format(file_size,
source.bytes))
print('Removing..')
os.unlink(ofile)
return False
file_md5sum = md5sum_file(ofile)
print('File md5: {0}'.format(file_md5sum))
if file_md5sum != source.md5:
print("File has wrong md5. {0} vs.{1}".format(file_md5sum,
source.md5))
print('Removing..')
os.unlink(ofile)
return False
print("Downloaded file OK...")
return True
def norm_path(p):
p = str(p)
if p.endswith('_1.fastq.gz'):
return pathlib.PurePath(p[:-len('_1.fastq.gz')] + '.pair.1.fq.gz')
if p.endswith('_2.fastq.gz'):
return pathlib.PurePath(p[:-len('_2.fastq.gz')] + '.pair.2.fq.gz')
if p.endswith('.fastq.gz'):
return pathlib.PurePath(p[:-len('.fastq.gz')] + '.single.fq.gz')
# if p.endswith('.fq1.gz'):
# return pathlib.PurePath(p[:-len('.fq1.gz')] + '.single.fq.gz')
raise ValueError("Cannot normalize {}".format(p))
def build_link_structure(filetable, mirror_basedir, data_basedir, sample_fname):
data_basedir = pathlib.PurePath(data_basedir)
makedirs(data_basedir, exist_ok=True)
with open(data_basedir / sample_fname, 'w') as samplefile:
for s in set(filetable.sample_accession):
samplefile.write("{}\n".format(s))
prefix_fields = [col for col in
('library_layout',
'library_strategy',
'library_source',
'library_selection')
if filetable[col].nunique() > 1]
n = len(filetable)
for i in range(n):
source = filetable.iloc[i]
if type(source.ftp) == float and isnan(source.ftp):
continue
target = data_basedir
if prefix_fields:
target = target / "_".join(source[s] for s in prefix_fields)
target = target / source.sample_accession
makedirs(target, exist_ok=True)
target = target / norm_path(source.ftp).name
try:
os.symlink(mirror_path(mirror_basedir, source.ftp), target)
except FileExistsError as e:
os.unlink(target)
os.symlink(mirror_path(mirror_basedir, source.ftp), target)
def create_ena_file_map(studies_tables, vol_map, MIRROR_BASEDIR):
def annotate_link(p):
def drop_hostname(addr):
while not addr.startswith("vol1"):
newaddr = addr.split('/', 1)
if len(newaddr) == 1:
raise ValueError("Couldn't find mirror root")
addr = newaddr[-1]
return addr
if pd.isnull(p):
return (p, p)
p = drop_hostname(p)
if p.endswith('_1.fastq.gz'):
return ("fastq_1", p)
if p.endswith('R1.fastq.gz'):
return ("fastq_1", p)
if p.endswith('_2.fastq.gz'):
return ("fastq_2", p)
if p.endswith('R2.fastq.gz'):
return ("fastq_2", p)
if p.endswith('.fastq.gz'):
return ("fastq_single", p)
# else:
# return ("fastq_single", p)
raise ValueError("Cannot annotate {}".format(p))
with open(path.join(MIRROR_BASEDIR, vol_map), 'w') as out:
out.write("#study_accession\trun_accession\tsample_accession\texperiment_accession\tfastq_1\tfastq_2\tfastq_single\n")
for study in studies_tables:
data = bvalue(studies_tables[study]) ## bvalue because this is a Tasklet
if data is None or len(data) == 0:
print("We got no file information for", study, ". Incomplete/older jug internal state? Skipping", study)
continue
annotations = data["ftp"].map(annotate_link)
data["filetype"], data["filepath"] = zip(*annotations)
subdata = data[["study_accession", "run_accession", "sample_accession",
"experiment_accession"]].drop_duplicates()
files = data.pivot(values="filepath", columns="filetype")
grouped = pd.merge(subdata, files, left_index=True, right_index=True)
for _, record in grouped.iterrows():
# 6 columns: project_accession, sample_accession, experiment_accession, fastq_1, fastq_2, fastq_single
try:
fastq_1 = record.fastq_1
except:
fastq_1 = ''
else:
if fastq_1 is None:
fastq_1 = ''
else:
fastq_1 = path.join(MIRROR_BASEDIR, fastq_1)
try:
fastq_2 = record.fastq_2
except:
fastq_2 = ''
else:
if fastq_2 is None:
fastq_2 = ''
else:
fastq_2 = path.join(MIRROR_BASEDIR, fastq_2)
try:
fastq_single = record.fastq_single
except:
fastq_single = ''
else:
if fastq_single is None:
fastq_single = ''
else:
fastq_single = path.join(MIRROR_BASEDIR, fastq_single)
out.write(f"{record.study_accession}\t{record.run_accession}\t{record.sample_accession}\t{record.experiment_accession}\t{fastq_1}\t{fastq_2}\t{fastq_single}\n")