Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import primer3
|
|
| 4 |
from Bio import SeqIO
|
| 5 |
import os
|
| 6 |
from io import StringIO
|
|
|
|
| 7 |
|
| 8 |
# Ensure the 'temp' directory exists for saving temporary files
|
| 9 |
temp_dir = "temp"
|
|
@@ -15,7 +16,6 @@ uploaded_file = st.file_uploader("Upload a GenBank file", type=['gb', 'gbk'])
|
|
| 15 |
|
| 16 |
def extract_features_from_genbank(genbank_content, feature_types=['CDS', 'tRNA', 'gene']):
|
| 17 |
"""Extracts specified features from GenBank content."""
|
| 18 |
-
# Convert binary stream (if present) to a StringIO object for text handling
|
| 19 |
text_stream = StringIO(genbank_content.decode("utf-8")) if isinstance(genbank_content, bytes) else genbank_content
|
| 20 |
record = SeqIO.read(text_stream, "genbank")
|
| 21 |
features = {ftype: [] for ftype in feature_types}
|
|
@@ -24,11 +24,14 @@ def extract_features_from_genbank(genbank_content, feature_types=['CDS', 'tRNA',
|
|
| 24 |
features[feature.type].append(feature)
|
| 25 |
return features, record
|
| 26 |
|
| 27 |
-
def design_primers_for_region(sequence, num_to_return=5):
|
| 28 |
"""Design primers for a specific sequence."""
|
|
|
|
|
|
|
| 29 |
return primer3.bindings.designPrimers(
|
| 30 |
{
|
| 31 |
'SEQUENCE_TEMPLATE': str(sequence),
|
|
|
|
| 32 |
},
|
| 33 |
{
|
| 34 |
'PRIMER_OPT_SIZE': 20,
|
|
@@ -43,56 +46,56 @@ def design_primers_for_region(sequence, num_to_return=5):
|
|
| 43 |
}
|
| 44 |
)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if uploaded_file is not None:
|
| 47 |
genbank_content = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
| 48 |
features, record = extract_features_from_genbank(genbank_content)
|
| 49 |
feature_type = st.selectbox('Select feature type:', ['CDS', 'tRNA', 'gene'])
|
| 50 |
|
| 51 |
-
# Enhanced selection box to show names and locations
|
| 52 |
feature_options = [f"{feature.qualifiers.get('gene', [''])[0]} ({feature.location})" for feature in features[feature_type]]
|
| 53 |
selected_index = st.selectbox(f'Select a {feature_type}:', options=range(len(feature_options)), format_func=lambda x: feature_options[x])
|
| 54 |
selected_feature = features[feature_type][selected_index]
|
| 55 |
|
| 56 |
-
feature_sequence = selected_feature.extract(record.seq)
|
| 57 |
st.write(f"Selected {feature_type} sequence (length: {len(feature_sequence)} bp):")
|
| 58 |
-
st.text(str(feature_sequence))
|
| 59 |
|
| 60 |
product_size_range = st.text_input("Enter desired PCR product size range (e.g., 150-500):", value="150-500")
|
| 61 |
|
| 62 |
if st.button(f'Design Primers for selected {feature_type}'):
|
| 63 |
-
|
| 64 |
-
primers = design_primers_for_region(feature_sequence, num_to_return=5)
|
| 65 |
-
|
| 66 |
-
# Lists to hold primer details, initializing with placeholders if keys are missing
|
| 67 |
-
sequences, tm_values, lengths, gc_percents = [], [], [], []
|
| 68 |
|
| 69 |
-
|
| 70 |
-
for i in range(5): #
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
gc_percents.append(primers.get(left_key_gc, 'N/A'))
|
| 84 |
-
gc_percents.append(primers.get(right_key_gc, 'N/A'))
|
| 85 |
-
|
| 86 |
-
# Construct the DataFrame only if we found some primers
|
| 87 |
-
if any(seq != 'N/A' for seq in sequences): # Check if we added any real sequences
|
| 88 |
-
primer_df = pd.DataFrame({
|
| 89 |
-
'Primer': ['Left Primer', 'Right Primer'] * (len(sequences) // 2),
|
| 90 |
-
'Sequence': sequences,
|
| 91 |
-
'Tm (°C)': tm_values,
|
| 92 |
-
'Length': lengths,
|
| 93 |
-
'GC%': gc_percents,
|
| 94 |
-
})
|
| 95 |
|
|
|
|
|
|
|
| 96 |
st.write('### Designed Primers')
|
| 97 |
st.dataframe(primer_df)
|
| 98 |
csv = primer_df.to_csv(index=False).encode('utf-8')
|
|
@@ -103,5 +106,8 @@ if uploaded_file is not None:
|
|
| 103 |
"text/csv",
|
| 104 |
key='download-csv'
|
| 105 |
)
|
|
|
|
|
|
|
|
|
|
| 106 |
else:
|
| 107 |
-
st.error('No primers were found. Please adjust your parameters and try again.')
|
|
|
|
| 4 |
from Bio import SeqIO
|
| 5 |
import os
|
| 6 |
from io import StringIO
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
|
| 9 |
# Ensure the 'temp' directory exists for saving temporary files
|
| 10 |
temp_dir = "temp"
|
|
|
|
| 16 |
|
| 17 |
def extract_features_from_genbank(genbank_content, feature_types=['CDS', 'tRNA', 'gene']):
|
| 18 |
"""Extracts specified features from GenBank content."""
|
|
|
|
| 19 |
text_stream = StringIO(genbank_content.decode("utf-8")) if isinstance(genbank_content, bytes) else genbank_content
|
| 20 |
record = SeqIO.read(text_stream, "genbank")
|
| 21 |
features = {ftype: [] for ftype in feature_types}
|
|
|
|
| 24 |
features[feature.type].append(feature)
|
| 25 |
return features, record
|
| 26 |
|
| 27 |
+
def design_primers_for_region(sequence, product_size_range, num_to_return=5):
|
| 28 |
"""Design primers for a specific sequence."""
|
| 29 |
+
# Parse the product size range
|
| 30 |
+
size_min, size_max = map(int, product_size_range.split('-'))
|
| 31 |
return primer3.bindings.designPrimers(
|
| 32 |
{
|
| 33 |
'SEQUENCE_TEMPLATE': str(sequence),
|
| 34 |
+
'PRIMER_PRODUCT_SIZE_RANGE': [[size_min, size_max]]
|
| 35 |
},
|
| 36 |
{
|
| 37 |
'PRIMER_OPT_SIZE': 20,
|
|
|
|
| 46 |
}
|
| 47 |
)
|
| 48 |
|
| 49 |
+
def plot_pcr_product(sequence, primers, num_pairs=5):
|
| 50 |
+
"""Visualize the PCR product based on primer locations."""
|
| 51 |
+
plt.figure(figsize=(10, 2))
|
| 52 |
+
plt.plot([0, len(sequence)], [0, 0], 'k-') # Draw the sequence line
|
| 53 |
+
for i in range(num_pairs):
|
| 54 |
+
left_pos = primers.get(f'PRIMER_LEFT_{i}', {}).get('position', -1)
|
| 55 |
+
right_pos = primers.get(f'PRIMER_RIGHT_{i}', {}).get('position', -1)
|
| 56 |
+
if left_pos != -1 and right_pos != -1:
|
| 57 |
+
plt.plot([left_pos, right_pos], [i, i], 'r-') # Draw the PCR product line
|
| 58 |
+
plt.text(left_pos, i, f'{i+1}', va='bottom', ha='right')
|
| 59 |
+
plt.yticks([])
|
| 60 |
+
plt.title('PCR Products')
|
| 61 |
+
plt.xlabel('Nucleotide position')
|
| 62 |
+
plt.show()
|
| 63 |
+
|
| 64 |
if uploaded_file is not None:
|
| 65 |
genbank_content = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
| 66 |
features, record = extract_features_from_genbank(genbank_content)
|
| 67 |
feature_type = st.selectbox('Select feature type:', ['CDS', 'tRNA', 'gene'])
|
| 68 |
|
|
|
|
| 69 |
feature_options = [f"{feature.qualifiers.get('gene', [''])[0]} ({feature.location})" for feature in features[feature_type]]
|
| 70 |
selected_index = st.selectbox(f'Select a {feature_type}:', options=range(len(feature_options)), format_func=lambda x: feature_options[x])
|
| 71 |
selected_feature = features[feature_type][selected_index]
|
| 72 |
|
| 73 |
+
feature_sequence = selected_feature.extract(record.seq)
|
| 74 |
st.write(f"Selected {feature_type} sequence (length: {len(feature_sequence)} bp):")
|
| 75 |
+
st.text(str(feature_sequence))
|
| 76 |
|
| 77 |
product_size_range = st.text_input("Enter desired PCR product size range (e.g., 150-500):", value="150-500")
|
| 78 |
|
| 79 |
if st.button(f'Design Primers for selected {feature_type}'):
|
| 80 |
+
primers = design_primers_for_region(feature_sequence, product_size_range, num_to_return=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
primer_data = []
|
| 83 |
+
for i in range(5): # Collect data for 5 primer pairs
|
| 84 |
+
primer_info = {
|
| 85 |
+
'Primer Pair': i + 1,
|
| 86 |
+
'Left Sequence': primers.get(f'PRIMER_LEFT_{i}_SEQUENCE', 'N/A'),
|
| 87 |
+
'Right Sequence': primers.get(f'PRIMER_RIGHT_{i}_SEQUENCE', 'N/A'),
|
| 88 |
+
'Left TM (°C)': primers.get(f'PRIMER_LEFT_{i}_TM', 'N/A'),
|
| 89 |
+
'Right TM (°C)': primers.get(f'PRIMER_RIGHT_{i}_TM', 'N/A'),
|
| 90 |
+
'Left Length': primers.get(f'PRIMER_LEFT_{i}_SIZE', 'N/A'),
|
| 91 |
+
'Right Length': primers.get(f'PRIMER_RIGHT_{i}_SIZE', 'N/A'),
|
| 92 |
+
'PCR Product Size (bp)': primers.get(f'PRIMER_PAIR_{i}_PRODUCT_SIZE', 'N/A')
|
| 93 |
+
}
|
| 94 |
+
if primer_info['Left Sequence'] != 'N/A' and primer_info['Right Sequence'] != 'N/A':
|
| 95 |
+
primer_data.append(primer_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
if primer_data:
|
| 98 |
+
primer_df = pd.DataFrame(primer_data)
|
| 99 |
st.write('### Designed Primers')
|
| 100 |
st.dataframe(primer_df)
|
| 101 |
csv = primer_df.to_csv(index=False).encode('utf-8')
|
|
|
|
| 106 |
"text/csv",
|
| 107 |
key='download-csv'
|
| 108 |
)
|
| 109 |
+
# Plotting PCR products
|
| 110 |
+
st.write("### Visualization of PCR Products")
|
| 111 |
+
plot_pcr_product(feature_sequence, primers, num_pairs=5)
|
| 112 |
else:
|
| 113 |
+
st.error('No primers were found. Please adjust your parameters and try again.')
|