Build a protein fragment parser.
178172
def get_fragment_language() -> ParserElement:
    """"""
    _fragment_value_inner = fragment_range | missing_fragment(FRAGMENT_MISSING)
    _fragment_value = _fragment_value_inner | And([Suppress('"'), _fragment_value_inner, Suppress('"')])
    parser_element = fragment_tag + nest(_fragment_value + Optional(WCW + quote(FRAGMENT_DESCRIPTION)))
    return parser_element
950337
def parse_selection(lexer: Lexer) -> SelectionNode:
    """"""
    return (parse_fragment if peek(lexer, TokenKind.SPREAD) else parse_field)(lexer)
541723
def _read_mplain(self, lines):
        
        self.log(u"Parsing fragments from subtitles text format")
        word_separator = self._mplain_word_separator()
        self.log([u"Word separator is: '%s'", word_separator])
        lines = [line.strip() for line in lines]
        pairs = []
        i = 1
        current = 0
        tree = Tree()
        while current < len(lines):
            line_text = lines[current]
            if len(line_text) > 0:
                sentences = [line_text]
                following = current + 1
                while (following < len(lines)) and (len(lines[following]) > 0):
                    sentences.append(lines[following])
                    following += 1

                # here sentences holds the sentences for this paragraph

                # create paragraph node
                paragraph_identifier = u"p%06d" % i
                paragraph_lines = [u" ".join(sentences)]
                paragraph_fragment = TextFragment(
                    identifier=paragraph_identifier,
                    lines=paragraph_lines,
                    filtered_lines=paragraph_lines
                )
                paragraph_node = Tree(value=paragraph_fragment)
                tree.add_child(paragraph_node)
                self.log([u"Paragraph %s", paragraph_identifier])

                # create sentences nodes
                j = 1
                for s in sentences:
                    sentence_identifier = paragraph_identifier + u"s%06d" % j
                    sentence_lines = [s]
                    sentence_fragment = TextFragment(
                        identifier=sentence_identifier,
                        lines=sentence_lines,
                        filtered_lines=sentence_lines
                    )
                    sentence_node = Tree(value=sentence_fragment)
                    paragraph_node.add_child(sentence_node)
                    j += 1
                    self.log([u"  Sentence %s", sentence_identifier])

                    # create words nodes
                    k = 1
                    for w in [w for w in s.split(word_separator) if len(w) > 0]:
                        word_identifier = sentence_identifier + u"w%06d" % k
                        word_lines = [w]
                        word_fragment = TextFragment(
                            identifier=word_identifier,
                            lines=word_lines,
                            filtered_lines=word_lines
                        )
                        word_node = Tree(value=word_fragment)
                        sentence_node.add_child(word_node)
                        k += 1
                        self.log([u"    Word %s", word_identifier])

                # keep iterating
                current = following
                i += 1
            current += 1
        self.log(u"Storing tree")
        self.fragments_tree = tree
245706
def parseFullScan(self, i, modifications=False):
        
        scanObj = PeptideObject()
        peptide = str(i[1])
        pid=i[2]
        scanObj.acc = self.protein_map.get(i[4], i[4])
        if pid is None:
            return None
        if modifications:
            sql = 'select aam.ModificationName,pam.Position,aam.DeltaMass from peptidesaminoacidmodifications pam left join aminoacidmodifications aam on (aam.AminoAcidModificationID=pam.AminoAcidModificationID) where pam.PeptideID=%s'%pid
            for row in self.conn.execute(sql):
                scanObj.addModification(peptide[row[1]], str(row[1]), str(row[2]), row[0])
        else:
            mods = self.mods.get(int(pid))
            if mods is not None:
                for modId, modPosition in zip(mods[0].split(','),mods[1].split(',')):
                    modEntry = self.modTable[str(modId)]
                    scanObj.addModification(peptide[int(modPosition)], modPosition, modEntry[1], modEntry[0])
            tmods = self.tmods.get(int(pid))
            if tmods is not None:
                for modIds in tmods:
                    for modId in modIds.split(','):
                        modEntry = self.modTable[str(modId)]
                        scanObj.addModification('[', 0, modEntry[1], modEntry[0])
        scanObj.peptide = peptide
        if self.decompressScanInfo(scanObj, i[0]):
            return scanObj
        return None
1031608
def proc_atom(self):
        """"""
        atom_data = self.proc_line_coordinate(self.current_line)
        (at_type, at_ser, at_name, alt_loc, res_name, chain_id, res_seq,
         i_code, x, y, z, occupancy, temp_factor, element, charge) = atom_data
        # currently active state
        a_state = self.pdb_parse_tree['data'][self.state]
        res_id = (res_seq, i_code)
        if chain_id not in a_state:
            a_state[chain_id] = (set(), OrderedDict())
        if res_id not in a_state[chain_id][1]:
            a_state[chain_id][1][res_id] = (set(), OrderedDict())
        if at_type == 'ATOM':
            if res_name in standard_amino_acids.values():
                poly = 'P'
            else:
                poly = 'N'
        else:
            poly = 'H'
        a_state[chain_id][0].add((chain_id, at_type, poly))
        a_state[chain_id][1][res_id][0].add(
            (at_type, res_seq, res_name, i_code))
        if at_ser not in a_state[chain_id][1][res_id][1]:
            a_state[chain_id][1][res_id][1][at_ser] = [atom_data]
        else:
            a_state[chain_id][1][res_id][1][at_ser].append(atom_data)
        return
361888
def _parse(self):
        
        
        atom_counter = 0
        
        structure_build = self.structure_builder
        
        residues = self._extract_residues()
        
        cur_model = None
        cur_chain = None
        structure_build.init_seg(' ') # There is never a SEGID present
        
        for r in residues:

            # New model?
            if cur_model != r['model']:
                cur_model = r['model']
                try:
                    structure_build.init_model(cur_model)
                except PDBConstructionException, message:
                    self._handle_builder_exception(message, r) 
            
            # New chain?
            if cur_chain != r['chain']:
                cur_chain = r['chain']
                try:
                    structure_build.init_chain(cur_chain)
                except PDBConstructionException, message:
                    self._handle_builder_exception(message, r)

            # Create residue
            if r['name'] in AA_LIST: # Get residue type crudely since there is no HETATM / ATOM
                hetero_flag = ' '
            elif r['name'] == 'WAT' or r['name'] == 'HOH':
                hetero_flag = 'W'
            else:
                hetero_flag = 'H'
            
            # Some terminal atoms are added at residue 0. This residue has a small number of atoms.
            # Protonated non-terminal glycine has 7 atoms. Any of these residues is smaller.
            # HETATMs have only a couple of atoms (3 for water for example) and they are ok.
            
            if (len(r['atoms']) >= 7) or (hetero_flag != " "):
                try:
                    structure_build.init_residue(r['name'], hetero_flag, r['number'], r['icode'])
                except PDBConstructionException, message:
                    self._handle_builder_exception(message, r)
            
                # Create Atoms
                for atom in r['atoms']:
                    a = self._parse_atom(atom)

                    if not sum(a['coord']): # e.g. HG of metal bound CYS coords are 0,0,0.
                        continue
                        
                    try:
                        atom_counter += 1
                        # fullname = name; altloc is empty;
                        structure_build.init_atom(a['name'], a['coord'], a['bfactor'], a['occupancy'], ' ',
                                                    a['name'], atom_counter, a['element'],  hetero_flag)
                    except PDBConstructionException, message:
                        self._handle_builder_exception(message, r)      
            
            elif len(r['atoms']) < 7: # Terminal Residues
                                
                for atom in r['atoms']:
                    
                    a = self._parse_atom(atom)
                    
                    if not sum(a['coord']): # e.g. HG of metal bound CYS coords are 0,0,0.
                        continue

                    atom_counter += 1                    
                    ter_atom = Atom(a['name'], a['coord'], a['bfactor'], a['occupancy'], ' ',
                                    a['name'], atom_counter, a['element'], hetero_flag)
                    
                    if a['name'] in N_TERMINAL_ATOMS:
                        
                        inc_struct = self.structure_builder.get_structure()
                        for model in inc_struct:
                            for chain in model:
                                if chain.id == r['chain']:
                                    for residue in chain: # Find First residue matching name
                                        if residue.resname == r['name']:
                                            residue.add(ter_atom)
                                            break
                    
                    elif a['name'] in C_TERMINAL_ATOMS:

                        inc_struct = self.structure_builder.get_structure()
                        c_ter = None
                        for model in inc_struct:
                            for chain in model:
                                if chain.id == r['chain']:
                                    for residue in chain: # Find Last residue matching name
                                        if residue.resname == r['name']:
                                            c_ter = residue
                                    if c_ter:
                                        c_ter.add(ter_atom)
588111
def build_tree_from_alignment(aln, moltype=DNA, best_tree=False, params=None):
    
    if params is None:
        params = {}

    if moltype == DNA or moltype == RNA:
        params['-nt'] = True
    elif moltype == PROTEIN:
        params['-nt'] = False
    else:
        raise ValueError, \
                "FastTree does not support moltype: %s" % moltype.label

    app = FastTree(params=params)

    if best_tree:
        raise NotImplementedError, "best_tree not implemented yet"
    result = app(aln.toFasta())
    tree = DndParser(result['Tree'].read(), constructor=PhyloNode)
    return tree
104982
def parse(cls, msg):
        """"""
        lines = msg.splitlines()
        method, uri, version = lines[0].split()
        headers = cls.parse_headers('\r\n'.join(lines[1:]))
        return cls(version=version, uri=uri, method=method, headers=headers)
1168968
def parser_from_buffer(cls, fp):
        """"""
        yaml = YAML(typ="safe")
        return cls(yaml.load(fp))
709233
def _assemble_activeform(stmt):
    """"""
    subj_str = _assemble_agent_str(stmt.agent)
    if stmt.is_active:
        is_active_str = 'active'
    else:
        is_active_str = 'inactive'
    if stmt.activity == 'activity':
        stmt_str = subj_str + ' is ' + is_active_str
    elif stmt.activity == 'kinase':
        stmt_str = subj_str + ' is kinase-' + is_active_str
    elif stmt.activity == 'phosphatase':
        stmt_str = subj_str + ' is phosphatase-' + is_active_str
    elif stmt.activity == 'catalytic':
        stmt_str = subj_str + ' is catalytically ' + is_active_str
    elif stmt.activity == 'transcription':
        stmt_str = subj_str + ' is transcriptionally ' + is_active_str
    elif stmt.activity == 'gtpbound':
        stmt_str = subj_str + ' is GTP-bound ' + is_active_str
    return _make_sentence(stmt_str)
73938
def build_proteintable(pqdb, headerfields, mergecutoff, isobaric=False,
                       precursor=False, probability=False, fdr=False,
                       pep=False, genecentric=False):
    """"""
    pdmap = create_featuredata_map(pqdb, genecentric=genecentric,
                                   psm_fill_fun=pinfo.add_psms_to_proteindata,
                                   pgene_fill_fun=pinfo.add_protgene_to_protdata,
                                   count_fun=pinfo.count_peps_psms,
                                   get_uniques=True)
    empty_return = lambda x, y, z: {}
    iso_fun = {True: get_isobaric_quant, False: empty_return}[isobaric]
    ms1_fun = {True: get_precursor_quant, False: empty_return}[precursor]
    prob_fun = {True: get_prot_probability,
                False: empty_return}[probability]
    fdr_fun = {True: get_prot_fdr,
               False: empty_return}[fdr]
    pep_fun = {True: get_prot_pep,
               False: empty_return}[pep]
    pdata_fun = {True: get_protein_data_genecentric,
                 False: get_protein_data}[genecentric is not False]
    protein_sql, sqlfieldmap = pqdb.prepare_mergetable_sql(precursor, isobaric,
                                                           probability, fdr,
                                                           pep)
    accession_field = prottabledata.ACCESSIONS[genecentric]
    proteins = pqdb.get_merged_features(protein_sql)
    protein = next(proteins)
    outprotein = {accession_field: protein[sqlfieldmap['p_acc']]}
    check_prot = {k: v for k, v in outprotein.items()}
    if not mergecutoff or protein_pool_fdr_cutoff(protein, sqlfieldmap,
                                                  mergecutoff):
        fill_mergefeature(outprotein, iso_fun, ms1_fun, prob_fun, fdr_fun,
                          pep_fun, pdata_fun, protein, sqlfieldmap,
                          headerfields, pdmap, accession_field)
    for protein in proteins:
        if mergecutoff and not protein_pool_fdr_cutoff(protein, sqlfieldmap,
                                                       mergecutoff):
            continue
        p_acc = protein[sqlfieldmap['p_acc']]
        if p_acc != outprotein[accession_field]:
            # check if protein has been filled, otherwise do not output
            # sometimes proteins have NA in all fields
            if outprotein != check_prot:
                yield outprotein
            outprotein = {accession_field: p_acc}
            check_prot = {k: v for k, v in outprotein.items()}
        fill_mergefeature(outprotein, iso_fun, ms1_fun, prob_fun, fdr_fun,
                          pep_fun, pdata_fun, protein, sqlfieldmap,
                          headerfields, pdmap, accession_field)
    if outprotein != check_prot:
        yield outprotein
203091
def parse_fragment_name(lexer: Lexer) -> NameNode:
    """"""
    if lexer.token.value == "on":
        raise unexpected(lexer)
    return parse_name(lexer)
1168766
def translateDNA(sequence, frame = 'f1', translTable_id='default') :
	""""""

	protein = ""

	if frame == 'f1' :
		dna = sequence
	elif frame == 'f2':
		dna = sequence[1:]
	elif frame == 'f3' :
		dna = sequence[2:]
	elif frame == 'r1' :
		dna = reverseComplement(sequence)
	elif frame == 'r2' :
		dna = reverseComplement(sequence)
		dna = dna[1:]
	elif frame == 'r3' :
		dna = reverseComplement(sequence)
		dna = dna[2:]
	else :
		raise ValueError('unknown reading frame: %s, should be one of the following: fwd1, fwd2, fwd3, rev1, rev2, rev3' % frame)

	for i in range(0, len(dna),  3) :
		codon = dna[i:i+3]

		# Check if variant messed with selenocysteine codon
		if '!' in codon and codon != '!GA':
			codon = codon.replace('!', 'T')

		if (len(codon) == 3) :
			try :
				# MC
				protein += translTable[translTable_id][codon]
			except KeyError :
				combinaisons = polymorphicCodonCombinaisons(list(codon))
				translations = set()
				for ci in range(len(combinaisons)):
					translations.add(translTable[translTable_id][combinaisons[ci]])
				protein += '/'.join(translations)

	return protein
220173
def build_parser():
    """"""

    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    # Required args
    parser.add_argument("--in_path", "-i", required=True,
                        help="file path to input GCT(x) file")

    parser.add_argument("--rid", nargs="+", help="filepath to grp file or string array for including rows")
    parser.add_argument("--cid", nargs="+", help="filepath to grp file or string array for including cols")
    parser.add_argument("--exclude_rid", "-er", nargs="+", help="filepath to grp file or string array for excluding rows")
    parser.add_argument("--exclude_cid", "-ec", nargs="+", help="filepath to grp file or string array for excluding cols")
    parser.add_argument("--out_name", "-o", default="ds_subsetted.gct",
                        help="what to name the output file")
    parser.add_argument("--out_type", default="gct", choices=["gct", "gctx"],
                        help="whether to write output as GCT or GCTx")
    parser.add_argument("--verbose", "-v", action="store_true", default=False,
                        help="whether to increase the # of messages reported")

    return parser
1330502
def parse(self, limit=None):
        
        if limit is not None:
            LOG.info("Only parsing first %d rows", limit)

        protein_paths = self._get_file_paths(self.tax_ids, 'protein_links')
        col = ['NCBI taxid', 'entrez', 'STRING']
        for taxon in protein_paths:
            ensembl = Ensembl(self.graph_type, self.are_bnodes_skized)
            string_file_path = '/'.join((
                self.rawdir, protein_paths[taxon]['file']))

            with gzip.open(string_file_path, 'rb') as reader:
                dataframe = pd.read_csv(reader, sep=r'\s+')
            p2gene_map = dict()

            if taxon in self.id_map_files:
                LOG.info("Using string provided id_map files")
                map_file = '/'.join((self.rawdir, self.id_map_files[taxon]['file']))

                with gzip.open(map_file, 'rt') as reader:
                    line = next(reader).strip()
                    if line != '# NCBI taxid / entrez / STRING':
                        LOG.error(
                            'Expected Headers:\t%s\nRecived Headers:\t%s\n', col, line)
                        exit(-1)

                    for line in reader.readlines():
                        row = line.rstrip('\n').split('\t')
                        # tax = row[col.index(''NCBI taxid')].strip()
                        gene = row[col.index('entrez')].strip()
                        prot = row[col.index('STRING')].strip()

                        genes = gene.split('|')
                        p2gene_map[prot.replace(taxon + '.', '')] = [
                            "NCBIGene:" + entrez_id for entrez_id in genes]
            else:
                LOG.info("Fetching ensembl proteins for taxon %s", taxon)
                p2gene_map = ensembl.fetch_protein_gene_map(taxon)
                for key in p2gene_map:
                    for phen, gene in enumerate(p2gene_map[key]):
                        p2gene_map[key][phen] = "ENSEMBL:{}".format(gene)

            LOG.info(
                "Finished fetching ENSP ID mappings, fetched %i proteins",
                len(p2gene_map))

            LOG.info(
                "Fetching protein protein interactions for taxon %s", taxon)

            self._process_protein_links(dataframe, p2gene_map, taxon, limit)
130930
def argument_parser(version=None):
    """"""
    parser = argparse.ArgumentParser()
    parser.add_argument('group',
                        default=NgdConfig.get_default('group'),
                        help='The NCBI taxonomic group to download (default: %(default)s). '
                        'A comma-separated list of taxonomic groups is also possible. For example: "bacteria,viral"'
                        'Choose from: {choices}'.format(choices=NgdConfig.get_choices('group')))
    parser.add_argument('-s', '--section', dest='section',
                        choices=NgdConfig.get_choices('section'),
                        default=NgdConfig.get_default('section'),
                        help='NCBI section to download (default: %(default)s)')
    parser.add_argument('-F', '--format', dest='file_format',
                        default=NgdConfig.get_default('file_format'),
                        help='Which format to download (default: %(default)s).'
                        'A comma-separated list of formats is also possible. For example: "fasta,assembly-report". '
                        'Choose from: {choices}'.format(choices=NgdConfig.get_choices('file_format')))
    parser.add_argument('-l', '--assembly-level', dest='assembly_level',
                        default=NgdConfig.get_default('assembly_level'),
                        help='Assembly level of genomes to download (default: %(default)s). '
                        'A comma-separated list of assembly levels is also possible. For example: "complete,chromosome". '
                        'Coose from: {choices}'.format(choices=NgdConfig.get_choices('assembly_level')))
    parser.add_argument('-g', '--genus', dest='genus',
                        default=NgdConfig.get_default('genus'),
                        help='Only download sequences of the provided genus. '
                        'A comma-seperated list of genera is also possible. For example: '
                        '"Streptomyces coelicolor,Escherichia coli". (default: %(default)s)')
    parser.add_argument('-T', '--species-taxid', dest='species_taxid',
                        default=NgdConfig.get_default('species_taxid'),
                        help='Only download sequences of the provided species NCBI taxonomy ID. '
                             'A comma-separated list of species taxids is also possible. For example: "52342,12325". '
                             '(default: %(default)s)')
    parser.add_argument('-t', '--taxid', dest='taxid',
                        default=NgdConfig.get_default('taxid'),
                        help='Only download sequences of the provided NCBI taxonomy ID. '
                             'A comma-separated list of taxids is also possible. For example: "9606,9685". '
                             '(default: %(default)s)')
    parser.add_argument('-A', '--assembly-accessions', dest='assembly_accessions',
                        default=NgdConfig.get_default('assembly_accessions'),
                        help='Only download sequences matching the provided NCBI assembly accession(s). '
                        'A comma-separated list of accessions is possible, as well as a path to a filename '
                        'containing one accession per line.')
    parser.add_argument('-R', '--refseq-category', dest='refseq_category',
                        choices=NgdConfig.get_choices('refseq_category'),
                        default=NgdConfig.get_default('refseq_category'),
                        help='Only download sequences of the provided refseq category (default: %(default)s)')
    parser.add_argument('-o', '--output-folder', dest='output',
                        default=NgdConfig.get_default('output'),
                        help='Create output hierarchy in specified folder (default: %(default)s)')
    parser.add_argument('-H', '--human-readable', dest='human_readable', action='store_true',
                        help='Create links in human-readable hierarchy (might fail on Windows)')
    parser.add_argument('-u', '--uri', dest='uri',
                        default=NgdConfig.get_default('uri'),
                        help='NCBI base URI to use (default: %(default)s)')
    parser.add_argument('-p', '--parallel', dest='parallel', type=int, metavar="N",
                        default=NgdConfig.get_default('parallel'),
                        help='Run %(metavar)s downloads in parallel (default: %(default)s)')
    parser.add_argument('-r', '--retries', dest='retries', type=int, metavar="N",
                        default=0,
                        help='Retry download %(metavar)s times when connection to NCBI fails ('
                             'default: %(default)s)')
    parser.add_argument('-m', '--metadata-table', type=str,
                        help='Save tab-delimited file with genome metadata')
    parser.add_argument('-n', '--dry-run', dest='dry_run', action='store_true',
                        help="Only check which files to download, don't download genome files.")
    parser.add_argument('-N', '--no-cache', dest='use_cache', action='store_false',
                        help="Don't cache the assembly summary file in %s." % CACHE_DIR)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='increase output verbosity')
    parser.add_argument('-d', '--debug', action='store_true',
                        help='print debugging information')
    parser.add_argument('-V', '--version', action='version', version=version,
                        help='print version information')
    parser.add_argument('-M', '--type-material', dest='type_material',
                        default=NgdConfig.get_default('type_material'),
                        help='Specifies the relation to type material for the assembly (default: %(default)s). '
                        '"any" will include assemblies with no relation to type material value defined, "all" will download only assemblies with a defined value. '
                        'A comma-separated list of relatons. For example: "reference,synonym".  '
                        'Choose from: {choices} .  '.format(choices=NgdConfig.get_choices('type_material')))

    return parser
136325
def _splitGenoGeneWindow(self,annotation_file=None,cis=1e4,funct='protein_coding',minSnps=1.,maxSnps=SP.inf):
        
         #1. load annotation
        assert annotation_file is not None, 'Splitter:: specify annotation file'
        try:
            f = h5py.File(annotation_file,'r')
            geneID = f['geneID'][:]
            gene_chrom = f['chrom'][:]
            gene_start = f['start'][:]
            gene_end = f['end'][:]
            gene_strand = f['strand'][:]
            gene_function = f['function'][:]
            f.close()
        except:
            print('Splitter:: format annotation file not valid')

        # if funct is not None, it has to be a list
        if funct is not None and funct!=list:   funct=[funct]

        windows = []
        nSnps   = []
        Igene   = []
        #2. calculates windows 
        for gene_i in range(geneID.shape[0]):
            if funct is not None:
                if gene_function[gene_i] not in funct:
                    Igene.append(False)
                    continue
            wnd = [gene_chrom[gene_i],gene_start[gene_i]-cis,gene_end[gene_i]+cis]
            Ir = (self.chrom==wnd[0])*(self.pos>=wnd[1])*(self.pos<=wnd[2])
            _nSnps = Ir.sum()
            if _nSnps>=minSnps and _nSnps<=maxSnps:
                windows.append(wnd)
                nSnps.append(_nSnps)
                Igene.append(True)
            else:
                Igene.append(False)
        Igene = SP.array(Igene)
        self.info['nSnps'] = SP.array(nSnps)
        self.info['geneID']        = geneID[Igene]
        self.info['gene_start']    = gene_start[Igene]
        self.info['gene_end']      = gene_end[Igene]
        self.info['gene_strand']   = gene_strand[Igene]
        self.info['gene_function'] = gene_function[Igene]
        return SP.array(windows)