示例#1
0
文件: ped.py 项目: stjmonde/puzzle-2
def get_cases(variant_source, case_lines=None, case_type='ped',
              variant_type='snv', variant_mode='vcf'):
        """Create a cases and populate it with individuals

            Args:
                variant_source (str): Path to vcf files
                case_lines (Iterable): Ped like lines
                case_type (str): Format of case lines

            Returns:
                case_objs (list(puzzle.models.Case))
        """
        individuals = get_individuals(
            variant_source=variant_source,
            case_lines=case_lines,
            case_type=case_type,
            variant_mode=variant_mode
        )
        case_objs = []
        case_ids = set()

        compressed = False
        tabix_index = False
        #If no individuals we still need to have a case id
        if variant_source.endswith('.gz'):
            logger.debug("Found compressed variant source")
            compressed = True
            tabix_file = '.'.join([variant_source, 'tbi'])
            if os.path.exists(tabix_file):
                logger.debug("Found index file")
                tabix_index = True

        if len(individuals) > 0:
            for individual in individuals:
                case_ids.add(individual.case_id)
        else:
            case_ids = [os.path.basename(variant_source)]

        for case_id in case_ids:
            logger.info("Found case {0}".format(case_id))
            case = Case(
                case_id=case_id,
                name=case_id,
                variant_source=variant_source,
                variant_type=variant_type,
                variant_mode=variant_mode,
                compressed=compressed,
                tabix_index=tabix_index
                )

            # Add the individuals to the correct case
            for individual in individuals:
                if individual.case_id == case_id:
                    logger.info("Adding ind {0} to case {1}".format(
                        individual.name, individual.case_id
                    ))
                    case.add_individual(individual)

            case_objs.append(case)

        return case_objs
示例#2
0
            Args:
                case_id (str): A case id

            Returns:
                A Case object
        """
        if case_id:
            for case in self.case_objs:
                if case.case_id == case_id:
                    return case
        else:
            if self.cases:
                return list(self.case_objs)[0]

        return Case(case_id='unknown')

    def individual(self, ind_id=None):
        """Return a individual object
        
            Args:
                ind_id (str): A individual id
            
            Returns:
                individual (puzzle.models.individual)
        """
        for ind_obj in self.individual_objs:
            if ind_obj.ind_id == ind_id:
                return ind_obj
        return None