对于当前目录中的所有文件,请在另一个目录中查找具有相同前缀的文件.R.



我有一个非常基本的问题,如果在其他地方被问到,我深表歉意(我试图找到答案,我真的做到了)。

我编写了一个脚本,该脚本创建了两个目录,并以相同的名称在每个目录中保存给定文件的信息。在一个目录中,我格式化了数据以使用 ggplot 制作箱线图,在另一个目录中,我保存了注释信息。然后我制作了一个箱线图,我想在注释目录中搜索相应的注释文件,以便我可以将注释添加到箱线图。代码设置为对给定目录中的"所有文件"执行此操作,因此我不能简单地更改工作目录并按名称加载注释文件。这是我得到的:

在名为ggplot/data的目录中,文件保存为:my_data_1.csv

在名为 ggplot/annotation 的目录中,文件保存为:my_data_1.csv

最终的注释图形保存在 ggplot/graph_output 中。

# goto ggplot data directory
setwd("/home/path/to/ggplot/data")
#look for all files
inFilePaths = list.files(path=".", pattern=glob2rx("*"), full.names=TRUE)
#make a ggplot2 boxplot for every file with
for (inFilePath in inFilePaths)
{ 
  # Read in each data file as a dataframe
  inFileData = read_csv(inFilePath)
  # Make a ggplot. **This is only part of my code to save space**
  plot1 = ggplot(data =inFileData, mapping= aes(x=Sample, y=Expression)) +
    scale_fill_manual(values=c("#606060", "#29a329"))
  # Change directories to annottaion folder
  setwd("/home/path/to/ggplot/annotation")
  ####Help!!!!#### Write something to find the file with same inFilePath name to get annotations
  ##Maybe something like this:
  inFilePaths2 = list.files(path=".", pattern=glob2rx(inFileData), full.names=TRUE)
  ##This does not work because it cant find the same inFileData file used to make the ggplot
  # annotate gglot with corresponding annotation file
  for (inFilePath in FilePaths2)
  {
    palues = read_csv(...of the file that matches the file name of the ggplot data) 
    plt2_annot <- plot1 +
      geom_text(data=pvalues, aes(x=value, y=breaks,label = paste('P:',format.pval(pval, digits=1))))
  }

  # specify size of ggplot base on number of boxes displayed using total rows of data
  n = 0.25+(0.75*(nrow(unique(select(inFileData, Gene)))))
  # Change directories to graph output folder, and save graph
  setwd("/home/path/to/ggplot/graph_output")
  ggsave(filename = paste(inFilePath, ".png"), plot=plot2, height = 1.5, width = n, units = "in")
}

使用格雷戈尔的评论,我设法提出了一个非常简单的解决方案。

1)我更改了在每个目录中命名文件的方式,因此数据文件和相应的注释文件具有完全相同的名称。

2)与其尝试实现一些功能来查找当前inFilePath数据文件的相应注释文件,不如简单地将目录更改为注释目录并使用read_csv(inFilePath)重新加载inFilePath,从而加载相应的注释文件。

这是最终为我工作的代码:

# goto ggplot data directory
setwd("/home/path/to/ggplot/data")
#look for all files
inFilePaths = list.files(path=".", pattern=glob2rx("*"), full.names=TRUE)
#make a ggplot2 boxplot for every data file
for (inFilePath in inFilePaths)
{ 
  #Need to set directory again due to directory change lower in the loop
  setwd("/home/path/to/ggplot/data")
  # Read in each data file as a dataframe
  inFileData = read_csv(inFilePath)
  #check to see which data is loaded
  print(inFileData)
  #make a ggplot from the ggplot data
  # Change directories to annotation folder
  setwd("/home/path/to/ggplot/annotation")
  #load new annotation data. The file names are the same, so loading the same file name in the annotations
  # directory actually loads the annotations for the corresponding plot
  inFileData2 = read_csv(inFilePath)
  #check to make sure the correct annotation file is loaded
  print(inFileData2)
  #add annotation to ggplot graph
  #now that I can access the correct annotation, I'll work on this part next.
  #then save the graph
  }

感谢您的帮助。

最新更新