-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_manhatton_plot.py
398 lines (346 loc) · 12.8 KB
/
generate_manhatton_plot.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# import pandas as pd
# import numpy as np
# import plotly.graph_objects as go
# import pysam
# from pathlib import Path
# import glob
# # Constants
# CHROMOSOMES = [str(i) for i in range(1, 23)]
# ALTERNATING_COLORS = ['#DC2626', '#2563EB'] # Dark red and blue
# DOWNSAMPLE_LIMIT = 50000
# def fetch_tabix_data(chrom, gz_file_path, tbi_file_path):
# """Fetch data for a single chromosome from a tabix file."""
# results = []
# with pysam.TabixFile(gz_file_path, index=tbi_file_path) as tabix_file:
# for row in tabix_file.fetch(str(chrom)):
# fields = row.strip().split('\t')
# try:
# pos = int(float(fields[2])) # POS column (unchanged)
# pval = float(fields[7]) if fields[7] != 'NA' else None # Changed from 14 to 7 for P column
# if pval is not None:
# results.append({
# 'chrom': str(chrom),
# 'pos': pos,
# 'pval': pval,
# })
# except (ValueError, IndexError) as e:
# print(f"Error processing row for chromosome {chrom}: {str(e)}")
# continue
# return results
# def fetch_all_chromosomes(gz_file_path, tbi_file_path):
# """Fetch data for all chromosomes from a single file pair."""
# all_data = []
# for chrom in range(1, 23):
# all_data.extend(fetch_tabix_data(chrom, gz_file_path, tbi_file_path))
# return pd.DataFrame(all_data)
# def process_dataframe(df):
# """Process the dataframe and calculate manhattan plot coordinates."""
# df['log_p'] = -np.log10(df['pval'])
# df['chrom'] = df['chrom'].astype(int)
# df = df.sort_values(['chrom', 'pos'])
# # Calculate chromosome offsets with larger spacing
# chrom_sizes = df.groupby('chrom')['pos'].max()
# spacing = chrom_sizes.max() * 0.08
# chrom_offsets = pd.Series(0, index=range(1, 23))
# current_offset = 0
# for chrom in range(1, 23):
# chrom_offsets[chrom] = current_offset
# if chrom in chrom_sizes.index:
# current_offset += chrom_sizes[chrom] + spacing
# # Apply offsets
# df['x_manhattan'] = df.apply(lambda row: row['pos'] + chrom_offsets[row['chrom']], axis=1)
# return df, chrom_sizes, chrom_offsets
# def create_manhattan_plot(df, chrom_sizes, chrom_offsets, output_path, height=150):
# """Create and save a manhattan plot."""
# # Calculate tick positions
# tickvals = []
# ticktext = []
# for chrom in range(1, 23):
# if chrom in chrom_sizes.index:
# center = chrom_offsets[chrom] + chrom_sizes[chrom] / 2
# tickvals.append(center)
# ticktext.append(str(chrom))
# # Create plot data
# plot_data = []
# for chrom in range(1, 23):
# chrom_data = df[df['chrom'] == chrom]
# color_index = (chrom - 1) % 2
# plot_data.append(go.Scattergl(
# x=chrom_data['x_manhattan'],
# y=chrom_data['log_p'],
# mode='markers',
# marker=dict(
# color=ALTERNATING_COLORS[color_index],
# size=5,
# opacity=0.8
# ),
# hoverinfo='text',
# text=chrom_data.apply(
# lambda row: f"Chromosome: {row['chrom']}<br>Position: {row['pos']}<br>-log10 p-value: {row['log_p']:.2f}<br>P-value: {10**-row['log_p']:.2e}",
# axis=1
# ),
# showlegend=False
# ))
# # Create layout
# layout = go.Layout(
# showlegend=False,
# xaxis=dict(
# title=None,
# tickmode='array',
# tickvals=tickvals,
# ticktext=ticktext,
# showline=False,
# zeroline=False,
# showgrid=False,
# showticklabels=False,
# ticks='',
# ticklen=0,
# tickfont=dict(size=10),
# ),
# yaxis=dict(
# title=None,
# range=[0, 10],
# showline=False,
# zeroline=False,
# showgrid=False,
# showticklabels=False,
# ticks='',
# ticklen=0,
# ),
# width=1500,
# height=height,
# plot_bgcolor='white',
# paper_bgcolor='white',
# margin=dict(
# l=0,
# r=0,
# t=10,
# b=30
# ),
# )
# # Create and save figure
# fig = go.Figure(data=plot_data, layout=layout)
# fig.write_image(
# output_path,
# scale=3,
# width=1500,
# height=height
# )
# return fig
# def main():
# # Directory containing the .gz files
# data_dir = "/nfs/platlas_stor/tabix/"
# output_dir = "/nfs/platlas_stor/manhatton_plot"
# # Find all .gz files
# gz_files = glob.glob(f"{data_dir}/*.gz")
# # Filter out .tbi files from the list
# gz_files = [f for f in gz_files if not f.endswith('.tbi')]
# # Process each file pair
# for gz_file_path in gz_files:
# tbi_file_path = f"{gz_file_path}.tbi"
# # Skip if .tbi file doesn't exist
# if not Path(tbi_file_path).exists():
# print(f"Warning: No .tbi file found for {gz_file_path}")
# continue
# # Generate output filename
# file_name = Path(gz_file_path).stem
# output_path = Path(output_dir) / f"manhattan_{file_name}.png"
# print(f"Processing {file_name}...")
# # Fetch and process data
# df = fetch_all_chromosomes(gz_file_path, tbi_file_path)
# df, chrom_sizes, chrom_offsets = process_dataframe(df)
# # Create plot
# create_manhattan_plot(df, chrom_sizes, chrom_offsets, output_path)
# print(f"Saved plot to {output_path}")
# if __name__ == "__main__":
# main()
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import pysam
from pathlib import Path
import glob
# Constants
CHROMOSOMES = [str(i) for i in range(1, 23)]
ALTERNATING_COLORS = ['#DC2626', '#2563EB'] # Dark red and blue
SIGNIFICANCE_THRESHOLD = 5e-8 # Genome-wide significance threshold
def process_dataframe(df):
"""Process the dataframe and calculate manhattan plot coordinates."""
# Calculate -log10(p-value)
df['log_p'] = -np.log10(df['pval'].clip(1e-300)) # Clip to prevent infinity
df['chrom'] = df['chrom'].astype(int)
df = df.sort_values(['chrom', 'pos'])
# Calculate chromosome sizes and offsets with better spacing
chrom_sizes = df.groupby('chrom')['pos'].max()
max_size = chrom_sizes.max()
spacing = max_size * 0.08 # 8% of max chromosome size for spacing
# Calculate offsets with spacing
chrom_offsets = pd.Series(0, index=range(1, 23))
current_offset = 0
for chrom in range(1, 23):
chrom_offsets[chrom] = current_offset
if chrom in chrom_sizes.index:
current_offset += chrom_sizes[chrom] + spacing
# Apply offsets to create manhattan x-coordinates
df['x_manhattan'] = df.apply(lambda row: row['pos'] + chrom_offsets[row['chrom']], axis=1)
return df, chrom_sizes, chrom_offsets
def get_y_axis_config(max_qval):
"""Determine appropriate y-axis configuration based on max -log10(p-value)."""
if max_qval <= 14:
tick_interval = 2
max_range = 14
elif max_qval <= 28:
tick_interval = 4
max_range = 28
elif max_qval <= 40:
tick_interval = 8
max_range = 40
elif max_qval <= 70:
tick_interval = 10
max_range = 70
else:
tick_interval = 20
max_range = ((max_qval // 20) + 1) * 20
return {
'tick_interval': tick_interval,
'max_range': max_range
}
def create_manhattan_plot(df, chrom_sizes, chrom_offsets, output_path, height=600):
"""Create and save a manhattan plot with minimal visualization."""
# Calculate tick positions for chromosomes
tickvals = []
ticktext = []
for chrom in range(1, 23):
if chrom in chrom_sizes.index:
center = chrom_offsets[chrom] + chrom_sizes[chrom] / 2
tickvals.append(center)
ticktext.append(str(chrom))
# Get y-axis configuration
max_qval = df['log_p'].max()
y_config = get_y_axis_config(max_qval)
# Create plot data with alternating colors
plot_data = []
# Add significance threshold line
# plot_data.append(go.Scatter(
# x=[0, df['x_manhattan'].max()],
# y=[-np.log10(SIGNIFICANCE_THRESHOLD), -np.log10(SIGNIFICANCE_THRESHOLD)],
# mode='lines',
# line=dict(color='gray', width=1, dash='dash'),
# name='Significance',
# showlegend=False
# ))
# Add chromosome data
for chrom in range(1, 23):
chrom_data = df[df['chrom'] == chrom]
color_index = (chrom - 1) % 2
plot_data.append(go.Scattergl(
x=chrom_data['x_manhattan'],
y=chrom_data['log_p'],
mode='markers',
marker=dict(
color=ALTERNATING_COLORS[color_index],
size=3,
opacity=0.7
),
hoverinfo='text',
text=chrom_data.apply(
lambda row: (
f"Chromosome: {row['chrom']}<br>"
f"Position: {row['pos']:,}<br>"
f"-log10(p): {row['log_p']:.2f}<br>"
f"p-value: {row['pval']:.2e}"
),
axis=1
),
showlegend=False
))
# Create layout with minimal settings
layout = go.Layout(
showlegend=False,
xaxis=dict(
showticklabels=False, # Hide x-axis tick labels
showgrid=False, # Hide grid
zeroline=False, # Hide zero line
showline=False, # Hide axis line
ticks="", # Hide tick marks
),
yaxis=dict(
showticklabels=False, # Hide y-axis tick labels
showgrid=False, # Hide grid
zeroline=False, # Hide zero line
showline=False, # Hide axis line
ticks="", # Hide tick marks
),
width=1500,
height=height,
plot_bgcolor='white',
paper_bgcolor='white',
margin=dict(
l=0, # Reduced left margin since we don't have labels
r=0, # Reduced right margin
t=10, # Reduced top margin
b=10 # Reduced bottom margin
),
hovermode='closest'
)
# Create and save figure
fig = go.Figure(data=plot_data, layout=layout)
# Update hover template
fig.update_traces(
hovertemplate="%{text}<extra></extra>"
)
# Save the figure
fig.write_image(
output_path,
scale=3,
width=1500,
height=height
)
return fig
def fetch_tabix_data(chrom, gz_file_path, tbi_file_path):
"""Fetch data for a single chromosome from a tabix file."""
results = []
with pysam.TabixFile(gz_file_path, index=tbi_file_path) as tabix_file:
for row in tabix_file.fetch(str(chrom)):
fields = row.strip().split('\t')
try:
pos = int(float(fields[2]))
pval = float(fields[7]) if fields[7] != 'NA' else None
if pval is not None:
results.append({
'chrom': str(chrom),
'pos': pos,
'pval': pval,
})
except (ValueError, IndexError) as e:
print(f"Error processing row for chromosome {chrom}: {str(e)}")
continue
return results
def main():
# Directory paths
data_dir = "/nfs/platlas_stor/tabix"
output_dir = "/nfs/platlas_stor/mh_plots"
# Process each file
for gz_file_path in glob.glob(f"{data_dir}/*pval_up_to_0.1.gz"):
if gz_file_path.endswith('.tbi'):
continue
tbi_file_path = f"{gz_file_path}.tbi"
if not Path(tbi_file_path).exists():
print(f"Warning: No .tbi file found for {gz_file_path}")
continue
file_name = Path(gz_file_path).stem
output_path = Path(output_dir) / f"manhattan_{file_name}.png"
print(f"Processing {file_name}...")
# Process data and create plot
df = pd.DataFrame([
item for chrom in range(1, 23)
for item in fetch_tabix_data(chrom, gz_file_path, tbi_file_path)
])
if not df.empty:
df, chrom_sizes, chrom_offsets = process_dataframe(df)
create_manhattan_plot(df, chrom_sizes, chrom_offsets, output_path)
print(f"Saved plot to {output_path}")
else:
print(f"No valid data found in {file_name}")
if __name__ == "__main__":
main()