-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdilution_cal.py
executable file
·113 lines (76 loc) · 2.58 KB
/
dilution_cal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import argparse
""" this program takes a tsv list of fish sample IDs and their current concentrations and
returns the amount of volume needed to dilute the samples to a set concentration
currently two concentrations given and two dilutions calculated, modify accordingly
for single calculation"""
def calc_v1_set_h2o(c1,c2,v2_water):
""" calculate the amount of sample to add to a set water volume
to arrive at desired volume """
v1 = v2_water / ((c1/c2)-1)
return v1
def calc_v1(c1,c2,v2):
""" calculate the amount of initial sample needed
to dilute to a given concentration"""
v1 = (c2*v2)/c1
return (format(c1, '.2f'), format(v1, '.2f'), format(c2, '.2f'), format(v2, '.2f'))
def calc_v2(c1,v1,c2):
""" calculate the final volume of a dilution"""
v2 = (c1*v1)/c2
return (format(c1, '.2f'), format(v1, '.2f'), format(c2, '.2f'), format(v2, '.2f'))
def load_lines(cluster_file):
""" load a tab delimited file into memory as a list of lines"""
list_of_lines = []
with open(cluster_file) as file:
for line in file:
strip_line = line.rstrip()
split_line = strip_line.split('\t')
list_of_lines.append(split_line)
return list_of_lines
def write_lines(output_list, filename):
""" write a list of lines to a tab delimited file"""
for line in output_list:
outstring = '\t'.join(map(str, line))
output = open(filename, 'a')
output.write(outstring)
output.write('\n')
output.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("input", help="input the tsv with ids and concentrations")
args = parser.parse_args()
#values we are trying to get to
final_conc = 10. #ng/ul
set_water_volume = 100
input_data = load_lines(args.input)
output_list = []
for line in input_data:
""" get fish information """
sample_id = line[0]
if line[1] == '-':
outlist_stock = ['stock',
sample_id,
'-',
'-',
'-',
'-',
'-']
output_list.append(outlist_stock)
continue
c1_stock = float(line[1])
v1 = calc_v1_set_h2o(c1_stock, final_conc, set_water_volume)
v2 = set_water_volume + v1
outlist_stock = ['stock',
sample_id,
format(c1_stock, '.2f'),
format(v1, '.2f'),
format(final_conc, '.2f'),
format(v2, '.2f'),
set_water_volume]
output_list.append(outlist_stock)
""" write the output to file """
output_file = 'calculated_' + args.input
output_dat = open(output_file, 'a')
output_dat.write('source\tFish_ID\tstock_conc\tstock_vol_req\tc2\tv2\twater_needed\n')
output_dat.close()
write_lines(output_list,output_file)