提取OTU表格中分类学名称

在分类学注释结果中,物种的分类学名称包含所有的分类学水平,在绘制相对丰度图或绘制热图时,需要对分类学名称进行处理,只保留我们需要的分类学水平。

这里以L4水平为例,用到的函数为gsub函数(http://www.endmemo.com/r/gsub.php)

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)


• pattern: string to be matched, supports regular expression
• replacement: string for replacement
• x: string or string vector
• perl: logical. Should perl-compatible regexps be used? Has priority over extended
• fixed: logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments
• useBytes: logical. If TRUE the matching is done byte-by-byte rather than character-by-character

gsub()可以用于字段的删减、增补、替换和切割,可以处理一个字段也可以处理由字段组成的向量。

具体的使用方法为:gsub("目标字符", "替换字符", 对象)

在gsub函数中,任何字段处理都由将“替换字符”替换到“目标字符”这一流程中实现,令替换字符为''''可实现删除,令替换字符为"目标字符+增补内容"可实现增补,替换和切也是使用类似的操作。

Regular Expression Syntax:

Syntax

Description

\\d Digit, 0,1,2 ... 9

\\D Not Digit

\\s Space

\\S Not Space

\\w Word

\\W Not Word

\\t Tab

\\n New line

^ Beginning of the string

$ End of the string

\ Escape special characters, e.g. \\ is "\", \+ is "+"

| Alternation match. e.g. /(e|d)n/ matches "en" and "dn"

• Any character, except \n or line terminator

[ab] a or b

[^ab] Any character except a and b

[0-9] All Digit

[A-Z] All uppercase A to Z letters

[a-z] All lowercase a to z letters

[A-z] All Uppercase and lowercase a to z letters

i+ i at least one time

i* i zero or more times

i? i zero or 1 time

i{n} i occurs n times in sequence

i{n1,n2} i occurs n1 - n2 times in sequence

i{n1,n2}? non greedy match, see above example

i{n,} i occures >= n times

[:alnum:] Alphanumeric characters: [:alpha:] and [:digit:]

[:alpha:] Alphabetic characters: [:lower:] and [:upper:]

[:blank:] Blank characters: e.g. space, tab

[:cntrl:] Control characters

[:digit:] Digits: 0 1 2 3 4 5 6 7 8 9

[:graph:] Graphical characters: [:alnum:] and [:punct:]

[:lower:] Lower-case letters in the current locale

[:print:] Printable characters: [:alnum:], [:punct:] and space

[:punct:] Punctuation character: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

[:space:] Space characters: tab, newline, vertical tab, form feed, carriage return, space

[:upper:] Upper-case letters in the current locale

[:xdigit:] Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f

示例

abundance <- read.table("otu_taxa_table_L4.txt",header = TRUE,sep = "\t")

abundance <- as.data.frame(abundance)
taxon <- gsub(".*(o__)","",abundance$Taxon)
taxon <- gsub(".*(;)","",taxon)
taxon <- gsub(".*(Other)","",taxon)
taxon <- gsub("(\\[)","",taxon)
taxon <- gsub("(\\])","",taxon)
abundance$Taxon <- taxon
abundance[abundance==""] <- NA
abundance <- na.omit(abundance)
abundance <- subset(abundance,Taxon != "Unclassified")
abundance <- subset(abundance,Taxon != "norank")
abundance <- subset(abundance,Taxon != "uncultured")
abundance <- t(abundance)
colnames(abundance) <- abundance[1,]
abundance <- abundance[-1,]

 

你可能感兴趣的:(菌群分析,R语言)