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

时间:2017-01-12 20:56:47

标签: r

我有一个非常基本的问题,如果在其他地方被问到我会道歉(我试图找到答案,我确实这样做了。)

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

在名为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 个答案:

答案 0 :(得分:1)

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

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

  }

感谢您的帮助。