-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (94 loc) · 4 KB
/
main.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
import requests
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sys
# KEGG API base URL
KEGG_API_BASE = "http://rest.kegg.jp/"
def get_species_list(genus):
organism_url = KEGG_API_BASE + "list/organism"
response = requests.get(organism_url)
lines = response.text.split("\n")
species_list = []
for line in lines:
if line and genus in line:
parts = line.split("\t")
species_code = parts[1]
species_name = parts[2]
species_list.append((species_code, species_name))
return species_list
def get_metabolic_pathways(species_code):
pathways_url = KEGG_API_BASE + f"list/pathway/{species_code}"
response = requests.get(pathways_url)
lines = response.text.split("\n")
pathways = []
for line in lines:
if line:
parts = line.split("\t")
pathway_id = parts[0]
pathways.append(pathway_id)
return pathways
def pathways_lists(species_code):
pathways_url = KEGG_API_BASE + f"list/pathway/{species_code}"
response = requests.get(pathways_url)
lines = response.text.split("\n")
pathways = {}
for line in lines:
if line:
parts = line.split("\t")
pathway_id = parts[0].replace(species_code, "")
pathway_name = parts[1].split(" - ")[0]
pathways[pathway_id] = pathway_name
return pathways
def pathways_to_matrix(pathway_list, species_list):
flattened_list = [item for sublist in pathway_list for item in sublist]
prefixes = sorted(set([item[:item.find("0")] for item in flattened_list]))
pathway_ids = sorted(set([item[item.find("0"):] for item in flattened_list]))
matrix = np.zeros((len(prefixes), len(pathway_ids)))
for pathway in flattened_list:
prefix = pathway[:pathway.find("0")]
pathway_id = pathway[pathway.find("0"):]
row_idx = prefixes.index(prefix)
col_idx = pathway_ids.index(pathway_id)
matrix[row_idx][col_idx] = 1
species_fullnames = [species_name for _, species_name in species_list]
df = pd.DataFrame(matrix, index=species_fullnames, columns=pathway_ids)
return df
def plot_heatmap(matrix, pathways_dict, output_pdf):
fig, ax = plt.subplots(figsize=(25, 35))
cmap = sns.color_palette("coolwarm", as_cmap=True)
sns.heatmap(matrix.T, cmap=cmap, annot=False, linewidths=.5, ax=ax, cbar=False)
ax.invert_yaxis()
ax.set_ylabel("Pathway ID", fontsize=24)
ax.set_xlabel(" ", fontsize=24)
ax.set_title("Metabolic Pathway Presence Heatmap", fontsize=32)
ax.xaxis.tick_top()
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='left')
ax.tick_params(axis='y', which='both', length=0)
divider = make_axes_locatable(ax)
legend_ax = divider.append_axes("right", size="5%", pad=0.5)
n_lines = len(pathways_dict)
for idx, (pathway_id, pathway_name) in enumerate(pathways_dict.items()):
legend_ax.text(0, 1 - idx / n_lines, f'{pathway_id}: {pathway_name}', fontsize=8, ha='left', va='center')
legend_ax.set_axis_off()
plt.savefig(output_pdf, format='pdf')
def main(genus, output_csv, output_pdf):
species_list = get_species_list(genus)
pathways_list = []
pathways_dict = {}
for code, _ in species_list:
pathways_list.append(get_metabolic_pathways(code))
pathways_dict.update(pathways_lists(code))
pathway_matrix = pathways_to_matrix(pathways_list, species_list)
pathway_matrix.to_csv(output_csv, index=True, header=True, index_label='row_name', float_format='%.0f')
plot_heatmap(pathway_matrix, pathways_dict, output_pdf)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python <script_name> <genus_name> <output_matrix_csv> <output_heatmap_pdf>")
sys.exit(1)
genus_name = sys.argv[1]
output_matrix_csv = sys.argv[2]
output_heatmap_pdf = sys.argv[3]
main(genus_name, output_matrix_csv, output_heatmap_pdf)