yinuozhang commited on
Commit
260f3d0
·
1 Parent(s): a953180

class format

Browse files
Files changed (1) hide show
  1. app.py +45 -59
app.py CHANGED
@@ -401,7 +401,7 @@ def annotate_cyclic_structure(mol, sequence):
401
  AllChem.Compute2DCoords(mol)
402
 
403
  # Create drawer with larger size for annotations
404
- drawer = Draw.rdMolDraw2D.MolDraw2DCairo(2000, 2000) # Even larger size
405
 
406
  # Draw molecule first
407
  drawer.drawOptions().addAtomIndices = False
@@ -417,7 +417,6 @@ def annotate_cyclic_structure(mol, sequence):
417
  try:
418
  small_font = ImageFont.truetype("arial.ttf", 60)
419
  except OSError:
420
- # If no TrueType fonts are available, fall back to default
421
  print("Warning: TrueType fonts not available, using default font")
422
  small_font = ImageFont.load_default()
423
 
@@ -432,11 +431,11 @@ def annotate_cyclic_structure(mol, sequence):
432
  font=small_font, fill='black', anchor="mm")
433
 
434
  return img
 
435
  def create_enhanced_linear_viz(sequence, smiles):
436
- """
437
- Create an enhanced linear representation showing segment identification process
438
- with improved segment handling
439
- """
440
  # Create figure with two subplots
441
  fig = plt.figure(figsize=(15, 10))
442
  gs = fig.add_gridspec(2, 1, height_ratios=[1, 2])
@@ -449,18 +448,12 @@ def create_enhanced_linear_viz(sequence, smiles):
449
  else:
450
  residues = sequence.split('-')
451
 
452
- # Get molecule and analyze bonds
453
- mol = Chem.MolFromSmiles(smiles)
454
-
455
- # Split SMILES into segments for analysis
456
- bond_pattern = r'(NC\(=O\)|N\(C\)C\(=O\)|N\dC\(=O\)|OC\(=O\))'
457
- segments = re.split(bond_pattern, smiles)
458
- segments = [s for s in segments if s] # Remove empty segments
459
 
460
  # Debug print
461
  print(f"Number of residues: {len(residues)}")
462
  print(f"Number of segments: {len(segments)}")
463
- print("Segments:", segments)
464
 
465
  # Top subplot - Basic structure
466
  ax_struct.set_xlim(0, 10)
@@ -481,34 +474,27 @@ def create_enhanced_linear_viz(sequence, smiles):
481
 
482
  # Draw connecting bonds if not the last residue
483
  if i < num_residues - 1:
484
- # Find the next bond pattern after this residue
485
- bond_segment = None
486
- for j in range(len(segments)):
487
- if re.match(bond_pattern, segments[j]):
488
- if j > i*2 and j//2 == i: # Found the right bond
489
- bond_segment = segments[j]
490
- break
491
-
492
- if bond_segment:
493
- bond_type, is_n_methylated = identify_linkage_type(bond_segment)
494
- else:
495
- bond_type = 'peptide' # Default if not found
496
 
497
- bond_color = 'black' if bond_type == 'peptide' else 'red'
498
- linestyle = '-' if bond_type == 'peptide' else '--'
499
-
500
- # Draw bond line
501
- ax_struct.plot([x_pos+0.3, x_pos+spacing-0.3], [y_pos, y_pos],
502
- color=bond_color, linestyle=linestyle, linewidth=2)
503
-
504
- # Add bond type label
505
- mid_x = x_pos + spacing/2
506
- bond_label = f"{bond_type}"
507
- if is_n_methylated:
508
- bond_label += "\n(N-Me)"
509
- ax_struct.text(mid_x, y_pos+0.1, bond_label,
510
- ha='center', va='bottom', fontsize=10,
511
- color=bond_color)
512
 
513
  # Add residue label
514
  ax_struct.text(x_pos, y_pos-0.5, residues[i],
@@ -523,27 +509,27 @@ def create_enhanced_linear_viz(sequence, smiles):
523
  for i, segment in enumerate(segments):
524
  y = segment_y - i
525
 
526
- # Check if this is a bond segment
527
- if re.match(bond_pattern, segment):
528
- bond_type, is_n_methylated = identify_linkage_type(segment)
529
- text = f"Bond {i//2 + 1}: {bond_type}"
530
- if is_n_methylated:
531
- text += " (N-methylated)"
532
- color = 'red'
533
- else:
534
- # Get next and previous segments for context
535
- next_seg = segments[i+1] if i+1 < len(segments) else None
536
- prev_seg = segments[i-1] if i > 0 else None
537
-
538
- residue, modifications = identify_residue(segment, next_seg, prev_seg)
539
- text = f"Residue {i//2 + 1}: {residue}"
540
- if modifications:
541
- text += f" ({', '.join(modifications)})"
542
  color = 'blue'
 
 
 
 
 
 
 
 
 
 
543
 
544
  # Add segment analysis
545
  ax_detail.text(0.05, y, text, fontsize=12, color=color)
546
- ax_detail.text(0.5, y, f"SMILES: {segment}", fontsize=10, color='gray')
547
 
548
  # If cyclic, add connection indicator
549
  if sequence.startswith('cyclo('):
@@ -684,7 +670,7 @@ def process_input(smiles_input=None, file_obj=None, show_linear=False):
684
  return f"Error processing file: {str(e)}", None, None
685
 
686
  return "No input provided.", None, None
687
- # Create Gradio interface with simplified examples
688
  iface = gr.Interface(
689
  fn=process_input,
690
  inputs=[
 
401
  AllChem.Compute2DCoords(mol)
402
 
403
  # Create drawer with larger size for annotations
404
+ drawer = Draw.rdMolDraw2D.MolDraw2DCairo(2000, 2000)
405
 
406
  # Draw molecule first
407
  drawer.drawOptions().addAtomIndices = False
 
417
  try:
418
  small_font = ImageFont.truetype("arial.ttf", 60)
419
  except OSError:
 
420
  print("Warning: TrueType fonts not available, using default font")
421
  small_font = ImageFont.load_default()
422
 
 
431
  font=small_font, fill='black', anchor="mm")
432
 
433
  return img
434
+
435
  def create_enhanced_linear_viz(sequence, smiles):
436
+ """Create an enhanced linear representation using PeptideAnalyzer"""
437
+ analyzer = PeptideAnalyzer() # Create analyzer instance
438
+
 
439
  # Create figure with two subplots
440
  fig = plt.figure(figsize=(15, 10))
441
  gs = fig.add_gridspec(2, 1, height_ratios=[1, 2])
 
448
  else:
449
  residues = sequence.split('-')
450
 
451
+ # Get segments using analyzer
452
+ segments = analyzer.split_on_bonds(smiles)
 
 
 
 
 
453
 
454
  # Debug print
455
  print(f"Number of residues: {len(residues)}")
456
  print(f"Number of segments: {len(segments)}")
 
457
 
458
  # Top subplot - Basic structure
459
  ax_struct.set_xlim(0, 10)
 
474
 
475
  # Draw connecting bonds if not the last residue
476
  if i < num_residues - 1:
477
+ segment = segments[i] if i < len(segments) else None
478
+ if segment:
479
+ # Determine bond type from segment info
480
+ bond_type = 'ester' if 'O-linked' in segment.get('bond_after', '') else 'peptide'
481
+ is_n_methylated = 'N-Me' in segment.get('bond_after', '')
482
+
483
+ bond_color = 'red' if bond_type == 'ester' else 'black'
484
+ linestyle = '--' if bond_type == 'ester' else '-'
 
 
 
 
485
 
486
+ # Draw bond line
487
+ ax_struct.plot([x_pos+0.3, x_pos+spacing-0.3], [y_pos, y_pos],
488
+ color=bond_color, linestyle=linestyle, linewidth=2)
489
+
490
+ # Add bond type label
491
+ mid_x = x_pos + spacing/2
492
+ bond_label = f"{bond_type}"
493
+ if is_n_methylated:
494
+ bond_label += "\n(N-Me)"
495
+ ax_struct.text(mid_x, y_pos+0.1, bond_label,
496
+ ha='center', va='bottom', fontsize=10,
497
+ color=bond_color)
 
 
 
498
 
499
  # Add residue label
500
  ax_struct.text(x_pos, y_pos-0.5, residues[i],
 
509
  for i, segment in enumerate(segments):
510
  y = segment_y - i
511
 
512
+ # Check if this is a bond or residue
513
+ residue, mods = analyzer.identify_residue(segment)
514
+ if residue:
515
+ text = f"Residue {i+1}: {residue}"
516
+ if mods:
517
+ text += f" ({', '.join(mods)})"
 
 
 
 
 
 
 
 
 
 
518
  color = 'blue'
519
+ else:
520
+ # Must be a bond
521
+ text = f"Bond {i}: "
522
+ if 'O-linked' in segment.get('bond_after', ''):
523
+ text += "ester"
524
+ elif 'N-Me' in segment.get('bond_after', ''):
525
+ text += "peptide (N-methylated)"
526
+ else:
527
+ text += "peptide"
528
+ color = 'red'
529
 
530
  # Add segment analysis
531
  ax_detail.text(0.05, y, text, fontsize=12, color=color)
532
+ ax_detail.text(0.5, y, f"SMILES: {segment.get('content', '')}", fontsize=10, color='gray')
533
 
534
  # If cyclic, add connection indicator
535
  if sequence.startswith('cyclo('):
 
670
  return f"Error processing file: {str(e)}", None, None
671
 
672
  return "No input provided.", None, None
673
+
674
  iface = gr.Interface(
675
  fn=process_input,
676
  inputs=[