【问题标题】:For all files in current directory, find file with same prefix in another directory. R对于当前目录中的所有文件,在另一个目录中查找具有相同前缀的文件。 R
【发布时间】:2017-01-13 08:15:26
【问题描述】:

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

我编写了一个脚本,该脚本创建了两个目录,并将给定文件的信息保存在每个目录中的相同名称下。在一个目录中,我使用 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")
}

【问题讨论】:

  • 为了清楚起见,您有 CSV 文件,但不是使用标准的 .csv 扩展名,而是使用自己的 .text_data 扩展名?所以数据文件的全名是,例如,my_data_1.txt_data?而不是my_data_1.txt_data.csv 或只是my_data_1.csv
  • 另一个问题 - 你似乎假设相应的注释.txt_pval 在那里 - 如果该文件不存在,你没有任何错误处理 - 所以它是与其说你必须“找到一个匹配”,因为你知道匹配的文件名应该是什么,你只需要构造那个名字?将文件名末尾的"data" 替换为"pval" 工作吗?像这样:gsub(pattern = "data$", replacement = "pval", x = inFilePath).
  • 经过您的编辑,我更加困惑。现在看起来每个目录中的文件都具有相同的名称,因此您根本不需要“查找”第二个文件 - 更改工作目录并使用 read_csv 就足够了。你不需要filePaths2,你只需要再次使用filePath。并删除该内部 for 循环 - 每个数据文件都有一个注释文件,因此您不需要任何嵌套循环。
  • 关于文件扩展名全名的要点。我把所有的文件扩展名都设置成一样了,这帮我生成了注解目录下所有文件的列表,但是我还是无法从数据目录中选择与当前inFilePath同名的文件。
  • 是的! Gregor,您的最后一条评论是关键。这很可能是解决方案。今晚回家后我会尝试并发布答案。我知道我错过了一些简单的事情。感谢您的帮助。

标签: r


【解决方案1】:

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

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

  }

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2023-03-16
    • 2011-07-05
    相关资源
    最近更新 更多