Exemplo n.º 1
0
Arquivo: mongo.py Projeto: gpcr/scout
 def cases(self, collaborator=None):
   self.logger.info("Fetch all cases")
   if collaborator:
     self.logger.info("Use collaborator {0}".format(collaborator))
     return Case.objects(collaborators=collaborator).order_by('-updated_at')
   else:
     return Case.objects().order_by('-updated_at')
Exemplo n.º 2
0
 def cases(self, collaborator=None):
   self.logger.info("Fetch all cases")
   if collaborator:
     self.logger.info("Use collaborator {0}".format(collaborator))
     return Case.objects(collaborators=collaborator).order_by('-created_at')
   else:
     return Case.objects().order_by('-created_at')
Exemplo n.º 3
0
    def cases(self, collaborator=None, query=None, skip_assigned=False):
        """Fetches all cases from the backend.

        Args:
            collaborator(str): If collaborator should be considered
            query(dict): If a specific query is used

        Yields:
            Cases ordered by date
        """
        logger.info("Fetch all cases")
        if collaborator:
            logger.info("Use collaborator {0}".format(collaborator))
            case_query = Case.objects(collaborators=collaborator)
        else:
            case_query = Case.objects

        if query:
            # filter if any user names match query
            matching_users = User.objects(name__icontains=query)

            # filter cases by matching display name of case or individuals
            case_query = case_query.filter(
                Q(display_name__icontains=query) |
                Q(individuals__display_name__icontains=query) |
                Q(assignee__in=matching_users)
            )

        if skip_assigned:
            case_query = case_query.filter(assignee__exists=False)

        return case_query.order_by('-updated_at')
Exemplo n.º 4
0
def get_case(request):
    logger.info("setup a case")
    case = Case(
        case_id="acase",
        display_name="acase",
        owner='cust000',
        collaborators = ['cust000']
    )
    logger.info("Adding case to database")
    case.save()
    def teardown():
        print('\n')
        logger.info('Removing case')
        case.delete()
        logger.info('Case removed')
    request.addfinalizer(teardown)
    
    return case
Exemplo n.º 5
0
    def add_case(self, case_lines, case_type, owner, scout_configs={}):
        """Add a case to the database

            If case exists in database it will be updated.
            This method will take information in a ped like format and create
            a case object. If the case is already in database it will update
            the necessary information.

            Args:
                case_lines(Iterator): An iterator with the pedigree infromation
                case_type(str): The format of the case lines
                owner(str): The institute id for owner of the case
        """
        logger.info("Setting up a family parser")
        case_parser = FamilyParser(case_lines,
                                   family_type=case_type)

        if len(case_parser.families) != 1:
            raise SyntaxError("Only one case per ped file is allowed")

        case_id = list(case_parser.families.keys())[0]
        logger.info("Found case {0}".format(case_id))

        if not self.institute(institute_id=owner):
            logger.warning("Institute {0} does not exist in database".format(
                owner))
            logger.info("Creating new institute")
            self.add_institute(internal_id=owner, display_name=owner)

        logger.info("Creating Case with id {0}".format(
            '_'.join([owner, case_id])))

        case = Case(
            case_id='_'.join([owner, case_id]),
            display_name=case_id,
            owner=owner,
        )

        collaborators = scout_configs.get('collaborators') or set()
        if collaborators:
            if isinstance(collaborators, list):
                collaborators = set(collaborators)
            else:
                collaborators = set([collaborators])

        collaborators.add(owner)

        case['collaborators'] = list(collaborators)
        logger.debug("Setting collaborators to: {0}".format(
          ', '.join(collaborators)))

        analysis_type = scout_configs.get('analysis_type', 'unknown').lower()
        case['analysis_type'] = analysis_type
        logger.debug("Setting analysis type to: {0}".format(analysis_type))

        # Get the path of vcf from configs
        vcf = scout_configs.get('igv_vcf', '')
        case['vcf_file'] = vcf
        logger.debug("Setting igv vcf file to: {0}".format(vcf))

        # Add the genome build information
        genome_build = scout_configs.get('human_genome_build', '')
        case['genome_build'] = genome_build
        logger.debug("Setting genome build to: {0}".format(genome_build))

        # Get the genome version
        genome_version = float(scout_configs.get('human_genome_version', '0'))
        case['genome_version'] = genome_version
        logger.debug("Setting genome version to: {0}".format(genome_version))

        # Add the rank model version
        rank_model_version = scout_configs.get('rank_model_version', '')
        case['rank_model_version'] = rank_model_version
        logger.debug("Setting rank model version to: {0}".format(
              rank_model_version))

        # Check the analysis date
        analysis_date = scout_configs.get('analysis_date', '')
        case['analysis_date'] = analysis_date
        logger.debug("Setting analysis date to: {0}".format(analysis_date))

        # Add the pedigree picture, this is a xml file that will be read and
        # saved in the mongo database
        madeline_path = path(scout_configs.get('madeline', '/__menoexist.tXt'))
        if madeline_path.exists():
            logger.debug("Found madeline info")
            with madeline_path.open('r') as handle:
                case['madeline_info'] = handle.read()
                logger.debug("Madeline file was read succesfully")
        else:
            logger.info("No madeline file found. Skipping madeline file.")

        # Add the coverage report
        coverage_report_path = path(scout_configs.get('coverage_report', '/__menoexist.tXt'))
        if coverage_report_path.exists():
            logger.debug("Found a coverage report")
            with coverage_report_path.open('rb') as handle:
                case['coverage_report'] = handle.read()
                logger.debug("Coverage was read succesfully")
        else:
            logger.info("No coverage report found. Skipping coverage report.")

        individuals = []
        # Add the individuals
        for ind_id in case_parser.individuals:
            ped_individual = case_parser.individuals[ind_id]
            individual = Individual(
                individual_id = ind_id,
                father = ped_individual.father,
                mother = ped_individual.mother,
                display_name = ped_individual.extra_info.get(
                                    'display_name', ind_id),
                sex = str(ped_individual.sex),
                phenotype = ped_individual.phenotype,
            )
            # Path to the bam file for IGV:
            individual['bam_file'] = scout_configs.get(
                'individuals',{}).get(
                    ind_id, {}).get(
                        'bam_path', '')

            individual['capture_kits'] = scout_configs.get(
                'individuals',{}).get(
                    ind_id, {}).get(
                        'capture_kit', [])
            #Add affected individuals first
            if ped_individual.affected:
                logger.info("Adding individual {0} to case {1}".format(
                    ind_id, case_id))
                case.individuals.append(individual)
            else:
                individuals.append(individual)

        for individual in individuals:
            logger.info("Adding individual {0} to case {1}".format(
                    individual.individual_id, case_id))
            case.individuals.append(individual)

        clinical_panels = []
        research_panels = []

        for gene_list in scout_configs.get('gene_lists', {}):
            logger.info("Found gene list {0}".format(gene_list))
            panel_info = scout_configs['gene_lists'][gene_list]

            panel_path = panel_info.get('file')
            panel_type = panel_info.get('type', 'clinical')
            panel_date = panel_info.get('date')
            panel_version = float(panel_info.get('version', '0'))
            panel_id = panel_info.get('name')
            display_name = panel_info.get('full_name', panel_id)

            panel = get_gene_panel(
                list_file_name=panel_path,
                institute_id=owner,
                panel_id=panel_id,
                panel_version=panel_version,
                display_name=display_name,
                panel_date=panel_date)

            logger.info("Store gene panel {0} in database".format(
                            panel.panel_name))
            panel.save()

            if panel_type == 'clinical':
                logger.info("Adding {0} to clinical gene lists".format(
                                panel.panel_name))
                clinical_panels.append(panel)
            else:
                logger.info("Adding {0} to research gene lists".format(
                                panel.panel_name))
                research_panels.append(panel)

        case['clinical_panels'] = clinical_panels
        case['research_panels'] = research_panels

        default_panels = scout_configs.get('default_panels', [])
        logger.info("Adding {0} as default panels to case {1}".format(
            ', '.join(default_panels), case_id))
        case['default_panels'] = list(default_panels)

        #If the case exists we need tu update the information
        if self.case(institute_id=owner, case_id=case_id):
            case = self.update_case(case)
        else:
            logger.info("Adding case {0} to database".format(case_id))
            case.save()

        return case
Exemplo n.º 6
0
def get_case(scout_configs, family_type):
  """
  Take a case file and return the case on the specified format.

  Only one case per pedigree file is allowed.

  Args:
    family_type : A string that describe the format of the ped file
    scout_configs (dict): A dictionary scout info.

  Returns:
    case : A mongo engine object that describe the case
            found in the pedigree file.

  """
  logger = logging.getLogger(__name__)
  # Use ped_parser to get information from the pedigree file
  case_parser = FamilyParser(open(scout_configs['ped'], 'r'), 
                             family_type=family_type)

  # Check if there is a owner of the case
  try:
    owner = scout_configs['owner']
  except KeyError as e:
    logger.error("Scout config must include a owner")
    raise e

  # Check if there are any collaborators for the case, a case can belong to
  # several institutes
  collaborators = scout_configs.get('collaborators', None)
  if collaborators:
    collaborators = set(collaborators)
  else:
    collaborators = set()
  collaborators.add(owner)

  logger.info("Collaborators found: {0}".format(','.join(collaborators)))
  logger.info("Cases found in ped file: {0}".format(
    ', '.join(list(case_parser.families.keys()))))
  
  if len(case_parser.families) != 1:
    raise SyntaxError("Only one case per ped file is allowed")
  
  for case_id in case_parser.families:
    case = case_parser.families[case_id]
    # Create a mongo engine case
    mongo_case_id = '_'.join([owner, case_id])
    mongo_case = Case(case_id=mongo_case_id)
    logger.debug("Setting case id to: {0}".format(mongo_case_id))

    mongo_case['owner'] = owner
    logger.debug("Setting owner to: {0}".format(owner))

    mongo_case['collaborators'] = list(collaborators)
    logger.debug("Setting collaborators to: {0}".format(
      ', '.join(collaborators)))

    # We use the family id as display name for scout
    mongo_case['display_name'] = case_id
    logger.debug("Setting display name to: {0}".format(case_id))

    # Get the path of vcf from configs
    mongo_case['vcf_file'] = scout_configs.get('igv_vcf', '')
    logger.debug("Setting igv vcf file to: {0}".format(
      scout_configs.get('igv_vcf', '')))

    # Add the genome build information
    mongo_case['genome_build'] = scout_configs.get('human_genome_build', '')
    logger.debug("Setting genome build to: {0}".format(
      scout_configs.get('human_genome_build', '')))

    # Get the genome version
    mongo_case['genome_version'] = float(scout_configs.get('human_genome_version', '0'))
    logger.debug("Setting genome version to: {0}".format(
      scout_configs.get('human_genome_version', '0')))

    # Check the analysis date
    mongo_case['analysis_date'] = scout_configs.get('analysis_date', '')
    logger.debug("Setting analysis date to: {0}".format(
      scout_configs.get('analysis_date', '')))

    # Add the pedigree picture, this is a xml file that will be read and 
    # saved in the mongo database
    madeline_path = path(scout_configs.get('madeline', '/__menoexist.tXt'))
    if madeline_path.exists():
      logger.debug("Found madeline info")
      with madeline_path.open('r') as handle:
        mongo_case['madeline_info'] = handle.read()
        logger.debug("Madeline file was read succesfully")
    else:
      logger.info("No madeline file found. Skipping madeline file.")

    # Add the coverage report
    coverage_report_path = path(scout_configs.get('coverage_report', '/__menoexist.tXt'))
    if coverage_report_path.exists():
      logger.debug("Found a coverage report")
      with coverage_report_path.open('rb') as handle:
        mongo_case['coverage_report'] = handle.read()
        logger.debug("Coverage was read succesfully")
    else:
      logger.info("No coverage report found. Skipping coverage report.")

    clinical_gene_lists = []
    research_gene_lists = []

    for gene_list in scout_configs.get('gene_lists', {}):
      logger.info("Found gene list {0}".format(gene_list))
      list_info = scout_configs['gene_lists'][gene_list]

      list_type = list_info.get('type', 'clinical')
      list_id = list_info.get('name', '')
      version = float(list_info.get('version', 0))
      date = list_info.get('date', '')
      display_name = list_info.get('full_name', list_id)

      list_object = GeneList(
                          list_id=list_id,
                          version=version,
                          date=date,
                          display_name=display_name
                          )

      if list_type == 'clinical':
        logger.info("Adding {0} to clinical gene lists".format(list_object))
        clinical_gene_lists.append(list_object)
      else:
        logger.info("Adding {0} to research gene lists".format(list_object))
        research_gene_lists.append(list_object)

    mongo_case['clinical_gene_lists'] = clinical_gene_lists
    mongo_case['research_gene_lists'] = research_gene_lists

    default_gene_lists = scout_configs.get('default_gene_lists', [])

    mongo_case['default_gene_lists'] = list(default_gene_lists)

    individuals = []
    for individual_id in case.individuals:
      individual = case.individuals[individual_id]
      # Get info from configs for the individual
      config_info = scout_configs.get(
                                  'individuals', {}
                                  ).get(
                                  individual_id, {}
                                  )
      ind = Individual()
      ind['individual_id'] = individual_id
      ind['father'] = individual.father
      ind['mother'] = individual.mother
      ind['display_name'] = individual.extra_info.get('display_name', individual_id)
      ind['sex'] = str(individual.sex)
      ind['phenotype'] = individual.phenotype
      # Path to the bam file for IGV:
      ind['bam_file'] = config_info.get('bam_path', '')

      ind['capture_kits'] = config_info.get('capture_kit', [])

      individuals.append(ind)

    mongo_case['individuals'] = individuals