-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensembl_databases.py
executable file
·197 lines (169 loc) · 6.2 KB
/
ensembl_databases.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
#!/usr/bin/env python3
"""
.. See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
from os import path
import gzip
import ftplib
import json
import argparse
import fnmatch
import re
import math
# CHANGE THE LOOKUP FILE BY SETTING ENSEMBL_DBLOOKUP
# LOOKUP REQUIRES THE FORMAT
# { dbname : { database : "dbname", path: "/pub/path", server : "ftp.ensembl.org" } }
def dblookup():
default_lookup = path.join(path.split(path.abspath(__file__))[0], "dblookup.json")
dblookup = os.environ.get("ENSEMBL_DBLOOKUP", default_lookup)
with open(dblookup, "r") as file:
return json.load(file)
def main():
parser = argparse.ArgumentParser(description="Download Ensembl databases")
parser.add_argument(
"databases",
metavar="DB",
type=str,
nargs="*",
help="Specify the database name to download. Can use UNIX style glob matching to find multiple databases but quote to avoid command line globbing ",
)
parser.add_argument(
"-l",
"--list",
action="store_true",
help="List available databases. Can filter using DB",
)
parser.add_argument(
"-i",
"--info",
action="store_true",
help="Return a JSON blob of database information. Can filter using DB",
)
parser.add_argument(
"-d",
"--download",
action="store_true",
help="Download files as specified by DB",
)
parser.add_argument(
"-v",
"--validate",
action="store_true",
help="Validate files. Can filter using DB. Assumes files have been already downloaded",
)
parser.add_argument(
"-b",
"--basedir",
type=str,
default=os.path.curdir,
help="Give an optional path to where database dumps should be located/worked with",
)
args = parser.parse_args()
lookup = dblookup()
available_dbs = []
for db in args.databases:
matching = fnmatch.filter(lookup.keys(), db)
available_dbs.extend(matching)
# just list the keys of the lookup
if args.list:
if not args.databases:
available_dbs = lookup.keys()
for db in available_dbs:
print(db)
return
# add all info into an array & print (there's a better way to do this for sure)
if args.info:
dbs = [lookup.get(x) for x in available_dbs]
print(json.dumps(dbs, indent=2))
return
# Download the database by connecting to the FTP server, listing all files, then downloading
if args.download:
for db in available_dbs:
dbinfo = lookup.get(db)
server = dbinfo.get("server")
database = dbinfo.get("database")
local_dir = path.join(args.basedir, database)
ftp_target = dbinfo.get("path")
if not path.exists(local_dir):
os.mkdir(local_dir)
print("Logging into " + server)
ftp = ftplib.FTP()
ftp.connect(server)
ftp.login()
print("Listing database files for " + database)
ftp.cwd(ftp_target)
for f in ftp.nlst():
local_target = path.join(local_dir, f)
print("Downloading " + local_target)
with open(local_target, "wb") as localfile:
ftp.retrbinary("RETR " + f, localfile.write)
ftp.quit()
ftp.close()
# Open CHECKSUMS.gz, calculate sum for each one & make sure the files have downloaded correctly
if args.validate:
if not available_dbs:
print("No databases found to test")
os.sys.exit(0)
fail = False
for dbname in available_dbs:
db = path.join(args.basedir, dbname)
if not path.exists(db):
print('Cannot find database directory "' + db + '"')
fail = True
break
with gzip.open(path.join(db, "CHECKSUMS.gz"), "rt") as file:
checksums = [
tuple(re.split("\s+", x.strip())) for x in file.readlines()
]
for chk in checksums:
target_file = path.join(db, chk[2])
local_chk = (bsdchecksum(target_file), blocks(target_file), chk[2])
if local_chk != chk:
print(
"Checksum check failed for {0} and {1}. Expected '{2} {3}' but got '{4} {5}'".format(
db, chk[2], chk[0], chk[1], local_chk[0], local_chk[1]
)
)
fail = True
if fail:
os.sys.exit(1)
else:
print("All files are correct")
def bsdchecksum(infile):
"""Compute BSD checksum as defined by https://en.wikipedia.org/wiki/BSD_checksum
:param infile: File location to query
:type infile: filepath
:return: Calculated checksum
:rtype: str
"""
with open(infile, "rb", buffering=5) as f:
checksum = 0
byte = f.read(1)
while byte:
checksum = (checksum >> 1) + ((checksum & 1) << 15)
checksum += int.from_bytes(byte, byteorder="big")
checksum &= 0xFFFF
byte = f.read(1)
return str(checksum)
def blocks(infile):
"""Return the number of 1024 blocks available in the given file calculated by dividing st_size by 1024 and ceil the number
:param infile: File location to query
:type infile: filepath
:return: Numbers of blocks in the given file.
:rtype: str
"""
byte_size = os.stat(infile).st_size
return str(math.ceil(byte_size / 1024))
if __name__ == "__main__":
main()