import React, { useState, useRef } from 'react'; import { Upload, DNA, Search, BarChart3, MapPin, Download, Share2, Microscope } from 'lucide-react'; const BioStreamAI = () => { const [selectedFile, setSelectedFile] = useState(null); const [analysis, setAnalysis] = useState(null); const [loading, setLoading] = useState(false); const [dragActive, setDragActive] = useState(false); const [sampleLocation, setSampleLocation] = useState(''); const [waterSource, setWaterSource] = useState(''); const fileInputRef = useRef(null); const handleFileUpload = (event) => { const file = event.target.files[0]; if (file) { setSelectedFile(file); } }; const analyzeDNA = async (dnaFile) => { setLoading(true); setAnalysis(null); // It's good practice to clear old results // FormData will package the file for sending const formData = new FormData(); formData.append('dnaFile', dnaFile); try { // This is the new code that calls your server const response = await fetch('http://localhost:5000/api/analyze-dna', { method: 'POST', body: formData, }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || `Server error: ${response.status}`); } const analysisData = await response.json(); console.log("Frontend: Received analysis from backend:", analysisData); // Update the screen with the real data from the backend const finalReport = { ...analysisData, fileName: dnaFile.name, fileSize: (dnaFile.size / 1024 / 1024).toFixed(2) + ' MB', }; setAnalysis(finalReport); } catch (error) { console.error('Failed to fetch DNA analysis:', error); } finally { // This part always runs to make sure the spinner stops setLoading(false); } }; const handleDrop = (e) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); const files = e.dataTransfer.files; if (files && files[0]) { setSelectedFile(files[0]); } }; const handleDragOver = (e) => { e.preventDefault(); e.stopPropagation(); setDragActive(true); }; const handleDragLeave = (e) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); }; const getAbundanceColor = (abundance) => { switch (abundance) { case 'Very High': return '#f44336'; case 'High': return '#FF9800'; case 'Medium': return '#4CAF50'; case 'Low': return '#2196F3'; default: return '#666'; } }; const getStatusColor = (status) => { if (status.includes('Invasive') || status.includes('Pathogen')) return '#f44336'; if (status.includes('Concern')) return '#FF9800'; return '#4CAF50'; }; const getHealthColor = (health) => { switch (health) { case 'Excellent': return '#4CAF50'; case 'Good': return '#8BC34A'; case 'Fair': return '#FF9800'; case 'Poor': return '#f44336'; default: return '#666'; } }; return (
{/* Header */}

🧬 Bio-Stream AI: Environmental DNA Analysis

Revolutionary genetic-level ecosystem monitoring from a single drop of water

🧬 DNA Sequencing • 🤖 AI Species ID • 📊 Biodiversity Analysis • 🌍 Ecosystem Health
{/* Problem Statement */}

🚨 The Biodiversity Blindness Crisis

We have no idea what's truly living in our water systems. Traditional surveys are slow, expensive, and miss 99% of life (especially microbial). We are blind to the true state of our biodiversity.

• Traditional biodiversity surveys take months and cost $50,000+
• Visual identification misses 99% of microbial life
• No real-time ecosystem health monitoring
• Pollution and invasive species go undetected until it's too late
{/* Solution */}

💡 The Genetic Barcode Revolution

🎯 Environmental DNA (eDNA) Analysis:

Every living creature sheds DNA into its environment. This eDNA is a "genetic barcode" floating in the water. Our AI analyzes raw DNA sequences to create a complete, species-level census of an ecosystem from a single water sample.

💧

1. Collect Sample

Single drop of water contains all DNA

🧬

2. DNA Sequencing

Extract and sequence environmental DNA

🤖

3. AI Analysis

Match DNA to species database

📊

4. Ecosystem Report

Complete biodiversity analysis

{/* Sample Information */}

📍 Sample Information

setSampleLocation(e.target.value)} placeholder="e.g., Lake Washington, Seattle, WA" style={{ width: '100%', padding: '12px', fontSize: '1rem', borderRadius: '8px', border: '2px solid #4CAF50' }} />
{/* DNA File Upload */}

🧬 Upload DNA Sequence File

fileInputRef.current?.click()} style={{ border: `2px dashed ${dragActive ? '#4CAF50' : '#ccc'}`, borderRadius: '12px', padding: '40px 20px', textAlign: 'center', background: dragActive ? '#e8f5e8' : '#f9f9f9', transition: 'all 0.3s ease', cursor: 'pointer', marginBottom: '20px' }} >
{dragActive ? '📤' : '🧬'}

{dragActive ? 'Drop DNA sequence file here!' : 'Drag & drop DNA sequence file'}

Supports: FASTA, FASTQ, TXT • Raw sequencer output from MinION, Illumina, etc.

{selectedFile && (

Selected File: {selectedFile.name}

Size: {(selectedFile.size / 1024 / 1024).toFixed(2)} MB

)} {/* --- NEW ANALYSIS BUTTON --- */} {selectedFile && !loading && !analysis && (
)}
{/* Analysis Results */} {loading && (

🧬 Analyzing environmental DNA with AI...

Sequencing DNA fragments, matching to species database, calculating biodiversity metrics...

)} {/* --- FINAL, ULTRA-ROBUST RESULTS DISPLAY --- */} {analysis && !loading && (
{/* --- Analysis Overview Card --- */} {analysis.biodiversityMetrics && (

📊 Genetic Analysis Overview

Ecosystem Health: {analysis.biodiversityMetrics.ecosystemHealth || 'N/A'}

Biodiversity Score: {analysis.biodiversityMetrics.biodiversityScore || 'N/A'}
)} {/* --- Detected Species Card --- */}

🔬 Detected Species

{analysis.detectedSpecies && analysis.detectedSpecies.length > 0 ? (
{analysis.detectedSpecies.map((species, index) => (
{species.species || 'Unknown Species'} ({species.commonName || ''})

ID: {species.blastId || 'N/A'}

{species.abundance || 'N/A'} Abundance

{species.confidence || 'N/A'}% Confidence

))}
) : (

{analysis.error ? `Analysis Error: ${analysis.error}` : "No species from the database were detected in this sample."}

)}
{/* --- Recommendations Card --- */} {analysis.waterQualityAssessment && (

📋 Recommendations

    {analysis.waterQualityAssessment.recommendations?.map((rec, i) =>
  • {rec}
  • ) ||
  • No recommendations available.
  • }
)}
)}
); }; export default BioStreamAI;