Exemple #1
0
    A = line
    while (A[-1] in ['\n', '\r']):
        A = A[:-1]
    B = f.readline()
    while (B[-1] in ['\n', '\r']):
        B = B[:-1]
    f.readline()
    line = f.readline()

    # Run the NW algorithm

    print("First sequence:", A)
    print("Second sequence:", B)
    print("Calculating alignment distance by Needleman-Wunsch method...")

    z = nw(A, B, simMatrix, gapPenalty, alphEnum)

    print("Alignment of A: ", z[0])
    print("Alignment of B: ", z[1])
    print("Similarity score: ", z[2], '\n')

    # Write outputs to text file
    g.write(str(z[2]) + "\n")
    g.write(z[0] + "\n")
    g.write(z[1] + "\n")
    g.write("\n")

# Close the files and finish
f.close()
g.close()
random.seed()

# Number of trials
trials = 1000

print "Testing..."

for t in range(trials):
    fail = False
    n = random.randint(5, 100)
    k = random.randint(1, n)
    parent = ""
    for i in range(n):
        parent = parent + random.choice(alphabet)
    child, work = mutate(parent, k)
    nwout = nw(parent, child, simMatrix, gapPenalty, alphEnum)
    hbout = hirschberg(parent, child, simMatrix, gapPenalty, alphEnum)
    
    #Verification tests
    compress = nwout[0].replace('-', '')
    if compress != parent:
        fail = True
    compress = nwout[1].replace('-', '')
    if compress != child:
        fail = True
    compress = hbout[0].replace('-', '')
    if compress != parent:
        fail = True
    compress = hbout[1].replace('-', '')
    if compress != child:
        fail = True
trials = 100
repeats = 5

table = []

for t in range(trials):
    n = random.randint(1, 1001)
    m = random.randint(1, 1001)
    A = B = ""
    for i in range(n):
        A += random.choice(alphabet)
    for j in range(m):
        B += random.choice(alphabet)

    # Time the nw routine   
    t0 = time.clock()
    for k in range(repeats):
        nwout = nw(A, B, simMatrix, gapPenalty, alphEnum)
    t1 = time.clock()
    nwtime = (t1 - t0)/repeats

    # Time the hirschberg routine
    t0 = time.clock()
    for k in range(repeats):
        hbout = hirschberg(A, B, simMatrix, gapPenalty, alphEnum)
    t1 = time.clock()
    hbtime = (t1 - t0)/repeats

    table.append([n, m, n*m, nwtime, hbtime])
    print table[-1]
Exemple #4
0
random.seed()

# Number of trials
trials = 1000

print "Testing..."

for t in range(trials):
    fail = False
    n = random.randint(5, 100)
    k = random.randint(1, n)
    parent = ""
    for i in range(n):
        parent = parent + random.choice(alphabet)
    child, work = mutate(parent, k)
    nwout = nw(parent, child, simMatrix, gapPenalty, alphEnum)
    hbout = hirschberg(parent, child, simMatrix, gapPenalty, alphEnum)

    #Verification tests
    compress = nwout[0].replace('-', '')
    if compress != parent:
        fail = True
    compress = nwout[1].replace('-', '')
    if compress != child:
        fail = True
    compress = hbout[0].replace('-', '')
    if compress != parent:
        fail = True
    compress = hbout[1].replace('-', '')
    if compress != child:
        fail = True