From 3d916141895a78b52b080009184f87ae430d944c Mon Sep 17 00:00:00 2001 From: Moria Date: Mon, 28 Sep 2015 13:02:13 -0400 Subject: [PATCH] Fixed output formatting --- center_string.py | 35 ++++++++++++++++++++++++++--------- output.txt | 26 +++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/center_string.py b/center_string.py index d6692b3..9d508a3 100644 --- a/center_string.py +++ b/center_string.py @@ -39,26 +39,36 @@ OUTPUT import os import sys +# I need to symlink my Anaconda install with this :/ sys.path.append('/usr/local/lib/python2.7/dist-packages') import cplex -# Absolutepath to inputs, outputs +# Absolute path to inputs, outputs path_to_inputs = '/home/moria/Projects/Bioinformatics/CenterStringLP/Inputs/' path_to_outputs = '/home/moria/Projects/Bioinformatics/CenterStringLP/Outputs/' + +# Master bases bases = ['A','C','T','G'] +# For each input file... for fn in os.listdir(path_to_inputs): + # Split filename into prefix, suffix pre,suff = fn.split('.') - # Reset line counter + # Reset line counter, init array S to store sequences line_num = 0 - S = [] + S = [] + # Open the files f_in = open(path_to_inputs+fn, 'r') f_out = open(path_to_outputs+pre+'.lp', 'w') + + # For each line in the input file... for line in f_in: + + # If line num is 0, grab n,m else put stripped sequence into S if line_num == 0: # get number of strings, length of strings m,n = line.split(' ') @@ -66,9 +76,12 @@ for fn in os.listdir(path_to_inputs): n = int(n) line_num += 1 else: - S.append(str(line)) + S.append(str(line.strip())) + line_num += 1 # Write LP + + # We want to minimize d subject to constraints f_out.write('Minimize\n') f_out.write(' obj: d\n') f_out.write('Subject To\n') @@ -77,8 +90,9 @@ for fn in os.listdir(path_to_inputs): # For each sequence in S for s in S: + + # Write first part - c#: equation = ' ' - s = s.strip() equation += 'c' + str(count) + ': ' count += 1 @@ -87,15 +101,17 @@ for fn in os.listdir(path_to_inputs): equation += ' - ' + str(v) + str(i) equation += ' - d <= - ' + str(n) + '\n' f_out.write(equation) - + + # All Xi's must add up to 1 for number in range(n): constraint = ' c' + str(count) + ': ' count += 1 for letter in bases: constraint += ' + ' + letter + str(number) constraint += ' = 1\n' - f_out.write(constraint) + + # No bounds, just binary numbers f_out.write('Bounds\n') f_out.write('Binaries\n') for number in range(n): @@ -116,9 +132,10 @@ for fn in os.listdir(path_to_inputs): lp_problem.solve() soln = '' + # If Xi = 1, append it to the solution as this is the center string in element i for number in range(n): for letter in bases: if lp_problem.solution.get_values(letter+str(number)) == 1: soln += letter - print "Center string is: " + soln + "\n" - print "d is " + str(lp_problem.solution.get_values('d')) + "\n" + print "\n\nCenter string is: " + soln + "\n" + print "d is " + str(lp_problem.solution.get_values('d')) + "\n\n\n\n###################" diff --git a/output.txt b/output.txt index 818b66c..cda3e68 100644 --- a/output.txt +++ b/output.txt @@ -38,10 +38,15 @@ Parallel b&c, 2 threads: Wait time (average) = 0.00 sec. ------------ Total (root+branch&cut) = 0.01 sec. (6.49 ticks) + + Center string is: CGAAGACTATAAATAGCGAGGCCTTAAAGAACTTCCAATGTTCTAGAACCTATGCTTGAGCTGACACTACGGCATCGATTTTCTAATGCTCCGACCCATC d is 59.0 + + +################### Solving input3.lp Found incumbent of value 25.000000 after 0.00 sec. (0.01 ticks) @@ -86,10 +91,15 @@ Parallel b&c, 2 threads: Wait time (average) = 0.00 sec. ------------ Total (root+branch&cut) = 0.00 sec. (0.71 ticks) + + Center string is: CAGAGGTAAAAGAAAGGGGGACAAT d is 3.0 + + +################### Solving input1.lp Found incumbent of value 5.000000 after 0.00 sec. (0.00 ticks) @@ -124,20 +134,25 @@ Root relaxation solution time = 0.00 sec. (0.02 ticks) 0 0 2.5000 2 4.0000 2.5000 6 37.50% * 0+ 0 3.0000 2.5000 6 16.67% 0 0 cutoff 3.0000 3.0000 6 0.00% -Elapsed time = 0.03 sec. (0.17 ticks, tree = 0.00 MB, solutions = 3) +Elapsed time = 0.00 sec. (0.17 ticks, tree = 0.00 MB, solutions = 3) Root node processing (before b&c): - Real time = 0.03 sec. (0.17 ticks) + Real time = 0.00 sec. (0.17 ticks) Parallel b&c, 2 threads: Real time = 0.00 sec. (0.00 ticks) Sync time (average) = 0.00 sec. Wait time (average) = 0.00 sec. ------------ -Total (root+branch&cut) = 0.03 sec. (0.17 ticks) +Total (root+branch&cut) = 0.00 sec. (0.17 ticks) + + Center string is: AAACC d is 3.0 + + +################### Solving input2.lp Found incumbent of value 10.000000 after 0.00 sec. (0.01 ticks) @@ -183,7 +198,12 @@ Parallel b&c, 2 threads: Wait time (average) = 0.00 sec. ------------ Total (root+branch&cut) = 0.00 sec. (0.71 ticks) + + Center string is: ACAAACCCCC d is 6.0 + + +###################