Home > Back-end >  For loop with if statement and for loop not working
For loop with if statement and for loop not working

Time:06-07

I am trying to get the last for-loop in my R script to produce four cluster plots for each loop iteration for i if a condition is met. What am I doing wrong on this last for-loop? Currently it completes the first iteration and stops. I think I need to include i in the last loop, but not seeing how to do this.

## 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()

CodePudding user response:

Try changing:

for(a in seq(1:4))

to

for(a in seq_along(head(clustersList,4)))

This will limit a to either the number of elements of clustersList or 4, which ever is smaller, and will skip over the for loop completely if clustersList is empty.

  • Related