微生物組數(shù)據(jù)的典型格式
大多數(shù)處理高通量擴(kuò)增子數(shù)據(jù)的管道,例如mothur,QIIME和dada2娃胆,都會(huì)形成讀取計(jì)數(shù)矩陣。該矩陣的一維(即行或列)由操作分類單位(OTU)快集,系統(tǒng)型或精確序列變體(ESV)(通過各種方式“組合”相似的讀取序列)組成护戳。另一個(gè)維度包括樣本艺普。不同的工具將期望/輸出矩陣的不同方向棘钞,但是在我們的例子中,列是樣本拆火,行是OTU跳夭。有時(shí)是OTU數(shù)據(jù)和豐度矩陣是兩個(gè)單獨(dú)的表。通常會(huì)有另一個(gè)表们镜,其中的行有示例信息币叹。這樣可以輕松添加許多其他示例數(shù)據(jù)列,這些列可用于子集數(shù)據(jù)模狭。每個(gè)樣本和OTU都有唯一的ID颈抚。
將數(shù)據(jù)導(dǎo)入R
如果數(shù)據(jù)格式正確,則將數(shù)據(jù)導(dǎo)入R可能非常容易嚼鹉,但否則可能會(huì)非常令人沮喪贩汉。格式良好的數(shù)據(jù)的示例是.csv(逗號(hào)分隔值)或.tsv(制表符分隔值)文件,每個(gè)文件都有一個(gè)表锚赤,沒有其他注釋或格式(例如匹舞,合并的單元格)。這兩種格式都可能具有.txt擴(kuò)展名(擴(kuò)展名實(shí)際上并不重要线脚;它適用于人類赐稽,而不是計(jì)算機(jī))。有關(guān)正確的數(shù)據(jù)格式的詳細(xì)信息浑侥,請參閱該數(shù)據(jù)在格式化部分我們對reporducible研究指南姊舵。您應(yīng)該始終盡可能地導(dǎo)入原始輸出數(shù)據(jù),并避免對數(shù)據(jù)進(jìn)行任何“手動(dòng)”(即非腳本化)修改寓落,尤其是在Excel等程序中蠢莺,該程序會(huì)不時(shí)地處理數(shù)據(jù)(Zeeberg等人(2004年) ))。
在整個(gè)研討會(huì)中零如,我們將使用Wagner等人的數(shù)據(jù)。(2016)锄弱,一項(xiàng)研究植物年齡考蕾,基因型和環(huán)境對芥菜家族多年生草本植物Boechera stricta細(xì)菌微生物組的影響的研究。
Wagner等会宪。(2016)發(fā)布了他們的原始數(shù)據(jù)與文章肖卧,它可在這里的樹妖。這是如何共享原始數(shù)據(jù)的絕佳示例掸鹅!
通常有許多函數(shù)用于讀取表格數(shù)據(jù)塞帐,包括Rread.table和R等基本Rread.csv函數(shù)拦赠,但我們將使用新readr程序包中的函數(shù),該函數(shù)返回小滴而不是data.frames(R中的“表”)葵姥。
library(readr)# Loads the readr package so we can use `read_tsv`(加載readr? 包荷鼠,可使用read_tsv )
小貼士是一種data.frame打印效果更好,行為更一致的類型榔幸。單擊此處下載OTU表允乐。讓我們首先讀取原始OTU表:
otu_data <- read_tsv("data/otuTable97.txt.bz2")# You might need to change the path to the file
print(otu_data)# You can also enter just `otu_data` to print it
### A tibble: 47,806 x 1,699## OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38 M1551P90 M1551P71
##? ? <int>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>##? 1? ? ? 1? ? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0? ? ? ? 0
##? 2? ? ? 2? ? ? ? 41? ? ? 22? ? ? ? 4? ? ? 726? 112492? ? ? ? 2? ? ? 413? ? ? ? 2? ? ? ? 1
##? 3? ? ? 3? ? ? ? 67? ? ? 65? ? ? 12? ? 13514? ? ? ? 1? ? ? ? 4? ? 13314? ? ? 70? ? 5929
##? 4? ? ? 4? ? ? 3229? ? 13679? ? 1832? ? ? 951? ? ? 113? ? 2496? ? ? 567? ? 2428? ? 2156
##? 5? ? ? 5? ? ? 1200? ? ? 92? ? 3530? ? 2008? ? ? ? 0? ? ? 183? ? 2087? ? ? 292? ? 1058
##? 6? ? ? 6? ? ? ? 219? ? 1980? ? 1200? ? ? 499? ? ? ? 1? ? ? 781? ? ? 214? ? 2273? ? ? 171
##? 7? ? ? 7? ? ? ? 485? ? 5123? ? ? 755? ? 4080? ? ? 443? ? 1278? ? 2193? ? ? 401? ? 5320
##? 8? ? ? 8? ? ? ? 840? ? 7079? ? 3760? ? ? 22? ? ? ? 0? ? 2699? ? ? 22? ? 2870? ? ? 32
##? 9? ? ? 9? ? ? ? 40? ? ? 82? ? ? 91? ? ? 881? ? ? ? 1? ? ? 449? ? 1121? ? ? 90? ? 2283
## 10? ? 10? ? ? ? 11? ? ? 79? ? ? 277? ? 2879? ? ? ? 1? ? ? ? 0? ? 6811? ? ? 14? ? ? 243
## # ... with 47,796 more rows, and 1,689 more variables: M1551P12 <dbl>, M1551P84 <dbl>,
## #? M1551P48 <dbl>, M1551P4 <dbl>, M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>, M1551P31 <dbl>,
## #? M1551P75 <dbl>, M1551P88 <dbl>, …
這是一個(gè)大數(shù)據(jù)集,具有47,806行(OTU)和1,699列(1,698個(gè)樣本和一個(gè)OTU ID)削咆。如果您的計(jì)算機(jī)無法加載該文件牍疏,請不用擔(dān)心,我們稍后將在研討會(huì)的其余部分中提供一個(gè)子集拨齐。
在此數(shù)據(jù)集中鳞陨,OTU的分類分類位于不同的文件中。該信息本可以作為其他列包含在OTU表中瞻惋,并且通常包含在其他數(shù)據(jù)集中厦滤。單擊此處下載分類學(xué)分類表。
tax_data <- read_tsv("data/taxAssignments97.txt")
print(tax_data)# You can also enter `tax_data` to print it
# # A tibble: 47,806 x 8
##? ? `OTU ID`? taxonomy? ? ? ? ? ? ? ? ? ? ? Kingdom? Phylum? Class? ? Order? ? Family? ? Confidence
##? ? <chr>? ? <chr>? ? ? ? ? ? ? ? ? ? ? ? ? <chr>? ? <chr>? <chr>? ? <chr>? ? <chr>? ? ? ? ? <dbl>
##? 1 OTU_1? ? Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassi… Unassig… Unassig… Unassign…? ? ? 1.00
##? 2 OTU_10? ? Root;k__Bacteria;p__Bacteroid… Bacteria Bacter… Sphingo… Sphingo… Sphingob…? ? ? 1.00
##? 3 OTU_100? Root;k__Bacteria;p__Cyanobact… Bacteria Cyanob… Chlorop… Chlorop… NA? ? ? ? ? ? 1.00
##? 4 OTU_1000? Root;k__Bacteria;p__Actinobac… Bacteria Actino… Actinob… Actinom… Actinosy…? ? ? 0.670
##? 5 OTU_10000 Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassi… Unassig… Unassig… Unassign…? ? ? 1.00
##? 6 OTU_10001 Root;k__Bacteria;p__Chlamydia… Bacteria Chlamy… Chlamyd… Chlamyd… Parachla…? ? ? 1.00
##? 7 OTU_10002 Root;k__Bacteria;p__Proteobac… Bacteria Proteo… Alphapr… NA? ? ? NA? ? ? ? ? ? 1.00
##? 8 OTU_10003 Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassi… Unassig… Unassig… Unassign…? ? ? 1.00
##? 9 OTU_10004 Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassi… Unassig… Unassig… Unassign…? ? ? 1.00
## 10 OTU_10005 Root;k__Bacteria;p__Cyanobact… Bacteria Cyanob… 4C0d-2? MLE1-12? NA? ? ? ? ? ? 1.00
## # ... with 47,796 more rows
盡管與大多數(shù)數(shù)據(jù)相比熟史,這些數(shù)據(jù)的格式非常好馁害,但是仍然存在一些問題□迤ィ“ OTU ID”列的名稱中包含一個(gè)空格(因此在后面打勾)碘菜,這使得在R中使用該名稱更加煩人。更重要的是限寞,分類表中的OTU ID前綴為“ OTU_”忍啸, OTU表中沒有,因此我們必須刪除該前綴以使兩者匹配履植。函數(shù)sub和gsub用于搜索和替換部分文本计雌;sub僅替換第一個(gè)匹配項(xiàng)并gsub替換所有匹配項(xiàng)。一無所有("")可以有效地進(jìn)行搜索和刪除玫霎。
tax_data$`OTU ID`<- sub(tax_data$`OTU ID`,# ` are needed because of the space
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pattern ="OTU_",replacement ="")
print(tax_data)
### A tibble: 47,806 x 8## `OTU ID` taxonomy Kingdom Phylum Class Order Family Confidence
##? ? <chr>? ? <chr>? ? ? ? ? ? ? ? ? ? ? ? ? <chr>? ? <chr>? ? <chr>? ? <chr>? ? <chr>? ? ? ? ? <dbl>##? 1 1? ? ? ? Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassig… Unassig… Unassig… Unassign…? ? ? 1.00
##? 2 10? ? ? Root;k__Bacteria;p__Bacteroid… Bacteria Bactero… Sphingo… Sphingo… Sphingob…? ? ? 1.00
##? 3 100? ? ? Root;k__Bacteria;p__Cyanobact… Bacteria Cyanoba… Chlorop… Chlorop… NA? ? ? ? ? ? 1.00
##? 4 1000? ? Root;k__Bacteria;p__Actinobac… Bacteria Actinob… Actinob… Actinom… Actinosy…? ? ? 0.670
##? 5 10000? ? Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassig… Unassig… Unassig… Unassign…? ? ? 1.00
##? 6 10001? ? Root;k__Bacteria;p__Chlamydia… Bacteria Chlamyd… Chlamyd… Chlamyd… Parachla…? ? ? 1.00
##? 7 10002? ? Root;k__Bacteria;p__Proteobac… Bacteria Proteob… Alphapr… NA? ? ? NA? ? ? ? ? ? 1.00
##? 8 10003? ? Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassig… Unassig… Unassig… Unassign…? ? ? 1.00
##? 9 10004? ? Unassigned? ? ? ? ? ? ? ? ? ? Unassig… Unassig… Unassig… Unassig… Unassign…? ? ? 1.00
## 10 10005? ? Root;k__Bacteria;p__Cyanobact… Bacteria Cyanoba… 4C0d-2? MLE1-12? NA? ? ? ? ? ? 1.00
## # ... with 47,796 more rows
盡管我們可以使用單獨(dú)的OTU和分類表進(jìn)行分析凿滤,但讓我們將它們組合起來可以簡化事情。由于行的順序不同庶近,因此我們需要根據(jù)它們的OTU ID組合(也稱為“聯(lián)接”)它們翁脆。我們將為此使用dplyr包。
library(dplyr)# Loads the dplyr package so we can use `left_join`(加載dplyr包鼻种,可使用left_join)
tax_data$`OTU ID`<- as.character(tax_data$`OTU ID`)# Must be same type for join to work(數(shù)據(jù)要同一類型才可連接)
otu_data$OTU_ID <- as.character(otu_data$OTU_ID)# Must be same type for join to work
otu_data <- left_join(otu_data, tax_data,by =c("OTU_ID"= "OTU ID"))# identifies cols with shared IDs(共同ID鑒別)
print(otu_data)
## A tibble: 47,806 x 1,706## OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38 M1551P90 M1551P71
##? ? <chr>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>##? 1 1? ? ? ? ? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.
##? 2 2? ? ? ? ? ? 41.? ? ? 22.? ? ? 4.? ? 726.? 112492.? ? ? 2.? ? 413.? ? ? 2.? ? ? 1.
##? 3 3? ? ? ? ? ? 67.? ? ? 65.? ? ? 12.? 13514.? ? ? 1.? ? ? 4.? 13314.? ? ? 70.? ? 5929.
##? 4 4? ? ? ? ? 3229.? 13679.? ? 1832.? ? 951.? ? 113.? ? 2496.? ? 567.? ? 2428.? ? 2156.
##? 5 5? ? ? ? ? 1200.? ? ? 92.? ? 3530.? ? 2008.? ? ? 0.? ? 183.? ? 2087.? ? 292.? ? 1058.
##? 6 6? ? ? ? ? ? 219.? ? 1980.? ? 1200.? ? 499.? ? ? 1.? ? 781.? ? 214.? ? 2273.? ? 171.
##? 7 7? ? ? ? ? ? 485.? ? 5123.? ? 755.? ? 4080.? ? 443.? ? 1278.? ? 2193.? ? 401.? ? 5320.
##? 8 8? ? ? ? ? ? 840.? ? 7079.? ? 3760.? ? ? 22.? ? ? 0.? ? 2699.? ? ? 22.? ? 2870.? ? ? 32.
##? 9 9? ? ? ? ? ? 40.? ? ? 82.? ? ? 91.? ? 881.? ? ? 1.? ? 449.? ? 1121.? ? ? 90.? ? 2283.
## 10 10? ? ? ? ? ? 11.? ? ? 79.? ? 277.? ? 2879.? ? ? 1.? ? ? 0.? ? 6811.? ? ? 14.? ? 243.
## # ... with 47,796 more rows, and 1,696 more variables: M1551P12 <dbl>, M1551P84 <dbl>,
## #? M1551P48 <dbl>, M1551P4 <dbl>, M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>, M1551P31 <dbl>,
## #? M1551P75 <dbl>, M1551P88 <dbl>, …
列太多了反番,所有列都沒有顯示在打印輸出中,但是我們可以通過查看最后10個(gè)列名來驗(yàn)證它們是否存在:
tail(colnames(otu_data),n =10)# `tail` returns the last n elements(tail返回最后幾個(gè)元素)
## [1] "M1958P1043" "M1691P1526" "M1691P1557" "taxonomy" "Kingdom" "Phylum" "Class"
## [8] "Order" "Family" "Confidence"
接下來,加載樣本數(shù)據(jù)罢缸。單擊此處下載樣本數(shù)據(jù)表篙贸。
sample_data <- read_tsv("data/SMD.txt",col_types ="cccccccccccccccc")# each "c" means a column of "character"(每個(gè)c指一列字符)
print(sample_data)# You can also enter `sample_data` to print it
### A tibble: 1,698 x 16## SampleID Name Plant_ID Type Experiment Cohort Harvested Age Site Treatment Line Genotype
##? ? <chr>? ? <chr>? <chr>? ? <chr> <chr>? ? ? <chr>? <chr>? ? <chr> <chr> <chr>? ? <chr> <chr>?
##? 1 M1024P17… R_026… R_026? ? root? fieldBCMA? 2008? 2011? ? ? 3? ? LTM? field? ? 26? ? ril? ?
##? 2 M1024P17… R_073… R_073? ? root? fieldBCMA? 2008? 2011? ? ? 3? ? LTM? field? ? 73? ? ril? ?
##? 3 M1024P17… R_088… R_088? ? root? fieldBCMA? 2009? 2011? ? ? 2? ? LTM? field? ? 88? ? ril? ?
##? 4 M1024P18… R_156… R_156? ? root? fieldBCMA? 2009? 2011? ? ? 2? ? LTM? field? ? 156? ril? ?
##? 5 M1955P804 1_A_1… 1_A_1? ? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? par1… PAR? ?
##? 6 M1956P837 1_A_1… 1_A_12? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? mah3… MAH? ?
##? 7 M1957P983 1_A_3… 1_A_3? ? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? silL… SIL? ?
##? 8 M1956P845 1_A_4… 1_A_4? ? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? par1… PAR? ?
##? 9 M1957P987 1_A_7… 1_A_7? ? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? par9… PAR? ?
## 10 M1957P923 1_A_8… 1_A_8? ? root? ecoGH? ? ? NA? ? 2011? ? ? NA? ? Duke? MAHsoil? mil5… MIL? ?
## # ... with 1,688 more rows, and 4 more variables: Block <chr>, oldPlate <chr>, newPlate <chr>,
## #? Analysis <chr>
請注意,其中的示例列數(shù)otu_data等于其中的行數(shù)枫疆,sample_data并且列名稱otu_data出現(xiàn)在“ SampleID”列中爵川。這意味著的內(nèi)容sample_data$SampleID可用于子集OTU表中的列。
轉(zhuǎn)換為taxmap格式
盡管我們的數(shù)據(jù)現(xiàn)在以R表示养铸,但它的格式不是專門針對社區(qū)數(shù)量數(shù)據(jù)的格式雁芙;R只知道您有幾個(gè)大桌子。用于社區(qū)(例如微生物組)分析的不同R軟件包期望數(shù)據(jù)采用不同的格式或類別钞螟。用編程術(shù)語來說兔甘,類是定義的存儲(chǔ)數(shù)據(jù)的方式以及一些旨在與該數(shù)據(jù)交互的功能。當(dāng)您以這種方式格式化特定數(shù)據(jù)集時(shí)鳞滨,我們稱其為類的對象或“實(shí)例”洞焙。許多R包實(shí)現(xiàn)了自己的類和函數(shù),以將數(shù)據(jù)轉(zhuǎn)換為它們的格式拯啦,而某些包使用其他包中定義的類澡匪。對于如何在R中存儲(chǔ)按分類法分類的豐度矩陣,有幾種選擇(例如phyloseq對象)褒链,但我們將在此使用taxa包中定義的類唁情。該taxa軟件包的目標(biāo)是提供一種通用的標(biāo)準(zhǔn)方式來處理分配給分類法的任何類型的信息。Taxa提供一組靈活的解析器甫匹,只要設(shè)置正確甸鸟,它們就應(yīng)該能夠讀取幾乎任何格式。您可以taxa在此處閱讀有關(guān)解析分類學(xué)數(shù)據(jù)的更多信息:https?:?//github.com/ropensci/taxa#parsing-data兵迅。我們附加到豐度矩陣的分類數(shù)據(jù)具有以下形式:
head(otu_data$taxonomy, 10)
## [1] "Unassigned"
##? [2] "Root;k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria"? ? ? ?
##? [3] "Root;k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Sphingomonadales;f__Sphingomonadaceae"
##? [4] "Root;k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rickettsiales;f__mitochondria"? ? ? ?
##? [5] "Root;k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;f__Rhizobiaceae"? ? ? ? ?
##? [6] "Root;k__Bacteria;p__Actinobacteria;c__Actinobacteria;o__Actinomycetales;f__Kineosporiaceae"? ? ? ?
##? [7] "Root;k__Bacteria;p__Cyanobacteria;c__Chloroplast;o__Streptophyta;f__"? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
##? [8] "Root;k__Bacteria;p__Proteobacteria;c__Alphaproteobacteria;o__Rhizobiales;f__Bradyrhizobiaceae"? ?
##? [9] "Root;k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Pseudomonadales;f__Pseudomonadaceae"?
## [10] "Root;k__Bacteria;p__Bacteroidetes;c__Sphingobacteriia;o__Sphingobacteriales;f__Sphingobacteriaceae"
請注意抢韭,格式存在一些奇怪的方面,可能使其難以解析:
有些分類單元具有等級(jí)(例如 “k__Bacteria“))恍箭,有些分類則沒有(例如“Unassigned”和“root”)刻恭。一些分類單元具有等級(jí),但沒有名稱(例如“ f__”)扯夭。
如果我們僅將等級(jí)視為分類單元名稱的一部分鳍贾,那么它很容易解析:
library(taxa)
obj <- parse_tax_data(otu_data,
? ? ? ? ? ? ? ? ? ? ? class_cols = "taxonomy", # The column in the input table(輸入表中的列)
? ? ? ? ? ? ? ? ? ? ? class_sep = ";") # What each taxon is seperated by(每個(gè)分類單元由什么分開)
print(obj)
## <Taxmap>
##? 1558 taxa: aab. Unassigned, aac. Root ... chx. f__Methanospirillaceae, chy. f__
##? 1558 edges: NA->aab, NA->aac, aac->aad, aac->aae ... bel->chw, ays->chx, bem->chy
##? 1 data sets:
##? ? tax_data:
##? ? ? # A tibble: 47,806 x 1,707
##? ? ? ? taxon_id OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38
##? ? ? ? <chr>? ? <chr>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>
##? ? ? 1 aab? ? ? 1? ? ? ? ? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.
##? ? ? 2 ben? ? ? 2? ? ? ? ? ? 41.? ? ? 22.? ? ? 4.? ? 726.? 112492.? ? ? 2.? ? 413.
##? ? ? 3 beo? ? ? 3? ? ? ? ? ? 67.? ? ? 65.? ? ? 12.? 13514.? ? ? 1.? ? ? 4.? 13314.
##? ? ? # ... with 4.78e+04 more rows, and 1,698 more variables: M1551P90 <dbl>,
##? ? ? #? M1551P71 <dbl>, M1551P12 <dbl>, M1551P84 <dbl>, M1551P48 <dbl>, M1551P4 <dbl>,
##? ? ? #? M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>, M1551P31 <dbl>, …
##? 0 functions:
上面是taxmap對象的輸出。第一行告訴我們交洗,OTU已分配給1,558個(gè)唯一分類單元贾漏,并列出了它們的ID和名稱。這些分類單元ID是在轉(zhuǎn)換為taxmap格式時(shí)自動(dòng)生成的藕筋,并且不在原始數(shù)據(jù)集中。第二行描述了分類單元在樹中的相互關(guān)系。請注意隐圾,我們的原始數(shù)據(jù)現(xiàn)在如何在此對象內(nèi):
print(obj$data$tax_data)
### A tibble: 47,806 x 1,707## taxon_id OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38 M1551P90
##? ? <chr>? ? <chr>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>##? 1 aab? ? ? 1? ? ? ? ? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.
##? 2 ben? ? ? 2? ? ? ? ? ? 41.? ? ? 22.? ? ? 4.? ? 726.? 112492.? ? ? 2.? ? 413.? ? ? 2.
##? 3 beo? ? ? 3? ? ? ? ? ? 67.? ? ? 65.? ? ? 12.? 13514.? ? ? 1.? ? ? 4.? 13314.? ? ? 70.
##? 4 ben? ? ? 4? ? ? ? ? 3229.? 13679.? ? 1832.? ? 951.? ? 113.? ? 2496.? ? 567.? ? 2428.
##? 5 bep? ? ? 5? ? ? ? ? 1200.? ? ? 92.? ? 3530.? ? 2008.? ? ? 0.? ? 183.? ? 2087.? ? 292.
##? 6 beq? ? ? 6? ? ? ? ? ? 219.? ? 1980.? ? 1200.? ? 499.? ? ? 1.? ? 781.? ? 214.? ? 2273.
##? 7 ber? ? ? 7? ? ? ? ? ? 485.? ? 5123.? ? 755.? ? 4080.? ? 443.? ? 1278.? ? 2193.? ? 401.
##? 8 bes? ? ? 8? ? ? ? ? ? 840.? ? 7079.? ? 3760.? ? ? 22.? ? ? 0.? ? 2699.? ? ? 22.? ? 2870.
##? 9 bet? ? ? 9? ? ? ? ? ? 40.? ? ? 82.? ? ? 91.? ? 881.? ? ? 1.? ? 449.? ? 1121.? ? ? 90.
## 10 beu? ? ? 10? ? ? ? ? ? 11.? ? ? 79.? ? 277.? ? 2879.? ? ? 1.? ? ? 0.? ? 6811.? ? ? 14.
## # ... with 47,796 more rows, and 1,697 more variables: M1551P71 <dbl>, M1551P12 <dbl>,
## #? M1551P84 <dbl>, M1551P48 <dbl>, M1551P4 <dbl>, M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>,
## #? M1551P31 <dbl>, M1551P75 <dbl>, …
obj$data是任意的伍掀,用戶定義的數(shù)據(jù)集的列表。這些數(shù)據(jù)集可以命名為任何名稱暇藏,并且可以是任何R對象蜜笤,例如list,vectors或table盐碱。這與phyloseq對象具有不同的對象把兔,對象具有固定數(shù)量的預(yù)定義格式的數(shù)據(jù)集,因?yàn)閷ο蟮闹攸c(diǎn)taxa通常是分類phyloseq數(shù)據(jù)瓮顽,尤其是微生物組數(shù)據(jù)县好。請注意,我們的數(shù)據(jù)集現(xiàn)在具有“ taxon_id”列暖混,該列將表中的行與分類法中的分類單元相關(guān)聯(lián)缕贡。本專欄對于taxa了解如何處理這些數(shù)據(jù)集的操作功能至關(guān)重要,我們將在后面進(jìn)行演示拣播。
如果要在解析時(shí)拆分等級(jí)信息晾咪,可以使用正則表達(dá)式(也稱為“ regex”)來指定每個(gè)分類單元的哪一部分是等級(jí),哪一部分是名稱贮配。如果您不熟悉使用正則表達(dá)式谍倦,一開始可能很難理解,但是這是一項(xiàng)非常有用的技能泪勒,因此值得學(xué)習(xí)昼蛀。大多數(shù)正則表達(dá)式由一系列“要匹配的內(nèi)容”后跟“要匹配多少次”組成。一個(gè)與分類單元名稱模式匹配的正則表達(dá)式是^[a-z]{0,1}_{0,2}.*$酣藻。這看似令人生畏曹洽,但我們可以將其分解為可以理解的部分:
在^和$分別代表文本的開始和結(jié)束。如果這些內(nèi)容不存在辽剧,則該模式可能僅與文本的一部分匹配送淆。
方括號(hào)(例如[a-z])指定了可以匹配的字符范圍。同樣怕轿,.手段匹配任何字符偷崩。
花括號(hào)(例如{0,1})的內(nèi)容表示前面的樣式可以匹配的時(shí)間。同樣撞羽,*均值等于或大于0阐斜。例如,正則表達(dá)式的一部分^[a-z]{0,1}的裝置“相匹配的字符a通過z使得在字符串的開始出現(xiàn)零本或一個(gè)次”诀紊。
不是特殊正則表達(dá)式字符的任何文本(例如[和.)都將匹配自身谒出,因此_匹配_文本中的a?。要匹配的字符一樣[在你與“逃離”他們的文字\\(如\\[)。
整個(gè)正則表達(dá)式在通用英語中含義如下:
“從字符串的開頭笤喳,(^)匹配“ a”和“ z”之間的任何字符([a-z])零或一次({0,1})为居,然后是下劃線(_)出現(xiàn)在零至2倍({0,2})之間,然后是任何字符(.)出現(xiàn)零次或多次(*)杀狡,然后是文字結(jié)尾($)蒙畴。”
我們可以添加括號(hào)以指定模式的哪些部分在一起呜象;這些在正則表達(dá)式行話中稱為捕獲組膳凝。這些不會(huì)改變將要匹配的內(nèi)容。他們只是定義了模式的不同部分恭陡。在這種情況下蹬音,我們對分類單元等級(jí)(由匹配([a-z]{0,1}))和分類名稱(由匹配)感興趣(.*)。
包中的parse_tax_data函數(shù)taxa使用帶有捕獲組的正則表達(dá)式來隔離所需的信息子姜。對于正則表達(dá)式(也稱為“ regex”)中的每個(gè)捕獲組祟绊,將為class_key選項(xiàng)提供一個(gè)值,指定該組是什么(例如哥捕,分類單元名稱)牧抽。將所有這些放在一起,我們可以像這樣讀取數(shù)據(jù):
obj <- parse_tax_data(otu_data,
? ? ? ? ? ? ? ? ? ? ? class_cols = "taxonomy",
? ? ? ? ? ? ? ? ? ? ? class_sep = ";",
? ? ? ? ? ? ? ? ? ? ? class_regex = "^([a-z]{0,1})_{0,2}(.*)$",
? ? ? ? ? ? ? ? ? ? ? class_key = c("tax_rank" = "taxon_rank", "name" = "taxon_name"))
print(obj)
## ## 1558 taxa:aab. Unassigned, aac. Root ... chx. Methanospirillaceae, chy.
##? 1558 edges: NA->aab, NA->aac, aac->aad, aac->aae ... bel->chw, ays->chx, bem->chy##? 2 data sets:
##? ? tax_data:
##? ? ? # A tibble: 47,806 x 1,707##? ? ? ? taxon_id OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38
##? ? ? ? <chr>? ? <chr>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>##? ? ? 1 aab? ? ? 1? ? ? ? ? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.
##? ? ? 2 ben? ? ? 2? ? ? ? ? ? 41.? ? ? 22.? ? ? 4.? ? 726.? 112492.? ? ? 2.? ? 413.
##? ? ? 3 beo? ? ? 3? ? ? ? ? ? 67.? ? ? 65.? ? ? 12.? 13514.? ? ? 1.? ? ? 4.? 13314.
##? ? ? # ... with 4.78e+04 more rows, and 1,698 more variables: M1551P90 <dbl>,
##? ? ? #? M1551P71 <dbl>, M1551P12 <dbl>, M1551P84 <dbl>, M1551P48 <dbl>, M1551P4 <dbl>,
##? ? ? #? M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>, M1551P31 <dbl>, …
##? ? class_data:
##? ? ? # A tibble: 216,417 x 5##? ? ? ? taxon_id input_index tax_rank name? ? ? regex_match
##? ? ? ? <chr>? ? ? ? ? <int> <chr>? ? <chr>? ? ? <chr>? ? ? ##? ? ? 1 aab? ? ? ? ? ? ? ? 1 ""? ? ? Unassigned Unassigned
##? ? ? 2 aac? ? ? ? ? ? ? ? 2 ""? ? ? Root? ? ? Root? ? ?
##? ? ? 3 aad? ? ? ? ? ? ? ? 2 k? ? ? ? Bacteria? k__Bacteria
##? ? ? # ... with 2.164e+05 more rows
##? 0 functions:
請注意分類單元名稱不再具有等級(jí)信息:
head(taxon_names(obj))
## aab aac aad aae aaf
##? ? "Unassigned"? ? ? ? ? "Root"? ? ? "Bacteria"? ? ? ? "Archaea" "Proteobacteria"
##? ? ? ? ? ? ? aag
## "Actinobacteria"
相反遥赚,排名信息(以及任何其他捕獲組內(nèi)容)在單獨(dú)的數(shù)據(jù)集中:
obj$data$class_data
## # A tibble: 216,417 x 5
##? ? taxon_id input_index tax_rank name? ? ? ? ? ? ? ? regex_match? ? ? ? ?
##? ? <chr>? ? ? ? ? <int> <chr>? ? <chr>? ? ? ? ? ? ? <chr>? ? ? ? ? ? ? ?
##? 1 aab? ? ? ? ? ? ? ? 1 ""? ? ? Unassigned? ? ? ? ? Unassigned? ? ? ? ? ?
##? 2 aac? ? ? ? ? ? ? ? 2 ""? ? ? Root? ? ? ? ? ? ? ? Root? ? ? ? ? ? ? ? ?
##? 3 aad? ? ? ? ? ? ? ? 2 k? ? ? ? Bacteria? ? ? ? ? ? k__Bacteria? ? ? ? ?
##? 4 aaf? ? ? ? ? ? ? ? 2 p? ? ? ? Proteobacteria? ? ? p__Proteobacteria? ?
##? 5 add? ? ? ? ? ? ? ? 2 c? ? ? ? Alphaproteobacteria c__Alphaproteobacteria
##? 6 amg? ? ? ? ? ? ? ? 2 o? ? ? ? Rickettsiales? ? ? o__Rickettsiales? ? ?
##? 7 ben? ? ? ? ? ? ? ? 2 f? ? ? ? mitochondria? ? ? ? f__mitochondria? ? ?
##? 8 aac? ? ? ? ? ? ? ? 3 ""? ? ? Root? ? ? ? ? ? ? ? Root? ? ? ? ? ? ? ? ?
##? 9 aad? ? ? ? ? ? ? ? 3 k? ? ? ? Bacteria? ? ? ? ? ? k__Bacteria? ? ? ? ?
## 10 aaf? ? ? ? ? ? ? ? 3 p? ? ? ? Proteobacteria? ? ? p__Proteobacteria? ?
## # ... with 216,407 more rows
但是扬舒,也可以使用以下taxon_ranks功能訪問等級(jí):
head(taxon_ranks(obj))
## aab aac aad aae aaf aag
##? ""? "" "k" "k" "p" "p"
因此,我們實(shí)際上并不需要“ class_data”表凫佛,因此讓我們擺脫它:
obj$data$class_data <- NULL
讓我們也將“ tax_data”表重命名為更有用的信息:
names(obj$data) <- "otu_counts"
print(obj)
## ## 1558 taxa:aab. Unassigned, aac. Root ... chx. Methanospirillaceae, chy.
##? 1558 edges: NA->aab, NA->aac, aac->aad, aac->aae ... bel->chw, ays->chx, bem->chy##? 1 data sets:
##? ? otu_counts:
##? ? ? # A tibble: 47,806 x 1,707##? ? ? ? taxon_id OTU_ID M1024P1833 M1551P81 M1551P57 M1551P85 M1551P28 M1551P29 M1551P38
##? ? ? ? <chr>? ? <chr>? ? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>? ? <dbl>##? ? ? 1 aab? ? ? 1? ? ? ? ? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.? ? ? 0.
##? ? ? 2 ben? ? ? 2? ? ? ? ? ? 41.? ? ? 22.? ? ? 4.? ? 726.? 112492.? ? ? 2.? ? 413.
##? ? ? 3 beo? ? ? 3? ? ? ? ? ? 67.? ? ? 65.? ? ? 12.? 13514.? ? ? 1.? ? ? 4.? 13314.
##? ? ? # ... with 4.78e+04 more rows, and 1,698 more variables: M1551P90 <dbl>,
##? ? ? #? M1551P71 <dbl>, M1551P12 <dbl>, M1551P84 <dbl>, M1551P48 <dbl>, M1551P4 <dbl>,
##? ? ? #? M1551P52 <dbl>, M1551P3 <dbl>, M1551P15 <dbl>, M1551P31 <dbl>, …
##? 0 functions:
我們可以根據(jù)需要命名表或其他信息obj$data讲坎。obj$data是一個(gè)標(biāo)準(zhǔn)list,這意味著可以放入任何數(shù)量的任何類型的東西愧薛。