yashm commited on
Commit
d2c3263
·
verified ·
1 Parent(s): a937f0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -46,48 +46,44 @@ def design_primers_for_region(sequence, product_size_range, num_to_return=5):
46
  }
47
  )
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)
@@ -134,10 +130,9 @@ if uploaded_file is not None:
134
  key='download-csv'
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:
143
  st.error('No primers were found. Please adjust your parameters and try again.')
 
46
  }
47
  )
48
 
49
+ def plot_pcr_product_with_df(feature_sequence, primer_df, feature_length):
50
+ """Visualize the PCR product based on primer information in a DataFrame."""
51
+ import matplotlib.pyplot as plt
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
+ # Extracting primer information from the DataFrame
60
+ for index, row in primer_df.iterrows():
61
+ primer_type = "Left" if "Left" in row["Primer"] else "Right"
62
+ y_position = 1.1 if primer_type == "Left" else 0.9
63
+ label_y_position = 1.2 if primer_type == "Left" else 0.8
64
+ color = 'r' if primer_type == "Left" else 'g'
65
+
66
+ # Assuming the start of the feature is the reference point (0)
67
+ # For demonstration, this simplistic method places primers evenly across the feature length.
68
+ # This part needs adjustment based on actual primer positions if available.
69
+ position = (index + 1) * (feature_length // len(primer_df))
70
 
71
+ plt.plot([position, position], [y_position - 0.05, y_position + 0.05], color + '-', lw=2)
72
+ plt.text(position, label_y_position, f'{primer_type} {index + 1}', ha='center', color=color)
73
+
 
 
 
 
 
 
 
 
 
 
 
74
  plt.ylim(0.75, 1.25)
75
  plt.xlim(0, feature_length)
76
  plt.title('Feature and Primer Positions')
77
  plt.xlabel('Nucleotide position')
78
  plt.yticks([])
79
  plt.legend()
80
+ plt.grid(axis='x')
81
+
 
82
  st.pyplot(plt)
83
 
84
 
85
 
86
+
87
  if uploaded_file is not None:
88
  genbank_content = StringIO(uploaded_file.getvalue().decode("utf-8"))
89
  features, record = extract_features_from_genbank(genbank_content)
 
130
  key='download-csv'
131
  )
132
  # Plotting PCR products
133
+ if st.button(f'Design Primers for selected {feature_type}'):
134
+ # Assuming primer_df is already created with the required information
135
+ feature_length = len(feature_sequence) # Length of the selected feature
136
+ plot_pcr_product_with_df(feature_sequence, primer_df, feature_length)
 
137
  else:
138
  st.error('No primers were found. Please adjust your parameters and try again.')