“CFINDER”输出数据导入及对均值计算

“CFINDER” output data importation and calculation of mean of pairs

我遇到一个问题:我必须导入一个文本文件(from_soft.txt,它是 "CFINDER" 软件的输出)。它看起来像这样:

from_soft:
# text-text-text-text-text-text-text-text-text-text-
# text-text-text-text-text-text-text-text-text-text-
# text-text-text-text-text-text-text-text-text-text-
# text-text-text-text-text-text-text-text-text-text-

1: SpeciesA SpeciesB SpeciesC SpeciesD SpeciesE
2: SpeciesA SpeciesC SpeciesE SpeciesD SpeciesF SpeciesG SpeciesH
3: SpeciesB SpeciesC SpeciesF
4: SpeciesB SpeciesC SpeciesD SpeciesF SpeciesH
[...]

我设法用 readLines:

将它导入 R
cliques<-readLines("from_soft.txt",n=-1,ok=T,warn=T,encoding="unknow",skipNul=F)

然后我将第一行删除到:

from_soft<-as.data.frame(from_soft)[-(1:5),]

tablefrom_soft是这样的:

head(from_soft)
[1] 0: SpeciesA SpeciesB SpeciesC SpeciesD SpeciesE   1: SpeciesA SpeciesB SpeciesC SpeciesD SpeciesE   2: SpeciesA SpeciesB SpeciesC SpeciesD SpeciesE  
[4] 3: SpeciesB SpeciesC SpeciesD SpeciesF SpeciesH [...]

在另一边,我有一个 table ref 表示每对物种的 'value'。它看起来像这样:

print(ref)
3324   SpeciesA  SpeciesB       1
3325   SpeciesA  SpeciesC       2
3326   SpeciesA  SpeciesD      12
3327   SpeciesA  SpeciesE       1
3328   SpeciesA  SpeciesF      71
3329   SpeciesA  SpeciesG       6
3330   SpeciesA  SpeciesH      15
3331   SpeciesB  SpeciesC       2
3332   SpeciesB  SpeciesF       4
3333   SpeciesB  SpeciesD      17
[...]

from_soft的每一行对应一个图表中的'clique'。这意味着每个物种都是相互关联的。我想为每一行计算 'mean connection'.

作为第 1: 行的示例,这里是所有现有的对:

1: SpeciesASpeciesB|SpeciesASpeciesC|SpeciesASpeciesD|SpeciesASpeciesE|SpeciesBSpeciesC|SpeciesBSpeciesD|SpeciesBSpeciesE|SpeciesCSpeciesD|SpeciesCSpeciesE|SpeciesDSpeciesE|

每个现有对都有一个在 ref 中给出的值。我想要的输出文件是这样的 table:

1: 3.5 (= mean of all pairs in the clique '1:')
2: 4.2
3: 1.5
4: 6
[...]

有什么想法可以实现吗?

解决方案:

paires <- interaction(ref[,2:3] , drop = T)
rl <- readLines("from_soft.txt")
rl <- rl[-(1:5)]
l <- strsplit(rl , " ")
l <- lapply(l , tail , -1)
comb <- lapply(l , function(x) apply(combn(x , 2) , 2 , paste , collapse = "."))
sapply(comb , function(x) mean(ref$V4[match(x , paires)]))`

干杯,

R.