1

I am trying to implement the partial digest problem, the algorithm is given in this pdf https://cise.ufl.edu/class/cap5515sp10/Ch04_DNA_mapping.pdf on pages 35-36. There is an example given on the subsequent pages.

I am unable to get the the correct answer.

The value of X that I get is [0, 10, 8, 3, 6] and then the recursion stops with "Not ok".

May be I do not understand the algorithm or something else?

width = 0

def partialDigest(L):
    print "partialDigest"
    global width
    width = max(L)
    L.remove(width)
    X = [0, width]
    if place(L, X):
        print "Ok"
    else:
        print "Not ok"


def place(L, X):
    print "place"
    print "Width is", width
    print "L is ", L
    print "X is ", X

    if len(L) == 0:
        print "Output is: ", X
        return True

    y = max(L)
    print "Y is", y
    #L.remove(y)
    d = D(y, X)
    print "d is ", d

    if set(d).issubset(set(L)):
        print "First if"
        print "D is", d
        print "X before is ", X
        X.append(y)
        print "X after is ", X
        print "L before is", L
        L = removeElements(d, L)
        print "L after is ", L
        place(L, X)
        X.remove(y)
        L.extend(d)

    d1 = D(abs(width-y), X)
    print "d1 is ", d1

    if set(d1).issubset(set(L)):
        print "Second if"
        print "D is", d1
        print "X before is ", X
        X.append(abs(width-y))
        print "X after is ", X
        print "L before is", L
        L = removeElements(d1, L)
        print "L after is ", L
        place(L, X)
        X.remove(abs(width-y))
        L.extend(d1)

    return False

def D(y, X):
    diff = []
    for xi in X:
        diff.append(abs(y-xi))
    return diff

def removeElements(d, L):
    for i in L:
        for j in d:
            if i == j:
                L.remove(i)
                d.remove(i)
    return L

if __name__ == "__main__":
    print "Python implementation of partial digetive problem PDP on page 90."

    partialDigest([2, 2, 3, 3, 4, 5, 6, 7, 8, 10])
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
limitlessriver
  • 751
  • 1
  • 6
  • 9
  • Can you add the expected correct output? Otherwise it is difficult for other users to help you. – Maximilian Peters Jul 08 '16 at 10:30
  • @MaximilianPeters So [0, 10, 8, 3, 6] is one of the answer and the other answer is [0, 10, 2, 7, 4]. I have got the code working, in the new code I don't use issubset() function of python. If you have any better insights into this algorithm please let me know as I am new to bioinformatics. – limitlessriver Jul 08 '16 at 19:11

1 Answers1

1

Finally I have the code running correctly, may be I was messing up the python's global space or some helper function.

X = []

L = [2, 2, 3, 3, 4, 5, 6, 7, 8, 10]

width = 0

def partialDigest(L):
    global X, width
    width = max(L)
    L.remove(width)
    X = [0, width]
    place(L, X)


def place(L, X):

    if not L:
        print "Output is: ", X
        return

    y = max(L)

    if issubset(y, X, L):
        X.append(y)
        removeElements(y, X, L)
        place(L, X)
        if y in X:
            X.remove(y)
        L.extend(D(y, X))

    if issubset(abs(width-y), X, L):
        X.append(abs(width-y))
        removeElements(abs(width-y), X, L)
        place(L, X)
        if abs(width-y) in X:
            X.remove(abs(width-y))
        L.extend(D(abs(width-y), X))

    return


def D(y, X):
    diff = []
    for xi in X:
        diff.append(abs(y-xi))
    return diff


def removeElements(y, X, L):
    for xi in X:
        if abs(y - xi) in L:
            L.remove(abs(y - xi))


def issubset(y, X, L):
        for xi in X:
            if abs(y-xi) not in L:
                return False
        return True


if __name__ == "__main__":
    print "Python implementation of partial digetive problem PDP on page 90."

    partialDigest(L)
limitlessriver
  • 751
  • 1
  • 6
  • 9
  • The output solution that is shows me is Output is: [0, 10, 8, 3, 6] Output is: [0, 10, 2, 7, 4] so even if you get your right answer the other one is not correct. – Suliman Sharif Apr 29 '17 at 02:33