MacOS 12.0以后的系統(tǒng)更改了blas的位置护锤,具體去哪了現(xiàn)在不清楚官地,目前的辦法是brew安裝openblas,然后把openblas軟連接過去
問題與思考
在寫OneStepWGCNA插件的時(shí)候我就遇到過一個(gè)奇怪的現(xiàn)象烙懦,在Rstudio下計(jì)算TOM矩陣那一步很快驱入,8000個(gè)基因基本是幾十秒算完,但是一旦打包成插件氯析,或者用Rscript跑腳本的時(shí)候亏较,那一步會(huì)巨慢無比,8000個(gè)要等好幾分鐘掩缓。
當(dāng)時(shí)也不知道怎么回事雪情,就先放著,最近正在做WGCNA拾因,想用插件跑跑旺罢,發(fā)現(xiàn)實(shí)在是太慢了!>罴恰1獯铩!然后就開始研究怎么回事蠢熄,知道我看到terminal下卡主的步驟:
TOM calculation: adjacency..
..will not use multithreading.
Fraction of slow calculations: 0.000000
..connectivity..
..matrix multiplication (system BLAS)..
BLAS 之前在WGCNA-FAQ中好像看到過這么個(gè)名詞跪解,于是就回去查看了一下文件。果然签孔,當(dāng)時(shí)沒搞懂的叉讥,現(xiàn)在一下明朗了:
1 How can I make network construction execute faster?
When constructing a network from a data set of a typical genomic size (i.e., between 10 000 and 30 000 genes or other variables), the most time consuming step is the calculation of Topological Overlap Matrix which involves multiplying matrices with tens of thousands of rows and columns. With a standard R distribution, this may take multiple hours even on a modern workstation since matrix multiplication in standard R does not take advantage of multi-threading (parallel execution). It is possible to speed up this process by a factor of 10-100 by installing a speed-optimized Basic Linear Algebra Subprograms (BLAS) library and compiling R against it. The process of compiling R against an enhanced BLAS library is described in the R installation and adminitration manual. Compiling R on Linux and Unix flavors is usually relatively simple and straightforward. On Mac OSX and (more so) on Windows it requires installing additional tools and packages. Although it is helpful to have administrator privileges to compile and install R, it is usually not necessary. See the R installation and adminitration manual for full details.
作者大致意思是如果你想提高R的矩陣運(yùn)算能力,需要使用強(qiáng)化版的BLAS饥追,R默認(rèn)調(diào)用的是基礎(chǔ)班的BLAS图仓,強(qiáng)化版的要比基礎(chǔ)版的快10-100倍。但是這玩意不好裝但绕,需要從源碼編譯救崔,linux下簡(jiǎn)單,但是win和mac下就比較蛋疼了捏顺。
But六孵! 我發(fā)現(xiàn)在win和mac下其實(shí)也沒有那么難,雖然我也不懂什么BLAS幅骄,但是作為一個(gè)調(diào)包俠劫窒,最基礎(chǔ)也是最重要的一個(gè)技能就是,善用各種搜索拆座。不斷的google下終于發(fā)下了mac和win下的解決方案主巍,不需要編譯冠息,mac只需要敲兩行命令,win復(fù)制幾個(gè)文件就搞定了.
先發(fā)參考鏈接:
Faster matrix math in R on macOS
Windows使用OpenBLAS加速R語言計(jì)算速度
方法
MacOS big sur 11.2.1測(cè)試成功
其實(shí)就是蘋果系統(tǒng)下煤禽,調(diào)用蘋果自己的BLAS铐达,具體:Apple’s implementation of the Basic Linear Algebra Subprograms (BLAS). 參考上面大胡子老哥的操作岖赋,就是把蘋果自己的BLAS文庫替換掉R默認(rèn)的BLAS
## TBtools的R安裝在Rserver下檬果,所以TBtoolsR的lib位置如下,先進(jìn)入這個(gè)路徑
cd ~/.TBtools/.Plugin/Rserver/bin/macR/lib
## 將系統(tǒng)Apple的BLAS軟連接過來
ln -sf /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib libRblas.dylib
如果這個(gè)操作過后發(fā)現(xiàn)你的Rserver掛了唐断,用下面的命令恢復(fù):
ln -sf libRblas.0.dylib libRblas.dylib
雖然你測(cè)試sessionInfo()
發(fā)現(xiàn)沒有和帖子里說的一樣出現(xiàn)下面提示选脊,但是確實(shí)是快了!反正我的Nuc8是直接風(fēng)扇起飛了....感覺至少快了30倍脸甘,當(dāng)然隨著基因數(shù)目的增多恳啥,這個(gè)倍數(shù)也會(huì)翻倍....
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
Win10下測(cè)試成功
win10下用的是openBLAS,具體請(qǐng)看Github-openBLAS
原貼實(shí)在豆瓣看到的:讓R在windows下開足馬力進(jìn)行計(jì)算 后來CSDN那個(gè)老哥都給下載整理好了丹诀,直接拿來用钝的,剛開始擔(dān)心版本問題,后來看來是想多了铆遭,直接把帖子中作者整理好的網(wǎng)盤里的文件替換掉:在操作之前先把x64文件夾下的文件備份一下
## 按順序打開:
我的電腦 -> c盤 -> 用戶 -> 你的用戶名對(duì)應(yīng)的文件夾 -> .TBtools -> .Plugin -> Rserver -> bin -> winR -> bin -> x64(32的對(duì)應(yīng)打開32) -> 粘貼
然后就搞定了硝桩,再試試OneStepWGCNA吧,速度嗖嗖的枚荣!