Mercurial > repos > iuc > data_manager_bakta
comparison data_manager/bakta_build_database.py @ 6:97b1b5ad1cda draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_build_bakta_database commit e0ce56ac52cff0e8f85e546440d28ca46853b11d
author | iuc |
---|---|
date | Thu, 20 Jun 2024 19:13:58 +0000 |
parents | baceff842902 |
children |
comparison
equal
deleted
inserted
replaced
5:baceff842902 | 6:97b1b5ad1cda |
---|---|
133 def download(self): | 133 def download(self): |
134 bakta_path = Path(self.db_dir).joinpath(self.tar_name) | 134 bakta_path = Path(self.db_dir).joinpath(self.tar_name) |
135 try: | 135 try: |
136 with bakta_path.open("wb") as fh_out, requests.get( | 136 with bakta_path.open("wb") as fh_out, requests.get( |
137 self.db_url, stream=True) as resp: | 137 self.db_url, stream=True) as resp: |
138 total_length = resp.headers.get("content-length") | 138 # total_length = resp.headers.get("content-length") |
139 if total_length is None: # no content length header | 139 for data in resp.iter_content(chunk_size=1024 * 1024): |
140 for data in resp.iter_content(chunk_size=1024 * 1024): | 140 fh_out.write(data) |
141 fh_out.write(data) | |
142 else: | |
143 for data in resp.iter_content(chunk_size=1024 * 1024): | |
144 fh_out.write(data) | |
145 print(f"Download bakta database {self.db_version}") | 141 print(f"Download bakta database {self.db_version}") |
146 self.tarball_path = bakta_path | 142 self.tarball_path = bakta_path |
147 except IOError: | 143 except IOError: |
148 print( | 144 print( |
149 f"ERROR: Could not download file from Zenodo!" | 145 f"ERROR: Could not download file from Zenodo!" |
156 with self.tarball_path.open("rb") as fh_in, tarfile.open( | 152 with self.tarball_path.open("rb") as fh_in, tarfile.open( |
157 fileobj=fh_in, mode="r:gz" | 153 fileobj=fh_in, mode="r:gz" |
158 ) as tar_file: | 154 ) as tar_file: |
159 tar_file.extractall(path=db_path) | 155 tar_file.extractall(path=db_path) |
160 print(f"Untar the database in {db_path}") | 156 print(f"Untar the database in {db_path}") |
161 | |
162 if not self.test_mode: | |
163 self.move_files(db_path=db_path) | |
164 | |
165 except OSError: | 157 except OSError: |
166 sys.exit(f"ERROR: Could not extract {self.tar_name} " f"to {db_path}") | 158 sys.exit(f"ERROR: Could not extract {self.tar_name} " f"to {db_path}") |
159 if not self.test_mode: | |
160 self.move_files(db_path=db_path) | |
161 self.db_dir = db_path.resolve() | |
162 | |
163 def delete_folder(self, path): | |
164 for sub in path.iterdir(): | |
165 if sub.is_dir() and sub.name != "latest": | |
166 self.delete_folder(sub) | |
167 else: | |
168 sub.unlink() | |
169 path.rmdir() | |
167 | 170 |
168 def move_files(self, db_path): | 171 def move_files(self, db_path): |
169 if db_path.joinpath("db-light").is_dir(): | 172 if db_path.joinpath("db-light").is_dir(): |
170 input_dir = db_path.joinpath("db-light") | 173 input_dir = db_path.joinpath("db-light") |
171 elif db_path.joinpath("db").is_dir(): | 174 elif db_path.joinpath("db").is_dir(): |
172 input_dir = db_path.joinpath("db") | 175 input_dir = db_path.joinpath("db") |
173 output_dir = db_path | 176 output_dir = db_path |
174 for file in input_dir.iterdir(): | 177 for file in input_dir.iterdir(): |
175 if file.is_file(): # to avoid moving amrfinder-plus folder | 178 if file.is_file(): # to avoid moving amrfinder-plus folder |
176 input = input_dir.joinpath(file) | 179 output = output_dir.joinpath(file.name) |
177 output = output_dir.joinpath(file) | 180 file.rename(output) |
178 input.rename(output) | 181 self.delete_folder(input_dir) |
179 | 182 |
180 def calc_md5_sum(self, buffer_size=1048576): | 183 def calc_md5_sum(self, buffer_size=1048576): |
181 tarball_path = Path(self.db_dir).joinpath(self.tar_name) | 184 tarball_path = Path(self.db_dir).joinpath(self.tar_name) |
182 md5 = hashlib.md5() | 185 md5 = hashlib.md5() |
183 with tarball_path.open("rb") as fh: | 186 with tarball_path.open("rb") as fh: |