3
\$\begingroup\$

I wrote this code so I could figure out the number of molecules within a plasmid and an insert of DNA. I then get the amount of insert DNA I would need to use (in uL) for a ligation with the DNA at different insert ratios and dilutions.

How can I clean up this code? It works correctly. However, I feel like I typed this out all the extremely long way and I would love to know how to make this more performant.

In my next project I would like to not have to type all of these out. I imagine that I am missing some functions or loops. I am still a very new beginner and this is my first project outside of a classroom.

print "Please put the amount of BPs: "
number = int(raw_input())
solution = (number * 660)
Avo = (6.022 * (10 ** 23))
Grams_Avo = ( solution / Avo)
print "Put in Concentration (ng/ul): "
conc = float(raw_input())
multiple = (10 ** -9)
amount = (conc * multiple)
total = float(amount / Grams_Avo)
print "Amount of molecules:  %s" % total


print "Amount of ul used by the plasmid: "
plasmid = float(raw_input())
particles = float(plasmid * total)
print "What ratio amount will you use?: "
ratio = int(raw_input())
molecules_needed = float(particles * ratio)
print molecules_needed


print "Conc. of your oligo?: "
oligo = float(raw_input())
oligo_num = float(oligo * multiple) # amount 
print "Enter number of G in Oligo: "
G = int(raw_input())
print "Enter number of A in Oligo: "
A = int(raw_input())
print "Enter number of T in Oligo: "
T = int(raw_input())
print "Enter number of C in Oligo: "
C = int(raw_input())
sumG = (G * 329)
sumA = (A * 313)
sumT = (T * 304)
sumC = (C * 289)
daoligo = (sumG + sumA + sumT + sumC) # g/mol

print "Total amount of your insert %s" % daoligo

oligototal = float(oligo_num / (daoligo/Avo)) # molecules for insert
complete = float(molecules_needed/oligototal) #amount of insert needed from stock ul

print "What dilution will you use?"
print "1. Stock"
print "2. 1:10"
print "3. 1:20"
print "4. 1:30"
print "5. 1:50"
code = float(raw_input())
if code == 1:
    print complete #stock
elif code == 2:
    code2 = (oligototal/10)
    complete1 = float(molecules_needed/code2)
    print complete1 #1:10
elif code == 3:
    code3 (oligototal/20)
    complete2 = float(molecules_needed/code3)
    print complete2 #1:20
elif code == 4:
    code4 = (oligototal/30)
    complete3 = float(molecules_needed/code4)
    print complete3 #1:30
elif code == 5:
    code_5 = (oligototal/50)
    complete4 = float(molecules_needed/code_5)
    print complete4 #1:50
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

For the starter, you can create small function which will get input since your code requires too much user input.

Just making that one function will reduce repetitive code of raw_input() and printing it's msg.

Also, your code will be more readable.

Code:

def get_input(msg):
    print msg
    val = int(raw_input())
    return val

number = get_input("Please put the amount of BPs: ")

solution = (number * 660)
Avo = (6.022 * (10 ** 23))
Grams_Avo = ( solution / Avo)

conc = float(get_input("Put in Concentration (ng/ul): "))

multiple = (10 ** -9)
amount = (conc * multiple)
total = float(amount / Grams_Avo)
print "Amount of molecules:  %s" % total

plasmid = float(get_input("Amount of ul used by the plasmid: "))
particles = float(plasmid * total)

ratio = get_input("What ratio amount will you use?: ")

molecules_needed = float(particles * ratio)
print molecules_needed

oligo = float(get_input("Conc. of your oligo?: "))
oligo_num = float(oligo * multiple) # amount

G = get_input("Enter number of G in Oligo: ")

A = get_input("Enter number of A in Oligo: ")

T = get_input("Enter number of T in Oligo: ")

C = get_input("Enter number of C in Oligo: ")

sumG = (G * 329)
sumA = (A * 313)
sumT = (T * 304)
sumC = (C * 289)
daoligo = (sumG + sumA + sumT + sumC) # g/mol

print "Total amount of your insert %s" % daoligo

oligototal = float(oligo_num / (daoligo/Avo)) # molecules for insert
complete = float(molecules_needed/oligototal) #amount of insert needed from stock ul

m = """
    What dilution will you use?
    1. Stock
    2. 1:10
    3. 1:20
    4. 1:30
    5. 1:50
    """

code = float(raw_input())
code =  get_input(m)

if code == 1:
    print complete #stock
elif code == 2:
    code2 = (oligototal/10)
    complete1 = float(molecules_needed/code2)
    print complete1 #1:10
elif code == 3:
    code3 = (oligototal/20)
    complete2 = float(molecules_needed/code3)
    print complete2 #1:20
elif code == 4:
    code4 = (oligototal/30)
    complete3 = float(molecules_needed/code4)
    print complete3 #1:30
elif code == 5:
    code_5 = (oligototal/50)
    complete4 = float(molecules_needed/code_5)
    print complete4

Output

Please put the amount of BPs: 
12
Put in Concentration (ng/ul): 
1
Amount of molecules:  76035353535.4
Amount of ul used by the plasmid: 
2
What ratio amount will you use?: 
3
4.56212121212e+11
Conc. of your oligo?: 
4
Enter number of G in Oligo: 
4
Enter number of A in Oligo: 
6
Enter number of T in Oligo: 
7
Enter number of C in Oligo: 
8
Total amount of your insert 7634
9

    What dilution will you use?
    1. Stock
    2. 1:10
    3. 1:20
    4. 1:30
    5. 1:50

1
1.44583333333
\$\endgroup\$
0
1
\$\begingroup\$

There are some valid points in the other answer but it doesn't address all problems with the code. I have a few additions to make it cleaner IMO

  1. Make it work for both python3 and python2.
  2. use a if __name__ == '__main__': body.
  3. Added a dictionary to map the code (no more need for an if else block)
  4. According to the python style guide, functions and variables should be snake_case

def get_input(msg):
    return float(raw_input(msg))

def main():
    number = get_input("Please put the amount of BPs: ")
    solution = (number * 660)
    avo = (6.022 * (10 ** 23))
    grams_avo = ( solution / avo)

    conc = float(get_input("Put in Concentration (ng/ul): "))
    multiple = (10 ** -9)
    amount = (conc * multiple)
    total = float(amount / grams_avo)
    print ("Amount of molecules: {}".format(total))

    plasmid = float(get_input("Amount of ul used by the plasmid: "))
    particles = float(plasmid * total)

    ratio = get_input("What ratio amount will you use?: ")
    molecules_needed = float(particles * ratio)
    print (molecules_needed)

    oligo = float(get_input("Conc. of your oligo?: "))
    oligo_num = float(oligo * multiple)

    G = get_input("Enter number of G in Oligo: ")
    A = get_input("Enter number of A in Oligo: ")
    T = get_input("Enter number of T in Oligo: ")
    C = get_input("Enter number of C in Oligo: ")

    sum_G = (G * 329)
    sum_A = (A * 313)
    sum_T = (T * 304)
    sum_C = (C * 289)
    daoligo = (sum_G + sum_A + sum_T + sum_C)

    print ("Total amount of your insert {}".format(daoligo))

    oligototal = float(oligo_num / (daoligo/ avo))
    complete = float(molecules_needed/oligototal)

    m = """
What dilution will you use?
1. Stock
2. 1:10
3. 1:20
4. 1:30
5. 1:50
"""

    code = get_input(m)
    code_dict = {1: 1, 2: 10, 3: 20, 4: 30, 5: 50}
    print (float(molecules_needed/code_dict[code]))

if __name__ == '__main__':
    # Cheat way to make input work in both Python3 and Python2
    try:
        raw_input = input
    except NameError:
        pass

    main()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.