PGx Analyzer Team commited on
Commit
2893dcb
·
1 Parent(s): 3e26d96

Expand PDF Report Generator to include all 5 geographic regions, HWE table, gender stratification, and CPIC phenotype calls

Browse files
Files changed (1) hide show
  1. reports/report_generator.py +112 -42
reports/report_generator.py CHANGED
@@ -124,7 +124,7 @@ class ReportGenerator:
124
 
125
  @classmethod
126
  def export_to_pdf(cls, full_results: Dict[str, Any]) -> bytes:
127
- """Generates a publication-ready PDF summary report."""
128
  buffer = io.BytesIO()
129
  doc = SimpleDocTemplate(buffer, pagesize=letter, rightMargin=36, leftMargin=36, topMargin=36, bottomMargin=36)
130
  elements = []
@@ -133,87 +133,157 @@ class ReportGenerator:
133
  title_style = ParagraphStyle(
134
  'TitleStyle',
135
  parent=styles['Heading1'],
136
- fontSize=18,
137
  textColor=colors.HexColor('#0F172A'),
138
- spaceAfter=12
139
  )
140
  heading_style = ParagraphStyle(
141
  'HeadingStyle',
142
  parent=styles['Heading2'],
143
- fontSize=13,
144
  textColor=colors.HexColor('#1E3A8A'),
145
- spaceBefore=10,
146
- spaceAfter=8
147
  )
148
  body_style = styles['BodyText']
149
 
150
- # Title & Subtitle
151
- elements.append(Paragraph("Population Pharmacogenomics Executive Report", title_style))
152
- elements.append(Paragraph("Automated Analysis of Genotype, Allele (p & q), HWE, Geographic Population Groups & Phenotypes", body_style))
153
- elements.append(Spacer(1, 12))
154
 
155
- # Summary Overview Table
156
  overall_stats = full_results.get('overall', {})
157
  total_samples = overall_stats.get('sample_count', 0)
158
 
159
- table_data = [
160
- ["Metric", "Value"],
161
- ["Total Samples Analyzed", str(total_samples)],
162
- ["Geographic Population Groups", "North India, South India, East India, West India"],
163
- ["Target Pharmacogene", "CYP2C19 (*2, *3, *17)"]
 
 
164
  ]
165
 
166
- t = Table(table_data, colWidths=[200, 300])
167
- t.setStyle(TableStyle([
168
  ('BACKGROUND', (0, 0), (1, 0), colors.HexColor('#0F172A')),
169
  ('TEXTCOLOR', (0, 0), (1, 0), colors.white),
170
  ('FONTNAME', (0, 0), (1, 0), 'Helvetica-Bold'),
171
- ('BOTTOMPADDING', (0, 0), (1, 0), 6),
172
  ('BACKGROUND', (0, 1), (-1, -1), colors.HexColor('#F8FAFC')),
173
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
174
  ]))
175
- elements.append(t)
176
- elements.append(Spacer(1, 14))
177
 
178
- # Geographic Population Groups Section
179
- elements.append(Paragraph("Geographic Population Groups Summary", heading_style))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  regional_results = full_results.get('regional', {})
181
- reg_table_data = [["Geographic Group", "Sample N", "CYP2C19*2 Freq (A)", "HWE Status", "PM (%)"]]
182
 
183
- for r_name in ['North India', 'South India', 'East India', 'West India']:
 
184
  r_data = regional_results.get(r_name, {})
185
- cyp2_a = r_data.get('snps', {}).get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0)
186
- hwe_stat = r_data.get('snps', {}).get('CYP2C19*2', {}).get('hwe', {}).get('interpretation', 'In HWE')
 
 
 
187
  pm_pct = r_data.get('cyp2c19_summary', {}).get('phenotypes', {}).get('Poor Metabolizer (PM)', {}).get('percentage', 0)
188
- reg_table_data.append([r_name, str(r_data.get('sample_count', 0)), str(cyp2_a), hwe_stat, f"{pm_pct}%"])
189
 
190
- rtable = Table(reg_table_data, colWidths=[120, 80, 110, 100, 90])
191
- rtable.setStyle(TableStyle([
192
- ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1E3A8A')),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
194
  ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
195
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
196
  ]))
197
- elements.append(rtable)
198
- elements.append(Spacer(1, 14))
199
 
200
- # Phenotype Breakdown Section
201
- elements.append(Paragraph("CYP2C19 Phenotype Summary (Overall Population)", heading_style))
202
- pgx = overall_stats.get('cyp2c19_summary', {}).get('phenotypes', {})
 
203
 
204
- p_table_data = [["Metabolizer Phenotype", "Count", "Percentage (%)"]]
205
- for pheno_name, pdata in pgx.items():
206
  p_table_data.append([pheno_name, str(pdata.get('count', 0)), f"{pdata.get('percentage', 0)}%"])
207
 
208
- ptable = Table(p_table_data, colWidths=[250, 100, 150])
209
- ptable.setStyle(TableStyle([
210
  ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1E40AF')),
211
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
212
  ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
213
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#9CA3AF'))
214
  ]))
215
- elements.append(ptable)
216
- elements.append(Spacer(1, 16))
217
 
218
  doc.build(elements)
219
  return buffer.getvalue()
 
124
 
125
  @classmethod
126
  def export_to_pdf(cls, full_results: Dict[str, Any]) -> bytes:
127
+ """Generates a publication-ready comprehensive PDF report."""
128
  buffer = io.BytesIO()
129
  doc = SimpleDocTemplate(buffer, pagesize=letter, rightMargin=36, leftMargin=36, topMargin=36, bottomMargin=36)
130
  elements = []
 
133
  title_style = ParagraphStyle(
134
  'TitleStyle',
135
  parent=styles['Heading1'],
136
+ fontSize=16,
137
  textColor=colors.HexColor('#0F172A'),
138
+ spaceAfter=8
139
  )
140
  heading_style = ParagraphStyle(
141
  'HeadingStyle',
142
  parent=styles['Heading2'],
143
+ fontSize=12,
144
  textColor=colors.HexColor('#1E3A8A'),
145
+ spaceBefore=12,
146
+ spaceAfter=6
147
  )
148
  body_style = styles['BodyText']
149
 
150
+ # 1. Document Header
151
+ elements.append(Paragraph("Automated Population Pharmacogenomics Analysis Report", title_style))
152
+ elements.append(Paragraph("Development of an Automated Platform for Genotype, Allele, HWE, Diplotype, and CPIC Phenotype Analysis", body_style))
153
+ elements.append(Spacer(1, 10))
154
 
155
+ # 2. Executive Metadata Summary Table
156
  overall_stats = full_results.get('overall', {})
157
  total_samples = overall_stats.get('sample_count', 0)
158
 
159
+ meta_table = [
160
+ ["Parameter", "Details"],
161
+ ["Total Cohort Analyzed", f"{total_samples:,} Samples"],
162
+ ["Geographic Population Groups", "South India, North India, East India, West India, Central India"],
163
+ ["Target Gene / SNPs", "CYP2C19 (*2 rs4244285, *3 rs4986893, *17 rs12248560)"],
164
+ ["Statistical Engines", "Chi-Square Goodness-of-Fit (df=1) & Haldane Exact Test"],
165
+ ["Interpretation Framework", "CPIC Guidelines for CYP2C19 Metabolizer Phenotypes"]
166
  ]
167
 
168
+ t_meta = Table(meta_table, colWidths=[180, 360])
169
+ t_meta.setStyle(TableStyle([
170
  ('BACKGROUND', (0, 0), (1, 0), colors.HexColor('#0F172A')),
171
  ('TEXTCOLOR', (0, 0), (1, 0), colors.white),
172
  ('FONTNAME', (0, 0), (1, 0), 'Helvetica-Bold'),
173
+ ('BOTTOMPADDING', (0, 0), (1, 0), 5),
174
  ('BACKGROUND', (0, 1), (-1, -1), colors.HexColor('#F8FAFC')),
175
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
176
  ]))
177
+ elements.append(t_meta)
178
+ elements.append(Spacer(1, 12))
179
 
180
+ # 3. Hardy-Weinberg Equilibrium (HWE) Summary Section
181
+ elements.append(Paragraph("1. Overall Population Hardy-Weinberg Equilibrium (HWE) Analysis", heading_style))
182
+ snps_data = overall_stats.get('snps', {})
183
+ hwe_table_data = [["SNP Variant", "Valid N", "Allele Freq p / q", "Chi2 (χ²)", "P-Value", "HWE Status"]]
184
+
185
+ for snp_name, s_res in snps_data.items():
186
+ hw = s_res.get('hwe', {})
187
+ af = s_res.get('allele_freqs', {})
188
+ ref_a = list(af.keys())[0] if af else 'Ref'
189
+ var_a = list(af.keys())[1] if len(af) > 1 else 'Var'
190
+ freq_str = f"{ref_a}:{af.get(ref_a, 0)} · {var_a}:{af.get(var_a, 0)}"
191
+ hwe_table_data.append([
192
+ snp_name,
193
+ str(s_res.get('valid_samples', 0)),
194
+ freq_str,
195
+ str(hw.get('chi2_stat', 0)),
196
+ str(hw.get('p_value', 1.0)),
197
+ str(hw.get('interpretation', 'In HWE'))
198
+ ])
199
+
200
+ t_hwe = Table(hwe_table_data, colWidths=[90, 60, 150, 70, 70, 100])
201
+ t_hwe.setStyle(TableStyle([
202
+ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1E3A8A')),
203
+ ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
204
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
205
+ ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
206
+ ]))
207
+ elements.append(t_hwe)
208
+ elements.append(Spacer(1, 12))
209
+
210
+ # 4. Geographic Population Groups Section (All 5 Regions)
211
+ elements.append(Paragraph("2. Geographic Population Groups Matrix (All 5 Regions)", heading_style))
212
  regional_results = full_results.get('regional', {})
213
+ all_regions = ['South India', 'North India', 'East India', 'West India', 'Central India']
214
 
215
+ reg_table_data = [["Geographic Region", "Sample N", "*2 Var Freq f(A)", "*2 Chi2 (P-Val)", "*17 Var Freq f(T)", "PM (%)"]]
216
+ for r_name in all_regions:
217
  r_data = regional_results.get(r_name, {})
218
+ r_snps = r_data.get('snps', {})
219
+ cyp2_a = r_snps.get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0)
220
+ cyp2_chi = r_snps.get('CYP2C19*2', {}).get('hwe', {}).get('chi2_stat', 0)
221
+ cyp2_p = r_snps.get('CYP2C19*2', {}).get('hwe', {}).get('p_value', 1.0)
222
+ cyp17_t = r_snps.get('CYP2C19*17', {}).get('allele_freqs', {}).get('T', 0)
223
  pm_pct = r_data.get('cyp2c19_summary', {}).get('phenotypes', {}).get('Poor Metabolizer (PM)', {}).get('percentage', 0)
 
224
 
225
+ reg_table_data.append([
226
+ r_name,
227
+ str(r_data.get('sample_count', 0)),
228
+ str(cyp2_a),
229
+ f"{cyp2_chi} (p={cyp2_p})",
230
+ str(cyp17_t),
231
+ f"{pm_pct}%"
232
+ ])
233
+
234
+ t_reg = Table(reg_table_data, colWidths=[110, 70, 110, 110, 80, 60])
235
+ t_reg.setStyle(TableStyle([
236
+ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#0F766E')),
237
+ ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
238
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
239
+ ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
240
+ ]))
241
+ elements.append(t_reg)
242
+ elements.append(Spacer(1, 12))
243
+
244
+ # 5. Gender Stratification Section
245
+ elements.append(Paragraph("3. Gender Stratification Analysis (Male vs Female vs Overall)", heading_style))
246
+ gender_results = full_results.get('gender', {})
247
+ g_table_data = [["Cohort / Gender", "Sample N", "*2 Var Freq f(A)", "*17 Var Freq f(T)", "Normal (NM) %", "Poor (PM) %"]]
248
+
249
+ for g_name in ['Male', 'Female']:
250
+ g_data = gender_results.get(g_name, {})
251
+ g_snps = g_data.get('snps', {})
252
+ cyp2_a = g_snps.get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0)
253
+ cyp17_t = g_snps.get('CYP2C19*17', {}).get('allele_freqs', {}).get('T', 0)
254
+ g_phenos = g_data.get('cyp2c19_summary', {}).get('phenotypes', {})
255
+ nm_p = g_phenos.get('Normal Metabolizer (NM)', {}).get('percentage', 0)
256
+ pm_p = g_phenos.get('Poor Metabolizer (PM)', {}).get('percentage', 0)
257
+
258
+ g_table_data.append([g_name, str(g_data.get('sample_count', 0)), str(cyp2_a), str(cyp17_t), f"{nm_p}%", f"{pm_p}%"])
259
+
260
+ t_gen = Table(g_table_data, colWidths=[110, 70, 110, 110, 80, 60])
261
+ t_gen.setStyle(TableStyle([
262
+ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#4338CA')),
263
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
264
  ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
265
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#CBD5E1'))
266
  ]))
267
+ elements.append(t_gen)
268
+ elements.append(Spacer(1, 12))
269
 
270
+ # 6. CPIC Diplotypes & Metabolizer Phenotypes Section
271
+ elements.append(Paragraph("4. CPIC CYP2C19 Diplotypes & Metabolizer Phenotypes", heading_style))
272
+ pgx = overall_stats.get('cyp2c19_summary', {})
273
+ pheno_data = pgx.get('phenotypes', {})
274
 
275
+ p_table_data = [["Metabolizer Phenotype Category", "Count (N)", "Percentage (%)"]]
276
+ for pheno_name, pdata in pheno_data.items():
277
  p_table_data.append([pheno_name, str(pdata.get('count', 0)), f"{pdata.get('percentage', 0)}%"])
278
 
279
+ t_pheno = Table(p_table_data, colWidths=[240, 130, 170])
280
+ t_pheno.setStyle(TableStyle([
281
  ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1E40AF')),
282
  ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
283
  ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
284
  ('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#9CA3AF'))
285
  ]))
286
+ elements.append(t_pheno)
 
287
 
288
  doc.build(elements)
289
  return buffer.getvalue()