yashm commited on
Commit
cbd2c18
·
verified ·
1 Parent(s): cc50a4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -10
app.py CHANGED
@@ -3,7 +3,7 @@ import streamlit as st
3
  import pandas as pd
4
  from Bio import SeqIO
5
  from Bio.SeqUtils.ProtParam import ProteinAnalysis
6
- from Bio.Graphics import GenomeDiagram
7
  from reportlab.lib import colors
8
  from reportlab.lib.units import cm
9
  from io import StringIO
@@ -76,23 +76,58 @@ def add_protein_features(cds_info):
76
  cds['Isoelectric Point'] = 'N/A'
77
  return cds_info
78
 
79
- # Function to generate genome diagram
80
  def create_genome_diagram(genbank_content, output_file_path, colors_dict, diagram_type="linear", diagram_size=(30, 10)):
81
  record = SeqIO.read(StringIO(genbank_content), "genbank")
82
- gd_diagram = GenomeDiagram.Diagram(record.id)
83
- gd_track_for_features = gd_diagram.new_track(1, name="Annotated Features")
84
- gd_feature_set = gd_track_for_features.new_set()
85
-
 
 
 
 
 
 
 
 
86
  for feature in record.features:
87
  feature_type = feature.type
88
  if feature_type in colors_dict:
89
  color = colors.HexColor(colors_dict[feature_type])
90
- gd_feature_set.add_feature(feature, color=color, label=True, label_size=10, label_angle=0)
91
-
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  if diagram_type.lower() == "circular":
93
- gd_diagram.draw(format="circular", circular=True, pagesize=(diagram_size[0]*cm, diagram_size[1]*cm), start=0, end=len(record), circle_core=0.7)
 
 
 
 
 
 
 
94
  else:
95
- gd_diagram.draw(format="linear", pagesize=(diagram_size[0]*cm, diagram_size[1]*cm), fragments=1, start=0, end=len(record))
 
 
 
 
 
 
 
96
  gd_diagram.write(output_file_path, "SVG")
97
 
98
  # Function to search for a motif or pattern within the DNA sequence
 
3
  import pandas as pd
4
  from Bio import SeqIO
5
  from Bio.SeqUtils.ProtParam import ProteinAnalysis
6
+ from Bio.Graphics.GenomeDiagram import Diagram, Track, FeatureSet
7
  from reportlab.lib import colors
8
  from reportlab.lib.units import cm
9
  from io import StringIO
 
76
  cds['Isoelectric Point'] = 'N/A'
77
  return cds_info
78
 
79
+ # Updated Function to generate genome diagram
80
  def create_genome_diagram(genbank_content, output_file_path, colors_dict, diagram_type="linear", diagram_size=(30, 10)):
81
  record = SeqIO.read(StringIO(genbank_content), "genbank")
82
+ gd_diagram = Diagram(record.id)
83
+
84
+ # Create separate tracks for different feature types
85
+ max_tracks = len(colors_dict)
86
+ track_indices = {feature_type: idx+1 for idx, feature_type in enumerate(colors_dict.keys())}
87
+ feature_tracks = {}
88
+ for feature_type, idx in track_indices.items():
89
+ feature_tracks[feature_type] = gd_diagram.new_track(
90
+ idx, name=feature_type, scale=False, greytrack=False, height=0.5
91
+ )
92
+ feature_tracks[feature_type].new_set()
93
+
94
  for feature in record.features:
95
  feature_type = feature.type
96
  if feature_type in colors_dict:
97
  color = colors.HexColor(colors_dict[feature_type])
98
+ # Adjust label parameters to minimize overlap
99
+ feature_set = feature_tracks[feature_type].sets[0]
100
+ feature_set.add_feature(
101
+ feature,
102
+ color=color,
103
+ label=True,
104
+ label_size=6, # Decreased label size
105
+ label_angle=0,
106
+ label_position="start", # Position label at the start of the feature
107
+ label_strand=1 if feature.strand == 1 else -1, # Position labels according to strand
108
+ sigil="ARROW", # Use arrows to represent features
109
+ arrowshaft_height=1.0,
110
+ arrowhead_length=1.0,
111
+ )
112
+
113
  if diagram_type.lower() == "circular":
114
+ gd_diagram.draw(
115
+ format="circular",
116
+ circular=True,
117
+ pagesize=(diagram_size[0]*cm, diagram_size[1]*cm),
118
+ start=0,
119
+ end=len(record),
120
+ circle_core=0.7
121
+ )
122
  else:
123
+ gd_diagram.draw(
124
+ format="linear",
125
+ pagesize=(diagram_size[0]*cm, diagram_size[1]*cm),
126
+ fragments=1,
127
+ start=0,
128
+ end=len(record),
129
+ tracklines=False # Remove track lines to reduce clutter
130
+ )
131
  gd_diagram.write(output_file_path, "SVG")
132
 
133
  # Function to search for a motif or pattern within the DNA sequence