如果滿足條件,我試圖在我的 R 腳本中獲取最后一個 for 回圈,以便為 i 的每個回圈迭代生成四個聚類圖。我在最后一個 for 回圈中做錯了什么?目前它完成了第一次迭代并停止。我想我需要在最后一個回圈中包含 i ,但沒有看到如何做到這一點。
## Libraries to load
# Assumes dependencies are installed via install.packages() or BiocManager::install()
# openxlsx package will read in excel file
# STRINGdb package is used for pathway analysis
library('readxl')
library(STRINGdb)
## set path
setwd("DEtables")
## Path to CSV file
# Needs to be modified for each pj
filenames = list.files(path = ".", pattern="*.csv", full.names = FALSE)
## Load in String for pathways
string_db <- STRINGdb$new( version="11.5", species=9606, score_threshold=200, input_directory="")
## pathway Analysis
for (i in filenames) {
CSVFile <-read.csv(i, na.strings=c("","NA"), header=TRUE)
# Map data and get hits
example1_mapped <- string_db$map(CSVFile, "genes", removeUnmappedRows = TRUE )
hits <- example1_mapped$STRING_id[1:200]
# filter by p-value and add a color column
# (i.e. green down-regulated gened and red for up-regulated genes)
example1_mapped_pval05 <- string_db$add_diff_exp_color(subset(example1_mapped, FDR<0.05), logFcColStr="logFC" )
if(length(example1_mapped_pval05$STRING_id) > 0){
# post payload information to the STRING server
payload_id <- string_db$post_payload(example1_mapped_pval05$STRING_id, colors=example1_mapped_pval05$color )
# create an empty PNG file to plot hires image
png(file= paste0("../PathwayOuts/",sub('\\.csv$','',i),"Pathways.png"), width = 4, height = 4, units = 'in', res=600)
par(mar=c(3,3,3,3))
# display a STRING network png with the "halo"
string_db$plot_network( hits, payload_id=payload_id )
## Compute enrichment in GO annotations
# Needs to be modified for each pj
enrichmentGO<- string_db$get_enrichment(hits, category = "Process")
enrichmentKEGG<- string_db$get_enrichment(hits, category = "KEGG")
write.csv(enrichmentGO, file = paste0("../PathwayOuts/",sub('\\.csv$','',i),"EnrichMentGO.csv"), row.names=FALSE)
write.csv(enrichmentKEGG, file = paste0("../PathwayOuts/",sub('\\.csv$','',i),"EnrichMentKEGG.csv"), row.names=FALSE)
## get clusters
clustersList = string_db$get_clusters(example1_mapped$STRING_id[1:600])
# plot first 4 clusters
# Needs to be modified for each pj
for(a in seq(1:4)){
png(file= paste0("../PathwayOuts/",sub('\\.csv$','',i),"Cluster",a,".png"), width = 4, height =4, units = 'in', res=600)
string_db$plot_network(clustersList[[a]], payload_id=payload_id)
}
}
}
dev.off()
graphics.off()
uj5u.com熱心網友回復:
嘗試改變:
for(a in seq(1:4))
至
for(a in seq_along(head(clustersList,4)))
這將限制a為clustersList或 4 的元素數量,以較小者為準,如果clustersList為空,則將完全跳過 for 回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487215.html
上一篇:預期運算元(錯誤標記是“/ur/sbin/nologin>=1000”)
下一篇:獲取R中所有列的行之間的差異
