-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep1_get_data.py
More file actions
201 lines (164 loc) · 8.18 KB
/
step1_get_data.py
File metadata and controls
201 lines (164 loc) · 8.18 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
190
191
192
193
194
195
196
197
198
199
200
201
import pandas as pd
from ftplib import FTP
import os
import glob
import shutil
species_df = pd.DataFrame(columns=['database', 'mean_gene_length', 'variance_gene_length', 'mean_exon_count', 'variance_exon_count', 'mean_protein_length', 'variance_protein_length'])
if os.path.exists("temp_folder"):
shutil.rmtree("temp_folder")
os.makedirs("temp_folder", exist_ok=True)
os.chdir("temp_folder")
ftp = FTP('ftp.ensemblgenomes.org')
ftp.login()
ftp.cwd('/pub/release-61')
for kingdom in ['fungi', 'metazoa','plants', 'protists','plants']:
print(f"Processing {kingdom}")
os.makedirs(kingdom, exist_ok=True)
ftp.cwd(f'/pub/release-61/{kingdom}/gff3')
species_list = ftp.nlst()
species_list = [s for s in species_list if 'collection' not in s] # Filter out collection directories
species_count = len(species_list)
species_finished = 0
printing_threshold = 0.1
for species in species_list:
# Remove all non-directory files in the current directory
for f in os.listdir('.'):
if os.path.isfile(f):
os.remove(f)
# Download the GFF3 file
ftp.cwd(f'/pub/release-61/{kingdom}/gff3/{species}')
gff_files = ftp.nlst()
gff_files = sorted(gff_files, key=lambda f: ftp.size(f) if f.endswith('gff3.gz') else 0, reverse=True)
gff_files = [f for f in gff_files if 'abinitio' not in f]
filename = gff_files[0]
if 'abinitio' in filename:
print(f"Skipping abinitio file for {species}: {filename}")
continue
try:
with open(filename, 'wb') as local_file_obj:
ftp.retrbinary("RETR " + filename, local_file_obj.write)
except Exception as e:
print(f"Error downloading GFF3 for {filename}: {e}")
continue
# Download the corresponding FASTA file
ftp.cwd(f'/pub/release-61/{kingdom}/fasta/{species}/pep')
fa_files = ftp.nlst()
fa_files = sorted(fa_files, key=lambda f: ftp.size(f) if f.endswith('fa.gz') else 0, reverse=True)
fa_files = [f for f in fa_files if 'abinitio' not in f]
filename = fa_files[0]
try:
with open(filename, 'wb') as local_file_obj:
ftp.retrbinary("RETR " + filename, local_file_obj.write)
except Exception as e:
print(f"Error downloading fasta for {filename}: {e}")
continue
# Extract species name and database from the directory structure
species_df.at[species, 'database'] = kingdom
# Run the shell script to extract GTF data
os.system(f"bash ../extract_data.sh")
# Read the extracted data into the DataFrame
tsv_files = glob.glob('*.tsv')
if not tsv_files:
print(f"No TSV files found for {species}. Skipping...")
continue
elif len(tsv_files) > 1:
print(f"Multiple TSV files found for {species}. Using the first one: {tsv_files[0]}")
tsv_file = tsv_files[0]
extracted_data = pd.read_csv(tsv_file, sep='\t')
# Calculate statistics
species_df.at[species, 'mean_gene_length'] = extracted_data['gene_length'].mean()
species_df.at[species, 'variance_gene_length'] = extracted_data['gene_length'].var()
species_df.at[species, 'mean_exon_count'] = extracted_data['coding_exon_count'].mean()
species_df.at[species, 'variance_exon_count'] = extracted_data['coding_exon_count'].var()
species_df.at[species, 'mean_protein_length'] = extracted_data['protein_length'].mean()
species_df.at[species, 'variance_protein_length'] = extracted_data['protein_length'].var()
# Move the TSV file to the kingdom folder
kingdom_folder = os.path.join(kingdom)
new_tsv_path = os.path.join(kingdom_folder, os.path.basename(tsv_file))
os.rename(tsv_file, new_tsv_path)
species_finished += 1
if species_finished >= species_count * printing_threshold:
# Save species_df
species_df.to_csv(os.path.join('../', 'panel_a_data.tsv'), sep='\t', index_label='gtf_name')
print(f"Processed {printing_threshold*100:.1f}%")
printing_threshold += 0.1
ftp.close()
ftp = FTP('ftp.ensembl.org')
ftp.login()
ftp.cwd('/pub/release-114')
for kingdom in ['vertebrate']:
print(f"Processing {kingdom}")
os.makedirs(kingdom, exist_ok=True)
ftp.cwd(f'/pub/release-114/gff3')
species_list = ftp.nlst()
species_count = len(species_list)
species_finished = 0
printing_threshold = 0.1
for species in species_list:
# Remove all non-directory files in the current directory
for f in os.listdir('.'):
if os.path.isfile(f):
os.remove(f)
# Download the GFF3 file
ftp.cwd(f'/pub/release-114/gff3/{species}')
gff_files = ftp.nlst()
gff_files = sorted(gff_files, key=lambda f: ftp.size(f) if f.endswith('gff3.gz') else 0, reverse=True)
gff_files = [f for f in gff_files if 'abinitio' not in f]
filename = gff_files[0]
if 'abinitio' in filename:
print(f"Skipping abinitio file for {species}: {filename}")
continue
try:
with open(filename, 'wb') as local_file_obj:
ftp.retrbinary("RETR " + filename, local_file_obj.write)
except Exception as e:
print(f"Error downloading GFF3 for {filename}: {e}")
continue
# Download the corresponding FASTA file
ftp.cwd(f'/pub/release-114/fasta/{species}/pep')
fa_files = ftp.nlst()
fa_files = sorted(fa_files, key=lambda f: ftp.size(f) if f.endswith('fa.gz') else 0, reverse=True)
fa_files = [f for f in fa_files if 'abinitio' not in f]
filename = fa_files[0]
try:
with open(filename, 'wb') as local_file_obj:
ftp.retrbinary("RETR " + filename, local_file_obj.write)
except Exception as e:
print(f"Error downloading fasta for {filename}: {e}")
continue
# Extract species name and database from the directory structure
species_df.at[species, 'database'] = kingdom
# Run the shell script to extract GTF data
os.system(f"bash ../extract_data.sh")
# Read the extracted data into the DataFrame
tsv_files = glob.glob('*.tsv')
if not tsv_files:
print(f"No TSV files found for {species}. Skipping...")
continue
elif len(tsv_files) > 1:
print(f"Multiple TSV files found for {species}. Using the first one: {tsv_files[0]}")
tsv_file = tsv_files[0]
extracted_data = pd.read_csv(tsv_file, sep='\t')
# Calculate statistics
species_df.at[species, 'mean_gene_length'] = extracted_data['gene_length'].mean()
species_df.at[species, 'variance_gene_length'] = extracted_data['gene_length'].var()
species_df.at[species, 'mean_exon_count'] = extracted_data['coding_exon_count'].mean()
species_df.at[species, 'variance_exon_count'] = extracted_data['coding_exon_count'].var()
species_df.at[species, 'mean_protein_length'] = extracted_data['protein_length'].mean()
species_df.at[species, 'variance_protein_length'] = extracted_data['protein_length'].var()
# Move the TSV file to the kingdom folder
kingdom_folder = os.path.join(kingdom)
new_tsv_path = os.path.join(kingdom_folder, os.path.basename(tsv_file))
os.rename(tsv_file, new_tsv_path)
species_finished += 1
species_df.to_csv(os.path.join('../', 'panel_a_data.tsv'), sep='\t', index_label='gtf_name')
if species_finished >= species_count * printing_threshold:
print(f"Processed {printing_threshold*100:.1f}%")
printing_threshold += 0.1
ftp.cwd('..') # Go back to the kingdom directory
ftp.close()
# Save the final DataFrame
species_df.to_csv(os.path.join('..', 'panel_a_data.tsv'), sep='\t', index_label='gtf_name')
# Clean up temporary folder
os.chdir('..')
os.rename("temp_folder", "ensembl_data")