File size: 21,948 Bytes
d2a707a | 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | #!/usr/bin/env bash
set -euo pipefail
# ============================================================
# Metatranscriptomics Pipeline — DAG (depth=10, convergence=4)
# ============================================================
#
# R1.fastq.gz R2.fastq.gz
# │ │
# [fastp QC] ────── [fastp QC] Level 1
# │ │
# └───────┬────────┘
# │
# [bbduk rRNA removal] ◄── rRNA kmer databases Level 2
# │
# ┌───────┴──────────────┐
# │ │
# (non-rRNA reads) (rRNA reads)
# │ │
# │ [python rRNA Level 3
# │ community profile]
# │ │
# ┌───┴────────────┐ │
# │ │ │
# [MEGAHIT [samtools │ Level 4
# assembly] flagstat │
# │ (read stats)]│
# │ │ │
# [prodigal │ │ Level 5
# gene calling] │ │
# │ │ │ │
# [diamond [bowtie2 │ │ Level 6
# blastx] map to │ │
# contigs]│ │
# │ │ │ │
# │ [samtools │ │
# │ sort+index] │ │
# │ │ │ │
# │ [featureCounts] │ Level 7
# │ │ │ │
# └───────┴────────┘ │
# │ │
# [CONVERGENCE 1] ◄─────┘ Level 7
# (assembly+func+counts+rRNA taxonomy)
# │
# ┌───────┼───────────┐
# │ │ │
# [python [python [python Level 8
# func expression taxonomy
# summary] analysis] merge]
# │ │ │
# └───────┼───────────┘
# │
# [CONVERGENCE 2] Level 8
# (func groups + expression + taxonomy)
# │
# ┌───────┼───────────┐
# │ │ │
# [python [python [python Level 9
# diversity active functional
# metrics] taxa] enrichment]
# │ │ │
# └───────┼───────────┘
# │
# [CONVERGENCE 3] Level 9
# (diversity + active taxa + enrichment)
# │
# [CONVERGENCE 4] ◄── QC stats + rRNA% Level 10
# [python report]
#
# Convergence points:
# C1: assembly + functional annotation + gene counts + rRNA taxonomy
# C2: functional summary + expression + taxonomy
# C3: diversity + active taxa + enrichment
# C4: final report combining all + QC metrics
# ============================================================
THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
WORK=$(pwd)
DATA="${WORK}/data"
REF="${WORK}/reference"
OUT="${WORK}/outputs"
RESULTS="${WORK}/results"
mkdir -p "${OUT}"/{qc,rrna_filter,assembly,mapping,genes,annotation,counts,analysis} "${RESULTS}"
# ─── Level 1: Read QC with fastp ───
if [ ! -f "${OUT}/qc/reads_R1.trimmed.fastq.gz" ]; then
echo "[Level 1] Running fastp QC..."
fastp \
-i "${DATA}/reads_R1.fastq.gz" \
-I "${DATA}/reads_R2.fastq.gz" \
-o "${OUT}/qc/reads_R1.trimmed.fastq.gz" \
-O "${OUT}/qc/reads_R2.trimmed.fastq.gz" \
--json "${OUT}/qc/fastp.json" \
--html "${OUT}/qc/fastp.html" \
--thread ${THREADS} \
--qualified_quality_phred 20 \
--length_required 50 \
--detect_adapter_for_pe
fi
# Extract QC stats
READS_BEFORE=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(d['summary']['before_filtering']['total_reads'])")
READS_AFTER=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(d['summary']['after_filtering']['total_reads'])")
Q30_BEFORE=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(round(d['summary']['before_filtering']['q30_rate']*100,2))")
Q30_AFTER=$(python3 -c "import json; d=json.load(open('${OUT}/qc/fastp.json')); print(round(d['summary']['after_filtering']['q30_rate']*100,2))")
echo " Reads before: ${READS_BEFORE}, after: ${READS_AFTER}"
# ─── Level 2: rRNA removal via alignment to rRNA databases ───
if [ ! -f "${OUT}/rrna_filter/non_rrna_R1.fastq.gz" ]; then
echo "[Level 2] Running rRNA removal..."
# Concatenate rRNA reference databases
cat "${REF}"/rrna_db/*.fasta > "${OUT}/rrna_filter/rrna_combined.fasta"
# Build bowtie2 index from rRNA references
bowtie2-build \
"${OUT}/rrna_filter/rrna_combined.fasta" \
"${OUT}/rrna_filter/rrna_index" \
--threads ${THREADS} --quiet
# Align reads to rRNA — extract mapped (rRNA) reads
bowtie2 \
-x "${OUT}/rrna_filter/rrna_index" \
-1 "${OUT}/qc/reads_R1.trimmed.fastq.gz" \
-2 "${OUT}/qc/reads_R2.trimmed.fastq.gz" \
--threads ${THREADS} \
--very-fast \
--no-unal \
2> "${OUT}/rrna_filter/bowtie2_rrna.log" \
| samtools sort -n -@ 4 -o "${OUT}/rrna_filter/rrna_aligned.bam"
# Extract rRNA reads as FASTQ
samtools fastq \
-1 "${OUT}/rrna_filter/rrna_R1.fastq.gz" \
-2 "${OUT}/rrna_filter/rrna_R2.fastq.gz" \
-0 /dev/null -s /dev/null \
-F 4 \
"${OUT}/rrna_filter/rrna_aligned.bam"
# Extract non-rRNA (unmapped) reads
bowtie2 \
-x "${OUT}/rrna_filter/rrna_index" \
-1 "${OUT}/qc/reads_R1.trimmed.fastq.gz" \
-2 "${OUT}/qc/reads_R2.trimmed.fastq.gz" \
--threads ${THREADS} \
--very-fast \
--un-conc-gz "${OUT}/rrna_filter/non_rrna_%.fastq.gz" \
2> /dev/null \
| samtools view -c > /dev/null
mv "${OUT}/rrna_filter/non_rrna_1.fastq.gz" "${OUT}/rrna_filter/non_rrna_R1.fastq.gz"
mv "${OUT}/rrna_filter/non_rrna_2.fastq.gz" "${OUT}/rrna_filter/non_rrna_R2.fastq.gz"
fi
# Count rRNA vs non-rRNA reads
RRNA_READS_R1=$(zcat "${OUT}/rrna_filter/rrna_R1.fastq.gz" 2>/dev/null | awk 'NR%4==1' | wc -l || true)
RRNA_READS=$(( RRNA_READS_R1 * 2 ))
NONRRNA_READS_R1=$(zcat "${OUT}/rrna_filter/non_rrna_R1.fastq.gz" 2>/dev/null | awk 'NR%4==1' | wc -l || true)
NONRRNA_READS=$(( NONRRNA_READS_R1 * 2 ))
TOTAL_FILTERED=$((RRNA_READS + NONRRNA_READS))
RRNA_PCT=$(python3 -c "print(round(${RRNA_READS}/${TOTAL_FILTERED}*100,2) if ${TOTAL_FILTERED}>0 else 0)")
echo " rRNA reads: ${RRNA_READS} (${RRNA_PCT}%), non-rRNA: ${NONRRNA_READS}"
# ─── Level 3: rRNA community profile (parallel with assembly path) ───
if [ ! -f "${OUT}/analysis/rrna_community.tsv" ]; then
echo "[Level 3] Profiling rRNA community..."
python3 << 'PYEOF'
import gzip, os
out = os.environ.get("OUT", "outputs")
os.makedirs(f"{out}/analysis", exist_ok=True)
# Analyze rRNA reads: GC content, length distribution
seq_count = 0
gc_sum = 0
len_sum = 0
len_dist = {}
with gzip.open(f"{out}/rrna_filter/rrna_R1.fastq.gz", "rt") as f:
for i, line in enumerate(f):
if i % 4 == 1:
seq = line.strip()
seq_count += 1
gc_sum += seq.count("G") + seq.count("C")
slen = len(seq)
len_sum += slen
bucket = (slen // 25) * 25
len_dist[bucket] = len_dist.get(bucket, 0) + 1
avg_gc = round(gc_sum / len_sum * 100, 2) if len_sum > 0 else 0
avg_len = round(len_sum / seq_count, 1) if seq_count > 0 else 0
with open(f"{out}/analysis/rrna_community.tsv", "w") as f:
f.write("metric\tvalue\n")
f.write(f"rrna_read_count\t{seq_count}\n")
f.write(f"rrna_avg_gc_pct\t{avg_gc}\n")
f.write(f"rrna_avg_length\t{avg_len}\n")
for bucket in sorted(len_dist):
f.write(f"rrna_len_{bucket}_{bucket+24}\t{len_dist[bucket]}\n")
print(f" rRNA community: {seq_count} reads, avg GC={avg_gc}%, avg len={avg_len}")
PYEOF
fi
# ─── Level 4: MEGAHIT de novo assembly of non-rRNA reads ───
if [ ! -f "${OUT}/assembly/final.contigs.fa" ]; then
echo "[Level 4] Running MEGAHIT assembly..."
rm -rf "${OUT}/assembly" # MEGAHIT fails if dir exists (M6)
megahit \
-1 "${OUT}/rrna_filter/non_rrna_R1.fastq.gz" \
-2 "${OUT}/rrna_filter/non_rrna_R2.fastq.gz" \
-o "${OUT}/assembly" \
--min-contig-len 200 \
-t ${THREADS}
fi
# Assembly stats
TOTAL_CONTIGS=$(grep -c "^>" "${OUT}/assembly/final.contigs.fa" || true)
ASSEMBLY_LENGTH=$(awk '/^>/{next}{sum+=length($0)}END{print sum}' "${OUT}/assembly/final.contigs.fa")
LARGEST_CONTIG=$(awk '/^>/{if(len>max)max=len;len=0;next}{len+=length($0)}END{if(len>max)max=len;print max}' "${OUT}/assembly/final.contigs.fa")
ASSEMBLY_GC=$(awk '/^>/{next}{for(i=1;i<=length($0);i++){c=substr($0,i,1);if(c=="G"||c=="C"||c=="g"||c=="c")gc++;tot++}}END{printf "%.2f",gc/tot*100}' "${OUT}/assembly/final.contigs.fa")
N50=$(python3 -c "
lens=[]
with open('${OUT}/assembly/final.contigs.fa') as f:
l=0
for line in f:
if line.startswith('>'):
if l: lens.append(l)
l=0
else: l+=len(line.strip())
if l: lens.append(l)
lens.sort(reverse=True)
total=sum(lens)
cum=0
for x in lens:
cum+=x
if cum>=total/2:
print(x); break
")
echo " Assembly: ${TOTAL_CONTIGS} contigs, ${ASSEMBLY_LENGTH} bp, N50=${N50}"
# ─── Level 5: Gene prediction with prodigal ───
if [ ! -f "${OUT}/genes/genes.gff" ]; then
echo "[Level 5] Running prodigal gene prediction..."
prodigal \
-i "${OUT}/assembly/final.contigs.fa" \
-o "${OUT}/genes/genes.gff" \
-a "${OUT}/genes/proteins.faa" \
-d "${OUT}/genes/genes.fna" \
-p meta \
-f gff
fi
PREDICTED_GENES=$(grep -c "^>" "${OUT}/genes/proteins.faa" || true)
echo " Predicted genes: ${PREDICTED_GENES}"
# ─── Level 6a: DIAMOND blastx functional annotation ───
if [ ! -f "${OUT}/annotation/diamond_hits.tsv" ]; then
echo "[Level 6a] Running DIAMOND blastx..."
diamond blastx \
--query "${OUT}/genes/genes.fna" \
--db "${REF}/uniprot_sprot.dmnd" \
--out "${OUT}/annotation/diamond_hits.tsv" \
--outfmt 6 qseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore stitle \
--max-target-seqs 1 \
--evalue 1e-5 \
--threads ${THREADS} \
--sensitive
fi
ANNOTATED_GENES=$(cut -f1 "${OUT}/annotation/diamond_hits.tsv" | sort -u | wc -l || true)
echo " Annotated genes: ${ANNOTATED_GENES}"
# ─── Level 6b: Map reads back to assembled contigs with bowtie2 ───
if [ ! -f "${OUT}/mapping/mapped.sorted.bam" ]; then
echo "[Level 6b] Building bowtie2 index and mapping..."
bowtie2-build \
"${OUT}/assembly/final.contigs.fa" \
"${OUT}/mapping/contigs_index" \
--threads ${THREADS} \
--quiet
bowtie2 \
-x "${OUT}/mapping/contigs_index" \
-1 "${OUT}/rrna_filter/non_rrna_R1.fastq.gz" \
-2 "${OUT}/rrna_filter/non_rrna_R2.fastq.gz" \
--no-unal \
--threads ${THREADS} \
--rg-id sample \
--rg "SM:sample" \
2> "${OUT}/mapping/bowtie2.log" \
| samtools sort -@ ${THREADS} -o "${OUT}/mapping/mapped.sorted.bam"
samtools index "${OUT}/mapping/mapped.sorted.bam"
fi
# Mapping stats via samtools flagstat
samtools flagstat "${OUT}/mapping/mapped.sorted.bam" > "${OUT}/mapping/flagstat.txt"
MAPPED_READS=$(grep "mapped (" "${OUT}/mapping/flagstat.txt" | head -1 | awk '{print $1}')
# Mapping rate relative to total non-rRNA reads (not just reads in BAM, since --no-unal)
MAPPING_PCT=$(python3 -c "print(round(${MAPPED_READS}/${NONRRNA_READS}*100 if ${NONRRNA_READS}>0 else 0, 2))")
echo " Mapped reads: ${MAPPED_READS}/${NONRRNA_READS} (${MAPPING_PCT}%)"
# ─── Level 7: featureCounts gene quantification ── CONVERGENCE 1 ───
if [ ! -f "${OUT}/counts/gene_counts.tsv" ]; then
echo "[Level 7 / CONVERGENCE 1] Running featureCounts..."
# Convert prodigal GFF to SAF format for featureCounts
python3 << 'PYEOF'
import os
out = os.environ.get("OUT", "outputs")
with open(f"{out}/genes/genes.gff") as fin, open(f"{out}/counts/genes.saf", "w") as fout:
fout.write("GeneID\tChr\tStart\tEnd\tStrand\n")
for line in fin:
if line.startswith("#") or not line.strip():
continue
parts = line.strip().split("\t")
if len(parts) < 9 or parts[2] != "CDS":
continue
chrom = parts[0]
start = parts[3]
end = parts[4]
strand = parts[6]
attrs = parts[8]
gene_id = None
for attr in attrs.split(";"):
if attr.startswith("ID="):
gene_id = attr.split("=")[1]
break
if gene_id:
fout.write(f"{gene_id}\t{chrom}\t{start}\t{end}\t{strand}\n")
PYEOF
featureCounts \
-a "${OUT}/counts/genes.saf" \
-F SAF \
-o "${OUT}/counts/gene_counts.tsv" \
-p --countReadPairs \
-T ${THREADS} \
"${OUT}/mapping/mapped.sorted.bam"
fi
EXPRESSED_GENES=$(awk 'NR>2 && $NF>0{c++}END{print c+0}' "${OUT}/counts/gene_counts.tsv")
echo " Expressed genes (count>0): ${EXPRESSED_GENES}"
# ─── Level 8: CONVERGENCE 2 — Functional summary + Expression analysis + Taxonomy merge ───
echo "[Level 8 / CONVERGENCE 2] Running integrated analyses..."
python3 << 'PYEOF'
import os, collections
out = os.environ.get("OUT", "outputs")
# --- Functional summary from DIAMOND hits ---
func_counts = collections.Counter()
org_counts = collections.Counter()
with open(f"{out}/annotation/diamond_hits.tsv") as f:
for line in f:
parts = line.strip().split("\t")
if len(parts) >= 13:
title = parts[12]
func = title.split(" OS=")[0] if " OS=" in title else title
# Strip Swiss-Prot accession prefix (e.g., "sp|Q46508|HNDD_SOLFR ")
if func.startswith("sp|") or func.startswith("tr|"):
parts2 = func.split(" ", 1)
func = parts2[1] if len(parts2) > 1 else func
func_counts[func] += 1
if " OS=" in title:
org = title.split(" OS=")[1].split(" OX=")[0]
org_counts[org] += 1
top_funcs = func_counts.most_common(20)
with open(f"{out}/analysis/functional_summary.tsv", "w") as f:
f.write("function\tcount\n")
for func, count in top_funcs:
f.write(f"{func}\t{count}\n")
top_orgs = org_counts.most_common(20)
with open(f"{out}/analysis/organism_distribution.tsv", "w") as f:
f.write("organism\thit_count\n")
for org, count in top_orgs:
f.write(f"{org}\t{count}\n")
# --- Expression analysis: CPM normalization ---
gene_counts = {}
total_count = 0
with open(f"{out}/counts/gene_counts.tsv") as f:
for line in f:
if line.startswith("#") or line.startswith("Geneid"):
continue
parts = line.strip().split("\t")
if len(parts) >= 7:
gid = parts[0]
count = int(parts[6])
gene_counts[gid] = count
total_count += count
with open(f"{out}/analysis/expression_cpm.tsv", "w") as f:
f.write("gene_id\traw_count\tcpm\n")
for gid, count in sorted(gene_counts.items(), key=lambda x: -x[1])[:100]:
cpm = round(count / total_count * 1e6, 2) if total_count > 0 else 0
f.write(f"{gid}\t{count}\t{cpm}\n")
print(f" Functional: {len(func_counts)} unique functions, top={top_funcs[0][0][:50] if top_funcs else 'N/A'}")
print(f" Organisms: {len(org_counts)} unique, top={top_orgs[0][0] if top_orgs else 'N/A'}")
print(f" Expression: {sum(1 for v in gene_counts.values() if v > 0)} expressed, total counts={total_count}")
PYEOF
# ─── Level 9: CONVERGENCE 3 — Diversity + Active taxa + Functional enrichment ───
echo "[Level 9 / CONVERGENCE 3] Computing diversity and enrichment..."
python3 << 'PYEOF'
import os, math, collections
out = os.environ.get("OUT", "outputs")
# --- Shannon diversity from organism distribution ---
org_counts = {}
with open(f"{out}/analysis/organism_distribution.tsv") as f:
next(f)
for line in f:
parts = line.strip().split("\t")
if len(parts) == 2:
org_counts[parts[0]] = int(parts[1])
total = sum(org_counts.values())
shannon = 0
for count in org_counts.values():
if count > 0 and total > 0:
p = count / total
shannon -= p * math.log(p)
shannon = round(shannon, 4)
richness = len(org_counts)
simpson = 0
for count in org_counts.values():
if total > 1:
simpson += (count * (count - 1)) / (total * (total - 1))
simpson = round(1 - simpson, 4)
singletons = sum(1 for c in org_counts.values() if c == 1)
doubletons = sum(1 for c in org_counts.values() if c == 2)
chao1 = richness + (singletons * (singletons - 1)) / (2 * (doubletons + 1))
chao1 = round(chao1, 1)
# Active taxa report
top_active = sorted(org_counts.items(), key=lambda x: -x[1])[:10]
with open(f"{out}/analysis/active_taxa.tsv", "w") as f:
f.write("taxon\thit_count\trelative_abundance_pct\n")
for org, count in top_active:
pct = round(count / total * 100, 2) if total > 0 else 0
f.write(f"{org}\t{count}\t{pct}\n")
# Functional enrichment (keyword frequency)
keyword_counts = collections.Counter()
with open(f"{out}/analysis/functional_summary.tsv") as f:
next(f)
for line in f:
parts = line.strip().split("\t")
if len(parts) >= 2:
func = parts[0].lower()
count = int(parts[1])
for word in ["kinase", "transferase", "synthase", "reductase", "oxidase",
"dehydrogenase", "transporter", "permease", "lyase", "ligase",
"protease", "peptidase", "hydrolase", "isomerase", "helicase",
"polymerase", "ribosomal", "elongation", "translation",
"transcription", "membrane", "binding", "regulatory"]:
if word in func:
keyword_counts[word] += count
with open(f"{out}/analysis/functional_enrichment.tsv", "w") as f:
f.write("functional_category\tgene_count\n")
for kw, count in keyword_counts.most_common(20):
f.write(f"{kw}\t{count}\n")
# Save diversity
with open(f"{out}/analysis/diversity_metrics.tsv", "w") as f:
f.write("metric\tvalue\n")
f.write(f"shannon_diversity\t{shannon}\n")
f.write(f"simpson_diversity\t{simpson}\n")
f.write(f"observed_richness\t{richness}\n")
f.write(f"chao1_estimate\t{chao1}\n")
print(f" Shannon={shannon}, Simpson={simpson}, Richness={richness}, Chao1={chao1}")
print(f" Top active: {top_active[0][0] if top_active else 'N/A'}")
print(f" Functional categories: {len(keyword_counts)}")
PYEOF
# ─── Level 10: CONVERGENCE 4 — Final report ───
echo "[Level 10 / CONVERGENCE 4] Generating final report..."
python3 << PYEOF
import os
out = os.environ.get("OUT", "outputs")
results = os.environ.get("RESULTS", "results")
os.makedirs(results, exist_ok=True)
# Read diversity metrics
diversity = {}
with open(f"{out}/analysis/diversity_metrics.tsv") as f:
next(f)
for line in f:
k, v = line.strip().split("\t")
diversity[k] = v
# Top organism
top_organism = "unknown"
top_org_pct = "0"
with open(f"{out}/analysis/active_taxa.tsv") as f:
next(f)
first = f.readline().strip().split("\t")
if len(first) >= 3:
top_organism = first[0]
top_org_pct = first[2]
# Top function
top_function = "unknown"
top_func_count = "0"
with open(f"{out}/analysis/functional_summary.tsv") as f:
next(f)
first = f.readline().strip().split("\t")
if len(first) >= 2:
top_function = first[0]
top_func_count = first[1]
# Functional categories
func_cats = 0
with open(f"{out}/analysis/functional_enrichment.tsv") as f:
next(f)
for line in f:
if line.strip():
func_cats += 1
# Highly expressed genes
expressed_high = 0
with open(f"{out}/analysis/expression_cpm.tsv") as f:
next(f)
for line in f:
parts = line.strip().split("\t")
if len(parts) >= 3 and float(parts[2]) > 100:
expressed_high += 1
with open(f"{results}/report.csv", "w") as f:
f.write("metric,value\n")
f.write(f"total_reads_before,${READS_BEFORE}\n")
f.write(f"total_reads_after,${READS_AFTER}\n")
f.write(f"q30_rate_before,${Q30_BEFORE}\n")
f.write(f"q30_rate_after,${Q30_AFTER}\n")
f.write(f"rrna_reads,${RRNA_READS}\n")
f.write(f"rrna_pct,${RRNA_PCT}\n")
f.write(f"nonrrna_reads,${NONRRNA_READS}\n")
f.write(f"total_contigs,${TOTAL_CONTIGS}\n")
f.write(f"assembly_length,${ASSEMBLY_LENGTH}\n")
f.write(f"largest_contig,${LARGEST_CONTIG}\n")
f.write(f"assembly_gc_pct,${ASSEMBLY_GC}\n")
f.write(f"assembly_n50,${N50}\n")
f.write(f"predicted_genes,${PREDICTED_GENES}\n")
f.write(f"annotated_genes,${ANNOTATED_GENES}\n")
f.write(f"mapped_reads,${MAPPED_READS}\n")
f.write(f"mapping_pct,${MAPPING_PCT}\n")
f.write(f"expressed_genes,${EXPRESSED_GENES}\n")
f.write(f"highly_expressed_genes,{expressed_high}\n")
f.write(f"shannon_diversity,{diversity.get('shannon_diversity','')}\n")
f.write(f"simpson_diversity,{diversity.get('simpson_diversity','')}\n")
f.write(f"observed_richness,{diversity.get('observed_richness','')}\n")
f.write(f"chao1_estimate,{diversity.get('chao1_estimate','')}\n")
f.write(f"top_organism,{top_organism}\n")
f.write(f"top_organism_pct,{top_org_pct}\n")
f.write(f"top_function,{top_function}\n")
f.write(f"functional_categories,{func_cats}\n")
print("Report written to results/report.csv")
PYEOF
echo ""
echo "=== Pipeline complete ==="
cat "${RESULTS}/report.csv"
|