【deeptools相關(guān)】讀取Excel文件,畫樣本相關(guān)性散點(diǎn)圖

Tips:

https://deeptools.readthedocs.io/en/develop/content/tools/plotCorrelation.html
我們比較樣本的重復(fù)性,一般都是用deeptoolsplotCorrelation 畫散點(diǎn)圖。比如下面類似的命令.

### Step1 multiBamSummary
$ multiBamSummary bins 
--bamfiles *.uniq.bam 
--minMappingQuality 30 
-out scores_per_transcript.npz 
--outRawCount scores_per_transcript.tab

### Step2 plotCorrelation
$ deepTools2.0/bin/plotCorrelation \
-in scores_per_transcript.npz \
--corMethod pearson --skipZeros \
--plotTitle "Pearson Correlation of Average Scores Per Transcript" \
--whatToPlot scatterplot \
-o scatterplot_PearsonCorr_bigwigScores.png   \
--outFileCorMatrix PearsonCorr_bigwigScores.tab

得到圖:


image.png

假設(shè)只有tab 文件,但plotCorrelation 只能識(shí)別npz 格式(numpy存儲(chǔ)的格式。類似R里面.RData 一樣,存儲(chǔ)特定數(shù)據(jù)結(jié)構(gòu)),要怎么才可以畫圖?
也就是說tab 格式如何轉(zhuǎn)化成npz 格式呢?

查看npz 里面是什么東西 讀取npz 的方法:菜鳥教程

image.png

類似字典,有l(wèi)abels 和matrix 兩個(gè)鍵。我們將tab整理成這個(gè)格式就可以了。

實(shí)踐:

1. 下載文章數(shù)據(jù) 41556_2019_383_MOESM9_ESM.xlsx ,發(fā)現(xiàn)數(shù)據(jù)都是excel 格式保存的。如下圖:
image.png

需要讀取excel 的sheet 里面的數(shù)據(jù),保存為fig1b_scatterplot.csv。
R代碼:

########################
rm(list=ls())
options(warn = -1)
options(stringsAsFactors = F)
setwd("C:/Users/16926/Desktop/2020-1/單細(xì)胞/sc-ChIP")

##############
## 1.加載包,讀取xlsx(最新格式)
## 參考:
## 1.不同R包比較 http://www.itdecent.cn/p/5ed6e4b5d181
## 2.openxlsx::read.xlsx 參數(shù)介紹 :https://blog.csdn.net/zyunnketsu/article/details/78053179
##############
library(openxlsx)
a<-read.xlsx("41556_2019_383_MOESM9_ESM.xlsx",
             sheet=1,startRow = 14)#文件名+sheet的序號(hào),簡單粗暴
head(a)
s1 <- object.size(a)
print(sl, units = "auto", standard = "SI") 

write.csv(a,file = "./fig1/fig1b_scatterplot.csv",row.names = F)

提取到fig1b_scatterplot.csv 內(nèi)容如下:


image.png
2.將fig1b_scatterplot.csv 轉(zhuǎn)換成npz 格式
####################
## time:2020/3/23 10:27
## author: caokai
## email:1692679247@qq.com
## aim : convert csv format to npz format to use deeptools software
####################

####################
## 1.加載模塊
import os
import numpy as np
import pandas as pd
os.chdir("C:/Users/16926/Desktop/2020-1/單細(xì)胞/sc-ChIP/fig1")
os.listdir(".")


####################
## 2.讀取及其保存格式
## 參考:np:https://www.runoob.com/numpy/numpy-io.html
## pd: https://blog.csdn.net/xidianbaby/article/details/88831145
## mian:https://blog.csdn.net/yuzhihuan1224/article/details/99634355?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

dat = pd.read_csv("fig1b_scatterplot.csv")



labels = np.array(dat.columns[3:], dtype="|S22")  # 去除前三列,labels轉(zhuǎn)換成np np.array([bytes(c, encoding='utf8') for c in dat.columns])
matrix = dat.iloc[:,3:].values   # 去除前三列,value轉(zhuǎn)換成np np.array(dat[dat.columns[3:]])
## 計(jì)算log10(FPM)
matrix = np.log10(matrix+1)


## 保存為npz 格式
np.savez(r"fig1.scatterplot.npz",

         labels = labels,
         matrix = matrix)

data_z = np.load("fig1.scatterplot.npz")
print(data_z.files) # 查看各個(gè)數(shù)組名稱
print(data_z["labels"]) # 數(shù)組 labels
print(data_z["matrix"]) # 數(shù)組 matrix

所以我們得到了fig1.scatterplot.npz 數(shù)據(jù),可以直接畫圖了。

3.deeptools 畫圖
plotCorrelation   -in fig1.scatterplot.npz 
--corMethod pearson 
--skipZeros --plotTitle 'Pearson Correlation' 
--whatToPlot scatterplot 
--removeOutliers 
-o fig1_scatterplot_PearsonCorr.png 
--outFileCorMatrix fig1_scatterplot_PearsonCorr.tab

散點(diǎn)圖如下圖:


2020年3月23日15:20:23

總結(jié)思考:

  • 讀取Excel 的包推薦openxlsx
  • 了解npynpz 差別,及其numpy修改數(shù)據(jù)格式的方式。
    np.array(dat.columns[3:], dtype="|S22"),將字符串轉(zhuǎn)換成bite 類型。
  • deeptoools 無法對數(shù)據(jù)進(jìn)行log10 scale(只提供--log1p:自然對數(shù)e
    顏色deeptools --colorMap參數(shù),畫散點(diǎn)圖沒有用.

歡迎大家討論~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容