-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcr3Tool.py
113 lines (106 loc) · 4.29 KB
/
lcr3Tool.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 09:29:08 2019
@author: Pablo Carbonell, SYNBIOCHEM
@description: Run the LCR3 worklist generation.
"""
import argparse
import pandas as pd
import csv
import os
import re
import subprocess
import shutil
import zipfile
import glob
import tempfile
import shutil
def localTool(source,target,plates):
""" LCR2 requires having in data/plates the appropriate plates
for the current parts. Here the strategy is to copy the source
code and replace data/plates with user's plates
"""
shutil.copytree(source, target)
if plates is not None:
""" If plates is empty, take the default plates,
otherwise, replace the plate files"""
dpath = os.path.join(target,'data',)
ppath = os.path.join(dpath,'plates')
for p in glob.glob(os.path.join(ppath, '*')):
os.unlink(p)
for p in plates:
if zipfile.is_zipfile(p):
with zipfile.ZipFile(p) as myzip:
myzip.extractall(path=dpath)
else:
shutil.copy(p,ppath)
""" Empty out folder """
outdir = os.path.join(target,'out')
if os.path.exists(outdir):
shutil.rmtree(outdir)
if not os.path.exists(outdir):
os.mkdir(outdir)
def configureTool(args):
""" Configure a local LCR3 tool based on the template """
template = os.path.join( os.path.dirname( __file__), 'lcr3_job.sh' )
if args.tempFolder is None:
tmpFolder = tempfile.mkdtemp()
else:
tmpFolder = args.tempFolder
if not os.path.exists( tmpFolder ):
os.makedirs( tmpFolder )
script = os.path.join( tmpFolder, 'job.sh' )
log = os.path.join( tmpFolder, 'log.sh' )
df = pd.read_csv(args.plasmids)
icelist = [str(x) for x in df['ICE']]
icelist = ' '.join( icelist )
source = os.getenv('SBC_ASSEMBLY_PATH')
target = os.path.join( tmpFolder, os.path.basename(source) )
with open( template ) as hin, open( script, 'w' ) as hout:
for line in hin:
line = re.sub( '{{plasmids}}', icelist, line )
line = re.sub( '{{path}}', target, line )
hout.write( line )
localTool(source,target,args.plates)
return script, log, target
# Zip the files from given directory that matches the filter
def zipFilesInDir(dirName, zipFileName):
# create a ZipFile object
with zipfile.ZipFile(zipFileName, 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
print(os.path.join(os.path.relpath(folderName, dirName), filename))
zipObj.write(filePath, arcname=os.path.join(os.path.relpath(folderName, dirName), filename))
def arguments():
parser = argparse.ArgumentParser(description='Work list generation for Ligase Cycling Reaction (LCR). 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('-plasmids',
help='Plasmid csv file.')
parser.add_argument('-output',
help='Output zip file.')
parser.add_argument('-tempFolder',
help='Tool temporary folder.')
parser.add_argument('-plates', action="append",
help='Plate files (csv or xlsx or zip).')
return parser
if __name__ == '__main__':
parser = arguments()
args = parser.parse_args()
# Fill out template and create a tmp copy of the code with the plates
script, log, target = configureTool( args )
logout = open(log, 'w')
print('Running lcr3 script...')
subprocess.call( "bash "+script, shell=True, stdout=logout, stderr=logout )
print('Done.')
# Zip the contents of the "out" folder into the output file
zipFilesInDir(os.path.join( target, 'out'), args.output )