File size: 11,714 Bytes
badc964 | 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 | #!/bin/bash
set -e
# =============================================================================
# Task 13: Mobile Genetic Element Characterization (Diamond DAG, depth=5)
#
# DAG structure:
#
# L0: reads
# L1: fastp (trim)
# L2: shovill (assemble)
# │
# L3: ├── prokka (annotate) ──────────────────────────────────────┐
# │ ├── L4a: isescan (IS elements) │
# │ └── L4b: integron_finder (integrons) │
# │ │
# ├── amrfinder (protein AMR) ───┐ │
# │ ├─ L4c: cross_validate_amr │
# ├── abricate CARD (nuc AMR) ───┘ │
# │ │
# ├── mob_recon (plasmid recon) ─── L4d: plasmidfinder.py ────┤
# │ (replicon typing) │
# ├── abricate VFDB (virulence) ─────────────────────────────┤
# │ │
# └── quast + busco (QC) ────────────────────────────────────┤
# │
# L5: MERGE ──────────────────────────────────────────────────────┘
#
# =============================================================================
THREADS=$(( $(nproc) > 8 ? 8 : $(nproc) ))
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DATA="${SCRIPT_DIR}/data"
OUT="${SCRIPT_DIR}/outputs"
RES="${SCRIPT_DIR}/results"
log_step() {
echo "=================================================================="
echo "STEP: $1"
echo "Started: $(date)"
echo "=================================================================="
}
mkdir -p "${OUT}"/{trimmed,assembly,prokka,isescan,integrons,amr,plasmids,virulence,qc} "${RES}"
# ===========================================================================
# L0-L1: Preprocessing
# ===========================================================================
log_step "L1: Quality trimming with fastp"
if [ ! -f "${OUT}/trimmed/R1.fastq" ]; then
fastp --in1 "${DATA}/reads_R1.fastq" --in2 "${DATA}/reads_R2.fastq" \
--out1 "${OUT}/trimmed/R1.fastq" --out2 "${OUT}/trimmed/R2.fastq" \
--detect_adapter_for_pe --cut_front --cut_tail --cut_mean_quality 20 \
--length_required 30 --thread ${THREADS} \
--json "${OUT}/trimmed/fastp.json" --html "${OUT}/trimmed/fastp.html"
else echo "Skipping (exists)"; fi
# ===========================================================================
# L2: Assembly
# ===========================================================================
log_step "L2: Genome assembly with shovill"
if [ ! -f "${OUT}/assembly/contigs.fa" ]; then
shovill --R1 "${OUT}/trimmed/R1.fastq" --R2 "${OUT}/trimmed/R2.fastq" \
--outdir "${OUT}/assembly" --gsize 2914567 --cpus ${THREADS} --ram 8 --force
else echo "Skipping (exists)"; fi
CONTIGS="${OUT}/assembly/contigs.fa"
# ===========================================================================
# L3: DIAMOND BRANCHES (all depend only on contigs.fa)
# ===========================================================================
# --- L3-A: Gene annotation with Prokka ---
log_step "L3-A: Gene annotation with Prokka"
if [ ! -f "${OUT}/prokka/MRSA.gff" ]; then
prokka "${CONTIGS}" --outdir "${OUT}/prokka" --prefix MRSA \
--cpus ${THREADS} --kingdom Bacteria --genus Staphylococcus --species aureus --force
else echo "Skipping (exists)"; fi
# --- L3-B: AMR detection with AMRFinderPlus (protein-homology method) ---
log_step "L3-B: AMR detection with AMRFinderPlus"
if [ ! -f "${OUT}/amr/amrfinder_results.tsv" ]; then
amrfinder --nucleotide "${CONTIGS}" --organism Staphylococcus_aureus \
--threads ${THREADS} --output "${OUT}/amr/amrfinder_results.tsv" --plus 2>&1 || true
else echo "Skipping (exists)"; fi
# --- L3-C: AMR detection with ABRicate CARD (nucleotide method) ---
log_step "L3-C: AMR detection with ABRicate (CARD)"
if [ ! -f "${OUT}/amr/abricate_card.tsv" ]; then
abricate "${CONTIGS}" --db card --minid 80 --mincov 60 > "${OUT}/amr/abricate_card.tsv"
else echo "Skipping (exists)"; fi
# --- L3-D: Plasmid reconstruction with mob_recon ---
log_step "L3-D: Plasmid reconstruction with mob_recon"
if [ ! -f "${OUT}/plasmids/contig_report.txt" ]; then
mob_recon --infile "${CONTIGS}" --outdir "${OUT}/plasmids" \
--num_threads ${THREADS} --force 2>&1 || true
else echo "Skipping (exists)"; fi
# --- L3-E: Virulence factor detection with ABRicate VFDB ---
log_step "L3-E: Virulence detection with ABRicate (VFDB)"
if [ ! -f "${OUT}/virulence/abricate_vfdb.tsv" ]; then
abricate "${CONTIGS}" --db vfdb --minid 80 --mincov 60 > "${OUT}/virulence/abricate_vfdb.tsv"
else echo "Skipping (exists)"; fi
# --- L3-F: Assembly QC with QUAST ---
log_step "L3-F: Assembly QC with QUAST"
if [ ! -f "${OUT}/qc/quast/report.tsv" ]; then
quast "${CONTIGS}" -o "${OUT}/qc/quast" -t ${THREADS}
else echo "Skipping (exists)"; fi
# --- L3-G: Assembly completeness with BUSCO ---
log_step "L3-G: Assembly completeness with BUSCO"
if [ ! -d "${OUT}/qc/busco" ]; then
busco -i "${CONTIGS}" -o busco --out_path "${OUT}/qc" \
-l bacteria_odb10 -m genome -c ${THREADS} --force
else echo "Skipping (exists)"; fi
# ===========================================================================
# L4: SUB-BRANCHES (depend on L3 outputs → deeper diamond)
# ===========================================================================
# --- L4-A: IS element detection with ISEScan (depends on Prokka proteome/contigs) ---
log_step "L4-A: IS element detection with ISEScan"
if [ ! -d "${OUT}/isescan/hmm" ]; then
cp "${CONTIGS}" "${OUT}/isescan/MRSA.fa"
isescan.py --seqfile "${OUT}/isescan/MRSA.fa" --output "${OUT}/isescan" --nthread ${THREADS} || true
else echo "Skipping (exists)"; fi
# --- L4-B: Integron detection with integron_finder (depends on annotation context) ---
log_step "L4-B: Integron detection with integron_finder"
if [ ! -d "${OUT}/integrons/Results_Integron_Finder_MRSA" ]; then
cp "${CONTIGS}" "${OUT}/integrons/MRSA.fasta"
integron_finder --local-max --cpu ${THREADS} \
--outdir "${OUT}/integrons" \
"${OUT}/integrons/MRSA.fasta" 2>&1 || true
else echo "Skipping (exists)"; fi
# --- L4-C: Cross-validate AMR (merge AMRFinderPlus + ABRicate CARD results) ---
log_step "L4-C: Cross-validate AMR results"
echo "gene,method,identity" > "${OUT}/amr/cross_validated.csv"
# Genes found by AMRFinderPlus
if [ -s "${OUT}/amr/amrfinder_results.tsv" ]; then
tail -n +2 "${OUT}/amr/amrfinder_results.tsv" | \
awk -F'\t' 'BEGIN{OFS=","} {print $7,"amrfinder",$3}' >> "${OUT}/amr/cross_validated.csv"
fi
# Genes found by ABRicate CARD
if [ -s "${OUT}/amr/abricate_card.tsv" ]; then
tail -n +2 "${OUT}/amr/abricate_card.tsv" | \
awk -F'\t' 'BEGIN{OFS=","} {print $6,"abricate_card",$10}' >> "${OUT}/amr/cross_validated.csv"
fi
AMR_BOTH=$(awk -F',' 'NR>1{genes[$1]++} END{c=0; for(g in genes) if(genes[g]>=2) c++; print c}' \
"${OUT}/amr/cross_validated.csv")
echo "AMR genes confirmed by both methods: ${AMR_BOTH}"
# --- L4-D: Replicon typing with plasmidfinder (depends on mob_recon output) ---
log_step "L4-D: Replicon typing with plasmidfinder"
if [ ! -f "${OUT}/plasmids/plasmidfinder_results.tsv" ]; then
# Run plasmidfinder on each plasmid contig identified by mob_recon
PLASMID_FA="${OUT}/plasmids/plasmid_contigs.fasta"
if [ -f "${OUT}/plasmids/plasmid_AA109.fasta" ] || ls "${OUT}/plasmids/plasmid_"*.fasta 1>/dev/null 2>&1; then
cat "${OUT}/plasmids/plasmid_"*.fasta > "${PLASMID_FA}" 2>/dev/null || true
fi
if [ -s "${PLASMID_FA}" ]; then
plasmidfinder.py -i "${PLASMID_FA}" -o "${OUT}/plasmids/pf_out" -x 2>&1 || true
cp "${OUT}/plasmids/pf_out/results_tab.tsv" "${OUT}/plasmids/plasmidfinder_results.tsv" 2>/dev/null || true
else
# No plasmid contigs found by mob_recon — run on full assembly
plasmidfinder.py -i "${CONTIGS}" -o "${OUT}/plasmids/pf_out" -x 2>&1 || true
cp "${OUT}/plasmids/pf_out/results_tab.tsv" "${OUT}/plasmids/plasmidfinder_results.tsv" 2>/dev/null || true
fi
else echo "Skipping (exists)"; fi
# ===========================================================================
# L5: MERGE — Combine all branch results
# ===========================================================================
log_step "L5-MERGE: Building comprehensive results CSV"
# Extract QUAST
TOTAL_LEN=$(grep "^Total length" "${OUT}/qc/quast/report.tsv" | head -1 | cut -f2)
NUM_CONTIGS=$(grep "^# contigs " "${OUT}/qc/quast/report.tsv" | head -1 | cut -f2)
N50=$(grep "^N50" "${OUT}/qc/quast/report.tsv" | cut -f2)
GC=$(grep "^GC" "${OUT}/qc/quast/report.tsv" | cut -f2)
LARGEST=$(grep "^Largest contig" "${OUT}/qc/quast/report.tsv" | cut -f2)
# Extract BUSCO
BUSCO_SUM=$(grep "C:" "${OUT}/qc/busco/short_summary.specific.bacteria_odb10.busco.txt" 2>/dev/null \
| head -1 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' || echo "N/A")
# Extract Prokka
CDS=$(grep "^CDS" "${OUT}/prokka/MRSA.txt" | awk '{print $2}')
TRNA=$(grep "^tRNA" "${OUT}/prokka/MRSA.txt" | awk '{print $2}')
RRNA=$(grep "^rRNA" "${OUT}/prokka/MRSA.txt" | awk '{print $2}')
# IS elements
IS_COUNT=$(tail -n +2 "${OUT}/isescan/MRSA.fa.is.csv" 2>/dev/null | wc -l || echo "0")
IS_COUNT=$(echo $IS_COUNT | tr -d ' ')
# Integrons
INTEGRON_COUNT=$(grep -c "^" "${OUT}/integrons/Results_Integron_Finder_MRSA/MRSA.integrons" 2>/dev/null || echo "0")
INTEGRON_COUNT=$(echo $INTEGRON_COUNT | tr -d ' ')
# AMR counts
AMR_PROTEIN=$(tail -n +2 "${OUT}/amr/amrfinder_results.tsv" 2>/dev/null | wc -l || echo "0")
AMR_PROTEIN=$(echo $AMR_PROTEIN | tr -d ' ')
AMR_NUC=$(tail -n +2 "${OUT}/amr/abricate_card.tsv" 2>/dev/null | wc -l || echo "0")
AMR_NUC=$(echo $AMR_NUC | tr -d ' ')
# Virulence
VF_COUNT=$(tail -n +2 "${OUT}/virulence/abricate_vfdb.tsv" 2>/dev/null | wc -l || echo "0")
VF_COUNT=$(echo $VF_COUNT | tr -d ' ')
# Plasmids
PLASMID_COUNT=$(grep -c "plasmid" "${OUT}/plasmids/contig_report.txt" 2>/dev/null || echo "0")
PLASMID_COUNT=$(echo $PLASMID_COUNT | tr -d ' ')
REPLICON_COUNT=$(tail -n +2 "${OUT}/plasmids/plasmidfinder_results.tsv" 2>/dev/null | wc -l || echo "0")
REPLICON_COUNT=$(echo $REPLICON_COUNT | tr -d ' ')
# Write main CSV
cat > "${RES}/genomic_characterization.csv" << CSVEOF
metric,value
total_length,${TOTAL_LEN}
num_contigs,${NUM_CONTIGS}
n50,${N50}
gc_content,${GC}
largest_contig,${LARGEST}
completeness,${BUSCO_SUM}
cds_count,${CDS}
trna_count,${TRNA}
rrna_count,${RRNA}
is_elements_found,${IS_COUNT}
integrons_found,${INTEGRON_COUNT}
amr_genes_protein_method,${AMR_PROTEIN}
amr_genes_nucleotide_method,${AMR_NUC}
amr_genes_confirmed_both,${AMR_BOTH}
virulence_factors,${VF_COUNT}
plasmid_contigs,${PLASMID_COUNT}
replicon_types,${REPLICON_COUNT}
CSVEOF
# Write detailed AMR cross-validation CSV
cp "${OUT}/amr/cross_validated.csv" "${RES}/amr_cross_validated.csv"
echo ""
echo "=== Pipeline complete ==="
cat "${RES}/genomic_characterization.csv"
echo ""
ls -lh "${RES}/"
|