Spaces:
Running
Running
Commit
·
b0490f8
1
Parent(s):
e52dbe4
bug fixing
Browse files- app.py +37 -96
- requirements.txt +1 -1
app.py
CHANGED
@@ -816,8 +816,14 @@ class PeptideStructureGenerator:
|
|
816 |
|
817 |
return sio.getvalue().encode('utf-8')
|
818 |
|
819 |
-
def process_input(
|
820 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
821 |
"""Process input and create visualizations using PeptideAnalyzer"""
|
822 |
analyzer = PeptideAnalyzer()
|
823 |
temp_dir = tempfile.mkdtemp() if generate_3d else None
|
@@ -859,77 +865,38 @@ def process_input(smiles_input=None, file_obj=None, show_linear=False,
|
|
859 |
except Exception as e:
|
860 |
return f"Error generating 3D structures: {str(e)}", None, None, []
|
861 |
|
862 |
-
|
|
|
863 |
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
# Only include segment analysis in output if requested
|
868 |
-
if show_segment_details:
|
869 |
-
output_text += "Segment Analysis:\n"
|
870 |
-
for i, segment in enumerate(segments):
|
871 |
-
output_text += f"\nSegment {i}:\n"
|
872 |
-
output_text += f"Content: {segment['content']}\n"
|
873 |
-
output_text += f"Bond before: {segment.get('bond_before', 'None')}\n"
|
874 |
-
output_text += f"Bond after: {segment.get('bond_after', 'None')}\n"
|
875 |
-
|
876 |
-
residue, mods = analyzer.identify_residue(segment)
|
877 |
-
if residue:
|
878 |
-
if mods:
|
879 |
-
sequence_parts.append(f"{residue}({','.join(mods)})")
|
880 |
-
else:
|
881 |
-
sequence_parts.append(residue)
|
882 |
-
output_text += f"Identified as: {residue}\n"
|
883 |
-
output_text += f"Modifications: {mods}\n"
|
884 |
-
else:
|
885 |
-
output_text += f"Warning: Could not identify residue in segment: {segment['content']}\n"
|
886 |
-
output_text += "\n"
|
887 |
-
else:
|
888 |
-
for segment in segments:
|
889 |
-
residue, mods = analyzer.identify_residue(segment)
|
890 |
-
if residue:
|
891 |
-
if mods:
|
892 |
-
sequence_parts.append(f"{residue}({','.join(mods)})")
|
893 |
-
else:
|
894 |
-
sequence_parts.append(residue)
|
895 |
-
|
896 |
-
is_cyclic, peptide_cycles, aromatic_cycles = analyzer.is_cyclic(smiles)
|
897 |
-
three_letter = '-'.join(sequence_parts)
|
898 |
-
one_letter = ''.join(analyzer.three_to_one.get(aa.split('(')[0], 'X') for aa in sequence_parts)
|
899 |
-
|
900 |
-
if is_cyclic:
|
901 |
-
three_letter = f"cyclo({three_letter})"
|
902 |
-
one_letter = f"cyclo({one_letter})"
|
903 |
-
|
904 |
-
img_cyclic = annotate_cyclic_structure(mol, three_letter)
|
905 |
|
906 |
# Create linear representation if requested
|
907 |
img_linear = None
|
908 |
if show_linear:
|
909 |
-
fig_linear = create_enhanced_linear_viz(three_letter, smiles)
|
910 |
buf = BytesIO()
|
911 |
fig_linear.savefig(buf, format='png', bbox_inches='tight', dpi=300)
|
912 |
buf.seek(0)
|
913 |
img_linear = Image.open(buf)
|
914 |
plt.close(fig_linear)
|
915 |
-
|
|
|
916 |
summary = "Summary:\n"
|
917 |
-
summary += f"Sequence: {three_letter}\n"
|
918 |
-
summary += f"One-letter code: {one_letter}\n"
|
919 |
-
summary += f"Is Cyclic: {'Yes' if is_cyclic else 'No'}\n"
|
920 |
-
#if is_cyclic:
|
921 |
-
#summary += f"Peptide Cycles: {', '.join(peptide_cycles)}\n"
|
922 |
-
#summary += f"Aromatic Cycles: {', '.join(aromatic_cycles)}\n"
|
923 |
|
924 |
if structure_files:
|
925 |
summary += "\n3D Structures Generated:\n"
|
926 |
for filepath in structure_files:
|
927 |
summary += f"- {os.path.basename(filepath)}\n"
|
928 |
|
929 |
-
|
|
|
930 |
|
931 |
except Exception as e:
|
932 |
-
return f"Error processing SMILES: {str(e)}", None, None,
|
933 |
|
934 |
# Handle file input
|
935 |
if file_obj is not None:
|
@@ -943,46 +910,24 @@ def process_input(smiles_input=None, file_obj=None, show_linear=False,
|
|
943 |
output_text = ""
|
944 |
for line in content.splitlines():
|
945 |
smiles = line.strip()
|
946 |
-
if smiles:
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
for i, segment in enumerate(segments):
|
957 |
-
output_text += f"\nSegment {i}:\n"
|
958 |
-
output_text += f"Content: {segment['content']}\n"
|
959 |
-
output_text += f"Bond before: {segment.get('bond_before', 'None')}\n"
|
960 |
-
output_text += f"Bond after: {segment.get('bond_after', 'None')}\n"
|
961 |
-
residue, mods = analyzer.identify_residue(segment)
|
962 |
-
if residue:
|
963 |
-
if mods:
|
964 |
-
sequence_parts.append(f"{residue}({','.join(mods)})")
|
965 |
-
else:
|
966 |
-
sequence_parts.append(residue)
|
967 |
-
output_text += f"Identified as: {residue}\n"
|
968 |
-
output_text += f"Modifications: {mods}\n"
|
969 |
-
else:
|
970 |
-
for segment in segments:
|
971 |
-
residue, mods = analyzer.identify_residue(segment)
|
972 |
-
if residue:
|
973 |
-
if mods:
|
974 |
-
sequence_parts.append(f"{residue}({','.join(mods)})")
|
975 |
-
else:
|
976 |
-
sequence_parts.append(residue)
|
977 |
-
|
978 |
-
is_cyclic, peptide_cycles, aromatic_cycles = analyzer.is_cyclic(smiles)
|
979 |
-
sequence = f"cyclo({'-'.join(sequence_parts)})" if is_cyclic else '-'.join(sequence_parts)
|
980 |
|
981 |
output_text += f"\nSummary for SMILES: {smiles}\n"
|
982 |
-
output_text += f"Sequence: {
|
983 |
-
output_text += f"
|
984 |
-
if is_cyclic
|
985 |
-
|
|
|
|
|
986 |
output_text += "-" * 50 + "\n"
|
987 |
|
988 |
return output_text, None, None, []
|
@@ -1008,10 +953,6 @@ iface = gr.Interface(
|
|
1008 |
label="Show linear representation",
|
1009 |
value=False
|
1010 |
),
|
1011 |
-
gr.Checkbox(
|
1012 |
-
label="Show segment details",
|
1013 |
-
value=False
|
1014 |
-
),
|
1015 |
gr.Checkbox(
|
1016 |
label="Generate 3D structure (sdf file format)",
|
1017 |
value=False
|
|
|
816 |
|
817 |
return sio.getvalue().encode('utf-8')
|
818 |
|
819 |
+
def process_input(
|
820 |
+
smiles_input=None,
|
821 |
+
file_obj=None,
|
822 |
+
show_linear=False,
|
823 |
+
show_segment_details=False,
|
824 |
+
generate_3d=False,
|
825 |
+
use_uff=False
|
826 |
+
):
|
827 |
"""Process input and create visualizations using PeptideAnalyzer"""
|
828 |
analyzer = PeptideAnalyzer()
|
829 |
temp_dir = tempfile.mkdtemp() if generate_3d else None
|
|
|
865 |
except Exception as e:
|
866 |
return f"Error generating 3D structures: {str(e)}", None, None, []
|
867 |
|
868 |
+
# Process the structure
|
869 |
+
result = analyzer.analyze_structure(smiles)
|
870 |
|
871 |
+
# Create the 2D structure visualization
|
872 |
+
img_cyclic = annotate_cyclic_structure(mol, result['three_letter'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
873 |
|
874 |
# Create linear representation if requested
|
875 |
img_linear = None
|
876 |
if show_linear:
|
877 |
+
fig_linear = create_enhanced_linear_viz(result['three_letter'], smiles)
|
878 |
buf = BytesIO()
|
879 |
fig_linear.savefig(buf, format='png', bbox_inches='tight', dpi=300)
|
880 |
buf.seek(0)
|
881 |
img_linear = Image.open(buf)
|
882 |
plt.close(fig_linear)
|
883 |
+
|
884 |
+
# Prepare the summary text
|
885 |
summary = "Summary:\n"
|
886 |
+
summary += f"Sequence: {result['three_letter']}\n"
|
887 |
+
summary += f"One-letter code: {result['one_letter']}\n"
|
888 |
+
summary += f"Is Cyclic: {'Yes' if result['is_cyclic'] else 'No'}\n"
|
|
|
|
|
|
|
889 |
|
890 |
if structure_files:
|
891 |
summary += "\n3D Structures Generated:\n"
|
892 |
for filepath in structure_files:
|
893 |
summary += f"- {os.path.basename(filepath)}\n"
|
894 |
|
895 |
+
# Return the results
|
896 |
+
return summary, img_cyclic, img_linear, structure_files if structure_files else []
|
897 |
|
898 |
except Exception as e:
|
899 |
+
return f"Error processing SMILES: {str(e)}", None, None, []
|
900 |
|
901 |
# Handle file input
|
902 |
if file_obj is not None:
|
|
|
910 |
output_text = ""
|
911 |
for line in content.splitlines():
|
912 |
smiles = line.strip()
|
913 |
+
if not smiles:
|
914 |
+
continue
|
915 |
+
|
916 |
+
if not analyzer.is_peptide(smiles):
|
917 |
+
output_text += f"Skipping non-peptide SMILES: {smiles}\n"
|
918 |
+
continue
|
919 |
+
|
920 |
+
try:
|
921 |
+
# Process the structure
|
922 |
+
result = analyzer.analyze_structure(smiles)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
923 |
|
924 |
output_text += f"\nSummary for SMILES: {smiles}\n"
|
925 |
+
output_text += f"Sequence: {result['three_letter']}\n"
|
926 |
+
output_text += f"One-letter code: {result['one_letter']}\n"
|
927 |
+
output_text += f"Is Cyclic: {'Yes' if result['is_cyclic'] else 'No'}\n"
|
928 |
+
output_text += "-" * 50 + "\n"
|
929 |
+
except Exception as e:
|
930 |
+
output_text += f"Error processing SMILES: {smiles} - {str(e)}\n"
|
931 |
output_text += "-" * 50 + "\n"
|
932 |
|
933 |
return output_text, None, None, []
|
|
|
953 |
label="Show linear representation",
|
954 |
value=False
|
955 |
),
|
|
|
|
|
|
|
|
|
956 |
gr.Checkbox(
|
957 |
label="Generate 3D structure (sdf file format)",
|
958 |
value=False
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
gradio
|
2 |
pandas==2.2.0
|
3 |
rdkit==2023.9.1
|
4 |
Pillow==10.0.0
|
|
|
1 |
+
gradio
|
2 |
pandas==2.2.0
|
3 |
rdkit==2023.9.1
|
4 |
Pillow==10.0.0
|