Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,9 +6,8 @@ from itertools import product
|
|
6 |
import torch.nn as nn
|
7 |
import matplotlib.pyplot as plt
|
8 |
import matplotlib.colors as mcolors
|
9 |
-
import
|
10 |
-
from PIL import Image
|
11 |
-
from scipy.interpolate import interp1d
|
12 |
from Bio.Graphics import GenomeDiagram
|
13 |
from Bio.SeqFeature import SeqFeature, FeatureLocation
|
14 |
from reportlab.lib import colors
|
@@ -670,6 +669,12 @@ def analyze_gene_features(sequence_file, features_file, fasta_text="", features_
|
|
670 |
|
671 |
def create_genome_diagram(gene_results, genome_length):
|
672 |
"""Create genome diagram using BioPython"""
|
|
|
|
|
|
|
|
|
|
|
|
|
673 |
|
674 |
# Create diagram
|
675 |
gd_diagram = GenomeDiagram.Diagram("Genome SHAP Analysis")
|
@@ -700,21 +705,29 @@ def create_genome_diagram(gene_results, genome_length):
|
|
700 |
name=f"{gene['gene_name']}\n(SHAP: {gene['avg_shap']:.3f})"
|
701 |
)
|
702 |
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
718 |
|
719 |
###############################################################################
|
720 |
# 12. DOWNLOAD FUNCTIONS
|
|
|
6 |
import torch.nn as nn
|
7 |
import matplotlib.pyplot as plt
|
8 |
import matplotlib.colors as mcolors
|
9 |
+
from io import BytesIO
|
10 |
+
from PIL import Image, ImageDraw
|
|
|
11 |
from Bio.Graphics import GenomeDiagram
|
12 |
from Bio.SeqFeature import SeqFeature, FeatureLocation
|
13 |
from reportlab.lib import colors
|
|
|
669 |
|
670 |
def create_genome_diagram(gene_results, genome_length):
|
671 |
"""Create genome diagram using BioPython"""
|
672 |
+
from Bio.Graphics import GenomeDiagram
|
673 |
+
from Bio.SeqFeature import SeqFeature, FeatureLocation
|
674 |
+
from reportlab.lib import colors
|
675 |
+
from io import BytesIO
|
676 |
+
from PIL import Image
|
677 |
+
import io # Ensure io is imported at the top level
|
678 |
|
679 |
# Create diagram
|
680 |
gd_diagram = GenomeDiagram.Diagram("Genome SHAP Analysis")
|
|
|
705 |
name=f"{gene['gene_name']}\n(SHAP: {gene['avg_shap']:.3f})"
|
706 |
)
|
707 |
|
708 |
+
try:
|
709 |
+
# Draw diagram
|
710 |
+
gd_diagram.draw(
|
711 |
+
format="linear",
|
712 |
+
orientation="landscape",
|
713 |
+
pagesize=(15, 5),
|
714 |
+
start=0,
|
715 |
+
end=genome_length,
|
716 |
+
fragments=1
|
717 |
+
)
|
718 |
+
|
719 |
+
# Save to BytesIO and convert to PIL Image
|
720 |
+
buffer = io.BytesIO()
|
721 |
+
gd_diagram.write(buffer, "PNG")
|
722 |
+
buffer.seek(0)
|
723 |
+
return Image.open(buffer)
|
724 |
+
except Exception as e:
|
725 |
+
print(f"Error creating genome diagram: {str(e)}")
|
726 |
+
# Create a simple error image
|
727 |
+
error_img = Image.new('RGB', (800, 100), color='white')
|
728 |
+
draw = ImageDraw.Draw(error_img)
|
729 |
+
draw.text((10, 40), f"Error creating genome diagram: {str(e)}", fill='black')
|
730 |
+
return error_img
|
731 |
|
732 |
###############################################################################
|
733 |
# 12. DOWNLOAD FUNCTIONS
|