-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_fastq_from_list_ids.py
196 lines (152 loc) · 8.04 KB
/
get_fastq_from_list_ids.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" This python script downloads fastq file given an ACCESSION related to MGnify.
__author__ = Marco Reverenna
__copyright__ = Copyright 2024-2025
__date__ = 14 Dec 2023
__maintainer__ = Marco Reverenna
__email__ = [email protected]
__status__ = Dev
"""
import os
import json
from ftplib import FTP
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
# ftplib allows to transfer files between your computer and a server
def extract_column_names(input_file, output_name, output_directory):
""" This function creates a txt file with the IDs of a specific ACCESSION of MGnify.
Args:
input_file (_tsv_): file which contains the IDs of the ACCENSION
output_name (_txt_): file name which contains all the IDs
output_directory (str): directory to store the output file
"""
full_output_path = os.path.join(output_directory, output_name)
with open(input_file, 'r') as file:
column_names = file.readline().strip().split('\t')[1:]
# exclude the first column considering from the second and so on
with open(full_output_path, 'w') as id_file:
id_file.write('\n'.join(column_names))
# writes all the columns on txt file
print(f"File created in {output_directory}")
def download_files_from_list(server, input_ids_file, remote_directory, local_directory):
""" This function downloads fatsq file given a txt file wich contains the IDs.
Args:
server (_str_): server address (first part of the path)
input_ids_file (_file_txt_): file which contains a list of IDs obtained from an ACCESSION
remote_directory (_str_): path of the folder where are stored the fastq files
local_directory (_str_): path to your local directory which contains script and data
"""
try:
ftp = FTP(server)
ftp.login()
with open(input_ids_file, 'r') as id_file:
ids = id_file.readlines()
for id_name in ids:
id_name = id_name.strip()
folder_name = id_name[:6]
remote_path = f"{remote_directory}/{folder_name}/{id_name}/"
local_path = f"{local_directory}/{folder_name}/{id_name}/"
os.makedirs(local_path, exist_ok=True)
ftp.cwd(remote_path)
files_to_download = ftp.nlst()
for file in files_to_download:
with open(os.path.join(local_path, file), 'wb') as local_file:
# wb = write - binary
ftp.retrbinary('RETR ' + file, local_file.write)
# retrbinary = download files in binary format (retrieve binary)
print(f"File {file} successfully downloaded in {local_path}")
except Exception as e:
print(f"Error: {e}")
finally:
ftp.quit()
def load_credentials(file_path = '~/Retrieve_info_MGnifyAPI/credentials.json'):
"""Load the credentials for connecting with Azure
Args:
file_path (file_json): file which contains your credentials to Azure account
Returns:
str: Account name and key to access to your account
"""
with open(file_path, 'r') as file:
data = json.load(file)
return data
def download_files_push_store(server, input_ids_file, remote_directory, local_directory, azure_connection_string, azure_container_name):
""" This function downloads fastq files given a txt file which contains the IDs, and uploads them to Azure Blob Storage.
Args:
server (str): server address (first part of the path)
input_ids_file (file_txt): file which contains a list of IDs obtained from an ACCESSION
remote_directory (str): path of the folder where are stored the fastq files
local_directory (str): path to your local directory which contains script and data
azure_connection_string (str): Azure Storage account connection string
azure_container_name (str): Name of the Azure Blob Storage container
"""
failed_files = []
try:
ftp = FTP(server)
ftp.login()
blob_service_client = BlobServiceClient.from_connection_string(azure_connection_string)
container_client = blob_service_client.get_container_client(azure_container_name)
with open(input_ids_file, 'r') as id_file:
ids = id_file.readlines()
for id_name in ids:
id_name = id_name.strip()
folder_name = id_name[:6]
remote_path = f"{remote_directory}/{folder_name}/{id_name}/"
local_path = f"{local_directory}/{folder_name}/{id_name}/"
os.makedirs(local_path, exist_ok=True)
ftp.cwd(remote_path)
files_to_download = ftp.nlst()
for file in files_to_download:
local_file_path = os.path.join(local_path, file)
with open(local_file_path, 'wb') as local_file:
ftp.retrbinary('RETR ' + file, local_file.write)
# upload files to Azure Blob Storage
try:
blob_client = container_client.get_blob_client(blob=os.path.join(folder_name, id_name, file))
with open(local_file_path, "rb") as data:
blob_client.upload_blob(data)
print(f"File {file} successfully uploaded to Azure Storage in {os.path.join(folder_name, id_name)}")
except Exception as e:
print(f"Failed to upload {file}: {e}")
failed_files.append(file)
os.remove(local_file_path) # delete the local file after upload
except Exception as e:
print(f"Error: {e}")
finally:
ftp.quit()
# write failed files to a log file
if failed_files:
with open("failed_uploads.log", "w") as log_file:
for file in failed_files:
log_file.write(f"{file}\n")
# download_files_push_store('server_address', 'ids_file.txt', '/remote/dir/', '/local/dir/', 'azure_connection_string', 'mycontainer')
if __name__ == "__main__":
# example of server address = ftp://ftp.sra.ebi.ac.uk/vol1/fastq/ERR977/ERR977413/ERR977413_1.fastq.gz
server_address = 'ftp.sra.ebi.ac.uk'
accession = 'MGYS00001392'
erp_id = 'ERP011345'
tsv_path = f'../Output/Unified_analyses/{accession}/{accession}_{erp_id}_taxonomy_abundances_v3.0.tsv'
local_download_directory = f'../Output/Unified_analyses/{accession}/'
# create a new folder for IDs
path = os.path.join('../Output/', "IDs")
if not os.path.exists(path):
os.makedirs(path)
# get a list of ERR ids for downloading fastq files
extract_column_names(input_file= tsv_path, output_name = f'ERR_IDs_from_{accession}.txt', output_directory = path)
# download FASTQ in local
#download_files_from_list(server_address, f'../Output/IDs/ERR_IDs_from_{accession}.txt', '/vol1/fastq/', local_download_directory)
# load personal credentials for Azure connection
credentials = load_credentials('../credentials.json')
# download fastq files and push them in the storage account
download_files_push_store(server_address,
f'../Output/IDs/ERR_IDs_from_{accession}.txt',
'/vol1/fastq/',
local_download_directory,
azure_connection_string = f"DefaultEndpointsProtocol=https;AccountName={ credentials['storageAccountName']};AccountKey={credentials['storageAccountKey']};EndpointSuffix=core.windows.net",
azure_container_name = 'retrievefastq'
)
# Next steps
"""
1. e' probabilmente una buona idea to rimuovere ERP e lasciare solo l'accession = verificare se per una accessione ci sono piu erp_id altrimenti rimuovere.
2. si potrebbe creare una tupla, quello di cui ho bisogno e' il lista di accessioni
3. considerare il path assoluto in modo tale da correre lo script python da qualsiasi posizione
"""