From 57d82513b25d75752ad17b8cc5606add01699f09 Mon Sep 17 00:00:00 2001 From: Floreal Cabanettes <floreal.cabanettes@inra.fr> Date: Thu, 29 Mar 2018 16:59:37 +0200 Subject: [PATCH] Refactoring --- build_pop.py | 4 +- exceptions.py => lib/exceptions.py | 0 .../genotype_results.py | 46 +++++++++++-------- .../variants_simulator.py | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) rename exceptions.py => lib/exceptions.py (100%) rename genotype_results.py => lib/genotype_results.py (93%) rename variants_simulator.py => lib/variants_simulator.py (99%) diff --git a/build_pop.py b/build_pop.py index c12f436..f5acfbb 100755 --- a/build_pop.py +++ b/build_pop.py @@ -11,8 +11,8 @@ import vcf from Bio import SeqIO import tempfile from pysam import tabix_compress, tabix_index, VariantFile -from variants_simulator import VariantsSimulator -from exceptions import InputException, ExecException +from lib.variants_simulator import VariantsSimulator +from lib.exceptions import InputException, ExecException import multiprocessing import subprocess diff --git a/exceptions.py b/lib/exceptions.py similarity index 100% rename from exceptions.py rename to lib/exceptions.py diff --git a/genotype_results.py b/lib/genotype_results.py similarity index 93% rename from genotype_results.py rename to lib/genotype_results.py index a236c05..2199f1f 100755 --- a/genotype_results.py +++ b/lib/genotype_results.py @@ -65,25 +65,7 @@ def getvarsize(variant): return variant.stop - variant.start + 1 -def main(genotypes, predicted, filtered, output, type_v="del", verbose=False): - true_genotypes = readgenotypes(vcffile=genotypes, - variants={}, - type_v=type_v) - pred_genotypes = {} - filtered_genotypes = {} - with open(predicted, "r") as preds: - for pred in preds: - pred = pred.rstrip() - if pred != "": - readgenotypes(vcffile=pred, - variants=pred_genotypes, - type_v=type_v) - if filtered: - readgenotypes(vcffile=pred, - variants=filtered_genotypes, - type_v=type_v, - dofilter=True) - +def build(true_genotypes, pred_genotypes, filtered_genotypes, output): true_pybed = variants_to_pybed(true_genotypes) pred_pybed = variants_to_pybed(pred_genotypes) @@ -103,6 +85,32 @@ def main(genotypes, predicted, filtered, output, type_v="del", verbose=False): (sv[3], "pass", varsize, correct / (wrong + correct), left_prec, right_prec)) out.write("%s\t%s\t%d\t%3.2f\t%d\t%d\n" % (sv[3], soft, varsize, correct / (wrong + correct), left_prec, right_prec)) + return pred_with_true + + +def main(genotypes, predicted, filtered, output, type_v="del", verbose=False): + true_genotypes = readgenotypes(vcffile=genotypes, + variants={}, + type_v=type_v) + pred_genotypes = {} + filtered_genotypes = {} + with open(predicted, "r") as preds: + for pred in preds: + pred = pred.rstrip() + if pred != "": + readgenotypes(vcffile=pred, + variants=pred_genotypes, + type_v=type_v) + if filtered: + readgenotypes(vcffile=pred, + variants=filtered_genotypes, + type_v=type_v, + dofilter=True) + + pred_with_true = build(true_genotypes=true_genotypes, + pred_genotypes=pred_genotypes, + filtered_genotypes=filtered_genotypes, + output=output) if verbose: print("%d variants" % (len(true_genotypes))) diff --git a/variants_simulator.py b/lib/variants_simulator.py similarity index 99% rename from variants_simulator.py rename to lib/variants_simulator.py index d05d551..8d5fc4f 100755 --- a/variants_simulator.py +++ b/lib/variants_simulator.py @@ -5,7 +5,7 @@ import sys import re import random -from exceptions import InputException +from lib.exceptions import InputException from Bio import SeqIO from joblib import Parallel, delayed import multiprocessing -- GitLab