scout/adapter/mongo/variant_loader.py
def load_variants(self, case_obj, variant_type='clinical', category='snv',
                      rank_threshold=None, chrom=None, start=None, end=None,
                      gene_obj=None, build='37'):
        """Load variants for a case into scout.

        Load the variants for a specific analysis type and category into scout.
        If no region is specified, load all variants above rank score threshold
        If region or gene is specified, load all variants from that region
        disregarding variant rank(if not specified)

        Args:
            case_obj(dict): A case from the scout database
            variant_type(str): 'clinical' or 'research'. Default: 'clinical'
            category(str): 'snv', 'str' or 'sv'. Default: 'snv'
            rank_threshold(float): Only load variants above this score. Default: 0
            chrom(str): Load variants from a certain chromosome
            start(int): Specify the start position
            end(int): Specify the end position
            gene_obj(dict): A gene object from the database

        Returns:
            nr_inserted(int)
        """
        # We need the institute object
        institute_id = self.institute(institute_id=case_obj['owner'])['_id']
        nr_inserted = 0

        variant_file = None
        if variant_type == 'clinical':
            if category == 'snv':
                variant_file = case_obj['vcf_files'].get('vcf_snv')
            elif category == 'sv':
                variant_file = case_obj['vcf_files'].get('vcf_sv')
            elif category == 'str':
                LOG.debug('Attempt to load STR VCF.')
                variant_file = case_obj['vcf_files'].get('vcf_str')
            elif category == 'cancer':
                # Currently this implies a paired tumor normal
                variant_file = case_obj['vcf_files'].get('vcf_cancer')
        elif variant_type == 'research':
            if category == 'snv':
                variant_file = case_obj['vcf_files'].get('vcf_snv_research')
            elif category == 'sv':
                variant_file = case_obj['vcf_files'].get('vcf_sv_research')
            elif category == 'cancer':
                variant_file = case_obj['vcf_files'].get('vcf_cancer_research')

        if not variant_file:
            raise SyntaxError("Vcf file does not seem to exist")

        # Check if there are any variants in file
        try:
            vcf_obj = VCF(variant_file)
            var = next(vcf_obj)
        except StopIteration as err:
            LOG.warning("Variant file %s does not include any variants", variant_file)
            return nr_inserted
        # We need to reload the file
        vcf_obj = VCF(variant_file)

        # Parse the neccessary headers from vcf file
        rank_results_header = parse_rank_results_header(vcf_obj)
        vep_header = parse_vep_header(vcf_obj)

        # This is a dictionary to tell where ind are in vcf
        individual_positions = {}
        for i, ind in enumerate(vcf_obj.samples):
            individual_positions[ind] = i
        
        # Dictionary for cancer analysis
        sample_info = {}
        if category == 'cancer':
            for ind in case_obj['individuals']:
                if ind['phenotype'] == 2:
                    sample_info[ind['individual_id']] = 'case'
                else:
                    sample_info[ind['individual_id']] = 'control'

        # Check if a region scould be uploaded
        region = ""
        if gene_obj:
            chrom = gene_obj['chromosome']
            # Add same padding as VEP
            start = max(gene_obj['start'] - 5000, 0)
            end = gene_obj['end'] + 5000
        if chrom:
            # We want to load all variants in the region regardless of rank score
            rank_threshold = rank_threshold or -1000
            if not (start and end):
                raise SyntaxError("Specify chrom start and end")
            region = "{0}:{1}-{2}".format(chrom, start, end)
        else:
            rank_threshold = rank_threshold or 0

        variants = vcf_obj(region)

        try:
            nr_inserted = self._load_variants(
                variants=variants,
                variant_type=variant_type,
                case_obj=case_obj,
                individual_positions=individual_positions,
                rank_threshold=rank_threshold,
                institute_id=institute_id,
                build=build,
                rank_results_header=rank_results_header,
                vep_header=vep_header,
                category=category,
                sample_info = sample_info
            )
        except Exception as error:
            LOG.exception('unexpected error')
            LOG.warning("Deleting inserted variants")
            self.delete_variants(case_obj['_id'], variant_type)
            raise error

        self.update_variant_rank(case_obj, variant_type, category=category)

        return nr_inserted
def load_variants(self, case_obj, variant_type='clinical', category='snv',
                      rank_threshold=None, chrom=None, start=None, end=None,
                      gene_obj=None, build='37'):
        """Load variants for a case into scout.

        Load the variants for a specific analysis type and category into scout.
        If no region is specified, load all variants above rank score threshold
        If region or gene is specified, load all variants from that region
        disregarding variant rank(if not specified)

        Args:
            case_obj(dict): A case from the scout database
            variant_type(str): 'clinical' or 'research'. Default: 'clinical'
            category(str): 'snv', 'str' or 'sv'. Default: 'snv'
            rank_threshold(float): Only load variants above this score. Default: 0
            chrom(str): Load variants from a certain chromosome
            start(int): Specify the start position
            end(int): Specify the end position
            gene_obj(dict): A gene object from the database

        Returns:
            nr_inserted(int)
        """
        # We need the institute object
        institute_id = self.institute(institute_id=case_obj['owner'])['_id']
        nr_inserted = 0

        variant_file = None
        if variant_type == 'clinical':
            if category == 'snv':
                variant_file = case_obj['vcf_files'].get('vcf_snv')
            elif category == 'sv':
                variant_file = case_obj['vcf_files'].get('vcf_sv')
            elif category == 'str':
                LOG.debug('Attempt to load STR VCF.')
                variant_file = case_obj['vcf_files'].get('vcf_str')
            elif category == 'cancer':
                # Currently this implies a paired tumor normal
                variant_file = case_obj['vcf_files'].get('vcf_cancer')
        elif variant_type == 'research':
            if category == 'snv':
                variant_file = case_obj['vcf_files'].get('vcf_snv_research')
            elif category == 'sv':
                variant_file = case_obj['vcf_files'].get('vcf_sv_research')
            elif category == 'cancer':
                variant_file = case_obj['vcf_files'].get('vcf_cancer_research')

        if not variant_file:
            raise SyntaxError("Vcf file does not seem to exist")

        # Check if there are any variants in file
        try:
            vcf_obj = VCF(variant_file)
            var = next(vcf_obj)
        except StopIteration as err:
            LOG.warning("Variant file %s does not include any variants", variant_file)
            return nr_inserted
        # We need to reload the file
        vcf_obj = VCF(variant_file)

        # Parse the neccessary headers from vcf file
        rank_results_header = parse_rank_results_header(vcf_obj)
        vep_header = parse_vep_header(vcf_obj)

        # This is a dictionary to tell where ind are in vcf
        individual_positions = {}
        for i, ind in enumerate(vcf_obj.samples):
            individual_positions[ind] = i
        
        # Dictionary for cancer analysis
        sample_info = {}
        if category == 'cancer':
            for ind in case_obj['individuals']:
                if ind['phenotype'] == 2:
                    sample_info[ind['individual_id']] = 'case'
                else:
                    sample_info[ind['individual_id']] = 'control'

        # Check if a region scould be uploaded
        region = ""
        if gene_obj:
            chrom = gene_obj['chromosome']
            # Add same padding as VEP
            start = max(gene_obj['start'] - 5000, 0)
            end = gene_obj['end'] + 5000
        if chrom:
            # We want to load all variants in the region regardless of rank score
            rank_threshold = rank_threshold or -1000
            if not (start and end):
                raise SyntaxError("Specify chrom start and end")
            region = "{0}:{1}-{2}".format(chrom, start, end)
        else:
            rank_threshold = rank_threshold or 0

        variants = vcf_obj(region)

        try:
            nr_inserted = self._load_variants(
                variants=variants,
                variant_type=variant_type,
                case_obj=case_obj,
                individual_positions=individual_positions,
                rank_threshold=rank_threshold,
                institute_id=institute_id,
                build=build,
                rank_results_header=rank_results_header,
                vep_header=vep_header,
                category=category,
                sample_info = sample_info
            )
        except Exception as error:
            LOG.exception('unexpected error')
            LOG.warning("Deleting inserted variants")
            self.delete_variants(case_obj['_id'], variant_type)
            raise error

        self.update_variant_rank(case_obj, variant_type, category=category)

        return nr_inserted
def load_variants ( self , case_obj , variant_type = 'clinical' , category = 'snv' , rank_threshold = None , chrom = None , start = None , end = None , gene_obj = None , build = '37' ) : # We need the institute object institute_id = self . institute ( institute_id = case_obj [ 'owner' ] ) [ '_id' ] nr_inserted = 0 variant_file = None if variant_type == 'clinical' : if category == 'snv' : variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_snv' ) elif category == 'sv' : variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_sv' ) elif category == 'str' : LOG . debug ( 'Attempt to load STR VCF.' ) variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_str' ) elif category == 'cancer' : # Currently this implies a paired tumor normal variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_cancer' ) elif variant_type == 'research' : if category == 'snv' : variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_snv_research' ) elif category == 'sv' : variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_sv_research' ) elif category == 'cancer' : variant_file = case_obj [ 'vcf_files' ] . get ( 'vcf_cancer_research' ) if not variant_file : raise SyntaxError ( "Vcf file does not seem to exist" ) # Check if there are any variants in file try : vcf_obj = VCF ( variant_file ) var = next ( vcf_obj ) except StopIteration as err : LOG . warning ( "Variant file %s does not include any variants" , variant_file ) return nr_inserted # We need to reload the file vcf_obj = VCF ( variant_file ) # Parse the neccessary headers from vcf file rank_results_header = parse_rank_results_header ( vcf_obj ) vep_header = parse_vep_header ( vcf_obj ) # This is a dictionary to tell where ind are in vcf individual_positions = { } for i , ind in enumerate ( vcf_obj . samples ) : individual_positions [ ind ] = i # Dictionary for cancer analysis sample_info = { } if category == 'cancer' : for ind in case_obj [ 'individuals' ] : if ind [ 'phenotype' ] == 2 : sample_info [ ind [ 'individual_id' ] ] = 'case' else : sample_info [ ind [ 'individual_id' ] ] = 'control' # Check if a region scould be uploaded region = "" if gene_obj : chrom = gene_obj [ 'chromosome' ] # Add same padding as VEP start = max ( gene_obj [ 'start' ] - 5000 , 0 ) end = gene_obj [ 'end' ] + 5000 if chrom : # We want to load all variants in the region regardless of rank score rank_threshold = rank_threshold or - 1000 if not ( start and end ) : raise SyntaxError ( "Specify chrom start and end" ) region = "{0}:{1}-{2}" . format ( chrom , start , end ) else : rank_threshold = rank_threshold or 0 variants = vcf_obj ( region ) try : nr_inserted = self . _load_variants ( variants = variants , variant_type = variant_type , case_obj = case_obj , individual_positions = individual_positions , rank_threshold = rank_threshold , institute_id = institute_id , build = build , rank_results_header = rank_results_header , vep_header = vep_header , category = category , sample_info = sample_info ) except Exception as error : LOG . exception ( 'unexpected error' ) LOG . warning ( "Deleting inserted variants" ) self . delete_variants ( case_obj [ '_id' ] , variant_type ) raise error self . update_variant_rank ( case_obj , variant_type , category = category ) return nr_inserted
Load variants for a case into scout.

        Load the variants for a specific analysis type and category into scout.
        If no region is specified, load all variants above rank score threshold
        If region or gene is specified, load all variants from that region
        disregarding variant rank(if not specified)

        Args:
            case_obj(dict): A case from the scout database
            variant_type(str): 'clinical' or 'research'. Default: 'clinical'
            category(str): 'snv', 'str' or 'sv'. Default: 'snv'
            rank_threshold(float): Only load variants above this score. Default: 0
            chrom(str): Load variants from a certain chromosome
            start(int): Specify the start position
            end(int): Specify the end position
            gene_obj(dict): A gene object from the database

        Returns:
            nr_inserted(int)
Load variants for a case into scout .