Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,10 +16,7 @@ uploaded_file = st.file_uploader("Upload a GenBank file", type=['gb', 'gbk'])
|
|
| 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 |
-
if isinstance(genbank_content, bytes)
|
| 20 |
-
text_stream = StringIO(genbank_content.decode("utf-8"))
|
| 21 |
-
else:
|
| 22 |
-
text_stream = genbank_content
|
| 23 |
record = SeqIO.read(text_stream, "genbank")
|
| 24 |
features = {ftype: [] for ftype in feature_types}
|
| 25 |
for feature in record.features:
|
|
@@ -27,13 +24,11 @@ def extract_features_from_genbank(genbank_content, feature_types=['CDS', 'tRNA',
|
|
| 27 |
features[feature.type].append(feature)
|
| 28 |
return features, record
|
| 29 |
|
| 30 |
-
def design_primers_for_region(sequence,
|
| 31 |
-
"""Design primers for a specific
|
| 32 |
-
target = [start, end - start]
|
| 33 |
return primer3.bindings.designPrimers(
|
| 34 |
{
|
| 35 |
'SEQUENCE_TEMPLATE': str(sequence),
|
| 36 |
-
'SEQUENCE_TARGET': target,
|
| 37 |
},
|
| 38 |
{
|
| 39 |
'PRIMER_OPT_SIZE': 20,
|
|
@@ -53,42 +48,38 @@ if uploaded_file is not None:
|
|
| 53 |
features, record = extract_features_from_genbank(genbank_content)
|
| 54 |
feature_type = st.selectbox('Select feature type:', ['CDS', 'tRNA', 'gene'])
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
feature_options = [f"{feature.qualifiers.get('
|
| 58 |
-
|
| 59 |
-
selected_feature = features[feature_type][
|
| 60 |
|
| 61 |
-
|
| 62 |
-
st.write(f"Selected {feature_type}
|
|
|
|
| 63 |
|
| 64 |
product_size_range = st.text_input("Enter desired PCR product size range (e.g., 150-500):", value="150-500")
|
| 65 |
|
| 66 |
-
if st.button(f'Design Primers for {feature_type}'):
|
| 67 |
-
|
| 68 |
-
primers = design_primers_for_region(
|
| 69 |
|
| 70 |
# Initialize lists to hold primer details
|
| 71 |
sequences, tm_values, lengths, gc_percents = [], [], [], []
|
| 72 |
|
| 73 |
# Check if primer keys exist and add their details to the lists
|
| 74 |
for i in range(5): # Assuming we are looking for 5 primer pairs
|
| 75 |
-
left_key = f'PRIMER_LEFT_{i}_SEQUENCE'
|
| 76 |
-
right_key = f'PRIMER_RIGHT_{i}_SEQUENCE'
|
| 77 |
if left_key in primers and right_key in primers:
|
| 78 |
-
sequences
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
lengths.append(primers[f'PRIMER_LEFT_{i}_SIZE'])
|
| 83 |
-
lengths.append(primers[f'PRIMER_RIGHT_{i}_SIZE'])
|
| 84 |
-
gc_percents.append(primers[f'PRIMER_LEFT_{i}_GC_PERCENT'])
|
| 85 |
-
gc_percents.append(primers[f'PRIMER_RIGHT_{i}_GC_PERCENT'])
|
| 86 |
|
| 87 |
# Check if any primers were found
|
| 88 |
if sequences:
|
| 89 |
# Construct the DataFrame using the collected primer details
|
| 90 |
primer_df = pd.DataFrame({
|
| 91 |
-
'Primer': ['Left Primer'
|
| 92 |
'Sequence': sequences,
|
| 93 |
'Tm (°C)': tm_values,
|
| 94 |
'Length': lengths,
|
|
@@ -106,4 +97,4 @@ if uploaded_file is not None:
|
|
| 106 |
key='download-csv'
|
| 107 |
)
|
| 108 |
else:
|
| 109 |
-
st.error('No primers were found. Please adjust your parameters and try again.')
|
|
|
|
| 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}
|
| 22 |
for feature in record.features:
|
|
|
|
| 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,
|
|
|
|
| 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) # Extract the sequence of the selected feature
|
| 57 |
+
st.write(f"Selected {feature_type} sequence (length: {len(feature_sequence)} bp):")
|
| 58 |
+
st.text(str(feature_sequence)) # Display the 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 |
+
# Design primers directly for the extracted sequence
|
| 64 |
+
primers = design_primers_for_region(feature_sequence, num_to_return=5)
|
| 65 |
|
| 66 |
# Initialize lists to hold primer details
|
| 67 |
sequences, tm_values, lengths, gc_percents = [], [], [], []
|
| 68 |
|
| 69 |
# Check if primer keys exist and add their details to the lists
|
| 70 |
for i in range(5): # Assuming we are looking for 5 primer pairs
|
| 71 |
+
left_key, right_key = f'PRIMER_LEFT_{i}_SEQUENCE', f'PRIMER_RIGHT_{i}_SEQUENCE'
|
|
|
|
| 72 |
if left_key in primers and right_key in primers:
|
| 73 |
+
sequences += [primers[left_key], primers[right_key]]
|
| 74 |
+
tm_values += [primers[f'PRIMER_LEFT_{i}_TM'], primers[f'PRIMER_RIGHT_{i}_TM']]
|
| 75 |
+
lengths += [primers[f'PRIMER_LEFT_{i}_SIZE'], primers[f'PRIMER_RIGHT_{i}_SIZE']]
|
| 76 |
+
gc_percents += [primers[f'PRIMER_LEFT_{i}_GC_PERCENT'], primers[f'PRIMER_RIGHT_{i}_GC_PERCENT']]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Check if any primers were found
|
| 79 |
if sequences:
|
| 80 |
# Construct the DataFrame using the collected primer details
|
| 81 |
primer_df = pd.DataFrame({
|
| 82 |
+
'Primer': ['Left Primer', 'Right Primer'] * (len(sequences) // 2),
|
| 83 |
'Sequence': sequences,
|
| 84 |
'Tm (°C)': tm_values,
|
| 85 |
'Length': lengths,
|
|
|
|
| 97 |
key='download-csv'
|
| 98 |
)
|
| 99 |
else:
|
| 100 |
+
st.error('No primers were found. Please adjust your parameters and try again.')
|