对于我的代码的每次迭代,我首先从基因源中找到来自PPI网络的值,这些值在每次运行结束时重新计算。如果基因在A或B类别中发现,我将其完成的分数和基因存储在另一个位置以进行分类和连接以进行进一步测试。搜索过程需要时间,我正在寻找优化它。问题是我已经开始批量处理300多个基因,并且运行计算的时间长达三天。额外信息矩阵是PPI网络内所有相互作用的总计~176,000X3。
慢代码:
#CREATE THE DNS LIST
DNSList = FALSE
DNSListNameHolder = NA
DNSListValueHolder = NA
DNSListHolder = 0
CN = 0
Prev = 0
while(!DNSList)
{
CN = CN + 1
IDNSList = FALSE
CNN = Prev
while(!IDNSList)
{
CNN = CNN + 1
if(as.character(ForDNSList$Gene.A[CNN]) == as.character(Candidate[CN]))
{
DNSListHolder = DNSListHolder + 1
DNSListValueHolder[DNSListHolder] = as.character(ForDNSList$Score[CNN])
DNSListValueHolder[DNSListHolder] = as.numeric(DNSListValueHolder[DNSListHolder])
DNSListNameHolder [DNSListHolder] = as.character(ForDNSList$Gene.B[CNN])
}
if(as.character(ForDNSList$Gene.B[CNN]) == as.character(Candidate[CN]))
{
DNSListHolder = DNSListHolder + 1
DNSListValueHolder[DNSListHolder] = as.character(ForDNSList$Score[CNN])
DNSListValueHolder[DNSListHolder] = as.numeric(DNSListValueHolder[DNSListHolder])
DNSListNameHolder [DNSListHolder] = as.character(ForDNSList$Gene.A[CNN])
}
if(CNN == length(ForDNSList$Gene.A))
IDNSList = TRUE
}
if(CN == length(Candidate))
DNSList = TRUE
print(paste("Pre-DNS List in Progress",CN/length(Candidate), sep = " "))
}
print("Pre-DNS List Completed")
出于示例目的,可以将候选列表设置为此
Candidate = c("BRCA1", "BRCA2", "ATK1", "FYN")
ForDNSList很长,所以这里有一个小摘录,可以了解如何列出外观。如果我正在寻找的基因位于基因列A或B中,它就会越来越随机。
> ForDNSList[1:50, 1:3]
Gene.A Gene.B Score
1 Q96BE0 POLR3A 0.126
2 Q96BE0 PDPK1 0.126
3 Q96BE0 MGEA5 0.126
4 Q96BE0 DNAJA2 0.126
5 Q96BE0 DNAJB6 0.126
6 Q96BE0 BAG4 0.126
7 Q96BE0 HSPA4L 0.126
8 THAP1 A0A024RA76 0.332
9 Q96BE0 BAG2 0.236
10 Q96BE0 BAG3 0.236
11 Q96BE0 EGFR 0.236
12 Q96BE0 MOS 0.126
13 Q96BE0 RAF1 0.126
14 Q96BE0 GABRB1 0.126
15 Q96BE0 GNAZ 0.126
16 MS4A7 HMGCL 0.286
17 Q96BE0 ATP5A1 0.126
18 Q96BE0 DNAJA1 0.126
19 DVL3 PPM1A 0.210
20 Q96BE0 MCM5 0.126
21 Q96BE0 MCM7 0.126
22 Q96BE0 HSPA4 0.126
23 Q96BE0 PSMC2 0.126
24 Q96BE0 GNAL 0.126
25 Q96BE0 AMT 0.126
26 MECP2 SOX18 0.286
27 Q96BE0 CSNK1E 0.126
28 Q96BE0 ST13 0.126
29 CSNK2A1 MYH9 0.454
30 Q96BE0 CDK9 0.126
31 Q96BE0 SEC24C 0.126
32 TUBA4A MYH9 0.081
33 Q96BE0 HSPA2 0.236
34 Q96BE0 PRAME 0.126
35 Q96BE0 FANCC 0.126
36 Q96BE0 HSF2 0.126
37 KDR MYO1C 0.126
38 Q96BE0 HCFC1 0.126
39 Q96BE0 RAD51 0.126
40 KDR FYN 0.210
41 Q96BE0 PSMD2 0.126
42 Q96BE0 SKP2 0.126
43 KDR MET 0.376
44 Q96BE0 IKBKE 0.126
45 Q96BE0 ENDOG 0.126
46 Q96BE0 GNA13 0.126
47 TSG101 EIF3L 0.183
48 Q96BE0 SETDB1 0.126
49 Q96BE0 CDK10 0.126
50 HSP90AB1 TNNI3K 0.126
答案 0 :(得分:0)
感谢上面的建议我删除了一个循环并用两个match()争论替换它。原始代码在第一次迭代中花了大约196秒,而这只需要20.4秒
Nx = 0
DNSList = FALSE
DNSListNameHolder = NA
DNSListValueHolder = NA
DNSListHolder = 0
CN = 0
Prev = 0
system.time(while(Nx < length(ForDNSList$Gene.A))
{
Nx = Nx + 1
#Check if Gene A is a candidate disease gene
if(is.element("TRUE",!is.na(match(Candidate,ForDNSList$Gene.A[Nx]))))
{
#if so push the holder one furter and fill the secondary varaibles with the complement and score info
DNSListHolder = DNSListHolder + 1
DNSListValueHolder[DNSListHolder] = as.character(ForDNSList$Score[CNN])
DNSListValueHolder[DNSListHolder] = as.numeric(DNSListValueHolder[DNSListHolder])
DNSListNameHolder [DNSListHolder] = as.character(ForDNSList$Gene.B[CNN])
}
#Check if Gene B is a candidate disease gene
if(is.element("TRUE",!is.na(match(Candidate,ForDNSList$Gene.B[Nx]))))
{
#if so push the holder one furter and fill the secondary varaibles with the complement and score info
DNSListHolder = DNSListHolder + 1
DNSListValueHolder[DNSListHolder] = as.character(ForDNSList$Score[CNN])
DNSListValueHolder[DNSListHolder] = as.numeric(DNSListValueHolder[DNSListHolder])
DNSListNameHolder [DNSListHolder] = as.character(ForDNSList$Gene.A[CNN])
}
print(Nx)
})
答案 1 :(得分:0)
最好使用apply
函数之一 - 我认为它们已经过优化以使用多处理功能,因此使用更多内核时,使用apply的操作可能会更快。而且,使用函数可能比使用循环更好,因为它更模块化,更容易编码。
这是我自己的代码中的一个示例,显示了&#34;抵抗异常值&#34;的部分实现。 Z-score算法:
rw <- assays(sum_exp)$fpkm
#remove genes that have zero counts
rw <- rw[apply(rw, 1, function(x){return (sum(x)>0)}),]
#
sample_means <- apply(rw, 2, function(x){median(x[x>0])})
z_median <- median(sample_means)
z_mad <- mad(sample_means)
z_scores <- unlist(lapply(sample_means, function(x) {return ((x - z_median)/(z_mad))}))
如果你想概念化它,想想你想在for循环的一次迭代中修改多个元素的可能性,比如实现Fibonacci的循环。 R不能并行优化循环,因为它不能隔离每个行/列/元素。使用apply
,sapply
和lapply
,您可以假设每个行/列/元素将被孤立地计算,因此,可以安全地划分在不同的核心之间工作。