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()