數(shù)據(jù)導(dǎo)入R

微生物組數(shù)據(jù)的典型格式

大多數(shù)處理高通量擴增子數(shù)據(jù)的管道,例如mothur,QIIME和dada2,都會形成讀取計數(shù)矩陣。該矩陣的一維(即行或列)由操作分類單位(OTU),系統(tǒng)型精確序列變體(ESV)(通過各種方式“組合”相似的讀取序列)組成。另一個維度包括樣本。不同的工具將期望/輸出矩陣的不同方向,但是在我們的例子中,列是樣本,行是OTU。有時是OTU數(shù)據(jù)和豐度矩陣是兩個單獨的表。通常會有另一個表,其中的行有示例信息。這樣可以輕松添加許多其他示例數(shù)據(jù)列,這些列可用于子集數(shù)據(jù)。每個樣本和OTU都有唯一的ID。


將數(shù)據(jù)導(dǎo)入R

如果數(shù)據(jù)格式正確,則將數(shù)據(jù)導(dǎo)入R可能非常容易,但否則可能會非常令人沮喪。格式良好的數(shù)據(jù)的示例是.csv(逗號分隔值).tsv(制表符分隔值)文件,每個文件都有一個表,沒有其他注釋或格式(例如,合并的單元格)。這兩種格式都可能具有.txt擴展名(擴展名實際上并不重要;它適用于人類,而不是計算機)。有關(guān)正確的數(shù)據(jù)格式的詳細(xì)信息,請參閱該數(shù)據(jù)在格式化部分我們對reporducible研究指南。您應(yīng)該始終盡可能地導(dǎo)入原始輸出數(shù)據(jù),并避免對數(shù)據(jù)進行任何“手動”(即非腳本化)修改,尤其是在Excel等程序中,該程序會不時地處理數(shù)據(jù)(Zeeberg等人(2004年) ))。

在整個研討會中,我們將使用Wagner等人的數(shù)據(jù)。(2016),一項研究植物年齡,基因型和環(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>, …


這是一個大數(shù)據(jù)集,具有47,806行(OTU)和1,699列(1,698個樣本和一個OTU ID)。如果您的計算機無法加載該文件,請不用擔(dān)心,我們稍后將在研討會的其余部分中提供一個子集。

在此數(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”列的名稱中包含一個空格(因此在后面打勾),這使得在R中使用該名稱更加煩人。更重要的是,分類表中的OTU ID前綴為“ OTU_”, OTU表中沒有,因此我們必須刪除該前綴以使兩者匹配。函數(shù)sub和gsub用于搜索和替換部分文本;sub僅替換第一個匹配項并gsub替換所有匹配項。一無所有("")可以有效地進行搜索和刪除。

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

盡管我們可以使用單獨的OTU和分類表進行分析,但讓我們將它們組合起來可以簡化事情。由于行的順序不同,因此我們需要根據(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個列名來驗證它們是否存在:

tail(colnames(otu_data),n =10)# `tail` returns the last n elements(tail返回最后幾個元素)

## [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"(每個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只知道您有幾個大桌子。用于社區(qū)(例如微生物組)分析的不同R軟件包期望數(shù)據(jù)采用不同的格式或類別。用編程術(shù)語來說,類是定義的存儲數(shù)據(jù)的方式以及一些旨在與該數(shù)據(jù)交互的功能。當(dāng)您以這種方式格式化特定數(shù)據(jù)集時,我們稱其為類的對象或“實例”。許多R包實現(xiàn)了自己的類和函數(shù),以將數(shù)據(jù)轉(zhuǎn)換為它們的格式,而某些包使用其他包中定義的類。對于如何在R中存儲按分類法分類的豐度矩陣,有幾種選擇(例如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"

請注意,格式存在一些奇怪的方面,可能使其難以解析:

有些分類單元具有等級(例如 “k__Bacteria“)),有些分類則沒有(例如“Unassigned”和“root”)。一些分類單元具有等級,但沒有名稱(例如“ f__”)。

如果我們僅將等級視為分類單元名稱的一部分,那么它很容易解析:

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(每個分類單元由什么分開)

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個唯一分類單元,并列出了它們的ID和名稱。這些分類單元ID是在轉(zhuǎn)換為taxmap格式時自動生成的,并且不在原始數(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對象,例如listvectors或table。這與phyloseq對象具有不同的對象,對象具有固定數(shù)量的預(yù)定義格式的數(shù)據(jù)集,因為對象的重點taxa通常是分類phyloseq數(shù)據(jù),尤其是微生物組數(shù)據(jù)。請注意,我們的數(shù)據(jù)集現(xiàn)在具有“ taxon_id”列,該列將表中的行與分類法中的分類單元相關(guān)聯(lián)。本專欄對于taxa了解如何處理這些數(shù)據(jù)集的操作功能至關(guān)重要,我們將在后面進行演示。

如果要在解析時拆分等級信息,可以使用正則表達式(也稱為“ regex”)來指定每個分類單元的哪一部分是等級,哪一部分是名稱。如果您不熟悉使用正則表達式,一開始可能很難理解,但是這是一項非常有用的技能,因此值得學(xué)習(xí)。大多數(shù)正則表達式由一系列“要匹配的內(nèi)容”后跟“要匹配多少次”組成。一個與分類單元名稱模式匹配的正則表達式是^[a-z]{0,1}_{0,2}.*$。這看似令人生畏,但我們可以將其分解為可以理解的部分:

在^和$分別代表文本的開始和結(jié)束。如果這些內(nèi)容不存在,則該模式可能僅與文本的一部分匹配。

方括號(例如[a-z])指定了可以匹配的字符范圍。同樣,.手段匹配任何字符。

花括號(例如{0,1})的內(nèi)容表示前面的樣式可以匹配的時間。同樣,*均值等于或大于0。例如,正則表達式的一部分^[a-z]{0,1}的裝置“相匹配的字符a通過z使得在字符串的開始出現(xiàn)零本或一個次”。

不是特殊正則表達式字符的任何文本(例如[和.)都將匹配自身,因此_匹配_文本中的a?。要匹配的字符一樣[在你與“逃離”他們的文字\\(如\\[)。

整個正則表達式在通用英語中含義如下:

“從字符串的開頭,(^)匹配“ a”和“ z”之間的任何字符([a-z])零或一次({0,1}),然后是下劃線(_)出現(xiàn)在零至2倍({0,2})之間,然后是任何字符(.)出現(xiàn)零次或多次(*),然后是文字結(jié)尾($)。”

我們可以添加括號以指定模式的哪些部分在一起;這些在正則表達式行話中稱為捕獲組。這些不會改變將要匹配的內(nèi)容。他們只是定義了模式的不同部分。在這種情況下,我們對分類單元等級(由匹配([a-z]{0,1}))和分類名稱(由匹配)感興趣(.*)。

包中的parse_tax_data函數(shù)taxa使用帶有捕獲組的正則表達式來隔離所需的信息。對于正則表達式(也稱為“ regex”)中的每個捕獲組,將為class_key選項提供一個值,指定該組是什么(例如,分類單元名稱)。將所有這些放在一起,我們可以像這樣讀取數(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:

請注意分類單元名稱不再具有等級信息:

head(taxon_names(obj))

## aab aac aad aae aaf

##? ? "Unassigned"? ? ? ? ? "Root"? ? ? "Bacteria"? ? ? ? "Archaea" "Proteobacteria"

##? ? ? ? ? ? ? aag

## "Actinobacteria"

相反,排名信息(以及任何其他捕獲組內(nèi)容)在單獨的數(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功能訪問等級:

head(taxon_ranks(obj))

## aab aac aad aae aaf aag

##? ""? "" "k" "k" "p" "p"

因此,我們實際上并不需要“ 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是一個標(biāo)準(zhǔn)list,這意味著可以放入任何數(shù)量的任何類型的東西。

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

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

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