def _write_feature_qualifier(self, key, value=None, quote=None): if value is None: # Value-less entry like /pseudo self.handle.write("%s/%s\n" % (self.QUALIFIER_INDENT_STR, key)) return # Quick hack with no line wrapping, may be useful for testing: # self.handle.write('%s/%s="%s"\n' % (self.QUALIFIER_INDENT_STR, key, value)) if quote is None: # Try to mimic unwritten rules about when quotes can be left out: if _is_int_or_long(value) or key in self.FTQUAL_NO_QUOTE: quote = False else: quote = True if quote: line = '%s/%s="%s"' % (self.QUALIFIER_INDENT_STR, key, value) else: line = '%s/%s=%s' % (self.QUALIFIER_INDENT_STR, key, value) if len(line) <= self.MAX_WIDTH: self.handle.write(line + "\n") return while line.lstrip(): if len(line) <= self.MAX_WIDTH: self.handle.write(line + "\n") return # Insert line break... for index in range(min(len(line) - 1, self.MAX_WIDTH), self.QUALIFIER_INDENT + 1, -1): if line[index] == " ": break if line[index] != " ": # No nice place to break... index = self.MAX_WIDTH assert index <= self.MAX_WIDTH self.handle.write(line[:index] + "\n") line = self.QUALIFIER_INDENT_STR + line[index:].lstrip()
def add_entry(self, entry): """Add an Entry element to the pathway.""" # We insist that the node ID is an integer assert _is_int_or_long(entry.id), \ "Node ID must be an integer, got %s (%s)" % (type(entry.id), entry.id) entry._pathway = self # Let the entry know about the pathway self.entries[entry.id] = entry
def remove_reaction(self, reaction): """Remove a Reaction element from the pathway.""" assert _is_int_or_long(reaction.id), \ "Node ID must be an integer, got %s (%s)" % (type(reaction.id), reaction.id) # We need to remove the reaction from any other elements that may # contain it, which means removing those elements # TODO del self._reactions[reaction.id]
def remove_entry(self, entry): """Remove an Entry element from the pathway.""" assert _is_int_or_long(entry.id), \ "Node ID must be an integer, got %s (%s)" % (type(entry.id), entry.id) # We need to remove the entry from any other elements that may # contain it, which means removing those elements # TODO del self.entries[entry.id]
def add_reaction(self, reaction): """Add a Reaction element to the pathway.""" # We insist that the node ID is an integer and corresponds to an entry assert _is_int_or_long(reaction.id), \ "Node ID must be an integer, got %s (%s)" % (type(reaction.id), reaction.id) assert reaction.id in self.entries, \ "Reaction ID %d has no corresponding entry" % reaction.id reaction._pathway = self # Let the reaction know about the pathway self._reactions[reaction.id] = reaction
def _get_taxon_id_from_ncbi_lineage(self, taxonomic_lineage): """This is recursive! (PRIVATE). taxonomic_lineage - list of taxonomy dictionaries from Bio.Entrez First dictionary in list is the taxonomy root, highest would be the species. Each dictionary includes: - TaxID (string, NCBI taxon id) - Rank (string, e.g. "species", "genus", ..., "phylum", ...) - ScientificName (string) (and that is all at the time of writing) This method will record all the lineage given, returning the taxon id (database key, not NCBI taxon id) of the final entry (the species). """ ncbi_taxon_id = taxonomic_lineage[-1]["TaxId"] # Is this in the database already? Check the taxon table... taxon_id = self.adaptor.execute_and_fetch_col0( "SELECT taxon_id FROM taxon" " WHERE ncbi_taxon_id=%s" % ncbi_taxon_id) if taxon_id: # we could verify that the Scientific Name etc in the database # is the same and update it or print a warning if not... if isinstance(taxon_id, list): assert len(taxon_id) == 1 return taxon_id[0] else: return taxon_id # We have to record this. if len(taxonomic_lineage) > 1: # Use recursion to find out the taxon id (database key) of the # parent. parent_taxon_id = self._get_taxon_id_from_ncbi_lineage( taxonomic_lineage[:-1]) assert _is_int_or_long(parent_taxon_id), repr(parent_taxon_id) else: parent_taxon_id = None # INSERT new taxon rank = taxonomic_lineage[-1].get("Rank", None) self.adaptor.execute( "INSERT INTO taxon(ncbi_taxon_id, parent_taxon_id, node_rank)" " VALUES (%s, %s, %s)", (ncbi_taxon_id, parent_taxon_id, rank)) taxon_id = self.adaptor.last_id("taxon") assert isinstance(taxon_id, (int, long)), repr(taxon_id) # ... and its name in taxon_name scientific_name = taxonomic_lineage[-1].get("ScientificName", None) if scientific_name: self.adaptor.execute( "INSERT INTO taxon_name(taxon_id, name, name_class)" " VALUES (%s, %s, 'scientific name')", (taxon_id, scientific_name[:255])) return taxon_id
def add_reaction(self, reaction): """Add a Reaction element to the pathway.""" # We insist that the node ID is an integer and corresponds to an entry if not _is_int_or_long(reaction.id): raise ValueError("Node ID must be an integer, got %s (%s)" % (type(reaction.id), reaction.id)) if reaction.id not in self.entries: raise ValueError("Reaction ID %d has no corresponding entry" % reaction.id) reaction._pathway = self # Let the reaction know about the pathway self._reactions[reaction.id] = reaction
def __getitem__(self, time): """Return a subset of signals or a single signal.""" if isinstance(time, slice): # Fix the missing values in the slice if time.start is None: start = 0 else: start = time.start if time.stop is None: stop = max(self.get_times()) else: stop = time.stop time = np.arange(start, stop, time.step) return list(self._interpolate(time)) elif _is_int_or_long(time) or isinstance(time, float): return self._interpolate(time) raise ValueError('Invalid index')
def _get_taxon_id_from_ncbi_lineage(self, taxonomic_lineage): """This is recursive! (PRIVATE). taxonomic_lineage - list of taxonomy dictionaries from Bio.Entrez First dictionary in list is the taxonomy root, highest would be the species. Each dictionary includes: - TaxID (string, NCBI taxon id) - Rank (string, e.g. "species", "genus", ..., "phylum", ...) - ScientificName (string) (and that is all at the time of writing) This method will record all the lineage given, returning the taxon id (database key, not NCBI taxon id) of the final entry (the species). """ ncbi_taxon_id = int(taxonomic_lineage[-1]["TaxId"]) left_value = None right_value = None parent_left_value = None parent_right_value = None # Is this in the database already? Check the taxon table... rows = self.adaptor.execute_and_fetchall( "SELECT taxon_id, left_value, right_value FROM taxon" " WHERE ncbi_taxon_id=%s" % ncbi_taxon_id) if rows: # we could verify that the Scientific Name etc in the database # is the same and update it or print a warning if not... assert len(rows) == 1 return rows[0] # We have to record this. if len(taxonomic_lineage) > 1: # Use recursion to find out the taxon id (database key) of the # parent. parent_taxon_id, parent_left_value, parent_right_value = self._get_taxon_id_from_ncbi_lineage( taxonomic_lineage[:-1]) left_value = parent_right_value right_value = parent_right_value + 1 assert _is_int_or_long(parent_taxon_id), repr(parent_taxon_id) else: # we have reached the top of the lineage but no current taxonomy # id has been found parent_taxon_id = None left_value = self.adaptor.execute_one( "SELECT MAX(left_value) FROM taxon")[0] if not left_value: left_value = 0 right_value = left_value + 1 self._update_left_right_taxon_values(left_value) # INSERT new taxon rank = str(taxonomic_lineage[-1].get("Rank")) self.adaptor.execute( "INSERT INTO taxon(ncbi_taxon_id, parent_taxon_id, node_rank, left_value, right_value)" " VALUES (%s, %s, %s, %s, %s)", (ncbi_taxon_id, parent_taxon_id, rank, left_value, right_value)) taxon_id = self.adaptor.last_id("taxon") # assert isinstance(taxon_id, int), repr(taxon_id) # ... and its name in taxon_name scientific_name = taxonomic_lineage[-1].get("ScientificName") if scientific_name: self.adaptor.execute( "INSERT INTO taxon_name(taxon_id, name, name_class)" " VALUES (%s, %s, 'scientific name')", (taxon_id, scientific_name[:255])) return taxon_id, left_value, right_value
def _is_numeric(x): return _py3k._is_int_or_long(x) or isinstance(x, (float, complex))
def _is_numeric(x): """Return True if is numeric.""" return _py3k._is_int_or_long(x) or isinstance(x, (float, complex))
def _get_taxon_id_from_ncbi_lineage(self, taxonomic_lineage): """This is recursive! (PRIVATE). taxonomic_lineage - list of taxonomy dictionaries from Bio.Entrez First dictionary in list is the taxonomy root, highest would be the species. Each dictionary includes: - TaxID (string, NCBI taxon id) - Rank (string, e.g. "species", "genus", ..., "phylum", ...) - ScientificName (string) (and that is all at the time of writing) This method will record all the lineage given, returning the taxon id (database key, not NCBI taxon id) of the final entry (the species). """ ncbi_taxon_id = int(taxonomic_lineage[-1]["TaxId"]) left_value = None right_value = None parent_left_value = None parent_right_value = None # Is this in the database already? Check the taxon table... rows = self.adaptor.execute_and_fetchall( "SELECT taxon_id, left_value, right_value FROM taxon" " WHERE ncbi_taxon_id=%s" % ncbi_taxon_id ) if rows: # we could verify that the Scientific Name etc in the database # is the same and update it or print a warning if not... assert len(rows) == 1 return rows[0] # We have to record this. if len(taxonomic_lineage) > 1: # Use recursion to find out the taxon id (database key) of the # parent. parent_taxon_id, parent_left_value, parent_right_value = self._get_taxon_id_from_ncbi_lineage( taxonomic_lineage[:-1] ) left_value = parent_right_value right_value = parent_right_value + 1 assert _is_int_or_long(parent_taxon_id), repr(parent_taxon_id) else: # we have reached the top of the lineage but no current taxonomy # id has been found parent_taxon_id = None left_value = self.adaptor.execute_one("SELECT MAX(left_value) FROM taxon")[0] if not left_value: left_value = 0 right_value = left_value + 1 self._update_left_right_taxon_values(left_value) # INSERT new taxon rank = str(taxonomic_lineage[-1].get("Rank")) self.adaptor.execute( "INSERT INTO taxon(ncbi_taxon_id, parent_taxon_id, node_rank, left_value, right_value)" " VALUES (%s, %s, %s, %s, %s)", (ncbi_taxon_id, parent_taxon_id, rank, left_value, right_value), ) taxon_id = self.adaptor.last_id("taxon") # assert isinstance(taxon_id, int), repr(taxon_id) # ... and its name in taxon_name scientific_name = taxonomic_lineage[-1].get("ScientificName") if scientific_name: self.adaptor.execute( "INSERT INTO taxon_name(taxon_id, name, name_class)" " VALUES (%s, %s, 'scientific name')", (taxon_id, scientific_name[:255]), ) return taxon_id, left_value, right_value