【发布时间】: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