yashm commited on
Commit
b112207
·
verified ·
1 Parent(s): c966738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -48,34 +48,46 @@ def design_primers_for_region(sequence, product_size_range, num_to_return=5):
48
 
49
  def plot_pcr_product(sequence, primers, feature_length, num_pairs=5):
50
  """Visualize the PCR product based on primer locations relative to a feature."""
51
- # Debugging: Print primer positions to check if they're being correctly extracted
52
- for i in range(num_pairs):
53
- print(f"Left Primer {i} Position: {primers.get(f'PRIMER_LEFT_{i}_POSITION', 'N/A')}")
54
- print(f"Right Primer {i} Position: {primers.get(f'PRIMER_RIGHT_{i}_POSITION', 'N/A')}")
55
 
56
- # Start the figure for plotting
57
  plt.figure(figsize=(10, 3))
58
- plt.plot([0, feature_length], [1, 1], 'b-', label='Selected Feature') # Blue line represents the selected feature
59
-
 
 
 
60
  for i in range(num_pairs):
61
- left_pos = primers.get(f'PRIMER_LEFT_{i}_POSITION', -1)
62
- right_pos = primers.get(f'PRIMER_RIGHT_{i}_POSITION', -1)
63
- if left_pos != -1 and right_pos != -1:
64
- plt.plot([left_pos, left_pos], [1.1, 1.3], 'r-', label=f'Left Primer {i+1}' if i == 0 else "") # Draw the left primer position
65
- plt.plot([right_pos, right_pos], [0.7, 0.9], 'g-', label=f'Right Primer {i+1}' if i == 0 else "") # Draw the right primer position
66
- plt.text(left_pos, 1.35, f'L{i+1}', ha='center') # Label for left primer
67
- plt.text(right_pos, 0.65, f'R{i+1}', ha='center') # Label for right primer
68
-
69
- plt.ylim(0, 2) # Set the limits of Y-axis to make the plot clearer
 
 
 
 
 
 
 
 
 
 
70
  plt.title('Feature and Primer Positions')
71
  plt.xlabel('Nucleotide position')
 
72
  plt.legend()
73
- plt.grid(True)
74
 
75
  # Use st.pyplot() to display the plot in Streamlit
76
  st.pyplot(plt)
77
 
78
 
 
79
  if uploaded_file is not None:
80
  genbank_content = StringIO(uploaded_file.getvalue().decode("utf-8"))
81
  features, record = extract_features_from_genbank(genbank_content)
@@ -123,6 +135,8 @@ if uploaded_file is not None:
123
  )
124
  # Plotting PCR products
125
  st.write("### Visualization of PCR Products")
 
 
126
  feature_length = len(feature_sequence)
127
  plot_pcr_product(feature_sequence, primers, feature_length, num_pairs=5)
128
  else:
 
48
 
49
  def plot_pcr_product(sequence, primers, feature_length, num_pairs=5):
50
  """Visualize the PCR product based on primer locations relative to a feature."""
51
+ import matplotlib.pyplot as plt # Ensure matplotlib is imported here if not already done
 
 
 
52
 
53
+ # Initialize the figure for plotting
54
  plt.figure(figsize=(10, 3))
55
+
56
+ # Plot the selected feature line in the middle
57
+ plt.plot([0, feature_length], [1, 1], 'b-', lw=2, label='Selected Feature')
58
+
59
+ # Iterate over the number of expected primer pairs
60
  for i in range(num_pairs):
61
+ left_key_pos = f'PRIMER_LEFT_{i}_POSITION'
62
+ right_key_pos = f'PRIMER_RIGHT_{i}_POSITION'
63
+
64
+ if left_key_pos in primers and right_key_pos in primers:
65
+ # Fetch positions for left and right primers
66
+ left_pos = primers[left_key_pos]
67
+ right_pos = primers[right_key_pos]
68
+
69
+ # Plotting the left primer above the feature line
70
+ plt.plot([left_pos, left_pos], [1.05, 1.15], 'r-', label=f'Left Primer {i+1}' if i == 0 else "")
71
+ plt.text(left_pos, 1.17, f'L{i+1}', ha='center', color='red')
72
+
73
+ # Plotting the right primer below the feature line
74
+ plt.plot([right_pos, right_pos], [0.85, 0.95], 'g-', label=f'Right Primer {i+1}' if i == 0 else "")
75
+ plt.text(right_pos, 0.83, f'R{i+1}', ha='center', color='green')
76
+
77
+ # Adjusting the plot
78
+ plt.ylim(0.75, 1.25)
79
+ plt.xlim(0, feature_length)
80
  plt.title('Feature and Primer Positions')
81
  plt.xlabel('Nucleotide position')
82
+ plt.yticks([])
83
  plt.legend()
84
+ plt.grid(axis='x') # Show only horizontal grid lines for clarity
85
 
86
  # Use st.pyplot() to display the plot in Streamlit
87
  st.pyplot(plt)
88
 
89
 
90
+
91
  if uploaded_file is not None:
92
  genbank_content = StringIO(uploaded_file.getvalue().decode("utf-8"))
93
  features, record = extract_features_from_genbank(genbank_content)
 
135
  )
136
  # Plotting PCR products
137
  st.write("### Visualization of PCR Products")
138
+ if st.button(f'Design Primers for selected {feature_type}'):
139
+ # Your primer design logic and data preparation
140
  feature_length = len(feature_sequence)
141
  plot_pcr_product(feature_sequence, primers, feature_length, num_pairs=5)
142
  else: