2017-01-12 4 views
0

私は非常に基本的な質問をしていますが、他の場所で尋ねられれば謝ります(私は答えを見つけようとしました、私は本当にしました)。現在のディレクトリにあるすべてのファイルについて、別のディレクトリに同じ接頭辞を持つファイルを探します。 R

私は、2つのディレクトリを作成し、同じ名前の各ディレクトリにファイルの情報を保存するスクリプトを作成しました。あるディレクトリでは、ggplotを使ってボックスプロットを作成するためのデータを作成しました。もう1つのディレクトリには、アノテーション情報を保存しました。私はボックスプロットを作成し、対応するアノテーションファイルのアノテーションディレクトリを検索して、アノテーションをboxplotに追加することができます。コードは、指定されたディレクトリ内の「すべてのファイル」に対してこの操作を実行するように設定されているため、単に作業ディレクトリを変更してアノテーションファイルを名前で読み込むことはできません。ここで私が得たものである:

ggplot/dataというディレクトリでは、ファイルは以下のように保存されます。my_data_1.csv

:ggplot /注釈というディレクトリに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") 
} 
+0

CSVファイルがありますが、標準の '.csv'拡張子を使用するのではなく、独自の' .text_data'拡張子を使用していますか?したがって、データファイルの完全なファイル名は、例えば、「my_data_1.txt_data」ですか? 「my_data_1.txt_data.csv」または「my_data_1.csv」だけではありませんか? – Gregor

+1

もう1つの質問 - 対応する注釈 '.txt_pval' *があると仮定しているようです* *そのファイルが存在しない場合はエラー処理はありませんので、 "あなたは、一致するファイル名が何であるべきかを知っているので、その名前を構築するだけでいいですか?ファイル名の末尾にある ''データ ''を' 'pval'' 'に置き換えるだけでしょうか?このように: 'gsub(pattern =" data $ "、replacement =" pval "、x = inFilePath)'。 – Gregor

+0

あなたの編集後、私はさらに混乱しています。これで、各ディレクトリに同じ名前のファイルがあるように見えるので、2番目のファイルを "見つける"必要はありません。作業ディレクトリを変更して、既に 'read_csv'を使って十分です。 'filePaths2'は必要ありません。もう一度' filePath'を使うだけです。その内側の 'for'ループを削除します - データファイルごとに1つの注釈ファイルがあるので、ネストされたループは必要ありません。 – Gregor

答えて

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 

    } 

感謝。

関連する問題