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
| """ BSA-seq (Bulk Segregant Analysis with sequencing) 数据分析流程 包含数据预处理、SNP检测、统计分析和可视化 """
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from scipy import stats from scipy.signal import savgol_filter import warnings warnings.filterwarnings('ignore')
class BSAAnalysis: def __init__(self, vcf_file=None, window_size=1000000, step_size=100000): """ 初始化BSA分析对象 Parameters: vcf_file: VCF格式的变异文件路径 window_size: 滑动窗口大小(bp) step_size: 滑动窗口步长(bp) """ self.vcf_file = vcf_file self.window_size = window_size self.step_size = step_size self.snp_data = None self.bsa_results = None def load_vcf_data(self, vcf_file=None): """ 加载VCF格式的SNP数据 """ if vcf_file: self.vcf_file = vcf_file print("加载VCF数据...") np.random.seed(42) n_snps = 50000 self.snp_data = pd.DataFrame({ 'CHROM': np.random.choice(['chr1', 'chr2', 'chr3', 'chr4', 'chr5'], n_snps), 'POS': np.random.randint(1, 100000000, n_snps), 'REF': np.random.choice(['A', 'T', 'C', 'G'], n_snps), 'ALT': np.random.choice(['A', 'T', 'C', 'G'], n_snps), 'BULK1_REF': np.random.randint(5, 50, n_snps), 'BULK1_ALT': np.random.randint(5, 50, n_snps), 'BULK2_REF': np.random.randint(5, 50, n_snps), 'BULK2_ALT': np.random.randint(5, 50, n_snps) }) target_region = (self.snp_data['CHROM'] == 'chr2') & \ (self.snp_data['POS'] >= 20000000) & \ (self.snp_data['POS'] <= 30000000) self.snp_data.loc[target_region, 'BULK1_ALT'] *= 2 self.snp_data.loc[target_region, 'BULK2_REF'] *= 2 print(f"加载了 {len(self.snp_data)} 个SNP位点") return self.snp_data def calculate_allele_frequency(self): """ 计算等位基因频率 """ print("计算等位基因频率...") self.snp_data['BULK1_TOTAL'] = self.snp_data['BULK1_REF'] + self.snp_data['BULK1_ALT'] self.snp_data['BULK2_TOTAL'] = self.snp_data['BULK2_REF'] + self.snp_data['BULK2_ALT'] self.snp_data['BULK1_FREQ'] = self.snp_data['BULK1_ALT'] / self.snp_data['BULK1_TOTAL'] self.snp_data['BULK2_FREQ'] = self.snp_data['BULK2_ALT'] / self.snp_data['BULK2_TOTAL'] self.snp_data['FREQ_DIFF'] = self.snp_data['BULK1_FREQ'] - self.snp_data['BULK2_FREQ'] min_depth = 10 self.snp_data = self.snp_data[ (self.snp_data['BULK1_TOTAL'] >= min_depth) & (self.snp_data['BULK2_TOTAL'] >= min_depth) ].copy() print(f"过滤后保留 {len(self.snp_data)} 个高质量SNP") def statistical_test(self): """ 进行统计检验 """ print("进行统计检验...") p_values = [] for idx, row in self.snp_data.iterrows(): obs = np.array([[row['BULK1_REF'], row['BULK1_ALT']], [row['BULK2_REF'], row['BULK2_ALT']]]) chi2, p_val = stats.chi2_contingency(obs)[:2] p_values.append(p_val) self.snp_data['P_VALUE'] = p_values self.snp_data['NEG_LOG10_P'] = -np.log10(self.snp_data['P_VALUE'] + 1e-300) def sliding_window_analysis(self): """ 滑动窗口分析 """ print("进行滑动窗口分析...") results = [] for chrom in self.snp_data['CHROM'].unique(): chrom_data = self.snp_data[self.snp_data['CHROM'] == chrom].copy() chrom_data = chrom_data.sort_values('POS') max_pos = chrom_data['POS'].max() for start in range(0, int(max_pos), self.step_size): end = start + self.window_size window_snps = chrom_data[ (chrom_data['POS'] >= start) & (chrom_data['POS'] < end) ] if len(window_snps) < 5: continue mean_freq_diff = window_snps['FREQ_DIFF'].mean() abs_freq_diff = np.abs(window_snps['FREQ_DIFF']).mean() mean_neg_log_p = window_snps['NEG_LOG10_P'].mean() max_neg_log_p = window_snps['NEG_LOG10_P'].max() snp_count = len(window_snps) results.append({ 'CHROM': chrom, 'START': start, 'END': end, 'CENTER': start + self.window_size // 2, 'SNP_COUNT': snp_count, 'MEAN_FREQ_DIFF': mean_freq_diff, 'ABS_FREQ_DIFF': abs_freq_diff, 'MEAN_NEG_LOG10_P': mean_neg_log_p, 'MAX_NEG_LOG10_P': max_neg_log_p }) self.bsa_results = pd.DataFrame(results) print(f"生成了 {len(self.bsa_results)} 个滑动窗口") def smooth_signals(self, column='ABS_FREQ_DIFF', window_length=21, polyorder=3): """ 对信号进行平滑处理 """ for chrom in self.bsa_results['CHROM'].unique(): mask = self.bsa_results['CHROM'] == chrom values = self.bsa_results.loc[mask, column].values if len(values) > window_length: smoothed = savgol_filter(values, window_length, polyorder) self.bsa_results.loc[mask, f'{column}_SMOOTH'] = smoothed else: self.bsa_results.loc[mask, f'{column}_SMOOTH'] = values def identify_peaks(self, threshold_percentile=95): """ 识别显著峰值区域 """ print("识别候选区域...") threshold = np.percentile(self.bsa_results['ABS_FREQ_DIFF_SMOOTH'], threshold_percentile) candidate_regions = self.bsa_results[ self.bsa_results['ABS_FREQ_DIFF_SMOOTH'] > threshold ].copy() print(f"识别到 {len(candidate_regions)} 个候选窗口,阈值: {threshold:.3f}") return candidate_regions def plot_manhattan(self, figsize=(15, 8)): """ 绘制曼哈顿图 """ fig, (ax1, ax2) = plt.subplots(2, 1, figsize=figsize, sharex=True) colors = plt.cm.Set3(np.linspace(0, 1, len(self.bsa_results['CHROM'].unique()))) chrom_colors = dict(zip(self.bsa_results['CHROM'].unique(), colors)) chrom_positions = {} x_pos = 0 x_ticks = [] x_labels = [] for chrom in sorted(self.bsa_results['CHROM'].unique()): chrom_data = self.bsa_results[self.bsa_results['CHROM'] == chrom] positions = chrom_data['CENTER'] / 1e6 + x_pos chrom_positions[chrom] = positions x_ticks.append(positions.mean()) x_labels.append(chrom.replace('chr', '')) ax1.scatter(positions, chrom_data['ABS_FREQ_DIFF'], c=[chrom_colors[chrom]], alpha=0.6, s=20) ax1.plot(positions, chrom_data['ABS_FREQ_DIFF_SMOOTH'], color='red', linewidth=2, alpha=0.8) ax2.scatter(positions, chrom_data['MEAN_NEG_LOG10_P'], c=[chrom_colors[chrom]], alpha=0.6, s=20) x_pos = positions.max() + 10 ax1.set_ylabel('绝对频率差异', fontsize=12) ax1.set_title('BSA-seq 分析结果 - 频率差异', fontsize=14, fontweight='bold') ax1.grid(True, alpha=0.3) ax2.set_ylabel('-log₁₀(P值)', fontsize=12) ax2.set_xlabel('染色体位置 (Mb)', fontsize=12) ax2.set_title('统计显著性', fontsize=14, fontweight='bold') ax2.grid(True, alpha=0.3) ax2.set_xticks(x_ticks) ax2.set_xticklabels(x_labels) threshold = np.percentile(self.bsa_results['ABS_FREQ_DIFF_SMOOTH'], 95) ax1.axhline(y=threshold, color='red', linestyle='--', alpha=0.7, label=f'95%阈值 ({threshold:.3f})') ax1.legend() plt.tight_layout() return fig def plot_chromosome_detail(self, chrom, figsize=(12, 8)): """ 绘制单个染色体的详细图 """ chrom_data = self.bsa_results[self.bsa_results['CHROM'] == chrom].copy() chrom_snps = self.snp_data[self.snp_data['CHROM'] == chrom].copy() fig, axes = plt.subplots(3, 1, figsize=figsize, sharex=True) positions_mb = chrom_data['CENTER'] / 1e6 snp_pos_mb = chrom_snps['POS'] / 1e6 axes[0].scatter(snp_pos_mb, chrom_snps['FREQ_DIFF'], alpha=0.5, s=10, color='gray') axes[0].plot(positions_mb, chrom_data['MEAN_FREQ_DIFF'], 'b-', linewidth=2, label='窗口均值') axes[0].set_ylabel('频率差异') axes[0].set_title(f'{chrom} 详细分析') axes[0].grid(True, alpha=0.3) axes[0].legend() axes[1].plot(positions_mb, chrom_data['ABS_FREQ_DIFF'], 'o-', color='green', linewidth=2, markersize=4, label='原始信号') axes[1].plot(positions_mb, chrom_data['ABS_FREQ_DIFF_SMOOTH'], 'r-', linewidth=3, label='平滑信号') axes[1].set_ylabel('绝对频率差异') axes[1].grid(True, alpha=0.3) axes[1].legend() axes[2].scatter(snp_pos_mb, chrom_snps['NEG_LOG10_P'], alpha=0.5, s=10, color='purple') axes[2].plot(positions_mb, chrom_data['MEAN_NEG_LOG10_P'], 'orange', linewidth=2) axes[2].set_ylabel('-log₁₀(P值)') axes[2].set_xlabel('位置 (Mb)') axes[2].grid(True, alpha=0.3) plt.tight_layout() return fig def generate_report(self, output_file='bsa_report.txt'): """ 生成分析报告 """ candidates = self.identify_peaks() with open(output_file, 'w', encoding='utf-8') as f: f.write("BSA-seq 分析报告\n") f.write("=" * 50 + "\n\n") f.write(f"总SNP数量: {len(self.snp_data)}\n") f.write(f"滑动窗口数量: {len(self.bsa_results)}\n") f.write(f"窗口大小: {self.window_size/1e6:.1f} Mb\n") f.write(f"步长: {self.step_size/1e3:.0f} kb\n\n") f.write("候选区域:\n") f.write("-" * 30 + "\n") for idx, row in candidates.iterrows(): f.write(f"染色体: {row['CHROM']}\n") f.write(f"位置: {row['START']/1e6:.2f}-{row['END']/1e6:.2f} Mb\n") f.write(f"频率差异: {row['ABS_FREQ_DIFF']:.3f}\n") f.write(f"显著性: {row['MEAN_NEG_LOG10_P']:.2f}\n") f.write(f"SNP数量: {row['SNP_COUNT']}\n") f.write("-" * 30 + "\n") print(f"报告已保存至: {output_file}") def run_complete_analysis(self): """ 运行完整的BSA分析流程 """ print("开始BSA-seq完整分析流程...") if self.snp_data is None: self.load_vcf_data() self.calculate_allele_frequency() self.statistical_test() self.sliding_window_analysis() self.smooth_signals() manhattan_fig = self.plot_manhattan() plt.show() self.generate_report() print("BSA分析完成!") return self.bsa_results
if __name__ == "__main__": bsa = BSAAnalysis(window_size=1000000, step_size=100000) results = bsa.run_complete_analysis() detail_fig = bsa.plot_chromosome_detail('chr2') plt.show() candidates = bsa.identify_peaks() print("\n候选区域:") print(candidates[['CHROM', 'START', 'END', 'ABS_FREQ_DIFF', 'MEAN_NEG_LOG10_P']].head())
|