def test_load_par_variant(mongo_adapter, par_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(par_variant) family_id = case_obj.family_id individuals = case_obj.individuals mongo_variant = db.variant.find_one() assert mongo_variant == None load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id] assert mongo_variant['observations'] == 1 assert mongo_variant['homozygote'] == 0 assert mongo_variant['hemizygote'] == 0
def test_delete_variants(mongo_adapter, het_variant, case_obj, ind_positions): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, ind_positions=ind_positions, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id] delete_variants(adapter=mongo_adapter, family_id=family_id, ind_positions=ind_positions, individuals=individuals, vcf=vcf) mongo_variant = db.variant.find_one() assert mongo_variant == None
def frequency(context, bulk, remove): """Upload or remove variants to/from frequency database.""" loqusdb_auth = context.obj.get("loqusdb", {}) adapter = LoqusdbAdapter() adapter.connect(**loqusdb_auth) mip_case = context.obj["case"] if mip_case.is_old: logger.warn("unsupported MIP version for frequency: %s", mip_case.version) else: vcf_path = mip_case.research_vcf case_group_id = group_id(mip_case) # we only care about "rare" variants ped_path = mip_case.configs.pedigree try: affected_inds = affected_individuals(ped_path) except FrequencyError as error: logger.warn("LOQUSDB: {}".format(error.message)) if "affected_inds" in locals(): with open(vcf_path, "r") as vcf_stream: try: if remove: count = delete_variants(adapter, vcf_stream, case_group_id, affected_inds) logger.info("removed {} variants".format(count)) else: load_variants( adapter, case_group_id, affected_inds, vcf_stream, bulk_insert=bulk, vcf_path=vcf_path ) except CaseError as error: logger.warn("LOQUSDB: {}".format(error.message))
def test_load_par_variant(mongo_adapter, par_variant, case_obj, ind_positions): ## GIVEN an adapter and a vcf with one PAR variant db = mongo_adapter.db vcf = [] vcf.append(par_variant) family_id = case_obj.family_id individuals = case_obj.individuals assert db.variant.find_one() == None ## WHEN loading the variant into the database load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, ind_positions=ind_positions, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() ## THEN assert that the variant is loaded correct assert mongo_variant['families'] == [family_id] assert mongo_variant['observations'] == 1 assert mongo_variant['homozygote'] == 0 assert mongo_variant['hemizygote'] == 0
def test_delete_variants(mongo_adapter, het_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id] delete_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf ) mongo_variant = db.variant.find_one() assert mongo_variant == None
def test_load_same_variant_different_case(mongo_adapter, het_variant, case_obj, ind_positions): ## GIVEN an adapter and a vcf db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals ## WHEN loading the variant into the database load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, ind_positions=ind_positions, vcf=vcf, ) family_id_2 = '2' load_variants( adapter=mongo_adapter, family_id=family_id_2, individuals=individuals, ind_positions=ind_positions, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['observations'] == 2 assert mongo_variant['families'] == [family_id, family_id_2] # # This test works when using a real mongo adapter but not with mongomock yet... # # def test_load_same_variant_many_cases(real_mongo_adapter, family_variant): # # """docstring for test_load_variants""" # # db = real_mongo_adapter.db # # # # vcf = [] # # vcf.append(family_variant) # # individuals=['proband', 'mother', 'father'] # # # # for i in range(40): # # family_id = str(i) # # # # load_variants( # # adapter=real_mongo_adapter, # # family_id=family_id, # # individuals=individuals, # # vcf=vcf, # # ) # # # # mongo_variant = db.variant.find_one() # # # # assert mongo_variant['observations'] == 40 # # assert len(mongo_variant['families']) == 20
def test_load_variants_skip_case_id(mongo_adapter, het_variant, case_obj, ind_positions): ## GIVEN an adapter and a vcf with tho heterygote variants db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals ## WHEN loading the variants into the database load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, ind_positions=ind_positions, vcf=vcf, skip_case_id=True, gq_treshold=20, ) mongo_variant = db.variant.find_one() ## THEN assert that the variant is loaded correct assert mongo_variant.get('families') == None
def test_load_two_variants(mongo_adapter, het_variant, case_obj, ind_positions): ## GIVEN an adapter and a vcf with tho heterygote variants db = mongo_adapter.db vcf = [] vcf.append(het_variant) vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals ## WHEN loading the variants into the database load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, ind_positions=ind_positions, vcf=vcf, gq_treshold=20, ) ## THEN assert that the variant is loaded correct mongo_variant = db.variant.find_one() assert mongo_variant['observations'] == 2
def test_load_same_variant_different_case(mongo_adapter, het_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf, ) family_id_2 = '2' individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id_2, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['observations'] == 2 assert mongo_variant['families'] == [family_id, family_id_2] # # This test works when using a real mongo adapter but not with mongomock yet... # # def test_load_same_variant_many_cases(real_mongo_adapter, family_variant): # # """docstring for test_load_variants""" # # db = real_mongo_adapter.db # # # # vcf = [] # # vcf.append(family_variant) # # individuals=['proband', 'mother', 'father'] # # # # for i in range(40): # # family_id = str(i) # # # # load_variants( # # adapter=real_mongo_adapter, # # family_id=family_id, # # individuals=individuals, # # vcf=vcf, # # ) # # # # mongo_variant = db.variant.find_one() # # # # assert mongo_variant['observations'] == 40 # # assert len(mongo_variant['families']) == 20
def upload(adapter, vcf_path, affected, case_id, force=False, bulk=True): with open(vcf_path, 'r') as vcf_stream: try: if force: count = delete_variants(adapter, vcf_stream, case_id, affected) logger.info("removed {} variants".format(count)) load_variants(adapter, case_id, affected, vcf_stream, bulk_insert=bulk, vcf_path=vcf_path) except CaseError as error: logger.warn("LOQUSDB: {}".format(error.message))
def test_delete_variant(mongo_adapter, het_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id_1 = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id_1, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id_1] family_id_2 = '2' load_variants( adapter=mongo_adapter, family_id=family_id_2, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id_1, family_id_2] assert mongo_variant['observations'] == 2 delete_variants(adapter=mongo_adapter, family_id=family_id_2, individuals=individuals, vcf=vcf) mongo_variant = db.variant.find_one() assert mongo_variant['families'] == [family_id_1] assert mongo_variant['observations'] == 1
def test_load_variants_skip_case_id(mongo_adapter, het_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf, skip_case_id=True, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant.get('families') == None
def test_load_two_variants(mongo_adapter, het_variant, case_obj): """docstring for test_load_variants""" db = mongo_adapter.db vcf = [] vcf.append(het_variant) vcf.append(het_variant) family_id = case_obj.family_id individuals = case_obj.individuals load_variants( adapter=mongo_adapter, family_id=family_id, individuals=individuals, vcf=vcf, gq_treshold=20, ) mongo_variant = db.variant.find_one() assert mongo_variant['observations'] == 2
def load(ctx, variant_file, family_file, family_type, bulk_insert): """Load the variants of a case The loading is based on if the variant is seen in a ny affected individual in the family. """ if not family_file: logger.error("Please provide a family file") ctx.abort() if variant_file == '-': logger.info("Parsing variants from stdin") variant_file = get_vcf_handle(fsock=sys.stdin) else: logger.info("Start parsing variants from stdin") variant_path = os.path.abspath(variant_file) variant_file = get_vcf_handle(infile=variant_file) try: family = get_family(family_lines=family_file, family_type=family_type) except SyntaxError as error: logger.warning(error.message) ctx.abort() if not family.affected_individuals: logger.error("No affected individuals could be found in ped file") ctx.abort() logger.info("Found affected individuals in ped file: {0}" .format(', '.join(family.affected_individuals))) adapter = ctx.obj['adapter'] try: load_variants(adapter, family.family_id, family.affected_individuals, variant_file, bulk_insert=bulk_insert, vcf_path=variant_path) except CaseError as error: logger.error(error.message) ctx.abort()