R函数保持返回NULL

时间:2014-05-12 14:07:54

标签: r function recursion null return

我在R中遇到了一个非常奇怪的问题。问题是为全局和半全球排列制定一个函数。制作了适当的算法,能够打印出#34;正确的分配。但是"返回"对于半全局算法来说,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是什么。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

SGA_rec似乎缺少返回值。在最后else {return(<something>))之后,您需要if

插图:

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

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

相关问题