Rで解析しようとしているこのデータセットがあります.HMDBからのデータとデータセット名はSerum Metabolites
(xmlファイルの形式で)です。xml ファイルには約 25,000 の代謝物ノードが含まれており、それぞれをサブノードに解析したい
XML ファイルを R のリスト オブジェクトに解析するコードがあります。XML ファイルは非常に大きく、各代謝物に必要なサブノードが約 12 あるため、ファイルの解析に長い時間がかかります。約 3 時間で 1,000 の代謝物になります。パッケージを使用しようとしていますparallel
が、受信してエラーになります。
パッケージ:
library("XML")
library("xml2")
library( "magrittr" ) #for pipe operator %>%
library("pbapply") # to track on progress
library("parallel")
関数:
# The function receives an XML file (its location) and returns a list of nodes
Short_Parser_HMDB <- function(xml.file_location){
start.time<- Sys.time()
# Read as xml file
doc <- read_xml( xml.file_location )
#get metabolite nodes (only first three used in this sample)
met.nodes <- xml_find_all( doc, ".//d1:metabolite" ) [1:1000] # [(i*1000+1):(1000*i+1000)] # [1:3]
#list of data.frame
xpath_child.v <- c( "./d1:accession",
"./d1:name" ,
"./d1:description",
"./d1:synonyms/d1:synonym" ,
"./d1:chemical_formula" ,
"./d1:smiles" ,
"./d1:inchikey" ,
"./d1:biological_properties/d1:pathways/d1:pathway/d1:name" ,
"./d1:diseases/d1:disease/d1:name" ,
"./d1:diseases/d1:disease/d1:references",
"./d1:kegg_id" ,
"./d1:meta_cyc_id"
)
child.names.v <- c( "accession",
"name" ,
"description" ,
"synonyms" ,
"chemical_formula" ,
"smiles" ,
"inchikey" ,
"pathways_names" ,
"diseases_name",
"references",
"kegg_id" ,
"meta_cyc_id"
)
#first, loop over the met.nodes
L.sec_acc <- parLapply(cl, met.nodes, function(x) { # pblapply to track progress or lapply but slows down dramticlly the function and parLapply fo parallel
#second, loop over the xpath desired child-nodes
temp <- parLapply(cl, xpath_child.v, function(y) {
xml_find_all(x, y ) %>% xml_text(trim = T) %>% data.frame( value = .)
})
#set their names
names(temp) = child.names.v
return(temp)
})
end.time<- Sys.time()
total.time<- end.time-start.time
print(total.time)
return(L.sec_acc )
}
次に、環境を作成します。
# select the location where the XML file is
location= "D:/path/to/file//HMDB/DataSets/serum_metabolites/serum_metabolites.xml"
cl <-makeCluster(detectCores(), type="PSOCK")
clusterExport(cl, c("Short_Parser_HMDB", "cl"))
clusterEvalQ(cl,{library("parallel")
library("magrittr")
library("XML")
library("xml2")
})
そして実行します:
Short_outp<-Short_Parser_HMDB(location)
stopCluster(cl)
受け取ったエラー:
> Short_outp<-Short_Parser_HMDB(location)
Error in checkForRemoteErrors(val) :
one node produced an error: invalid connection
これらのリンクに基づいて、並列を実装しようとしました:
- R での並列処理
- 関数からグローバル関数を呼び出す方法は
parLapply
? - R parallel のエラー: checkForRemoteErrors(val) のエラー: 2 つのノードでエラーが発生しました。最初のエラー: 接続を開けません
invalid connection
しかし、エラーとして見つかりませんでした
Windows 10 で最新の R バージョン 4.0.2 を使用しています (十分な情報かどうかはわかりません)。
ヒントやアイデアをいただければ幸いです