imputation前後のlogistic解析/線形解析のplot

library(qqman)

# 解析・プロット用の一括関数
process_and_plot <- function(file_path) {
  # 1. 読み込み
  df <- read.table(file_path, header = TRUE, sep = "\t", 
                   comment.char = "", stringsAsFactors = FALSE, check.names = FALSE)
  
  # 2. 列名の変更
  colnames(df)[colnames(df) == "#CHROM"] <- "CHR"
  colnames(df)[colnames(df) == "POS"]    <- "BP"
  colnames(df)[colnames(df) == "ID"]     <- "SNP"
  
  # 3. 『P』が”NA”,”0”を除外
  df <- df[!is.na(df$P) & df$P > 0 & df$P <= 1, ]
  
  # 4. データでかいので縮小(P≧0.01の10%だけを抽出)
  p_small <- subset(df, P < 0.01)
  p_large <- subset(df, P >= 0.01)
  p_large_thinned <- p_large[sample(nrow(p_large), nrow(p_large) * 0.1), ]
  plot_data <- rbind(p_small, p_large_thinned)
  
  # 5. highlightするSNP
  snps_to_highlight <- plot_data$SNP[plot_data$BP %in% c(1866413, 3362536)]
  
  # 6. プロット(タイトルにファイル名を表示)
  manhattan(plot_data, highlight = snps_to_highlight, main = basename(file_path))

}

# file_pathのリスト
file_list <- c(
  "/Users/iden/2025_kadai/1kgeas.PHENO1.glm.firth",
  "/Users/iden/2025_kadai/allchr_imputed_chr1_analysis.B1.glm.firth",
  "/Users/iden/2025_kadai/1kgeas_qt.PHENO1.glm.linear",
  "/Users/iden/2025_kadai/allchr_imputed_chr1_analysis_qt.B1.glm.linear"
)

# plot作成を一括実行
lapply(file_list, process_and_plot)

## [[1]]
## [[1]]$xpd
## [1] FALSE
## 
## 
## [[2]]
## [[2]]$xpd
## [1] FALSE
## 
## 
## [[3]]
## [[3]]$xpd
## [1] FALSE
## 
## 
## [[4]]
## [[4]]$xpd
## [1] FALSE