yinuozhang commited on
Commit
1b33af5
·
1 Parent(s): 54ac992

add more functions

Browse files
Files changed (1) hide show
  1. app.py +49 -7
app.py CHANGED
@@ -237,7 +237,7 @@ def analyze_single_smiles(smiles):
237
  #'Peptide Cycles': 'Error',
238
  #'Aromatic Cycles': 'Error'
239
  }
240
-
241
  def annotate_cyclic_structure(mol, sequence):
242
  """Create annotated 2D structure with clear, non-overlapping residue labels"""
243
  # Generate 2D coordinates
@@ -332,6 +332,36 @@ def annotate_cyclic_structure(mol, sequence):
332
 
333
  return img
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  def create_enhanced_linear_viz(sequence, smiles):
336
  """
337
  Create an enhanced linear representation showing segment identification process
@@ -515,9 +545,15 @@ def process_input(smiles_input=None, file_obj=None, show_linear=False):
515
  # Handle file input
516
  if file_obj is not None:
517
  try:
518
- content = file_obj.decode('utf-8')
 
 
 
 
 
 
519
  output_text = ""
520
- for line in StringIO(content):
521
  smiles = line.strip()
522
  if smiles:
523
  if not is_peptide(smiles):
@@ -528,6 +564,7 @@ def process_input(smiles_input=None, file_obj=None, show_linear=False):
528
  output_text += f"Is Cyclic: {result['Is Cyclic']}\n"
529
  output_text += "-" * 50 + "\n"
530
  return output_text, None, None
 
531
  except Exception as e:
532
  return f"Error processing file: {str(e)}", None, None
533
 
@@ -544,7 +581,8 @@ iface = gr.Interface(
544
  ),
545
  gr.File(
546
  label="Or upload a text file with SMILES",
547
- file_types=[".txt"]
 
548
  ),
549
  gr.Checkbox(
550
  label="Show linear representation"
@@ -573,9 +611,13 @@ iface = gr.Interface(
573
 
574
  Input: Either enter a SMILES string directly or upload a text file containing SMILES strings
575
 
576
- Example SMILES strings:
577
- 1. 'CC(C)C[C@@H]1NC(=O)[C@@H](CC(C)C)N(C)C(=O)[C@@H](C)N(C)C(=O)[C@H](Cc2ccccc2)NC(=O)[C@H](CC(C)C)N(C)C(=O)[C@H]2CCCN2C1=O'
578
- 2. 'C(C)C[C@@H]1NC(=O)[C@@H]2CCCN2C(=O)[C@@H](CC(C)C)NC(=O)[C@@H](CC(C)C)N(C)C(=O)[C@H](C)NC(=O)[C@H](Cc2ccccc2)NC1=O'
 
 
 
 
579
  """,
580
  flagging_mode="never"
581
  )
 
237
  #'Peptide Cycles': 'Error',
238
  #'Aromatic Cycles': 'Error'
239
  }
240
+ """
241
  def annotate_cyclic_structure(mol, sequence):
242
  """Create annotated 2D structure with clear, non-overlapping residue labels"""
243
  # Generate 2D coordinates
 
332
 
333
  return img
334
 
335
+ """
336
+ def annotate_cyclic_structure(mol, sequence):
337
+ """Create structure visualization with just the sequence header"""
338
+ # Generate 2D coordinates
339
+ AllChem.Compute2DCoords(mol)
340
+
341
+ # Create drawer with larger size for annotations
342
+ drawer = Draw.rdMolDraw2D.MolDraw2DCairo(2000, 2000) # Even larger size
343
+
344
+ # Draw molecule first
345
+ drawer.drawOptions().addAtomIndices = False
346
+ drawer.DrawMolecule(mol)
347
+ drawer.FinishDrawing()
348
+
349
+ # Convert to PIL Image
350
+ img = Image.open(BytesIO(drawer.GetDrawingText()))
351
+ draw = ImageDraw.Draw(img)
352
+ small_font = ImageFont.load_default()
353
+
354
+ # Add just the sequence header at the top
355
+ seq_text = f"Sequence: {sequence}"
356
+ bbox = draw.textbbox((1000, 100), seq_text, font=small_font)
357
+ padding = 10
358
+ draw.rectangle([bbox[0]-padding, bbox[1]-padding,
359
+ bbox[2]+padding, bbox[3]+padding],
360
+ fill='white', outline='white')
361
+ draw.text((1000, 100), seq_text,
362
+ font=small_font, fill='black', anchor="mm")
363
+
364
+ return img
365
  def create_enhanced_linear_viz(sequence, smiles):
366
  """
367
  Create an enhanced linear representation showing segment identification process
 
545
  # Handle file input
546
  if file_obj is not None:
547
  try:
548
+ # Handle file content based on file object type
549
+ if hasattr(file_obj, 'name'): # If it's a file path
550
+ with open(file_obj.name, 'r') as f:
551
+ content = f.read()
552
+ else: # If it's file content
553
+ content = file_obj.decode('utf-8') if isinstance(file_obj, bytes) else str(file_obj)
554
+
555
  output_text = ""
556
+ for line in content.splitlines():
557
  smiles = line.strip()
558
  if smiles:
559
  if not is_peptide(smiles):
 
564
  output_text += f"Is Cyclic: {result['Is Cyclic']}\n"
565
  output_text += "-" * 50 + "\n"
566
  return output_text, None, None
567
+
568
  except Exception as e:
569
  return f"Error processing file: {str(e)}", None, None
570
 
 
581
  ),
582
  gr.File(
583
  label="Or upload a text file with SMILES",
584
+ file_types=[".txt"],
585
+ type="binary"
586
  ),
587
  gr.Checkbox(
588
  label="Show linear representation"
 
611
 
612
  Input: Either enter a SMILES string directly or upload a text file containing SMILES strings
613
 
614
+ Example SMILES strings (copy and paste):
615
+ ```
616
+ CC(C)C[C@@H]1NC(=O)[C@@H](CC(C)C)N(C)C(=O)[C@@H](C)N(C)C(=O)[C@H](Cc2ccccc2)NC(=O)[C@H](CC(C)C)N(C)C(=O)[C@H]2CCCN2C1=O
617
+ ```
618
+ ```
619
+ C(C)C[C@@H]1NC(=O)[C@@H]2CCCN2C(=O)[C@@H](CC(C)C)NC(=O)[C@@H](CC(C)C)N(C)C(=O)[C@H](C)NC(=O)[C@H](Cc2ccccc2)NC1=O
620
+ ```
621
  """,
622
  flagging_mode="never"
623
  )