|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import subprocess |
| 6 | +import pandas as pd |
| 7 | +import yaml |
| 8 | + |
| 9 | +from string import Template |
| 10 | +#from snakemake_files.scripts.utils import getTargetInfo |
| 11 | + |
| 12 | +def getRuns(config): |
| 13 | + ret = {} |
| 14 | + #LEN: Weird, but using pandas to handle the comments in the file |
| 15 | + #KEY: need skipinitialspace to make it fault tolerant to spaces! |
| 16 | + metadata = pd.read_csv(config['metasheet'], index_col=0, sep=',', comment='#', skipinitialspace=True) |
| 17 | + f = metadata.to_csv().split() #make it resemble an actual file with lines |
| 18 | + #SKIP the hdr |
| 19 | + for l in f[1:]: |
| 20 | + tmp = l.strip().split(",") |
| 21 | + #print(tmp) |
| 22 | + ret[tmp[0]] = tmp[1:] |
| 23 | + #print(ret) |
| 24 | + config['runs'] = ret |
| 25 | + return config |
| 26 | + |
| 27 | +# def addCondaPaths_Config(config): |
| 28 | +# """ADDS the python2 paths to config""" |
| 29 | +# conda_root = subprocess.check_output('conda info --root',shell=True).decode('utf-8').strip() |
| 30 | +# config['conda_root'] = conda_root |
| 31 | +# config['wes_root'] = "%s/envs/wes" % conda_root |
| 32 | + |
| 33 | +def addCondaPaths_Config(config): |
| 34 | + """ADDS the python2 paths to config""" |
| 35 | + conda_root = subprocess.check_output('conda info --root',shell=True).decode('utf-8').strip() |
| 36 | + conda_path = os.path.join(conda_root, 'pkgs') |
| 37 | + current_path = os.getcwd() |
| 38 | + config['conda_root'] = conda_root |
| 39 | + config['rnaseq_root'] = "%s/envs/rna" % conda_root |
| 40 | + config['stat_root'] = "%s/envs/stat_perl_r" % conda_root |
| 41 | + config['centrifuge_root']= "/%s/envs/centrifuge_env" % conda_root |
| 42 | + config['rseqc_root']= "%s/envs/rseqc_env" % conda_root |
| 43 | + config['gatk4_root']= "/%s/envs/gatk4_env" % conda_root |
| 44 | + config['msisensor_root']= "/%s/envs/msisensor_env" % conda_root |
| 45 | + config['vep_root']= "/%s/envs/vep_env" % conda_root |
| 46 | + config['varscan_root']= "/%s/envs/varscan_env" % conda_root |
| 47 | + config['immnue_decov']= "%s/envs/imm_env" % conda_root |
| 48 | + config['trust4_env']= "%s/envs/trust4_env" % conda_root |
| 49 | + config['mMCP_env']= "%s/envs/mMCP_env" % conda_root |
| 50 | + config['pvacseq'] = "%s/envs/pvacseq" % conda_root |
| 51 | + |
| 52 | + |
| 53 | +def addExecPaths(config): |
| 54 | + conda_root = subprocess.check_output('conda info --root',shell=True).decode('utf-8').strip() |
| 55 | + conda_path = os.path.join(conda_root, 'pkgs') |
| 56 | + current_path = os.getcwd() |
| 57 | + TRUST4_path = "/liulab/RIMA_kraken" |
| 58 | + arcasHLA_path = "/liulab/linyang" |
| 59 | + #NEED the following when invoking python2 (to set proper PYTHONPATH) |
| 60 | + if not "pvacseq_path" in config or not config["pvacseq_path"]: |
| 61 | + config["pvacseq_path"] = os.path.join(conda_root, 'envs', 'pvacseq', 'bin') |
| 62 | + if not "trust4_path" in config or not config["trust4_path"]: |
| 63 | + config["trust4_path"] = os.path.join(TRUST4_path,'TRUST4') |
| 64 | + if not "arcasHLA_path" in config or not config["arcasHLA_path"]: |
| 65 | + config["arcasHLA_path"] = os.path.join(arcasHLA_path,'arcasHLA') |
| 66 | + if not "msisensor2_path" in config or not config["msisensor2_path"]: |
| 67 | + config["msisensor2_path"] = os.path.join(TRUST4_path,'msisensor2') |
| 68 | + return config |
| 69 | + |
| 70 | +def loadRef(config): |
| 71 | + f = open(config['ref']) |
| 72 | + ref_info = yaml.safe_load(f) |
| 73 | + f.close() |
| 74 | + #print(ref_info[config['assembly']]) |
| 75 | + for (k,v) in ref_info[config['assembly']].items(): |
| 76 | + #NO CLOBBERING what is user-defined! |
| 77 | + if k not in config: |
| 78 | + config[k] = v |
| 79 | + |
| 80 | +def load_config(config_file): |
| 81 | + #load the main config file including parameters which are not change a lot |
| 82 | + with open(config_file, 'r') as stream: |
| 83 | + try: |
| 84 | + return yaml.safe_load(stream) |
| 85 | + except yaml.YAMLError as exc: |
| 86 | + print(exc) |
| 87 | + |
| 88 | +def load_execution(execution_file): |
| 89 | + #load the main config file including parameters which are not change a lot |
| 90 | + with open(execution_file, 'r') as stream: |
| 91 | + try: |
| 92 | + return yaml.safe_load(stream) |
| 93 | + except yaml.YAMLError as exc: |
| 94 | + print(exc) |
| 95 | + |
| 96 | +#--------- CONFIG set up --------------- |
| 97 | +config = load_config('config.yaml') |
| 98 | +execution = load_execution('execution.yaml') |
| 99 | +addCondaPaths_Config(config) |
| 100 | +#config = getRuns(config) |
| 101 | +loadRef(config) |
| 102 | +addExecPaths(config) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +#------------------------------------------------------------------------------ |
| 108 | +# TARGETS |
| 109 | +#------------------------------------------------------------------------------ |
| 110 | +def all_targets(wildcards): |
| 111 | + ls = [] |
| 112 | + #------------------------------------------------------------------------ |
| 113 | + ##Level1 |
| 114 | + #------------------------------------------------------------------------ |
| 115 | + if execution["star"]: |
| 116 | + ls.extend(align_targets(wildcards)) |
| 117 | + ls.extend(star_report_tagets(wildcards)) |
| 118 | + if execution["salmon"]: |
| 119 | + ls.extend(salmon_targets(wildcards)) |
| 120 | + if execution["rseqc"]: |
| 121 | + ls.extend(rseqc_targets(wildcards)) |
| 122 | + #----------------------------------------------------------------------- |
| 123 | + ##Level2 |
| 124 | + #---------------------------------------------------------------------- |
| 125 | + if execution["batch_removal"]: |
| 126 | + ls.extend(combat_targets(wildcards)) |
| 127 | + if execution["deseq2"]: |
| 128 | + ls.extend(deseq2_targets(wildcards)) |
| 129 | + if execution["gsea"]: |
| 130 | + ls.extend(gsea_targets(wildcards)) |
| 131 | + if execution["ssgsea"]: |
| 132 | + ls.extend(ssgsea_targets(wildcards)) |
| 133 | + #-------------------------------------------------------------------- |
| 134 | + ##Level3 |
| 135 | + #-------------------------------------------------------------------- |
| 136 | + # if execution["fusion"]: |
| 137 | + # ls.extend(fusion_targets(wildcards)) |
| 138 | + if execution["microbiota"]: |
| 139 | + ls.extend(microbe_targets(wildcards)) |
| 140 | + if execution["trust4"]: |
| 141 | + ls.extend(trust4_targets(wildcards)) |
| 142 | + if execution["arcasHLA"]: |
| 143 | + ls.extend(arcasHLA_targets(wildcards)) |
| 144 | + #--------------------------------------------------------------------- |
| 145 | + ##Level4 |
| 146 | + #--------------------------------------------------------------------- |
| 147 | +# if execution["tide"]: |
| 148 | +# ls.extend(tide_targets(wildcards)) |
| 149 | + if execution["immunedeconv"]: |
| 150 | + ls.extend(immunedeconv_targets(wildcards)) |
| 151 | + if execution["mMCP"]: |
| 152 | + ls.extend(mMCP_targets(wildcards)) |
| 153 | + #------------------------------------------------------------------- |
| 154 | + ##Level5 |
| 155 | + #------------------------------------------------------------------- |
| 156 | + if execution["msisensor2"]: |
| 157 | + ls.extend(msisensor_targets(wildcards)) |
| 158 | + if execution["report"]: |
| 159 | + ls.extend(report_targets(wildcards)) |
| 160 | + if execution["pvacseq"]: |
| 161 | + ls.extend(neoantigen_targets(wildcards)) |
| 162 | + |
| 163 | + return ls |
| 164 | + |
| 165 | +rule target: |
| 166 | + input: |
| 167 | + all_targets |
| 168 | + message: "Compiling all output" |
| 169 | + |
| 170 | + ##Level1 |
| 171 | +if execution["star"]: |
| 172 | + include: "./snakemake_files/level1/star_final.snakefile" |
| 173 | +if execution["salmon"]: |
| 174 | + include: "./snakemake_files/level1/salmon_final.snakefile" |
| 175 | +if execution["rseqc"]: |
| 176 | + include: "./snakemake_files/level1/rseqc_final.snakefile" |
| 177 | +##Level2 |
| 178 | +if execution["batch_removal"]: |
| 179 | + include: "./snakemake_files/level2/batch_removal.snakefile" |
| 180 | +if execution["deseq2"]: |
| 181 | + include: "./snakemake_files/level2/deseq2.modified.snakefile" |
| 182 | +if execution["gsea"]: |
| 183 | + include: "./snakemake_files/level2/gsea.modified.snakefile" |
| 184 | +if execution["ssgsea"]: |
| 185 | + include: "./snakemake_files/level2/ssgsea_final.snakefile" |
| 186 | +##Level3 |
| 187 | +#if execution["fusion"]: |
| 188 | +# include: "./snakemake_files/level3_individual.snakefile" |
| 189 | +if execution["microbiota"]: |
| 190 | + include: "./snakemake_files/level3/microbiota_final.snakefile" |
| 191 | +if execution["trust4"]: |
| 192 | + include: "./snakemake_files/level3/trust4_final.snakefile" |
| 193 | +if execution["arcasHLA"]: |
| 194 | + include: "./snakemake_files/level3/arcasHLA.snakefile" |
| 195 | +##Level4 |
| 196 | +#if execution["tide"]: |
| 197 | +# include: "./snakemake_files/level4/tide.snakefile" |
| 198 | +if execution["immunedeconv"]: |
| 199 | + include: "./snakemake_files/level4/immunedeconv.snakefile" |
| 200 | +if execution["mMCP"]: |
| 201 | + include: "./snakemake_files/level4/mMCP.snakefile" |
| 202 | +##Level5 |
| 203 | +if execution["msisensor2"]: |
| 204 | + include: "./snakemake_files/level5/msisensor.snakefile" |
| 205 | +if execution["report"]: |
| 206 | + include: "./snakemake_files/report.snakefile" |
| 207 | +if execution["pvacseq"]: |
| 208 | + include: "./snakemake_files/level5/pvacseq_final.snakefile" |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
0 commit comments