美文网首页ggplot集锦
WGCNA之labels2colors()

WGCNA之labels2colors()

作者: 学生信的大叔 | 来源:发表于2022-08-22 13:20 被阅读0次

介绍

WGCNA官网教程第一部分的第二个脚本FemaleLiver-02-networkConstr-auto.R 中用到的labels2colors函数解读

表达式:moduleColors = labels2colors(net$colors)

后面的脚本中 moduleColors 被视为与基因顺序相同,

目的是为了看moduleColorsnet$colors 的顺序是否一一对应。

net$colors 简介

> net$colors %>% head(20)
MMT00000044 MMT00000046 MMT00000051 MMT00000076 MMT00000080 
          0           1           1          17           4 
MMT00000102 MMT00000149 MMT00000159 MMT00000207 MMT00000212 
         12           5           6           7           1 
MMT00000231 MMT00000241 MMT00000268 MMT00000283 MMT00000334 
          1           4           4           5           1 
MMT00000365 MMT00000368 MMT00000373 MMT00000384 MMT00000401 
          4           4           2           2           2 

函数式与解读

根据moduleColors=labels2colors(net$colors) 表达式进行解析。

> labels2colors
function (labels, zeroIsGrey = TRUE, colorSeq = NULL, naColor = "grey", 
    commonColorCode = TRUE) 
{
    # 1 if
    if (is.null(colorSeq)) #真,未定义该值
        colorSeq = standardColors() #执行,standardColors()为WGCNA定义的颜色函数,默认返回一个值为435个颜色的向量。
    
    # 2 if
    if (is.numeric(labels)) { #真,labels为数值
        # 2.1 if
        if (zeroIsGrey) #真,默认
            minLabel = 0 #执行,获取值,minLabel = 0
        else minLabel = 1
        
        # 2.2 if
        if (any(labels < 0, na.rm = TRUE)) #假。 
            minLabel = min(c(labels), na.rm = TRUE) # 上面if 省略了{},是单行表达式!!!坑!!!
        nLabels = labels #执行,注意这句与2.1 if和 2.2 if并列。该句在2 if为真的判断内。这个缩进真的不太好观察。⭐⭐⭐⭐
    }
    else { #跳过
        
        # 2.3 if
        if (commonColorCode) {
            factors = factor(c(as.matrix(as.data.frame(labels))))
            nLabels = as.numeric(factors)
            dim(nLabels) = dim(labels)
        }
        else {
            labels = as.matrix(as.data.frame(labels))
            factors = list()
            for (c in 1:ncol(labels)) factors[[c]] = factor(labels[, 
                c])
            nLabels = sapply(factors, as.numeric)
        }
    }
    # 3 if
    if (max(nLabels, na.rm = TRUE) > length(colorSeq)) { #此处nLabels = labels,max(nLabels, na.rm = TRUE) 返回最大模块数值18,length(colorSeq) = 435,假!跳过
        nRepeats = as.integer((max(labels) - 1)/length(colorSeq)) + 1
        warning(paste("labels2colors: Number of labels exceeds number of avilable colors.",             "Some colors will be repeated", nRepeats, "times."))
        extColorSeq = colorSeq
        for (rep in 1:nRepeats) extColorSeq = c(extColorSeq, 
            paste(colorSeq, ".", rep, sep = ""))
    }
    else { #真,执行
        nRepeats = 1 #这个有啥用啊?下面函数没用到呀!
        extColorSeq = colorSeq #colorSeq 为长度435的颜色向量,
    }
    colors = rep("grey", length(nLabels)) #length(nLabels)等于基因数目3600,生成3600个重复的"grey"
    fin = !is.na(nLabels) #非NA的值为TRUE,3600 个TRUE的逻辑向量。(!is.na(moduleLabels)) %>% table() 
    colors[!fin] = naColor #反转向量fin,缺失值NA为TRUE,提取所有为TRUE的值,转换为naColor,默认为"grey"。#也就是说,原来nLabels缺失值部分可以转换默认的”grey“,也可以通过naColor转换为其它配色。并不是无用。因为默认naColor = "grey",所以这里的colors仍全部为”grey“
    finLabels = nLabels[fin]
    colors[fin][finLabels != 0] = extColorSeq[finLabels[finLabels !=  0]]
    # 4 if
    if (!is.null(dim(labels))) 
        dim(colors) = dim(labels)
    colors
}
<bytecode: 0x000002ac4939e770>
<environment: namespace:WGCNA>

支线函数

standardColors() 函数式

WGCNA自带函数。

n指定输出颜色个数,最大435。这个函数输出的颜色,给我那29条的染色体,这不就正好了吗?找个这么长的颜色向量不容易。

不太理解.GlobalStandardColors 含义,猜测是WGCNA内置内容。

> standardColors
function (n = NULL) 
{
    if (is.null(n)) 
        return(.GlobalStandardColors)
    if ((n > 0) && (n <= length(.GlobalStandardColors))) {
        return(.GlobalStandardColors[c(1:n)])
    }
    else {
        stop("Invalid number of standard colors requested.")
    }
}
<bytecode: 0x000002ac3026ca78>
<environment: namespace:WGCNA>

any(labels < 0, na.rm = TRUE) 此处为FALSE

any(labels < 0, na.rm = TRUE)中labels为 net$colors 数值向量,向量由WGCNA内函数安排,应该不会有<0的值。

读作:labels中去掉na值,然后与0作比较,如果有一个值<0则返回TRUE。

复现 ⭐⭐(绕晕了)

由于labels在base包中有同名函数。为了更好的重复,这里还是用了labels作为变量名称。
第3个 if位置看蒙圈了,复现下。这里加了0与NA,向量labels = c(2,NA,1,0,2) 中非0非NA的值有重复,修改naColor为非颜色值,防止混淆。

# #数据准备
> labels = c(2,NA,1,0,2)
> names(labels) = rep(paste0("gene",1:4))
> naColor = "apple" #设置了一个非颜色设置

## 转换
> labels
## gene1 gene2 gene3 gene4 gene5 
##     2    NA     1     0     2 
> nLabels = labels
# 准备colorSeq相关
> colorSeq = standardColors()
> head(colorSeq)
## [1] "turquoise" "blue"      "brown"    
## [4] "yellow"    "green"     "red" 
> length(colorSeq)
[1] 435
> extColorSeq = colorSeq 

# 最后的colors
> colors = rep("grey", length(nLabels))
> colors 
# [1] "grey" "grey" "grey" "grey" "grey"


> fin = !is.na(nLabels) 
> fin
## gene1 gene2 gene3 gene4 gene5 
## TRUE FALSE  TRUE  TRUE  TRUE

> colors[!fin] = naColor #定义原向量nLabels中为NA值对应位置的颜色为naColor。
> colors
#[1] "grey"  "apple" "grey"  "grey"  "grey" 

> finLabels = nLabels[fin]
> finLabels 
## gene1 gene3 gene4 gene5 
##    2     1     0     2 
> finLabels != 0 #观察
## gene1 gene3 gene4 gene5 
## TRUE  TRUE FALSE  TRUE
> colors[fin] #观察
# [1] "grey" "grey" "grey" "grey"
> finLabels[finLabels != 0] #观察
## gene1 gene3  gene5 
##     2     1      2 
> extColorSeq[finLabels[finLabels != 0]] #观察。按上面1,2数值位置索引出颜色,也就是按非空,非0模块基因索引出位置。
# 颜色向量顺序与非空基因顺序相同。
# [1] "blue"      "turquoise" "blue" 

> colors[fin][finLabels != 0]
# [1] "grey" "grey" "grey"

> colors[fin][finLabels != 0] = extColorSeq[finLabels[finLabels != 0]] #对非空非0的模块编号进行重新配色,替换掉原来的grey
    # 4 if
> dim(labels) #观察
# NULL
> is.null(dim(labels))
# [1] TRUE
> if (!is.null(dim(labels))) #不执行。
    dim(colors) = dim(labels)
> colors
## [1] "blue"      "apple"     "turquoise"
## [4] "grey"      "blue" 

本节小结:

第3个if的位置实现的是,对net$colors 中的0赋值为"grey",对net$colorsNA值赋值为了naColor,默认为"grey",也可以用其它值,这里设为了非颜色“apple”值。

另外这里对0值模块的配色是通过将非0值finLabels != 0重新配色给替换原"grey"完成的。检验NA赋值方式是通过将naColor赋值给非颜色值来探索,以免与原颜色值重复。

小结

  1. WGCNA中的standardColors() 函数输出的配色字符向量长度为435,可以用作一个配色板。

  2. 用层级标题的方式梳理if条件。

  3. 第3个if的位置实现的是,对net$colors 中的0赋值为"grey",对net$colorsNA值赋值为了naColor,默认为"grey",也可以用其它值。

    另外这里对0值模块的配色是通过将非0值finLabels != 0重新配色给替换原"grey"完成的。

相关文章

网友评论

    本文标题:WGCNA之labels2colors()

    本文链接:https://www.haomeiwen.com/subject/vhjigrtx.html