python計(jì)算RPKM操作示例詳解
操作
np.rot90(df, 1) #矩陣,逆時(shí)針,90度 np.rot90(df, -1) #矩陣,順時(shí)針,90度
矩陣/數(shù)組,使用循環(huán)速度特慢
#!/usr/bin python3
# -*- coding: UTF-8 -*-
import os,sys,re
import pandas as pd
df=pd.read_csv("final.head", index_col = 0, header = 0, sep = "\t")
length=pd.read_csv("final.length", index_col = 0, header = 0, sep = "\t")
for i in range(len(df.index)):
for j in range(len(df.columns)):
df.iloc[i,j]=df.iloc[i,j]/length.iloc[i,0]
df.to_csv('final.norm', sep='\t', index = True)
numpy不用循環(huán)實(shí)現(xiàn)矩陣除數(shù)組,類似R語(yǔ)言(按行除),
即每一行列數(shù)組中每一個(gè)個(gè)數(shù),按列除array[:,None]將數(shù)組豎著排實(shí)現(xiàn)按行除或用np.rot(df,)旋轉(zhuǎn)矩陣,不推薦,容易轉(zhuǎn)暈
#!/usr/bin/env python3 import os,re,sys import numpy as np ms, df, length, outfile = sys.argv df = np.loadtxt(df, dtype=np.int32, delimiter='\t') length = np.loadtxt(length, dtype=np.int32) out=df/length[:,None] # [:,None]將數(shù)組豎著排 np.savetxt(outfile,out,fmt='%.8f',delimiter='\t') #np.savetxt(outfile,out,fmt='%.20f',delimiter='\t') # 小數(shù)點(diǎn)后保留20位

計(jì)算rpkm
#!/usr/bin/env python3 import os,re,sys import numpy as np ms, df, length, mapped_reads, outfile = sys.argv # df[gene,sample] df = np.loadtxt(df, delimiter='\t') length = np.loadtxt(length) mapped_reads = np.loadtxt(mapped_reads) #np.dtype=int32 # 這是整形32位,不適用 out=1e3*1e6*df/(length[:,None]*mapped_reads) #使用旋轉(zhuǎn)矩陣法,過(guò)于復(fù)雜不推薦 #out=1e3*1e6*np.rot90((np.rot90(df,1)/length),-1)/mapped_reads np.savetxt(outfile,out,fmt='%.8f',delimiter='\t')
行列求和
#!/usr/bin python
# -*- coding: UTF-8 -*-
import pandas as pd
#t=[[1,2,3],[4,5,6],[7,8,9]]
#df=pd.DataFrame(t) # List轉(zhuǎn)為dataframe
df=pd.read_csv("test.df", index_col = 0, header = 0, sep = "\t")
total=df.apply(lambda x: x.sum())
total.to_csv('test.total', sep='\t', index = True)
#df['Row_sum'] = df.apply(lambda x: x.sum(),axis=1) # 按行求和,添加為新列
#df.loc['Col_sum'] = df.apply(lambda x: x.sum()) # 各列求和,添加新的行以上就是python計(jì)算RPKM操作示例詳解的詳細(xì)內(nèi)容,更多關(guān)于python計(jì)算RPKM的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計(jì)數(shù))
Counter 是一個(gè)簡(jiǎn)單的計(jì)數(shù)器,用于統(tǒng)計(jì)某些可哈希對(duì)象的數(shù)量。它以字典的形式存儲(chǔ)元素和它們的計(jì)數(shù),這篇文章主要介紹了Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計(jì)數(shù)),需要的朋友可以參考下2023-04-04
Python利用PyMobileDevice3控制iOS設(shè)備的完整教程
PyMobileDevice3是一個(gè)純Python 3實(shí)現(xiàn)的iOS設(shè)備控制工具庫(kù),為開(kāi)發(fā)者和技術(shù)愛(ài)好者提供了強(qiáng)大的iOS設(shè)備管理能力,所以本文給大家介紹了Python利用PyMobileDevice3控制iOS設(shè)備的完整教程,需要的朋友可以參考下2025-12-12
python爬蟲(chóng)入門教程之糗百圖片爬蟲(chóng)代碼分享
這篇文章主要介紹了python爬蟲(chóng)入門教程之糗百圖片爬蟲(chóng)代碼分享,本文以抓取糗事百科內(nèi)涵圖為需求寫了一個(gè)爬蟲(chóng),,需要的朋友可以參考下2014-09-09
一篇文章搞懂python混亂的切換操作與優(yōu)雅的推導(dǎo)式
這篇文章主要給大家介紹了如何通過(guò)一篇文章搞懂python混亂的切換操作與優(yōu)雅的推導(dǎo)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08
python的構(gòu)建工具setup.py的方法使用示例
本篇文章主要介紹了python的構(gòu)建工具setup.py的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10

