scout/parse/omim.py
def parse_genemap2(lines):
    """Parse the omim source file called genemap2.txt
    
    Explanation of Phenotype field:
    Brackets, "[ ]", indicate "nondiseases," mainly genetic variations that 
    lead to apparently abnormal laboratory test values.

    Braces, "{ }", indicate mutations that contribute to susceptibility to 
    multifactorial disorders (e.g., diabetes, asthma) or to susceptibility 
    to infection (e.g., malaria).

    A question mark, "?", before the phenotype name indicates that the 
    relationship between the phenotype and gene is provisional. 
    More details about this relationship are provided in the comment 
    field of the map and in the gene and phenotype OMIM entries.

    The number in parentheses after the name of each disorder indicates 
    the following: 
        (1) the disorder was positioned by mapping of the wildtype gene; 
        (2) the disease phenotype itself was mapped; 
        (3) the molecular basis of the disorder is known; 
        (4) the disorder is a chromosome deletion or duplication syndrome. 
    
    Args:
        lines(iterable(str))
    
    Yields:
        parsed_entry(dict)
    """
    LOG.info("Parsing the omim genemap2")
    header = []
    for i,line in enumerate(lines):
        line = line.rstrip()
        if line.startswith('#'):
            if i < 10:
                if line.startswith('# Chromosome'):
                    header = line[2:].split('\t')
            continue

        if len(line) < 5:
            continue

        parsed_entry = parse_omim_line(line, header)
        parsed_entry['mim_number'] = int(parsed_entry['Mim Number'])
        parsed_entry['raw'] = line
        
        # This is the approved symbol for the entry
        hgnc_symbol = parsed_entry.get("Approved Symbol")
        
        # If no approved symbol could be found choose the first of
        # the gene symbols
        gene_symbols = []
        if parsed_entry.get('Gene Symbols'):
            gene_symbols = [symbol.strip() for symbol in parsed_entry['Gene Symbols'].split(',')]
        parsed_entry['hgnc_symbols'] = gene_symbols

        if not hgnc_symbol and gene_symbols:
            hgnc_symbol = gene_symbols[0]
            
        parsed_entry['hgnc_symbol'] = hgnc_symbol
        
        # Gene inheritance is a construct. It is the union of all inheritance
        # patterns found in the associated phenotypes
        gene_inheritance = set()
        
        parsed_phenotypes = []
        # Information about the related phenotypes
        # Each related phenotype is separated by ';'
        for phenotype_info in parsed_entry.get('Phenotypes', '').split(';'):
            if not phenotype_info:
                continue

            phenotype_info = phenotype_info.lstrip()
            
            # First symbol in description indicates phenotype status
            # If no special symbol is used the phenotype is 'established'
            phenotype_status = OMIM_STATUS_MAP.get(phenotype_info[0], 'established')
            
            # Skip phenotype entries that not associated to disease
            if phenotype_status == 'nondisease':
                continue
            
            phenotype_description = ""

            # We will try to save the description    
            splitted_info = phenotype_info.split(',')
            for i, text in enumerate(splitted_info):
                # Everything before ([1,2,3])
                # We check if we are in the part where the mim number exists
                match = entry_pattern.search(text)
                if not match:
                    phenotype_description += text
                else:
                    # If we find the end of the entry
                    mimnr_match = mimnr_pattern.search(phenotype_info)
                    # Then if the entry have a mim number we choose that
                    if mimnr_match:
                        phenotype_mim = int(mimnr_match.group())
                    else:
                        phenotype_mim = parsed_entry['mim_number']
                        phenotype_description += text[:-4]
                    break
            # Find the inheritance
            inheritance = set()
            inheritance_text = ','.join(splitted_info[i:])
            for term in mim_inheritance_terms:
                if term in inheritance_text:
                    
                    inheritance.add(TERMS_MAPPER[term])
                    gene_inheritance.add(TERMS_MAPPER[term])
                
            parsed_phenotypes.append(
                {
                    'mim_number':phenotype_mim, 
                    'inheritance': inheritance,
                    'description': phenotype_description.strip('?\{\}'),
                    'status': phenotype_status,
                }
            )
        
        parsed_entry['phenotypes'] = parsed_phenotypes
        parsed_entry['inheritance'] = gene_inheritance
                
        yield parsed_entry
def parse_genemap2(lines):
    """Parse the omim source file called genemap2.txt
    
    Explanation of Phenotype field:
    Brackets, "[ ]", indicate "nondiseases," mainly genetic variations that 
    lead to apparently abnormal laboratory test values.

    Braces, "{ }", indicate mutations that contribute to susceptibility to 
    multifactorial disorders (e.g., diabetes, asthma) or to susceptibility 
    to infection (e.g., malaria).

    A question mark, "?", before the phenotype name indicates that the 
    relationship between the phenotype and gene is provisional. 
    More details about this relationship are provided in the comment 
    field of the map and in the gene and phenotype OMIM entries.

    The number in parentheses after the name of each disorder indicates 
    the following: 
        (1) the disorder was positioned by mapping of the wildtype gene; 
        (2) the disease phenotype itself was mapped; 
        (3) the molecular basis of the disorder is known; 
        (4) the disorder is a chromosome deletion or duplication syndrome. 
    
    Args:
        lines(iterable(str))
    
    Yields:
        parsed_entry(dict)
    """
    LOG.info("Parsing the omim genemap2")
    header = []
    for i,line in enumerate(lines):
        line = line.rstrip()
        if line.startswith('#'):
            if i < 10:
                if line.startswith('# Chromosome'):
                    header = line[2:].split('\t')
            continue

        if len(line) < 5:
            continue

        parsed_entry = parse_omim_line(line, header)
        parsed_entry['mim_number'] = int(parsed_entry['Mim Number'])
        parsed_entry['raw'] = line
        
        # This is the approved symbol for the entry
        hgnc_symbol = parsed_entry.get("Approved Symbol")
        
        # If no approved symbol could be found choose the first of
        # the gene symbols
        gene_symbols = []
        if parsed_entry.get('Gene Symbols'):
            gene_symbols = [symbol.strip() for symbol in parsed_entry['Gene Symbols'].split(',')]
        parsed_entry['hgnc_symbols'] = gene_symbols

        if not hgnc_symbol and gene_symbols:
            hgnc_symbol = gene_symbols[0]
            
        parsed_entry['hgnc_symbol'] = hgnc_symbol
        
        # Gene inheritance is a construct. It is the union of all inheritance
        # patterns found in the associated phenotypes
        gene_inheritance = set()
        
        parsed_phenotypes = []
        # Information about the related phenotypes
        # Each related phenotype is separated by ';'
        for phenotype_info in parsed_entry.get('Phenotypes', '').split(';'):
            if not phenotype_info:
                continue

            phenotype_info = phenotype_info.lstrip()
            
            # First symbol in description indicates phenotype status
            # If no special symbol is used the phenotype is 'established'
            phenotype_status = OMIM_STATUS_MAP.get(phenotype_info[0], 'established')
            
            # Skip phenotype entries that not associated to disease
            if phenotype_status == 'nondisease':
                continue
            
            phenotype_description = ""

            # We will try to save the description    
            splitted_info = phenotype_info.split(',')
            for i, text in enumerate(splitted_info):
                # Everything before ([1,2,3])
                # We check if we are in the part where the mim number exists
                match = entry_pattern.search(text)
                if not match:
                    phenotype_description += text
                else:
                    # If we find the end of the entry
                    mimnr_match = mimnr_pattern.search(phenotype_info)
                    # Then if the entry have a mim number we choose that
                    if mimnr_match:
                        phenotype_mim = int(mimnr_match.group())
                    else:
                        phenotype_mim = parsed_entry['mim_number']
                        phenotype_description += text[:-4]
                    break
            # Find the inheritance
            inheritance = set()
            inheritance_text = ','.join(splitted_info[i:])
            for term in mim_inheritance_terms:
                if term in inheritance_text:
                    
                    inheritance.add(TERMS_MAPPER[term])
                    gene_inheritance.add(TERMS_MAPPER[term])
                
            parsed_phenotypes.append(
                {
                    'mim_number':phenotype_mim, 
                    'inheritance': inheritance,
                    'description': phenotype_description.strip('?\{\}'),
                    'status': phenotype_status,
                }
            )
        
        parsed_entry['phenotypes'] = parsed_phenotypes
        parsed_entry['inheritance'] = gene_inheritance
                
        yield parsed_entry
Parse the omim source file called genemap2.txt
    
    Explanation of Phenotype field:
    Brackets, "[ ]", indicate "nondiseases," mainly genetic variations that 
    lead to apparently abnormal laboratory test values.

    Braces, "{ }", indicate mutations that contribute to susceptibility to 
    multifactorial disorders (e.g., diabetes, asthma) or to susceptibility 
    to infection (e.g., malaria).

    A question mark, "?", before the phenotype name indicates that the 
    relationship between the phenotype and gene is provisional. 
    More details about this relationship are provided in the comment 
    field of the map and in the gene and phenotype OMIM entries.

    The number in parentheses after the name of each disorder indicates 
    the following: 
        (1) the disorder was positioned by mapping of the wildtype gene; 
        (2) the disease phenotype itself was mapped; 
        (3) the molecular basis of the disorder is known; 
        (4) the disorder is a chromosome deletion or duplication syndrome. 
    
    Args:
        lines(iterable(str))
    
    Yields:
        parsed_entry(dict)