【问题标题】:R function keeps returning NULLR函数不断返回NULL
【发布时间】:2014-06-29 22:45:26
【问题描述】:

我在 R 中有一个非常奇怪的问题。问题是为全局和半全局对齐创建一个函数。制定了适当的算法,能够“打印出”正确的对齐方式。然而,“返回”alginment 似乎是半全局算法的一个问题。

以下是两个对齐的函数,它们都包含两个函数:一个计算得分矩阵,另一个输出对齐。如您所见,半全局的输出函数受到全局函数的启发,但尽管它能够打印出值 A 和 B,但当返回 A 和 B 时,返回值 NULL。

我注意到在定义 A 和 B 时,它们还包含一个 NULL 部分,通过在末尾打印 A 和 B 的结构可以看到。在全局对齐中也是如此,但在这里似乎不是问题。

全局对齐算法

########### GLOBAL ALLIGNMENT ALGORITHM ############

GA_score = function(v,w,score.gap=-3,score.match=8,score.mismatch=-5){
v = strsplit(v,split="")[[1]]
w = strsplit(w,split="")[[1]]

S = matrix(0,nrow=(length(v)+1),ncol = (length(w)+1) )
S[1,1] = 0

for(j in 2:dim(S)[2]){
    S[1,j] = score.gap*(j-1)
}

for(i in 2:dim(S)[1]){
    S[i,1] = score.gap*(i-1)
    for(j in 2:dim(S)[2]){
    if(v[i-1]==w[j-1]){diag = S[i-1,j-1] + score.match} else {diag =             S[i-1,j-1] + score.mismatch}
        down = S[i-1,j] + score.gap
        right = S[i,j-1] + score.gap
        S[i,j] = max(diag,down,right) 
    }
}
return(S)
}


GA_output = function(v,w,S,score.gap=-3,score.match=8,score.mismatch=-5){
v = strsplit(v,split="")[[1]]
w = strsplit(w,split="")[[1]]
A=c()
B=c()

GA_rec = function(A,B,S,i,j,v,w,score.gap,score.match,score.mismatch){

    if (i==1 | j==1){
        if(i>1){
            for(i1 in seq(i-1,1,-1)){ 
                A = c(v[i1],A)
                B = c("-",B)
            }
        }
        if(j>1){
            for(j1 in seq(j-1,1,-1)){
                A = c("-",A)
                B = c(w[j1],B)
            }
        }
    return(list(v=A,w=B))
    }

    if(v[i-1]==w[j-1] ){diag = score.match} else {diag=score.mismatch}


    if (S[i,j] == (S[i-1,j-1] + diag)){
        A.temp = c(v[i-1],A)
        B.temp = c(w[j-1],B)
        GA_rec(A.temp,B.temp,S,i-1,j-1,v,w,score.gap,score.match,score.mismatch)
    }

    else if (S[i,j] == (S[i-1,j] + score.gap)){
        A.temp <- c(v[i-1],A)
        B.temp <- c("-",B)
        GA_rec(A.temp,B.temp,S,i-1,j,v,w,score.gap,score.match,score.mismatch)
    }
    else {
        A.temp = c("-",A)
        B.temp = c(w[j-1],B)
        GA_rec(A.temp,B.temp,S,i,j-1,v,w,score.gap,score.match,score.mismatch)
    }
}

return( GA_rec(A,B,S,length(v)+1,length(w)+1,v,w,score.gap,score.match,score.mismatch))


}

半全局对齐算法

########### SEMI GLOBAL ALLIGNMENT ALGORITHM ############
SGA_score = function(sequence1,sequence2,score.gap=-1,score.match=1,score.mismatch=-1){
v=sequence2
w=sequence1

v = strsplit(v,split="")[[1]]
w = strsplit(w,split="")[[1]]
S = matrix(0,nrow=length(v)+1,ncol=length(w)+1)

for(i in 1:(length(w)+1)){
    for( j in 1:(length(v)+1)){
        if (i==1|j==1){S[i,j]=0}
        else{
            if((i==length(w)+1) | (j==length(v)+1)){
                from.top = S[i,j-1]
                from.left = S[i-1,j]
            }
            else{
                from.top = max(S[i,j-1]+score.gap)   # Max is artifact from max(0,... )
                from.left = max(S[i-1,j]+score.gap)
            }
            if(w[i-1] == v[j-1]){
                from.diag = S[i-1,j-1]+score.match
            }
            else{
                from.diag = S[i-1,j-1]+score.mismatch
            }
            S[i,j] = max(from.top,from.left,from.diag)
        }
    }

}
return(S)
}

SGA_output = function(v,w,S,score.gap=-1,score.match=1,score.mismatch=-1){
v = strsplit(v,split="")[[1]]
w = strsplit(w,split="")[[1]]
A=c()
B=c()
print(str(A))
print(str(B))

SGA_rec = function(A,B,S,i,j,v,w,score.gap,score.match,score.mismatch){

    if (i==1 | j==1){
        if(i>1){
            for(i1 in seq(i-1,1,-1)){ 
                A = c(v[i1],A)
                B = c("-",B)
            }
        }
        if(j>1){
            for(j1 in seq(j-1,1,-1)){
                A = c("-",A)
                B = c(w[j1],B)
            }
        }
        print(A)
        print(B)
        out = list(v=A,w=B)
        #print(out)
        print(str(A))
        print(str(B))
        print(str(out))
        return(out)
    }

    if(v[i-1]==w[j-1] ){diag = score.match} else {diag=score.mismatch}


    if (S[i,j] == (S[i-1,j-1] + diag)){
        A.temp = c(v[i-1],A)
        B.temp = c(w[j-1],B)
        SGA_rec(A.temp,B.temp,S,i-1,j-1,v,w,score.gap,score.match,score.mismatch)
    }

    #####
    if ( j==length(w)+1) {  # Are we in last row?
        score.temp = score.gap
        score.gap=0
    }
    else{score.temp=score.gap}

    if(S[i,j] == (S[i-1,j] + score.gap)){
        A.temp <- c(v[i-1],A)
        B.temp <- c("-",B)
        score.gap = score.temp
        SGA_rec(A.temp,B.temp,S,i-1,j,v,w,score.gap,score.match,score.mismatch)

    }
    score.gap=score.temp
    ####
    if(i==length(v)+1){
        score.temp=score.gap
        score.gap=0
    }
    else{score.temp=score.gap}

    if(S[i,j] == (S[i,j-1] + score.gap)){
        A.temp = c("-",A)
        B.temp = c(w[j-1],B)
        score.gap=score.temp
        SGA_rec(A.temp,B.temp,S,i,j-1,v,w,score.gap,score.match,score.mismatch)
    }

}

return(SGA_rec(A,B,S,length(v)+1,length(w)+1,v,w,score.gap,score.match,score.mismatch))


}


S1 = SGA_score("ACGTCAT","TCATGCA")
S1


align = SGA_output("ACGTCAT","TCATGCA",S1)
align

我很惊讶全局对齐有效,但半全局对齐无效,即使它们都有这个 NULL 部分(有人可以解释这是什么吗?它与函数中的内部对象有关吗?)半全局的知道 A 和 B 是什么。

非常感谢任何帮助!

【问题讨论】:

  • 听说过最小可重现的例子吗?

标签: r function recursion null return


【解决方案1】:

SGA_rec 似乎缺少返回值。在最后一个 if 之后需要一个 else {return(&lt;something&gt;))

插图:

fun <- function() if (FALSE) 1
x <- fun()
x
#NULL

阅读help("NULL") 了解其含义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2022-11-22
    • 2016-01-30
    • 2012-05-11
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多