alimotahharynia commited on
Commit
bb97c07
1 Parent(s): 97b184d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -17
app.py CHANGED
@@ -107,40 +107,71 @@ class SMILESGenerator:
107
  # Gradio interface
108
  def generate_smiles_gradio(sequence_input=None, uniprot_id=None, num_generated=10):
109
  results = {}
 
110
 
111
- # Process protein sequences
112
  if sequence_input:
113
  sequences = [seq.strip() for seq in sequence_input.split(",") if seq.strip()]
114
  for seq in sequences:
115
  try:
116
- # Always attempt to generate SMILES from the sequence (regardless of validity)
 
 
 
 
 
 
 
 
 
117
  smiles = generator.generate_smiles(seq, num_generated)
118
- results[seq] = {"sequence": seq, "smiles": smiles}
 
 
 
 
 
 
119
  except Exception as e:
120
- results[seq] = {"sequence": seq, "error": f"Error generating SMILES: {str(e)}"}
 
 
 
 
121
 
122
- # Process UniProt IDs
123
  if uniprot_id:
124
  uniprot_ids = [uid.strip() for uid in uniprot_id.split(",") if uid.strip()]
125
  for uid in uniprot_ids:
126
- sequence = uniprot_to_sequence.get(uid, None) # None if not found
127
  try:
128
- if sequence:
 
129
  smiles = generator.generate_smiles(sequence, num_generated)
130
- results[uid] = {"sequence": sequence, "smiles": smiles}
 
 
 
131
  else:
132
- # UniProt ID not found
133
- results[uid] = {"sequence": "N/A", "error": f"UniProt ID {uid} not found in dataset."}
 
 
134
  except Exception as e:
135
- results[uid] = {"sequence": "N/A", "error": f"Error generating SMILES: {str(e)}"}
 
 
 
136
 
 
137
  if not results:
138
- return {"error": "No valid input provided. Please try again with different sequences or UniProt IDs."}
139
 
140
- # Save
141
  file_path = save_smiles_to_file(results)
142
- return results, file_path
143
 
 
 
144
 
145
  # Main initialization and Gradio setup
146
  if __name__ == "__main__":
@@ -227,8 +258,16 @@ if __name__ == "__main__":
227
  .gr-row {
228
  margin-bottom: 20px;
229
  }
230
- """
231
 
 
 
 
 
 
 
 
 
 
232
  iface.css = custom_css
233
  gr.Markdown("## GPT-2 Drug Generator", elem_id="app-title")
234
  gr.Markdown(
@@ -246,7 +285,7 @@ if __name__ == "__main__":
246
  uniprot_id_input = gr.Textbox(
247
  label="UniProt IDs",
248
  placeholder="Enter UniProt IDs separated by commas (e.g., P12821, P37231, ...)",
249
- lines=1,
250
  )
251
 
252
  num_generated_slider = gr.Slider(
@@ -258,7 +297,10 @@ if __name__ == "__main__":
258
  )
259
 
260
  output = gr.JSON(label="Generated SMILES")
261
- file_output = gr.File(label="Download Results as JSON")
 
 
 
262
 
263
  generate_button = gr.Button("Generate SMILES", elem_id="generate-button")
264
 
 
107
  # Gradio interface
108
  def generate_smiles_gradio(sequence_input=None, uniprot_id=None, num_generated=10):
109
  results = {}
110
+ uniprot_counter = 0 # Counter for sequences without UniProt IDs
111
 
112
+ # Process sequence inputs and include UniProt ID if found
113
  if sequence_input:
114
  sequences = [seq.strip() for seq in sequence_input.split(",") if seq.strip()]
115
  for seq in sequences:
116
  try:
117
+ # Find the corresponding UniProt ID for the sequence
118
+ uniprot_id_for_seq = [uid for uid, s in uniprot_to_sequence.items() if s == seq]
119
+ if uniprot_id_for_seq:
120
+ uniprot_id_for_seq = uniprot_id_for_seq[0]
121
+ else:
122
+ # Assign a number as the key for sequences without UniProt IDs
123
+ uniprot_id_for_seq = str(uniprot_counter)
124
+ uniprot_counter += 1
125
+
126
+ # Generate SMILES for the sequence
127
  smiles = generator.generate_smiles(seq, num_generated)
128
+
129
+ # UniProt ID or the numeric key as the key
130
+ results[uniprot_id_for_seq] = {
131
+ "sequence": seq,
132
+ "smiles": smiles
133
+ }
134
+
135
  except Exception as e:
136
+ results[str(uniprot_counter)] = {
137
+ "sequence": seq,
138
+ "error": f"Error generating SMILES: {str(e)}"
139
+ }
140
+ uniprot_counter += 1
141
 
142
+ # Process UniProt ID inputs and include sequence if found
143
  if uniprot_id:
144
  uniprot_ids = [uid.strip() for uid in uniprot_id.split(",") if uid.strip()]
145
  for uid in uniprot_ids:
146
+ sequence = uniprot_to_sequence.get(uid, "N/A")
147
  try:
148
+ # Generate SMILES for the sequence found
149
+ if sequence != "N/A":
150
  smiles = generator.generate_smiles(sequence, num_generated)
151
+ results[uid] = {
152
+ "sequence": sequence,
153
+ "smiles": smiles
154
+ }
155
  else:
156
+ results[uid] = {
157
+ "sequence": "N/A",
158
+ "error": f"UniProt ID {uid} not found in the dataset."
159
+ }
160
  except Exception as e:
161
+ results[uid] = {
162
+ "sequence": "N/A",
163
+ "error": f"Error generating SMILES: {str(e)}"
164
+ }
165
 
166
+ # Check if no results were generated
167
  if not results:
168
+ return {"error": "No SMILES generated. Please try again with different inputs."}
169
 
170
+ # Save results to a file
171
  file_path = save_smiles_to_file(results)
 
172
 
173
+ # Return both results (JSON) and the file path
174
+ return results, file_path
175
 
176
  # Main initialization and Gradio setup
177
  if __name__ == "__main__":
 
258
  .gr-row {
259
  margin-bottom: 20px;
260
  }
 
261
 
262
+ .file-output {
263
+ height: 60px;
264
+ overflow-y: auto;
265
+ color: #ffffff !important;
266
+ padding: 10px;
267
+ border-radius: 10px;
268
+ border: 1px solid #555555;
269
+ }
270
+ """
271
  iface.css = custom_css
272
  gr.Markdown("## GPT-2 Drug Generator", elem_id="app-title")
273
  gr.Markdown(
 
285
  uniprot_id_input = gr.Textbox(
286
  label="UniProt IDs",
287
  placeholder="Enter UniProt IDs separated by commas (e.g., P12821, P37231, ...)",
288
+ lines=3,
289
  )
290
 
291
  num_generated_slider = gr.Slider(
 
297
  )
298
 
299
  output = gr.JSON(label="Generated SMILES")
300
+ file_output = gr.File(
301
+ label="Download Results as JSON",
302
+ elem_classes=["file-output"]
303
+ )
304
 
305
  generate_button = gr.Button("Generate SMILES", elem_id="generate-button")
306