-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRRConvert.py
More file actions
191 lines (77 loc) · 3.38 KB
/
Copy pathSRRConvert.py
File metadata and controls
191 lines (77 loc) · 3.38 KB
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
import os
import subprocess
import sys
import pandas as pd # Added missing import
import logging
from utils import log_message
# Setup logging to a single aggregated log file
log_file = "srrconvert.log"
logging.basicConfig(
filename=log_file,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
filemode="a"
)
class SRRConvert:
def __init__(self, base_dir):
"""
Initialize the SRRConvert class with the base directory for GEO accessions.
"""
self.base_dir = base_dir
def convert_srr_to_fastq(self, srr_ids, accession):
"""
Convert downloaded SRA files to FASTQ format.
"""
geo_dir = os.path.join(self.base_dir, accession)
for srr_id in srr_ids:
srr_output_base = os.path.join(geo_dir, f"{srr_id}_Fastq")
srr_subdir = os.path.join(srr_output_base, srr_id)
sra_file = os.path.join(srr_subdir, f"{srr_id}.sra")
if not os.path.exists(sra_file):
self._log_and_print(f"Skipping {srr_id}, no SRA file found in {srr_subdir}.", level="WARNING")
continue
self._log_and_print(f"Converting {srr_id} to FASTQ...")
try:
fasterq_command = ["fasterq-dump", "--split-files", sra_file, "-O", srr_subdir]
subprocess.run(fasterq_command, check=True, text=True, stdout=sys.stdout, stderr=sys.stderr)
single_fastq = os.path.join(srr_subdir, f"{srr_id}_1.fastq")
if os.path.exists(single_fastq):
os.rename(single_fastq, os.path.join(srr_subdir, f"{srr_id}.fastq"))
os.remove(sra_file)
self._log_and_print(f"Deleted SRA file for {srr_id} in {srr_subdir}.")
except subprocess.CalledProcessError as e:
self._log_and_print(f"Error processing {srr_id}: {e}", level="ERROR")
continue
def _log_and_print(self, message, level="INFO"):
"""
Logs message to both the console and an aggregated log file.
"""
log_message(message, level)
print(message, flush=True)
if level == "ERROR":
logging.error(message)
elif level == "WARNING":
logging.warning(message)
else:
logging.info(message)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python SRRConvert.py <geo_accessions_file> <base_dir>")
sys.exit(1)
geo_accessions_file = sys.argv[1]
base_dir = sys.argv[2]
with open(geo_accessions_file, "r") as f:
geo_accessions = [line.strip() for line in f if line.strip()]
srr_converter = SRRConvert(base_dir)
for accession in geo_accessions:
srr_csv_path = os.path.join(base_dir, accession, "SRR.csv")
if not os.path.exists(srr_csv_path):
srr_converter._log_and_print(f"Skipping {accession}, missing SRR.csv.", level="WARNING")
continue
try:
srr_df = pd.read_csv(srr_csv_path)
srr_ids = srr_df["Run"].tolist()
except Exception as e:
srr_converter._log_and_print(f"Error reading SRR.csv for {accession}: {e}", level="ERROR")
continue
srr_converter.convert_srr_to_fastq(srr_ids, accession)