# ========================================================
# By: Weersma Group, UMCG (2020)
#
# script for microbial co-abundance network analysis
# ========================================================

# uses SparCC network inference tool: http://psbweb05.psb.ugent.be/conet/microbialnetworks/sparcc.php

### invoke sparcc (Python implementation) for network inference [species]
# ===========================================
INPUT_PATH=dag3_8268samples_721species.txt # microbiome species (generated by Metaphlan2)
BOOT_ITER=1000 # number of bootstraps
OUTPUT=./output_species

python SparCC.py $INPUT_PATH --cor_file=$OUTPUT_PATH/real_cors_sparcc_sp.txt
python MakeBootstraps.py $INPUT_PATH -n $BOOT_ITER -t permutation_#.txt -p $OUTPUT_PATH/Resamplings/
# compute sparcc on permutated datasets [100 permutations]
for i in 0 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
do
python SparCC.py $OUTPUT_PATH/Resamplings/permutation_$i.txt --cor_file=$OUTPUT_PATH/Bootstraps/sim_cor_$i.txt
done
# compute p-value from bootstraps
python PseudoPvals.py $OUTPUT_PATH/real_cors_sparcc_sp_fg.txt $OUTPUT_PATH/Bootstraps/sim_cor_#.txt $BOOT_ITER -o $OUTPUT_PATH/pvals_two_sided_sp_fg.txt -t two_sided

### invoke sparcc (Python implementation) for network inference [species]
# ===========================================
INPUT_PATH=dag3_8268samples_569pathway_reads.txt #microbial pathways (MetaCyc, generated by humann2)
BOOT_ITER=1000
OUTPUT=./output_pathways

python SparCC.py $INPUT_PATH --cor_file=$OUTPUT_PATH/real_cors_sparcc_sp.txt
python MakeBootstraps.py $INPUT_PATH -n $BOOT_ITER -t permutation_#.txt -p $OUTPUT_PATH/Resamplings/
# compute sparcc on permutated datasets [100 permutations]
for i in 0 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
do
python SparCC.py $OUTPUT_PATH/Resamplings/permutation_$i.txt --cor_file=$OUTPUT_PATH/Bootstraps/sim_cor_$i.txt
done
# compute p-value from bootstraps
python PseudoPvals.py $OUTPUT_PATH/real_cors_sparcc_sp_fg.txt $OUTPUT_PATH/Bootstraps/sim_cor_#.txt $BOOT_ITER -o $OUTPUT_PATH/pvals_two_sided_sp_fg.txt -t two_sided
