yinuozhang commited on
Commit
5cfae69
·
1 Parent(s): c1ad2a1

bug fixing

Browse files
Files changed (1) hide show
  1. app.py +67 -13
app.py CHANGED
@@ -1,7 +1,17 @@
1
  import os
2
  import gradio as gr
3
  import gradio.blocks
4
- gradio.blocks.Blocks.get_api_info = lambda self: {}
 
 
 
 
 
 
 
 
 
 
5
  import re
6
  import pandas as pd
7
  from io import StringIO
@@ -867,35 +877,74 @@ def process_input(
867
  except Exception as e:
868
  return f"Error generating 3D structures: {str(e)}", None, None, []
869
 
870
- # Process the structure
871
- result = analyzer.analyze_structure(smiles)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
872
 
873
- # Create the 2D structure visualization
874
- img_cyclic = annotate_cyclic_structure(mol, result['three_letter'])
875
 
876
  # Create linear representation if requested
877
  img_linear = None
878
  if show_linear:
879
- fig_linear = create_enhanced_linear_viz(result['three_letter'], smiles)
880
  buf = BytesIO()
881
  fig_linear.savefig(buf, format='png', bbox_inches='tight', dpi=300)
882
  buf.seek(0)
883
  img_linear = Image.open(buf)
884
  plt.close(fig_linear)
885
-
886
- # Prepare the summary text
887
  summary = "Summary:\n"
888
- summary += f"Sequence: {result['three_letter']}\n"
889
- summary += f"One-letter code: {result['one_letter']}\n"
890
- summary += f"Is Cyclic: {'Yes' if result['is_cyclic'] else 'No'}\n"
 
 
 
891
 
892
  if structure_files:
893
  summary += "\n3D Structures Generated:\n"
894
  for filepath in structure_files:
895
  summary += f"- {os.path.basename(filepath)}\n"
896
 
897
- # Return the results
898
- return summary, img_cyclic, img_linear, structure_files if structure_files else []
899
 
900
  except Exception as e:
901
  return f"Error processing SMILES: {str(e)}", None, None, []
@@ -960,6 +1009,10 @@ iface = gr.Interface(
960
  label="Show linear representation",
961
  value=False
962
  ),
 
 
 
 
963
  gr.Checkbox(
964
  label="Generate 3D structure (sdf file format)",
965
  value=False
@@ -1014,4 +1067,5 @@ iface = gr.Interface(
1014
  )
1015
 
1016
  if __name__ == "__main__":
 
1017
  iface.launch(share=True)
 
1
  import os
2
  import gradio as gr
3
  import gradio.blocks
4
+ from gradio.blocks import Blocks
5
+
6
+ original_get_api_info = Blocks.get_api_info
7
+
8
+ def safe_get_api_info(self):
9
+ try:
10
+ return original_get_api_info(self)
11
+ except Exception as e:
12
+ print("⚠️ Failed to generate API schema:", e)
13
+ return {}
14
+
15
  import re
16
  import pandas as pd
17
  from io import StringIO
 
877
  except Exception as e:
878
  return f"Error generating 3D structures: {str(e)}", None, None, []
879
 
880
+ segments = analyzer.split_on_bonds(smiles)
881
+
882
+ sequence_parts = []
883
+ output_text = ""
884
+
885
+ # Only include segment analysis in output if requested
886
+ if show_segment_details:
887
+ output_text += "Segment Analysis:\n"
888
+ for i, segment in enumerate(segments):
889
+ output_text += f"\nSegment {i}:\n"
890
+ output_text += f"Content: {segment['content']}\n"
891
+ output_text += f"Bond before: {segment.get('bond_before', 'None')}\n"
892
+ output_text += f"Bond after: {segment.get('bond_after', 'None')}\n"
893
+
894
+ residue, mods = analyzer.identify_residue(segment)
895
+ if residue:
896
+ if mods:
897
+ sequence_parts.append(f"{residue}({','.join(mods)})")
898
+ else:
899
+ sequence_parts.append(residue)
900
+ output_text += f"Identified as: {residue}\n"
901
+ output_text += f"Modifications: {mods}\n"
902
+ else:
903
+ output_text += f"Warning: Could not identify residue in segment: {segment['content']}\n"
904
+ output_text += "\n"
905
+ else:
906
+ for segment in segments:
907
+ residue, mods = analyzer.identify_residue(segment)
908
+ if residue:
909
+ if mods:
910
+ sequence_parts.append(f"{residue}({','.join(mods)})")
911
+ else:
912
+ sequence_parts.append(residue)
913
+
914
+ is_cyclic, peptide_cycles, aromatic_cycles = analyzer.is_cyclic(smiles)
915
+ three_letter = '-'.join(sequence_parts)
916
+ one_letter = ''.join(analyzer.three_to_one.get(aa.split('(')[0], 'X') for aa in sequence_parts)
917
+
918
+ if is_cyclic:
919
+ three_letter = f"cyclo({three_letter})"
920
+ one_letter = f"cyclo({one_letter})"
921
 
922
+ img_cyclic = annotate_cyclic_structure(mol, three_letter)
 
923
 
924
  # Create linear representation if requested
925
  img_linear = None
926
  if show_linear:
927
+ fig_linear = create_enhanced_linear_viz(three_letter, smiles)
928
  buf = BytesIO()
929
  fig_linear.savefig(buf, format='png', bbox_inches='tight', dpi=300)
930
  buf.seek(0)
931
  img_linear = Image.open(buf)
932
  plt.close(fig_linear)
933
+
 
934
  summary = "Summary:\n"
935
+ summary += f"Sequence: {three_letter}\n"
936
+ summary += f"One-letter code: {one_letter}\n"
937
+ summary += f"Is Cyclic: {'Yes' if is_cyclic else 'No'}\n"
938
+ #if is_cyclic:
939
+ #summary += f"Peptide Cycles: {', '.join(peptide_cycles)}\n"
940
+ #summary += f"Aromatic Cycles: {', '.join(aromatic_cycles)}\n"
941
 
942
  if structure_files:
943
  summary += "\n3D Structures Generated:\n"
944
  for filepath in structure_files:
945
  summary += f"- {os.path.basename(filepath)}\n"
946
 
947
+ return summary + output_text, img_cyclic, img_linear, structure_files if structure_files else []
 
948
 
949
  except Exception as e:
950
  return f"Error processing SMILES: {str(e)}", None, None, []
 
1009
  label="Show linear representation",
1010
  value=False
1011
  ),
1012
+ gr.Checkbox(
1013
+ label="Show show segmentation details",
1014
+ value=False
1015
+ ),
1016
  gr.Checkbox(
1017
  label="Generate 3D structure (sdf file format)",
1018
  value=False
 
1067
  )
1068
 
1069
  if __name__ == "__main__":
1070
+ Blocks.get_api_info = safe_get_api_info
1071
  iface.launch(share=True)