Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import numpy as np
|
|
5 |
from itertools import product
|
6 |
import torch.nn as nn
|
7 |
import matplotlib.pyplot as plt
|
8 |
-
import matplotlib.colors as mcolors
|
9 |
import io
|
10 |
from io import BytesIO # Import io then BytesIO
|
11 |
from PIL import Image, ImageDraw, ImageFont
|
@@ -16,6 +16,9 @@ import pandas as pd
|
|
16 |
import tempfile
|
17 |
import os
|
18 |
from typing import List, Dict, Tuple, Optional, Any
|
|
|
|
|
|
|
19 |
|
20 |
###############################################################################
|
21 |
# 1. MODEL DEFINITION
|
@@ -567,18 +570,6 @@ def analyze_sequence_comparison(file1, file2, fasta1="", fasta2=""):
|
|
567 |
# 11. GENE FEATURE ANALYSIS
|
568 |
###############################################################################
|
569 |
|
570 |
-
import io
|
571 |
-
from io import BytesIO
|
572 |
-
from PIL import Image, ImageDraw, ImageFont
|
573 |
-
import numpy as np
|
574 |
-
import pandas as pd
|
575 |
-
import tempfile
|
576 |
-
import os
|
577 |
-
from typing import List, Dict, Tuple, Optional, Any
|
578 |
-
import matplotlib.pyplot as plt
|
579 |
-
from matplotlib.colors import LinearSegmentedColormap
|
580 |
-
import seaborn as sns
|
581 |
-
|
582 |
def parse_gene_features(text: str) -> List[Dict[str, Any]]:
|
583 |
"""Parse gene features from text file in FASTA-like format"""
|
584 |
genes = []
|
@@ -653,6 +644,19 @@ def compute_gene_statistics(gene_shap: np.ndarray) -> Dict[str, float]:
|
|
653 |
|
654 |
def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_length: int) -> Image.Image:
|
655 |
"""Create a simple genome diagram using PIL"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
656 |
# Image dimensions
|
657 |
width = 1500
|
658 |
height = 600
|
@@ -684,7 +688,7 @@ def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_leng
|
|
684 |
|
685 |
# Draw scale markers
|
686 |
for i in range(0, genome_length + 1, genome_length // 10):
|
687 |
-
x = margin + i * scale
|
688 |
draw.line([(x, line_y - 5), (x, line_y + 5)], fill='black', width=1)
|
689 |
draw.text((x - 20, line_y + 10), f"{i:,}", fill='black', font=font)
|
690 |
|
@@ -693,9 +697,9 @@ def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_leng
|
|
693 |
|
694 |
# Draw genes
|
695 |
for gene in sorted_genes:
|
696 |
-
# Calculate position
|
697 |
-
start_x = margin + gene['start'] * scale
|
698 |
-
end_x = margin + gene['end'] * scale
|
699 |
|
700 |
# Calculate color based on SHAP value
|
701 |
if gene['avg_shap'] > 0:
|
@@ -705,10 +709,10 @@ def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_leng
|
|
705 |
intensity = min(255, int(abs(gene['avg_shap'] * 500)))
|
706 |
color = (255 - intensity, 255 - intensity, 255) # Blue
|
707 |
|
708 |
-
# Draw gene box
|
709 |
draw.rectangle([
|
710 |
-
(start_x, line_y - track_height // 2),
|
711 |
-
(end_x, line_y + track_height // 2)
|
712 |
], fill=color, outline='black')
|
713 |
|
714 |
# Draw gene name
|
@@ -726,8 +730,8 @@ def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_leng
|
|
726 |
gene_width = end_x - start_x
|
727 |
if gene_width > label_width:
|
728 |
# Horizontal label
|
729 |
-
text_x = start_x + (gene_width - label_width) // 2
|
730 |
-
draw.text((text_x, text_y), label, fill='black', font=font)
|
731 |
elif gene_width > 20:
|
732 |
# Create rotated text image
|
733 |
txt_img = Image.new('RGBA', (label_width, 20), (255, 255, 255, 0))
|
|
|
5 |
from itertools import product
|
6 |
import torch.nn as nn
|
7 |
import matplotlib.pyplot as plt
|
8 |
+
import matplotlib.colors as mcolors LinearSegmentedColormap
|
9 |
import io
|
10 |
from io import BytesIO # Import io then BytesIO
|
11 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
16 |
import tempfile
|
17 |
import os
|
18 |
from typing import List, Dict, Tuple, Optional, Any
|
19 |
+
import io
|
20 |
+
from io import BytesIO
|
21 |
+
import seaborn as sns
|
22 |
|
23 |
###############################################################################
|
24 |
# 1. MODEL DEFINITION
|
|
|
570 |
# 11. GENE FEATURE ANALYSIS
|
571 |
###############################################################################
|
572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
573 |
def parse_gene_features(text: str) -> List[Dict[str, Any]]:
|
574 |
"""Parse gene features from text file in FASTA-like format"""
|
575 |
genes = []
|
|
|
644 |
|
645 |
def create_simple_genome_diagram(gene_results: List[Dict[str, Any]], genome_length: int) -> Image.Image:
|
646 |
"""Create a simple genome diagram using PIL"""
|
647 |
+
# Validate inputs
|
648 |
+
if not gene_results or genome_length <= 0:
|
649 |
+
img = Image.new('RGB', (800, 100), color='white')
|
650 |
+
draw = ImageDraw.Draw(img)
|
651 |
+
draw.text((10, 40), "Error: Invalid input data", fill='black')
|
652 |
+
return img
|
653 |
+
|
654 |
+
# Ensure all gene coordinates are valid integers
|
655 |
+
for gene in gene_results:
|
656 |
+
gene['start'] = max(0, int(gene['start']))
|
657 |
+
gene['end'] = min(genome_length, int(gene['end']))
|
658 |
+
if gene['start'] >= gene['end']:
|
659 |
+
print(f"Warning: Invalid coordinates for gene {gene['gene_name']}: {gene['start']}-{gene['end']}")
|
660 |
# Image dimensions
|
661 |
width = 1500
|
662 |
height = 600
|
|
|
688 |
|
689 |
# Draw scale markers
|
690 |
for i in range(0, genome_length + 1, genome_length // 10):
|
691 |
+
x = int(margin + i * scale)
|
692 |
draw.line([(x, line_y - 5), (x, line_y + 5)], fill='black', width=1)
|
693 |
draw.text((x - 20, line_y + 10), f"{i:,}", fill='black', font=font)
|
694 |
|
|
|
697 |
|
698 |
# Draw genes
|
699 |
for gene in sorted_genes:
|
700 |
+
# Calculate position and ensure integers
|
701 |
+
start_x = int(margin + gene['start'] * scale)
|
702 |
+
end_x = int(margin + gene['end'] * scale)
|
703 |
|
704 |
# Calculate color based on SHAP value
|
705 |
if gene['avg_shap'] > 0:
|
|
|
709 |
intensity = min(255, int(abs(gene['avg_shap'] * 500)))
|
710 |
color = (255 - intensity, 255 - intensity, 255) # Blue
|
711 |
|
712 |
+
# Draw gene box with integer coordinates
|
713 |
draw.rectangle([
|
714 |
+
(start_x, int(line_y - track_height // 2)),
|
715 |
+
(end_x, int(line_y + track_height // 2))
|
716 |
], fill=color, outline='black')
|
717 |
|
718 |
# Draw gene name
|
|
|
730 |
gene_width = end_x - start_x
|
731 |
if gene_width > label_width:
|
732 |
# Horizontal label
|
733 |
+
text_x = int(start_x + (gene_width - label_width) // 2)
|
734 |
+
draw.text((text_x, int(text_y)), label, fill='black', font=font)
|
735 |
elif gene_width > 20:
|
736 |
# Create rotated text image
|
737 |
txt_img = Image.new('RGBA', (label_width, 20), (255, 255, 255, 0))
|