Spaces:
Sleeping
Sleeping
File size: 2,560 Bytes
11a9944 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
from __future__ import print_function
import gradio as gr
import requests
from Bio import SeqIO
def calculate_gc_content(sequence):
"""Calculate the GC content of a given sequence."""
if len(sequence) == 0:
return 0.0
gc_count = sum(1 for base in sequence if base in 'GCgc')
return (gc_count / len(sequence)) * 100
def fetch_fasta_info(url, block_size):
min_gc = float('inf')
max_gc = float('-inf')
# Check if the URL is accessible
try:
response = requests.head(url)
response.raise_for_status() # Raise an error for bad responses
except requests.RequestException:
return None, None, "The URL is not accessible."
# Download the FASTA file
try:
fasta_response = requests.get(url)
fasta_response.raise_for_status() # Raise an error for bad responses
except requests.RequestException:
return None, None, "Failed to download the FASTA file."
# Save the content to a temporary file
fasta_file = "temp.fasta"
with open(fasta_file, 'wb') as f:
f.write(fasta_response.content)
# Parse the FASTA file and calculate GC content
try:
recs = SeqIO.parse(fasta_file, 'fasta')
for rec in recs:
if 'SO=chromosome' not in rec.description:
continue
size = len(rec.seq)
num_blocks = size // block_size + 1
for block in range(num_blocks):
start = block_size * block
end = size if block == num_blocks - 1 else block_size + start
block_seq = rec.seq[start:end]
block_gc = calculate_gc_content(block_seq)
min_gc = min(min_gc, block_gc)
max_gc = max(max_gc, block_gc)
except Exception as e:
return None, None, str(e)
return min_gc, max_gc, None
# Create the Gradio interface
iface = gr.Interface(
fn=fetch_fasta_info,
inputs=[
gr.Textbox(
label="Enter FASTA URL",
placeholder="Ex: https://example.com/path/to/your.fasta"
),
gr.Slider(
label="Block Size",
minimum=1000,
maximum=100000,
step=1000,
value=50000
)
],
outputs=[
gr.Textbox(label="Min GC Content", placeholder="Min GC"),
gr.Textbox(label="Max GC Content", placeholder="Max GC")
],
title="GC Content Calculator",
description="Enter a URL of a FASTA file to calculate GC content."
)
# Launch the app
iface.launch()
|