| 1 |
import os |
| 2 |
import sys |
| 3 |
sys.path.insert(0, '/localdisk/home/s1680070/repositories/pangoLEARN') |
| 4 |
|
| 5 |
from Bio import SeqIO |
| 6 |
|
| 7 |
import collections |
| 8 |
import hashlib |
| 9 |
import collections |
| 10 |
import csv |
| 11 |
from pangoLEARN.training.get_lineage_positions import get_relevant_positions |
| 12 |
from pangoLEARN.training.utils import * |
| 13 |
|
| 14 |
from Bio import SeqIO |
| 15 |
from datetime import date |
| 16 |
today = date.today() |
| 17 |
csv.field_size_limit(sys.maxsize) |
| 18 |
|
| 19 |
repo_path = config["repo_path"].rstrip("/") |
| 20 |
pangoLEARN_path = os.path.join(repo_path, "pangoLEARN") |
| 21 |
pango_designation_path = os.path.join(repo_path, "pango-designation") |
| 22 |
|
| 23 |
# config["pango_version"] = get_pango_version(pango_designation_path) |
| 24 |
|
| 25 |
data_date = config["data_date"] |
| 26 |
config["trim_start"] = 265 |
| 27 |
config["trim_end"] = 29674 |
| 28 |
config["lineages_csv"]=f"{pango_designation_path}/lineages.csv" |
| 29 |
config["reference"] = f"{pangoLEARN_path}/pangoLEARN/training/reference.fasta" |
| 30 |
config["genbank_ref"] = f"{pangoLEARN_path}/pangoLEARN/training/WH04.gb" |
| 31 |
config["datadir"]= f"/localdisk/home/shared/raccoon-dog/{data_date}_gisaid/publish/gisaid" |
| 32 |
|
| 33 |
rule all: |
| 34 |
input: |
| 35 |
os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 36 |
# os.path.join(config["outdir"],"lineage_recall_report.txt"), |
| 37 |
os.path.join(config["outdir"],"pangolearn.init.py"), |
| 38 |
os.path.join(config["outdir"],"pangolin_data.init.py"), |
| 39 |
os.path.join(config["outdir"],"training_summary.rf.txt"), |
| 40 |
os.path.join(config["outdir"],"decision_tree_rules.txt"), |
| 41 |
os.path.join(config["outdir"],"lineage.hash.csv") |
| 42 |
|
| 43 |
rule make_plearn_init: |
| 44 |
output: |
| 45 |
init = os.path.join(config["outdir"],"pangolearn.init.py") |
| 46 |
run: |
| 47 |
pangolearn_new_v = config["pangolearn_version"] |
| 48 |
pango_version = config["pango_version"] |
| 49 |
with open(output.init,"w") as fw: |
| 50 |
fw.write(f'''_program = "pangoLEARN" |
| 51 |
__version__ = "{pangolearn_new_v}" |
| 52 |
PANGO_VERSION = "{pango_version}" |
| 53 |
|
| 54 |
__all__ = ["training"] |
| 55 |
|
| 56 |
from pangoLEARN import * |
| 57 |
''') |
| 58 |
|
| 59 |
|
| 60 |
rule make_pdata_init: |
| 61 |
output: |
| 62 |
init = os.path.join(config["outdir"],"pangolin_data.init.py") |
| 63 |
run: |
| 64 |
pango_version = config["pango_version"] |
| 65 |
with open(output.init,"w") as fw: |
| 66 |
fw.write(f'''_program = "pangolin_data" |
| 67 |
__version__ = "{pango_version}" |
| 68 |
|
| 69 |
''') |
| 70 |
|
| 71 |
rule filter_alignment: |
| 72 |
input: |
| 73 |
csv = config["lineages_csv"], |
| 74 |
fasta = os.path.join(config["datadir"],f"gisaid_{data_date}_all_alignment.fa"), |
| 75 |
full_csv = os.path.join(config["datadir"],f"gisaid_{data_date}_all_metadata.csv") |
| 76 |
output: |
| 77 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 78 |
csv = os.path.join(config["outdir"],"lineages.metadata.filtered.csv"), |
| 79 |
csv_all = os.path.join(config["outdir"],"lineages.designated.csv") |
| 80 |
run: |
| 81 |
csv_len = 0 |
| 82 |
seqs_len = 0 |
| 83 |
lineages = {} |
| 84 |
all_lineages = {} |
| 85 |
all_len = 0 |
| 86 |
with open(input.csv,"r") as f: |
| 87 |
for l in f: |
| 88 |
l = l.rstrip("\n") |
| 89 |
name,lineage = l.split(",") |
| 90 |
|
| 91 |
lineages[name]=lineage |
| 92 |
csv_len +=1 |
| 93 |
all_lineages[name]=lineage |
| 94 |
all_len +=1 |
| 95 |
with open(output.csv,"w") as fw: |
| 96 |
with open(output.csv_all,"w") as fw2: |
| 97 |
with open(input.full_csv,"r") as f: |
| 98 |
|
| 99 |
reader = csv.DictReader(f) |
| 100 |
header = reader.fieldnames |
| 101 |
|
| 102 |
writer = csv.DictWriter(fw, fieldnames=header, lineterminator="\n") |
| 103 |
writer.writeheader() |
| 104 |
|
| 105 |
writer2 = csv.DictWriter(fw2, fieldnames=header, lineterminator="\n") |
| 106 |
writer2.writeheader() |
| 107 |
|
| 108 |
for row in reader: |
| 109 |
name = row["sequence_name"].replace("SouthAfrica","South_Africa") |
| 110 |
if name in lineages: |
| 111 |
new_row = row |
| 112 |
new_row["lineage"] = lineages[name] |
| 113 |
writer.writerow(new_row) |
| 114 |
if name in all_lineages: |
| 115 |
all_row = row |
| 116 |
all_row["lineage"] = all_lineages[name] |
| 117 |
writer2.writerow(all_row) |
| 118 |
written = {} |
| 119 |
with open(output.fasta,"w") as fw: |
| 120 |
for record in SeqIO.parse(input.fasta, "fasta"): |
| 121 |
record.id = record.id.replace("SouthAfrica","South_Africa") |
| 122 |
if record.id in lineages and not record.id in written: |
| 123 |
fw.write(f">{record.id}\n{record.seq}\n") |
| 124 |
|
| 125 |
written[record.id]=1 |
| 126 |
seqs_len +=1 |
| 127 |
|
| 128 |
print("Number of sequences in gisaid designated", all_len) |
| 129 |
print("Number of sequences going into pangolearn training",csv_len) |
| 130 |
print("Number of sequences found on gisaid", seqs_len) |
| 131 |
|
| 132 |
|
| 133 |
rule align_with_minimap2: |
| 134 |
input: |
| 135 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 136 |
reference = config["reference"] |
| 137 |
output: |
| 138 |
sam = os.path.join(config["outdir"],"alignment.sam") |
| 139 |
shell: |
| 140 |
""" |
| 141 |
minimap2 -a -x asm5 -t {workflow.cores} \ |
| 142 |
{input.reference:q} \ |
| 143 |
{input.fasta:q} > {output.sam:q} |
| 144 |
""" |
| 145 |
|
| 146 |
rule get_variants: |
| 147 |
input: |
| 148 |
sam = os.path.join(config["outdir"],"alignment.sam") |
| 149 |
output: |
| 150 |
csv = os.path.join(config["outdir"],"variants.csv") #gisaid.mutations.csv |
| 151 |
shell: |
| 152 |
""" |
| 153 |
gofasta sam variants -t {workflow.cores} \ |
| 154 |
--samfile {input.sam:q} \ |
| 155 |
--reference {config[reference]} \ |
| 156 |
--genbank {config[genbank_ref]} \ |
| 157 |
--outfile {output.csv} |
| 158 |
""" |
| 159 |
|
| 160 |
rule add_lineage: |
| 161 |
input: |
| 162 |
csv = os.path.join(config["outdir"],"variants.csv"), #gisaid.mutations.csv |
| 163 |
lineages = os.path.join(config["outdir"],"lineages.designated.csv") |
| 164 |
output: |
| 165 |
csv = os.path.join(config["outdir"],"variants.lineages.csv") |
| 166 |
run: |
| 167 |
lineages_dict = {} |
| 168 |
with open(input.lineages,"r") as f: |
| 169 |
reader= csv.DictReader(f) |
| 170 |
for row in reader: |
| 171 |
lineages_dict[row["sequence_name"]] = row["lineage"] |
| 172 |
with open(output.csv, "w") as fw: |
| 173 |
fw.write("sequence_name,nucleotide_variants,lineage,why_excluded\n") |
| 174 |
with open(input.csv,"r") as f: |
| 175 |
for l in f: |
| 176 |
l = l.strip("\n") |
| 177 |
name,variants = l.split(",") |
| 178 |
if name =="query": |
| 179 |
pass |
| 180 |
elif name in lineages_dict: |
| 181 |
fw.write(f"{name},{variants},{lineages_dict[name]},\n") |
| 182 |
|
| 183 |
rule filter_metadata: |
| 184 |
input: |
| 185 |
csv = os.path.join(config["outdir"],"variants.lineages.csv"), |
| 186 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta") |
| 187 |
output: |
| 188 |
csv = os.path.join(config["outdir"],"metadata.final.csv") |
| 189 |
run: |
| 190 |
in_list = {} |
| 191 |
for record in SeqIO.parse(input.fasta,"fasta"): |
| 192 |
in_list[record.id] = 1 |
| 193 |
|
| 194 |
with open(output.csv, "w") as fw: |
| 195 |
fw.write("sequence_name,lineage\n") |
| 196 |
with open(input.csv, "r") as f: |
| 197 |
reader = csv.DictReader(f) |
| 198 |
for row in reader: |
| 199 |
if row["sequence_name"] in in_list: |
| 200 |
name = row["sequence_name"] |
| 201 |
lineage = row["lineage"] |
| 202 |
fw.write(f"{name},{lineage}\n") |
| 203 |
|
| 204 |
rule get_relevant_postions: |
| 205 |
input: |
| 206 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 207 |
csv = os.path.join(config["outdir"],"metadata.final.csv"), |
| 208 |
reference = config["reference"] |
| 209 |
output: |
| 210 |
relevant_pos_obj = os.path.join(config["outdir"],"relevantPositions.pickle"), |
| 211 |
run: |
| 212 |
get_relevant_positions(input.csv,input.fasta,input.reference,output.relevant_pos_obj) |
| 213 |
|
| 214 |
rule run_rf_training: |
| 215 |
input: |
| 216 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 217 |
csv = os.path.join(config["outdir"],"metadata.final.csv"), |
| 218 |
reference = config["reference"], |
| 219 |
relevant_pos_obj = rules.get_relevant_postions.output.relevant_pos_obj |
| 220 |
params: |
| 221 |
path_to_script = pangoLEARN_path |
| 222 |
output: |
| 223 |
headers = os.path.join(config["outdir"],"randomForestHeaders_v1.joblib"), |
| 224 |
model = os.path.join(config["outdir"],"randomForest_v1.joblib"), |
| 225 |
txt = os.path.join(config["outdir"],"training_summary.rf.txt") |
| 226 |
shell: |
| 227 |
""" |
| 228 |
python {params.path_to_script}/pangoLEARN/training/pangoLEARNRandomForest_v1.py \ |
| 229 |
{input.csv:q} \ |
| 230 |
{input.fasta} \ |
| 231 |
{input.reference:q} \ |
| 232 |
{config[outdir]} \ |
| 233 |
{input.relevant_pos_obj} \ |
| 234 |
> {output.txt:q} |
| 235 |
""" |
| 236 |
|
| 237 |
rule run_dt_training: |
| 238 |
input: |
| 239 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 240 |
csv = os.path.join(config["outdir"],"metadata.final.csv"), |
| 241 |
reference = config["reference"], |
| 242 |
relevant_pos_obj = rules.get_relevant_postions.output.relevant_pos_obj |
| 243 |
params: |
| 244 |
path_to_script = pangoLEARN_path |
| 245 |
output: |
| 246 |
headers = os.path.join(config["outdir"],"decisionTreeHeaders_v1.joblib"), |
| 247 |
model = os.path.join(config["outdir"],"decisionTree_v1.joblib"), |
| 248 |
txt = os.path.join(config["outdir"],"training_summary.dt.txt") |
| 249 |
shell: |
| 250 |
""" |
| 251 |
python {params.path_to_script}/pangoLEARN/training/pangoLEARNDecisionTree_v1.py \ |
| 252 |
{input.csv:q} \ |
| 253 |
{input.fasta} \ |
| 254 |
{input.reference:q} \ |
| 255 |
{config[outdir]} \ |
| 256 |
{input.relevant_pos_obj} \ |
| 257 |
> {output.txt:q} |
| 258 |
""" |
| 259 |
|
| 260 |
rule get_decisions: |
| 261 |
input: |
| 262 |
headers = os.path.join(config["outdir"],"decisionTreeHeaders_v1.joblib"), |
| 263 |
model = os.path.join(config["outdir"],"decisionTree_v1.joblib"), |
| 264 |
txt = rules.run_dt_training.output.txt |
| 265 |
params: |
| 266 |
path_to_script = pangoLEARN_path |
| 267 |
output: |
| 268 |
txt = os.path.join(config["outdir"],"decision_tree_rules.txt"), |
| 269 |
zipped = os.path.join(config["outdir"],"decision_tree_rules.zip") |
| 270 |
shell: |
| 271 |
""" |
| 272 |
python {params.path_to_script}/pangoLEARN/training/getDecisionTreeRules.py \ |
| 273 |
{input.model:q} {input.headers:q} {input.txt:q} \ |
| 274 |
> {output.txt:q} && zip {output.zipped:q} {output.txt:q} |
| 275 |
""" |
| 276 |
|
| 277 |
# rule get_recall: |
| 278 |
# input: |
| 279 |
# txt = rules.run_rf_training.output.txt |
| 280 |
# params: |
| 281 |
# path_to_script = pangoLEARN_path |
| 282 |
# output: |
| 283 |
# txt = os.path.join(config["outdir"],"lineage_recall_report.txt") |
| 284 |
# shell: |
| 285 |
# """ |
| 286 |
# python {params.path_to_script}/pangoLEARN/training/processOutputFile.py {input.txt} > {output.txt} |
| 287 |
# """ |
| 288 |
|
| 289 |
rule create_hash: |
| 290 |
input: |
| 291 |
fasta = os.path.join(config["outdir"],"alignment.filtered.fasta"), |
| 292 |
lin_designation = os.path.join(config["outdir"],"lineages.designated.csv") |
| 293 |
output: |
| 294 |
csv = os.path.join(config["outdir"],"lineage.hash.csv"), |
| 295 |
fasta = os.path.join(config["outdir"],"lineage.hash.fasta"), |
| 296 |
hashed_designations = os.path.join(config["outdir"],"designations.hash.csv") |
| 297 |
run: |
| 298 |
designated = get_dict(input.lin_designation,"sequence_name","lineage") |
| 299 |
|
| 300 |
hash_map,seq_hash_dict = add_to_hash(input.fasta) |
| 301 |
|
| 302 |
with open(output.csv,"w") as fw: |
| 303 |
fw.write("seq_hash,lineage\n") |
| 304 |
for seq_hash in hash_map: |
| 305 |
seq_name = hash_map[seq_hash] |
| 306 |
set_name = designated[seq_name] |
| 307 |
fw.write(f"{seq_hash},{set_name}\n") |
| 308 |
|
| 309 |
num_seqs = 0 |
| 310 |
with open(output.hashed_designations, "w") as fw2: |
| 311 |
fw2.write("taxon,lineage\n") |
| 312 |
with open(output.fasta, "w") as fw: |
| 313 |
for seq in seq_hash_dict: |
| 314 |
num_seqs +=1 |
| 315 |
fw.write(f">{seq_hash_dict[seq]}\n{seq}\n") |
| 316 |
fw2.write(f"{seq_hash_dict[seq]},{designated[seq_hash_dict[seq]]}\n") |
| 317 |
|
| 318 |
print("Number of seqs going into training: ",f"{num_seqs}") |