-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
208 lines (184 loc) · 8.9 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from flask import Flask, request, render_template, send_file, redirect, url_for, abort
import os
import subprocess
app = Flask(__name__, static_folder='static', template_folder='templates')
UPLOAD_FOLDER = "uploads"
CONVERTED_FOLDER = "converted"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(CONVERTED_FOLDER, exist_ok=True)
def is_2d_sdf(file_path):
"""
Check if the SDF file contains Z-coordinates (3D information).
Returns True if the file is 2D, otherwise False.
"""
with open(file_path, 'r') as sdf_file:
for line in sdf_file:
if "V2000" in line: # Look for the atom block header
# Read the next few lines to check for Z-coordinates
atom_lines = sdf_file.readlines(10)
for atom_line in atom_lines:
try:
# Extract X, Y, Z coordinates
coords = atom_line.split()[:3]
if len(coords) == 3:
z_coord = float(coords[2])
if z_coord != 0.0:
return False # File is 3D
except (ValueError, IndexError):
pass
return True # File is 2D if no valid Z-coordinates found
return True # Assume 2D if unable to determine
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
smiles_input = request.form.get('smiles_input', None)
conversion_type = request.form['conversion_type']
apply_minimization = request.form.get('apply_minimization', False)
add_hydrogens = request.form.get('add_hydrogens', False)
apply_partial_charges = request.form.get('apply_partial_charges', False) # New option
protonation_ph = request.form.get('protonation_ph', None)
# Ensure conversion_type is valid
valid_conversion_types = [
'smiles_to_sdf', 'smiles_to_mol2', 'smiles_to_pdbqt',
'sdf_to_mol2', 'sdf_to_pdbqt', 'mol2_to_pdbqt', 'pdb_to_pdbqt'
]
if conversion_type not in valid_conversion_types:
return "Invalid conversion type.", 400
if smiles_input:
# Write SMILES string to a temporary file
input_path = os.path.join(UPLOAD_FOLDER, "input.smi")
with open(input_path, 'w') as smi_file:
smi_file.write(smiles_input)
else:
# Handle file upload
file = request.files['file']
if file:
input_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(input_path)
else:
return "No file or SMILES input provided.", 400
# Extract base name (without extension)
base_name, _ = os.path.splitext(os.path.basename(input_path))
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_converted")
# Conversion paths and commands
if conversion_type == 'smiles_to_sdf':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.sdf")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.sdf")
cmd = f"obabel {input_path} -O {output_path} --gen3d"
elif conversion_type == 'smiles_to_mol2':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.mol2")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.mol2")
cmd = f"obabel {input_path} -O {output_path} --gen3d"
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if apply_minimization:
cmd += " --minimize"
elif conversion_type == 'smiles_to_pdbqt':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
cmd = f"obabel {input_path} -O {output_path} --gen3d"
if add_hydrogens:
cmd += " --addh" # Add hydrogens if selected
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if apply_minimization:
cmd += " --minimize"
elif conversion_type == 'sdf_to_mol2':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}.mol2")
renamed_output_path = output_path # No "3D" for non-3D conversions
cmd = f"obabel {input_path} -O {output_path}"
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if is_2d_sdf(input_path):
cmd += " --gen3d"
if apply_minimization:
cmd += " --minimize"
elif conversion_type == 'sdf_to_pdbqt':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}.pdbqt")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
cmd = f"obabel {input_path} -O {output_path}"
if add_hydrogens:
cmd += " --addh" # Add hydrogens if selected
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if is_2d_sdf(input_path):
cmd += " --gen3d"
if apply_minimization:
cmd += " --minimize"
elif conversion_type == 'mol2_to_pdbqt':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
cmd = f"obabel {input_path} -O {output_path}"
if add_hydrogens:
cmd += " --addh" # Add hydrogens if selected
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if apply_minimization:
cmd += " --minimize"
elif conversion_type == 'pdb_to_pdbqt':
output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
renamed_output_path = os.path.join(CONVERTED_FOLDER, f"{base_name}_3D.pdbqt")
cmd = f"obabel {input_path} -O {output_path}"
if add_hydrogens:
cmd += " --addh" # Add hydrogens if selected
if apply_partial_charges:
cmd += " --partialcharge gasteiger" # Add partial charges if selected
if apply_minimization:
cmd += " --minimize"
else:
return "Invalid conversion type.", 400
# Add pH protonation option if specified
if protonation_ph:
try:
protonation_ph = float(protonation_ph) # Ensure pH is numeric
if 0 <= protonation_ph <= 12: # Validate pH range
cmd += f" --ph {protonation_ph}"
else:
return "Invalid pH value. Please provide a value between 0 and 12.", 400
except ValueError:
return "Invalid pH value. Please enter a numeric value.", 400
# Execute the conversion
try:
subprocess.run(cmd, shell=True, check=True)
except subprocess.CalledProcessError as e:
return f"Conversion failed: {e}", 500
# Check if output file exists
if not os.path.exists(output_path):
return "Error: Output file not found. Conversion may have failed.", 500
# Rename the file if necessary
if output_path != renamed_output_path:
os.rename(output_path, renamed_output_path)
# Return the file for download
return send_file(renamed_output_path, as_attachment=True)
# Return the file for download
try:
return send_file(
renamed_output_path,
as_attachment=True,
mimetype='application/octet-stream',
download_name=os.path.basename(renamed_output_path)
)
except Exception as e:
return f"Failed to send the file: {e}", 500
return redirect(url_for('download_file', filename=os.path.basename(renamed_output_path)))
return render_template('index.html')
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
"""
Additional endpoint to serve files from the CONVERTED_FOLDER.
"""
file_path = os.path.join(CONVERTED_FOLDER, filename)
if os.path.exists(file_path):
try:
return send_file(
file_path,
as_attachment=True,
mimetype='application/octet-stream',
download_name=filename
)
except Exception as e:
abort(500, description=f"Failed to download file: {e}")
else:
abort(404, description="File not found.")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))