-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimerTool.py
131 lines (119 loc) · 4.75 KB
/
primerTool.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 09:29:08 2019
@author: Pablo Carbonell, SYNBIOCHEM
@description: Run the primers generation.
"""
import argparse
import pandas as pd
import csv
import os
import re
import subprocess
import shutil
def rowCol( pos ):
row = re.search('^(\D+)', pos).group()
col = int( re.search('(\d+)$', pos).group() )
return (row,col)
def nextPos( pos ):
row, col = rowCol( pos )
if ord(row) < ord('H'):
npos = chr( ord(row)+1 ) + str(col)
else:
npos = 'A' + str(col+1)
return npos
def configureTool(args):
template = os.path.join( os.path.dirname( __file__), 'primers.sh' )
if not os.path.exists( args.tempFolder ):
os.makedirs( args.tempFolder )
script = os.path.join( args.tempFolder, 'job.sh' )
log = os.path.join( args.tempFolder, 'log.sh' )
if args.plate is not None:
plate = args.plate
else:
plate = 'None'
df = pd.read_csv(args.plasmids)
icelist = [str(x) for x in df['ICE']]
# Loop through each plasmid to avoid connection issues
seqs = {}
for ice in icelist:
# icelist = ' '.join( icelist )
with open( template ) as hin, open( script, 'w' ) as hout:
for line in hin:
line = re.sub( '{{enzymes}}', args.enzymes, line )
line = re.sub( '{{temp}}', args.temp, line )
line = re.sub( '{{plates}}', plate, line )
line = re.sub( '{{plasmids}}', ice, line )
hout.write( line )
logout = open(log, 'w')
print('Primers for plasmid',ice)
os.chmod(script,777)
subprocess.call( [script], shell=True, stdout=logout, stderr=logout )
os.chdir(os.getenv( 'SBC_ASSEMBLY_PATH' ))
# Output is generated sbc-assembly root folder, it would be better to make a local copy of the code
outfile1 = 'primer_1_primer_phospho.csv'
outfile2 = 'primer_1_primer_nonphospho.csv'
primers = pd.read_csv(outfile1)
for i in primers.index:
part = primers.loc[i,'Sequence Name']
partid,sense = part.split('_')
if partid not in seqs:
seqs[partid] = {}
if sense not in seqs[partid]:
seqs[partid][sense] = primers.loc[i,'Sequence']
if os.path.exists( outfile1 ):
os.unlink( outfile1 )
# Non phosphorylated primers are ignored
if os.path.exists( outfile2 ):
os.unlink( outfile2 )
pos = 'A1'
table = []
with open(args.output, 'w') as output:
for partid in sorted(seqs):
for sense in sorted(seqs[partid]):
table.append( (pos, '_'.join([partid,sense]),seqs[partid][sense]) )
pos = nextPos(pos)
df1 = pd.DataFrame( table,columns=primers.columns )
df1.to_csv(args.output, index=False)
return script, log
def arguments():
parser = argparse.ArgumentParser(description='Read list of primers and output primer plate. Pablo Carbonell, SYNBIOCHEM, 2019')
parser.add_argument('-iceServer', default=os.getenv('ICE_SERVER'),
help='ICE server url.')
parser.add_argument('-iceUser', default=os.getenv('ICE_USERNAME'),
help='ICE user.')
parser.add_argument('-icePass', default=os.getenv('ICE_PASSWORD'),
help='ICE password.')
parser.add_argument('-enzymes', default="MlyI",
help='Comma separated restriction enzymes.')
parser.add_argument('-temp', default="60",
help='Melting temperature.')
parser.add_argument('-plate',
help='Existing plate.')
parser.add_argument('-plasmids',
help='Plasmid csv file.')
parser.add_argument('-output',
help='Output csv file.')
parser.add_argument('-tempFolder',
help='Tool temporary folder.')
return parser
if __name__ == '__main__':
parser = arguments()
args = parser.parse_args()
script, log = configureTool( args )
if False:
logout = open(log, 'w')
print('Running primers script...')
os.chmod(script,777)
subprocess.call( [script], shell=True, stdout=logout, stderr=logout )
os.chdir(os.getenv( 'SBC_ASSEMBLY_PATH' ))
# Output is generated sbc-assembly root folder, it would be better to make a local copy of the code
outfile1 = 'primer_1_primer_phospho.csv'
outfile2 = 'primer_1_primer_nonphospho.csv'
if os.path.exists( outfile1 ):
shutil.copyfile( outfile1, args.output )
os.unlink( outfile1 )
# Non phosphorylated primers are ignored
if os.path.exists( outfile2 ):
os.unlink( outfile2 )