SciCodePile/SciCode-Domain-Code
DATA1: Domain-Specific Code Dataset Dataset Overview DATA1 is a large-scale domain-specific code dataset focusing on code samples from interdisciplinary fields such as biology, chemistry, materials science, and related areas. The dataset is collected and organized from GitHub repositories, covering 178 different domain topics with over 1.1 billion lines of code. Dataset Statistics Total Datasets: 178 CSV files Total Data Size: ~115 GB Total Lines… See the full description on the dataset page: https://huggingface.co/datasets/SciCodePile/SciCode-Domain-Code.
42.4k
1"keyword","repo_name","file_path","file_extension","file_size","line_count","content","language"
2"Metabolic","SBRG/cobradb","setup.py",".py","1322","44","# -*- coding: utf-8 -*-3 4from os.path import abspath, dirname5from sys import path6from setuptools import setup, find_packages7 8# To temporarily modify sys.path9SETUP_DIR = abspath(dirname(__file__))10 11setup(12 name='cobradb',13 version='0.3.0',14 description=""""""COBRAdb loads genome-scale metabolic models and genome15 annotations into a relational database."""""",16 url='https://github.com/SBRG/cobradb',17 author='Zachary King',18 author_email='zaking@ucsd.edu',19 license='MIT',20 classifiers=[21 'License :: OSI Approved :: MIT License',22 'Programming Language :: Python :: 3',23 'Programming Language :: Python :: 3.7',24 ],25 keywords='systems biology, genome-scale model',26 packages=find_packages(),27 install_requires=[28 'SQLAlchemy>=1.3.10,<2',29 # Be careful upgrading cobra for use with BiGG; ancient models (e.g.30 # iND750.xml) cause errors with cobra versions >=0.1531 'cobra>=0.14.2,<0.15',32 'python-libsbml>=5.18.0,<6',33 'numpy>=1.17.2,<2',34 'psycopg2>=2.8.3,<3',35 'biopython>=1.74,<2',36 'scipy>=1.3.1,<2',37 'lxml>=4.4.1,<5',38 'pytest>=4.6.6,<5',39 'six>=1.12.0,<2',40 'tornado>=4.5.3,<5',41 'escher>=1.7.3,<2',42 'configparser>=4.0.2,<5',43 ],44)45","Python"
46"Metabolic","SBRG/cobradb","cobradb/models.py",".py","15438","476","# -*- coding: utf-8 -*-47 48""""""Module to implement ORM to the ome database""""""49 50from cobradb.settings import db_connection_string51 52from sqlalchemy import (ForeignKey, Column, Integer, String, Float, Table,53 LargeBinary, Boolean, create_engine, MetaData, Enum,54 DateTime, UniqueConstraint)55from sqlalchemy.orm import sessionmaker, Session as _SA_Session56from sqlalchemy.ext.declarative import declarative_base57 58 59# Connect to postgres60engine = create_engine(db_connection_string)61Base = declarative_base(bind=engine)62metadata = MetaData(bind=engine)63Session = sessionmaker(bind=engine, class_=_SA_Session)64 65 66# Make the enums67_enum_l = [68 Enum('component', 'reaction', 'gene', 'compartmentalized_component',69 name='synonym_type'),70 Enum('pmid', 'doi',71 name='reference_type'),72 Enum('model_reaction', 'model_compartmentalized_component', 'model_gene',73 name='old_id_synonym_type'),74 Enum('is_version', name='is_version'),75 Enum('component', 'reaction', name='deprecated_id_types'),76 Enum('model_compartmentalized_component', 'model_reaction',77 name='escher_map_matrix_type')78]79custom_enums = { x.name: x for x in _enum_l }80 81#------------82# Exceptions83#------------84 85class NotFoundError(Exception):86 pass87 88class AlreadyLoadedError(Exception):89 pass90 91#--------92# Tables93#--------94 95class DatabaseVersion(Base):96 __tablename__ = 'database_version'97 98 is_version = Column(custom_enums['is_version'], primary_key=True)99 date_time = Column(DateTime, nullable=False)100 101 __table_args__ = (102 UniqueConstraint('is_version'),103 )104 105 def __init__(self, date_time):106 self.is_version = 'is_version'107 self.date_time = date_time108 109 110class Genome(Base):111 __tablename__ = 'genome'112 113 id = Column(Integer, primary_key=True)114 accession_type = Column(String(200), nullable=False)115 accession_value = Column(String(200), nullable=False)116 organism = Column(String(200), nullable=True)117 taxon_id = Column(String(200), nullable=True)118 ncbi_assembly_id = Column(String(200), nullable=True)119 120 __table_args__ = (121 UniqueConstraint('accession_type', 'accession_value'),122 )123 124 def __repr__(self):125 return ('<cobradb Genome(id={self.id}, accession_type={self.accession_type}, '126 'accession_value={self.accession_value})>'.format(self=self))127 128 129class Chromosome(Base):130 __tablename__ = 'chromosome'131 132 id = Column(Integer, primary_key=True)133 ncbi_accession = Column(String(200))134 genome_id = Column(Integer, ForeignKey('genome.id'))135 136 __table_args__ = (137 UniqueConstraint('ncbi_accession', 'genome_id'),138 )139 140 def __repr__(self):141 return ('<cobradb Chromosome(id={self.id}, ncbi_accession={self.ncbi_accession}, genome_id={self.genome_id})>'142 .format(self=self))143 144 145class GenomeRegion(Base):146 __tablename__ = 'genome_region'147 id = Column(Integer, primary_key=True)148 chromosome_id = Column(Integer, ForeignKey('chromosome.id'))149 bigg_id = Column(String, nullable=False)150 leftpos = Column(Integer, nullable=True)151 rightpos = Column(Integer, nullable=True)152 strand = Column(String(1), nullable=True)153 type = Column(String(20))154 dna_sequence = Column(String, nullable=True)155 protein_sequence = Column(String, nullable=True)156 157 __table_args__ = (158 UniqueConstraint('bigg_id', 'chromosome_id'),159 )160 161 __mapper_args__ = {162 'polymorphic_identity': 'genome_region',163 'polymorphic_on': type164 }165 166 def __repr__(self):167 return ('<cobradb GenomeRegion(id={self.id}, leftpos={self.leftpos}, rightpos={self.rightpos})>'168 .format(self=self))169 170 171class Component(Base):172 __tablename__ = 'component'173 174 id = Column(Integer, primary_key=True)175 bigg_id = Column(String)176 name = Column(String, nullable=True)177 type = Column(String(20))178 179 __table_args__ = (UniqueConstraint('bigg_id'), {})180 181 __mapper_args__ = {182 'polymorphic_identity': 'component',183 'polymorphic_on': type184 }185 186 def __repr__(self):187 return ""Component (#%d): %s"" % \188 (self.id, self.name)189 190 191class Reaction(Base):192 __tablename__ = 'reaction'193 194 id = Column(Integer, primary_key=True)195 type = Column(String(20))196 bigg_id = Column(String, nullable=False)197 name = Column(String, nullable=True)198 reaction_hash = Column(String, nullable=False)199 pseudoreaction = Column(Boolean, default=False)200 201 __table_args__ = (202 UniqueConstraint('bigg_id'),203 )204 205 __mapper_args__ = {206 'polymorphic_identity': 'reaction',207 'polymorphic_on': type208 }209 210 def __repr__(self):211 return ('<cobradb Reaction(id=%d, bigg_id=%s%s)>' %212 (self.id, self.bigg_id, ', pseudoreaction' if self.pseudoreaction else ''))213 214 215class DataSource(Base):216 __tablename__ = 'data_source'217 218 id = Column(Integer, primary_key=True)219 bigg_id = Column(String, nullable=False)220 name = Column(String(100))221 url_prefix = Column(String)222 223 __table_args__ = (224 UniqueConstraint('bigg_id'),225 )226 227 def __repr__(self):228 return (229 '<cobradb DataSource(id={self.id}, bigg_id={self.bigg_id}, '230 'name={self.name}, url_prefix={self.url_prefix})>'231 ).format(self=self)232 233 234class Synonym(Base):235 __tablename__ = 'synonym'236 id = Column(Integer, primary_key=True)237 ome_id = Column(Integer)238 synonym = Column(String)239 type = Column(custom_enums['synonym_type'])240 data_source_id = Column(Integer, ForeignKey('data_source.id', ondelete='CASCADE'))241 242 __table_args__ = (243 UniqueConstraint('ome_id', 'synonym', 'type', 'data_source_id'),244 )245 246 def __repr__(self):247 return ('<cobradb Synonym(id=%d, synonym=""%s"", type=""%s"", ome_id=%d, data_source_id=%d)>' %248 (self.id, self.synonym, self.type, self.ome_id, self.data_source_id))249 250 251class Publication(Base):252 __tablename__ = ""publication""253 id = Column(Integer, primary_key=True)254 reference_type = Column(custom_enums['reference_type'])255 reference_id = Column(String)256 257 __table_args__=(258 UniqueConstraint('reference_type', 'reference_id'),259 )260 261 262class PublicationModel(Base):263 __tablename__ = ""publication_model""264 model_id = Column(Integer,265 ForeignKey('model.id', ondelete='CASCADE'),266 primary_key=True)267 publication_id = Column(Integer,268 ForeignKey('publication.id', ondelete='CASCADE'),269 primary_key=True)270 271 __table_args__ = (272 UniqueConstraint('model_id', 'publication_id'),273 )274 275 276class OldIDSynonym(Base):277 __tablename__ = ""old_id_model_synonym""278 id = Column(Integer, primary_key=True)279 type = Column(custom_enums['old_id_synonym_type'])280 synonym_id = Column(Integer,281 ForeignKey('synonym.id', ondelete='CASCADE'),282 nullable=False)283 ome_id = Column(Integer, nullable=False)284 285 __table_args__ = (286 UniqueConstraint('synonym_id', 'ome_id'),287 )288 289 def __repr__(self):290 return ('<cobradb OldIDSynonym(id=%d, type=""%s"", ome_id=%d, synonym_id=%d)>' %291 (self.id, self.type, self.ome_id, self.synonym_id))292 293 294class GenomeRegionMap(Base):295 __tablename__ = 'genome_region_map'296 297 genome_region_id_1 = Column(Integer, ForeignKey('genome_region.id'), primary_key=True)298 genome_region_id_2 = Column(Integer, ForeignKey('genome_region.id'), primary_key=True)299 distance = Column(Integer)300 301 __table_args__ = (302 UniqueConstraint('genome_region_id_1','genome_region_id_2'),303 )304 305 def __repr__(self):306 return ""GenomeRegionMap (%d <--> %d) distance:%d"" % (self.genome_region_id_1, self.genome_region_id_2, self.distance)307 308 309class DeprecatedID(Base):310 __tablename__ = 'deprecated_id'311 312 id = Column(Integer, primary_key=True)313 type = Column(custom_enums['deprecated_id_types'])314 deprecated_id = Column(String)315 ome_id = Column(Integer)316 317 __table_args__ = (318 UniqueConstraint('type', 'deprecated_id', 'ome_id'),319 )320 321 def __repr__(self):322 return ('<cobradb DeprecatedID(type=""%s"", deprecated_id=""%s"", ome_id=%d)>' %323 (self.type, self.deprecated_id, self.ome_id))324 325 326class Model(Base):327 __tablename__='model'328 329 id = Column(Integer, primary_key=True)330 bigg_id = Column(String, nullable=False)331 genome_id = Column(Integer, ForeignKey('genome.id', onupdate='CASCADE', ondelete=""CASCADE""))332 organism = Column(String(200), nullable=True)333 published_filename = Column(String, nullable=True)334 335 __table_args__ = (336 UniqueConstraint('bigg_id', 'genome_id'),337 )338 339 def __repr__(self):340 return '<cobradb Model(id={self.id}, bigg_id={self.bigg_id})>'.format(self=self)341 342 343class ModelGene(Base):344 __tablename__='model_gene'345 346 id = Column(Integer, primary_key=True)347 model_id = Column(Integer,348 ForeignKey('model.id', onupdate=""CASCADE"", ondelete=""CASCADE""),349 nullable=False)350 gene_id = Column(Integer,351 ForeignKey('gene.id', onupdate=""CASCADE"", ondelete=""CASCADE""),352 nullable=False)353 354 __table_args__ = (355 UniqueConstraint('model_id', 'gene_id'),356 )357 358 359class ModelReaction(Base):360 __tablename__='model_reaction'361 362 id = Column(Integer, primary_key=True)363 reaction_id = Column(Integer,364 ForeignKey('reaction.id', onupdate=""CASCADE"", ondelete=""CASCADE""),365 nullable=False)366 model_id = Column(Integer,367 ForeignKey('model.id', onupdate=""CASCADE"", ondelete=""CASCADE""),368 nullable=False)369 copy_number = Column(Integer, nullable=False)370 371 objective_coefficient = Column(Float, nullable=False)372 lower_bound = Column(Float, nullable=False)373 upper_bound = Column(Float, nullable=False)374 gene_reaction_rule = Column(String, nullable=False)375 original_gene_reaction_rule = Column(String, nullable=True)376 subsystem = Column(String, nullable=True)377 378 __table_args__ = (379 UniqueConstraint('reaction_id', 'model_id', 'copy_number'),380 )381 382 def __repr__(self):383 return ('<cobradb ModelReaction(id={self.id}, reaction_id={self.reaction_id}, model_id={self.model_id}, copy_number={self.copy_number})>'384 .format(self=self))385 386 387class GeneReactionMatrix(Base):388 __tablename__ = 'gene_reaction_matrix'389 390 id = Column(Integer, primary_key=True)391 model_gene_id = Column(Integer,392 ForeignKey('model_gene.id', onupdate=""CASCADE"", ondelete=""CASCADE""),393 nullable=False)394 model_reaction_id = Column(Integer,395 ForeignKey('model_reaction.id', onupdate=""CASCADE"", ondelete=""CASCADE""),396 nullable=False)397 398 __table_args__ = (399 UniqueConstraint('model_gene_id', 'model_reaction_id'),400 )401 402 def __repr__(self):403 return ('<cobradb GeneReactionMatrix(id={self.id}, model_gene_id={self.model_gene_id}, model_reaction_id={self.model_reaction_id})>'404 .format(self=self))405 406 407class CompartmentalizedComponent(Base):408 __tablename__='compartmentalized_component'409 id = Column(Integer, primary_key=True)410 component_id = Column(Integer,411 ForeignKey('component.id', onupdate=""CASCADE"", ondelete=""CASCADE""),412 nullable=False)413 compartment_id = Column(Integer,414 ForeignKey('compartment.id', onupdate=""CASCADE"", ondelete=""CASCADE""),415 nullable=False)416 417 __table_args__ = (418 UniqueConstraint('compartment_id', 'component_id'),419 )420 421 422class ModelCompartmentalizedComponent(Base):423 __tablename__='model_compartmentalized_component'424 id = Column(Integer, primary_key=True)425 model_id = Column(Integer,426 ForeignKey('model.id', onupdate=""CASCADE"", ondelete=""CASCADE""),427 nullable=False)428 compartmentalized_component_id = Column(Integer,429 ForeignKey('compartmentalized_component.id'),430 nullable=False)431 formula = Column(String, nullable=True)432 charge = Column(Integer, nullable=True)433 434 __table_args__ = (435 UniqueConstraint('compartmentalized_component_id', 'model_id'),436 )437 438 439class Compartment(Base):440 __tablename__ = 'compartment'441 id = Column(Integer, primary_key=True)442 bigg_id = Column(String, unique = True)443 name = Column(String)444 445 def __repr__(self):446 return ('<cobradb Compartment(id={self.id}, bigg_id={self.bigg_id})>'447 .format(self=self))448 449 450class ReactionMatrix(Base):451 __tablename__ = 'reaction_matrix'452 id = Column(Integer, primary_key=True)453 reaction_id = Column(Integer, ForeignKey('reaction.id'), nullable=False)454 compartmentalized_component_id = Column(Integer,455 ForeignKey('compartmentalized_component.id',456 onupdate=""CASCADE"", ondelete=""CASCADE""),457 nullable=False)458 stoichiometry = Column(Float)459 460 __table_args__ = (461 UniqueConstraint('reaction_id', 'compartmentalized_component_id'),462 )463 464 465class EscherMap(Base):466 __tablename__ = 'escher_map'467 id = Column(Integer, primary_key=True)468 map_name = Column(String, nullable=False)469 map_data = Column(LargeBinary, nullable=False)470 model_id = Column(Integer, ForeignKey(Model.id), nullable=False)471 priority = Column(Integer, nullable=False)472 473 __table_args__ = (474 UniqueConstraint('map_name'),475 )476 477 478class EscherMapMatrix(Base):479 __tablename__ = 'escher_map_matrix'480 id = Column(Integer, primary_key=True)481 ome_id = Column(Integer, nullable=False)482 type = Column(custom_enums['escher_map_matrix_type'], nullable=False)483 escher_map_id = Column(Integer, ForeignKey(EscherMap.id), nullable=False)484 # the reaction id or node id485 escher_map_element_id = Column(String(50))486 487 __table_args__ = (488 UniqueConstraint('ome_id', 'type', 'escher_map_id'),489 )490 491 492class ModelCount(Base):493 __tablename__='model_count'494 id = Column(Integer, primary_key=True)495 model_id = Column(Integer,496 ForeignKey('model.id', onupdate=""CASCADE"", ondelete=""CASCADE""),497 nullable=False)498 reaction_count = Column(Integer)499 gene_count = Column(Integer)500 metabolite_count = Column(Integer)501 502 503class Gene(GenomeRegion):504 __tablename__ = 'gene'505 506 id = Column(Integer,507 ForeignKey('genome_region.id', onupdate=""CASCADE"", ondelete=""CASCADE""),508 primary_key=True)509 name = Column(String, nullable=True)510 locus_tag = Column(String, nullable=True)511 mapped_to_genbank = Column(Boolean, nullable=False)512 alternative_transcript_of = Column(Integer,513 ForeignKey('gene.id'),514 nullable=True)515 516 __mapper_args__ = {'polymorphic_identity': 'gene'}517 518 def __repr__(self):519 return '<cobradb Gene(id=%d, bigg_id=%s, name=%s)>' % (self.id, self.bigg_id,520 self.name)521","Python"
522"Metabolic","SBRG/cobradb","cobradb/settings.py",".py","2814","79","# -*- coding: utf-8 -*-523 524""""""Retrive local user settings""""""525 526from configparser import ConfigParser, NoOptionError527import os528from os.path import join, split, abspath, isfile, expanduser, dirname529from sys import modules530import six531 532self = modules[__name__]533 534# define various filepaths535 536config = ConfigParser()537 538# overwrite defaults settings with settings from the file539filepath = abspath(join(dirname(__file__), '..', 'settings.ini'))540if isfile(filepath):541 config.read(filepath)542else:543 raise Exception('No settings files at path: %s' % filepath)544 545# prefer environment variables for database settings546env_names = {547 'postgres_host': 'COBRADB_POSTGRES_HOST',548 'postgres_port': 'COBRADB_POSTGRES_PORT',549 'postgres_user': 'COBRADB_POSTGRES_USER',550 'postgres_password': 'COBRADB_POSTGRES_PASSWORD',551 'postgres_database': 'COBRADB_POSTGRES_DATABASE',552 'postgres_test_database': 'COBRADB_POSTGRES_TEST_DATABASE',553}554for setting_name, env_name in six.iteritems(env_names):555 if env_name in os.environ:556 print('Setting %s with environment variable %s' % (setting_name,557 env_name))558 setattr(self, setting_name, os.environ[env_name])559 else:560 setattr(self, setting_name, config.get('DATABASE', setting_name))561 562# set up the database connection string563self.db_connection_string = ('postgresql://%s:%s@%s:%s/%s' %564 (self.postgres_user, self.postgres_password,565 self.postgres_host, self.postgres_port,566 self.postgres_database))567 568# get the java executable (optional, for running Model Polisher)569if config.has_option('EXECUTABLES', 'java'):570 self.java = config.get('EXECUTABLES', 'java')571else:572 print('No Java executable provided.')573 574if not config.has_section('DATA'):575 raise Exception('DATA section was not found in settings.ini')576 577# these are required578try:579 self.model_directory = expanduser(config.get('DATA', 'model_directory'))580except NoOptionError:581 raise Exception('model_directory was not supplied in settings.ini')582 583try:584 self.refseq_directory = expanduser(config.get('DATA', 'refseq_directory'))585except NoOptionError:586 raise Exception('refseq_directory was not supplied in settings.ini')587try:588 self.model_genome = expanduser(config.get('DATA', 'model_genome'))589except NoOptionError:590 raise Exception('model_genome path was not supplied in settings.ini')591 592# these are optional593for data_pref in ['compartment_names', 'reaction_hash_prefs',594 'gene_reaction_rule_prefs', 'data_source_preferences',595 'metabolite_duplicates']:596 try:597 setattr(self, data_pref, expanduser(config.get('DATA', data_pref)))598 except NoOptionError:599 setattr(self, data_pref, None)600","Python"
601"Metabolic","SBRG/cobradb","cobradb/parse.py",".py","19644","561","# -*- coding: utf-8 -*-602 603from cobradb.models import NotFoundError604from cobradb.util import scrub_gene_id, load_tsv, increment_id605from cobradb import settings606 607import re608import cobra609import cobra.io610from os.path import join611import hashlib612import logging613from collections import defaultdict614import six615 616 617def _hash_fn(s):618 to_hash = s if isinstance(s, six.binary_type) else s.encode('utf8')619 # python 2: md5(bytes).hexdigest() => Py2 bytes str620 # python 3: md5(bytes).hexdigest() => Py3 unicode str621 return hashlib.md5(to_hash).hexdigest()622 623 624def hash_metabolite_dictionary(met_dict, string_only):625 """"""Generate a unique hash for the metabolites and coefficients of the626 reaction. Returns the native str type for Python 2 or 3.627 628 met_dict: A dictionary where keys are metabolite IDs and values and629 coefficients.630 631 string_only: If True, return the string that would be hashed.632 633 """"""634 sorted_mets = sorted([(m, v) for m, v in six.iteritems(met_dict)],635 key=lambda x: x[0])636 sorted_mets_str = ''.join(['%s%.3f' % t for t in sorted_mets])637 if string_only:638 return sorted_mets_str639 else:640 return _hash_fn(sorted_mets_str)641 642 643def hash_reaction(reaction, metabolite_dict, string_only=False, reverse=False):644 """"""Generate a unique hash for the metabolites and coefficients of the645 reaction.646 647 reaction: A COBRA Reaction.648 649 metabolite_dict: Dictionary to look up new metabolite ids.650 651 string_only: If True, return the string that would be hashed.652 653 """"""654 the_dict = {metabolite_dict[m.id]: (-v if reverse else v)655 for m, v in six.iteritems(reaction.metabolites)}656 return hash_metabolite_dictionary(the_dict, string_only)657 658 659def load_and_normalize(model_filepath):660 """"""Load a model, and give it a particular id style""""""661 662 # load the model663 if model_filepath.endswith('.xml'):664 model = cobra.io.read_sbml_model(model_filepath)665 elif model_filepath.endswith('.mat'):666 model = cobra.io.load_matlab_model(model_filepath)667 elif model_filepath.endswith('.json'):668 model = cobra.io.load_json_model(model_filepath)669 else:670 raise Exception('The %s file is not a valid filetype', model_filepath)671 # convert the ids672 model, old_ids = convert_ids(model)673 674 # extract metabolite formulas from names (e.g. for iAF1260)675 model = get_formulas_from_names(model)676 677 return model, old_ids678 679 680def _get_rule_prefs():681 """"""Get gene_reaction_rule prefs.""""""682 return load_tsv(settings.gene_reaction_rule_prefs, required_column_num=2)683 684 685def _check_rule_prefs(rule_prefs, rule):686 """"""Check the gene_reaction_rule against the prefs file, and return an existing687 rule or the fixed one.""""""688 for row in rule_prefs:689 old_rule, new_rule = row690 if old_rule == rule:691 return new_rule692 return rule693 694 695def remove_boundary_metabolites(model):696 """"""Remove boundary metabolites (end in _b and only present in exchanges). Be697 sure to loop through a static list of ids so the list does not get shorter698 as the metabolites are deleted.699 700 """"""701 for metabolite_id in [str(x) for x in model.metabolites]:702 metabolite = model.metabolites.get_by_id(metabolite_id)703 if not metabolite.id.endswith(""_b""):704 continue705 for reaction in list(metabolite._reaction):706 if reaction.id.startswith(""EX_""):707 metabolite.remove_from_model()708 break709 model.metabolites._generate_index()710 711#-----------------712# Pseudoreactions713#-----------------714 715class ConflictingPseudoreaction(Exception):716 pass717 718 719def _has_gene_reaction_rule(reaction):720 """"""Check if the reaction has a gene reaction rule.""""""721 rule = getattr(reaction, 'gene_reaction_rule', None)722 return rule is not None and rule.strip() != ''723 724 725def _reaction_single_met_coeff(reaction):726 if len(reaction.metabolites) == 1:727 return next(six.iteritems(reaction.metabolites))728 return None729 730 731def _reverse_reaction(reaction):732 """"""Reverse the metabolite coefficients and the upper & lower bounds.""""""733 reaction.add_metabolites({k: -v for k, v in six.iteritems(reaction.metabolites)},734 combine=False)735 reaction.upper_bound, reaction.lower_bound = -reaction.lower_bound, -reaction.upper_bound736 737 738def _fix_exchange(reaction):739 """"""Returns new id if the reaction was treated as an exchange.""""""740 # does it look like an exchange?741 met_coeff = _reaction_single_met_coeff(reaction)742 if met_coeff is None:743 return None744 met, coeff = met_coeff745 if split_compartment(remove_duplicate_tag(met.id))[1] != 'e':746 return None747 # check id748 if not re.search(r'^ex_', reaction.id, re.IGNORECASE):749 logging.warning('Reaction {r.id} looks like an exchange but it does not start with EX_. Renaming'750 .format(r=reaction))751 # check coefficient752 if abs(coeff) != 1:753 raise ConflictingPseudoreaction('Reaction {} looks like an exchange '754 'but it has a reactant with coefficient {}'755 .format(reaction.id, coeff))756 # reverse if necessary757 if coeff == 1:758 _reverse_reaction(reaction)759 logging.debug('Reversing pseudoreaction %s' % reaction.id)760 return 'EX_%s' % met.id, 'Extracellular exchange'761 762 763# for sink & demand functions764_sink_regex = re.compile(r'^(sink|sk)_', re.IGNORECASE)765 766 767def _fix_demand(reaction):768 """"""Returns new ID if the reaction was treated as a demand.""""""769 # does it look like a demand?770 met_coeff = _reaction_single_met_coeff(reaction)771 if met_coeff is None:772 return None773 met, coeff = met_coeff774 if split_compartment(met.id)[1] == 'e':775 return None776 # source bound should be 0777 if ((coeff > 0 and reaction.upper_bound != 0) or778 (coeff < 0 and reaction.lower_bound != 0)):779 return None780 # if it could be a demand, but it is named sink_ or SK_, then let it be a781 # sink (by returning None) because sink is really a superset of demand782 if _sink_regex.search(reaction.id):783 return None784 # check id785 if not re.search(r'^dm_', reaction.id, re.IGNORECASE):786 logging.warning('Reaction {r.id} looks like a demand but it does not start with DM_. Renaming.'787 .format(r=reaction))788 # check coefficient789 if abs(coeff) != 1:790 raise ConflictingPseudoreaction('Reaction {} looks like a demand '791 'but it has a reactant with coefficient {}'792 .format(reaction.id, coeff))793 # reverse if necessary794 if coeff == 1:795 _reverse_reaction(reaction)796 logging.debug('Reversing pseudoreaction %s' % reaction.id)797 return 'DM_%s' % met.id, 'Intracellular demand'798 799 800def _fix_sink(reaction):801 """"""Returns new ID if the reaction was treated as a sink.""""""802 # does it look like a sink?803 met_coeff = _reaction_single_met_coeff(reaction)804 if met_coeff is None:805 return None806 met, coeff = met_coeff807 if split_compartment(met.id)[1] == 'e':808 return None809 # check id810 if not _sink_regex.search(reaction.id):811 logging.warning('Reaction {r.id} looks like a sink but it does not start with sink_ or SK_. Renaming.'812 .format(r=reaction))813 # check coefficient814 if abs(coeff) != 1:815 raise ConflictingPseudoreaction('Reaction {} looks like a sink '816 'but it has a reactant with coefficient {}'817 .format(reaction.id, coeff))818 # reverse if necessary819 if coeff == 1:820 _reverse_reaction(reaction)821 logging.debug('Reversing pseudoreaction %s' % reaction.id)822 return 'SK_%s' % met.id, 'Intracellular source/sink'823 824 825def _fix_biomass(reaction):826 """"""Returns new ID if the reaction was treated as a biomass.""""""827 # does it look like an exchange?828 regex = re.compile(r'biomass', re.IGNORECASE)829 if not regex.search(reaction.id):830 return None831 new_id = ('BIOMASS_%s' % regex.sub('', reaction.id)).replace('__', '_')832 return new_id, 'Biomass and maintenance functions'833 834 835def _fix_atpm(reaction):836 """"""Returns new ID if the reaction was treated as a biomass.""""""837 # does it look like a atpm?838 mets = {k.id: v for k, v in six.iteritems(reaction.metabolites)}839 subsystem = 'Biomass and maintenance functions'840 if mets == {'atp_c': -1, 'h2o_c': -1, 'pi_c': 1, 'h_c': 1, 'adp_c': 1}:841 return 'ATPM', subsystem842 elif mets == {'atp_c': 1, 'h2o_c': 1, 'pi_c': -1, 'h_c': -1, 'adp_c': -1}:843 _reverse_reaction(reaction)844 logging.debug('Reversing pseudoreaction %s' % reaction.id)845 return 'ATPM', subsystem846 return None847 848 849def _normalize_pseudoreaction(new_style_id, reaction):850 """"""If the reaction is a pseudoreaction (exchange, demand, sink, biomass, or851 ATPM), then apply standard rules to it.""""""852 853 pseudo_id = None; subsystem = None854 855 # check atpm separately because there is a good reason for an atpm-like856 # reaction with a gene_reaction_rule857 is_atpm = False858 res = _fix_atpm(reaction)859 if res is not None:860 pseudo_id, subsystem = res861 reaction.subsystem = subsystem862 is_atpm = True863 864 # check for other pseudoreactions865 fns = [_fix_exchange, _fix_demand, _fix_sink, _fix_biomass]866 res = None867 for fn in fns:868 if res is not None:869 break870 res = fn(reaction)871 if res is not None:872 pseudo_id, subsystem = res873 reaction.subsystem = subsystem874 875 if pseudo_id is not None:876 # does it have a gene_reaction_rule? OK if atpm reaction has877 # gene_reaction_rule.878 if _has_gene_reaction_rule(reaction):879 if is_atpm:880 return881 raise ConflictingPseudoreaction('Reaction {r.id} looks like a pseudoreaction '882 'but it has a gene_reaction_rule: '883 '{r.gene_reaction_rule}'.format(r=reaction))884 885 return pseudo_id886 887 888#----------889# ID fixes890#----------891 892def remove_duplicate_tag(the_id):893 return re.sub(r'\$\$DROP.*', '', the_id)894 895def add_duplicate_tag(the_id):896 return '%s$$DROP' % the_id897 898 899def convert_ids(model):900 """"""Converts metabolite and reaction ids to the new style.901 902 Returns a tuple with the new model and a dictionary of old ids set up like this:903 904 {'reactions': {'new_id': 'old_id'},905 'metabolites': {'new_id': 'old_id'},906 'genes': {'new_id': 'old_id'}}907 908 """"""909 # loop through the ids:910 metabolite_id_dict = defaultdict(list)911 reaction_id_dict = defaultdict(list)912 gene_id_dict = defaultdict(list)913 914 # fix metabolites915 for metabolite in model.metabolites:916 new_id = id_for_new_id_style(fix_legacy_id(metabolite.id, use_hyphens=False),917 is_metabolite=True)918 metabolite_id_dict[new_id].append(metabolite.id)919 if new_id != metabolite.id:920 # new_id already exists, then merge921 if new_id in model.metabolites:922 new_id = add_duplicate_tag(new_id)923 while new_id in model.metabolites:924 new_id = increment_id(new_id)925 metabolite.id = new_id926 model.metabolites._generate_index()927 928 # take out the _b metabolites929 remove_boundary_metabolites(model)930 931 # load fixes for gene_reaction_rule's932 rule_prefs = _get_rule_prefs()933 934 # separate ids and compartments, and convert to the new_id_style935 for reaction in model.reactions:936 # apply new id style937 new_style_id = id_for_new_id_style(fix_legacy_id(reaction.id, use_hyphens=False))938 939 # normalize pseudoreaction IDs940 try:941 pseudo_id = _normalize_pseudoreaction(new_style_id, reaction)942 except ConflictingPseudoreaction as e:943 logging.warning(str(e))944 # keep going despite the warning945 pass946 947 new_id = pseudo_id if pseudo_id is not None else new_style_id948 949 # don't merge reactions with conflicting new_id's950 if new_id != reaction.id and new_id in model.reactions:951 new_id = add_duplicate_tag(new_id)952 while new_id in model.reactions:953 new_id = increment_id(new_id)954 955 reaction_id_dict[new_id].append(reaction.id)956 reaction.id = new_id957 958 # fix the gene reaction rules959 reaction.gene_reaction_rule = _check_rule_prefs(rule_prefs, reaction.gene_reaction_rule)960 961 model.reactions._generate_index()962 963 # update the genes964 for gene in list(model.genes):965 new_id = scrub_gene_id(gene.id)966 gene_id_dict[new_id].append(gene.id)967 for reaction in gene.reactions:968 reaction.gene_reaction_rule = re.sub(r'\b' + re.escape(gene.id) + r'\b', new_id,969 reaction.gene_reaction_rule)970 971 # remove old genes972 from cobra.manipulation import remove_genes973 remove_genes(model, [gene for gene in model.genes974 if len(gene.reactions) == 0])975 976 # fix the model id977 bigg_id = re.sub(r'[^a-zA-Z0-9_]', '_', model.id)978 model.id = bigg_id979 980 old_ids = {'metabolites': metabolite_id_dict,981 'reactions': reaction_id_dict,982 'genes': gene_id_dict}983 984 return model, old_ids985 986 987# the regex to separate the base id, the chirality ('_L') and the compartment ('_c')988reg_compartment = re.compile(r'(.*?)[_\(\[]([a-z][a-z0-9]?)[_\)\]]?$')989reg_chirality = re.compile(r'(.*?)_?_([LDSRM])$')990def id_for_new_id_style(old_id, is_metabolite=False):991 """""" Get the new style id""""""992 new_id = old_id993 994 def _join_parts(the_id, the_compartment):995 if the_compartment:996 the_id = the_id + '_' + the_compartment997 return the_id998 999 def _remove_d_underscore(s):1000 """"""Removed repeated, leading, and trailing underscores.""""""1001 s = re.sub(r'_+', '_', s)1002 s = re.sub(r'^_+', '', s)1003 s = re.sub(r'_+$', '', s)1004 return s1005 1006 # remove parentheses and brackets, for SBML & BiGG spec compatibility1007 new_id = re.sub(r'[^a-zA-Z0-9_]', '_', new_id)1008 1009 compartment_match = reg_compartment.match(new_id)1010 if compartment_match is None:1011 # still remove double underscores1012 new_id = _remove_d_underscore(new_id)1013 else:1014 base, compartment = compartment_match.groups()1015 chirality_match = reg_chirality.match(base)1016 if chirality_match is None:1017 new_id = _join_parts(_remove_d_underscore(base), compartment)1018 else:1019 new_base = '%s__%s' % (_remove_d_underscore(chirality_match.group(1)),1020 chirality_match.group(2))1021 new_id = _join_parts(new_base, compartment)1022 1023 return new_id1024 1025 1026def get_formulas_from_names(model):1027 reg = re.compile(r'.*_([A-Z][A-Z0-9]*)$')1028 # support cobra 0.3 and 0.41029 for metabolite in model.metabolites:1030 if (metabolite.formula is not None and str(metabolite.formula) != '' and getattr(metabolite, 'formula', None) is not None):1031 continue1032 name = getattr(metabolite, 'name', None)1033 if name:1034 m = reg.match(name)1035 if m:1036 metabolite.formula = m.group(1)1037 return model1038 1039 1040def invalid_formula(formula):1041 return formula is not None and re.search(r'[^A-Za-z0-9]', formula)1042 1043#-------------1044# Model setup1045#-------------1046 1047def setup_model(model, substrate_reactions, aerobic=True, sur=10, max_our=10,1048 id_style='cobrapy', fix_iJO1366=False):1049 """"""Set up the model with environmntal parameters.1050 1051 model: a cobra model1052 substrate_reactions: A single reaction id, list of reaction ids, or dictionary with reaction1053 ids as keys and max substrate uptakes as keys. If a list or single id is1054 given, then each substrate will be limited to /sur/1055 aerobic: True or False1056 sur: substrate uptake rate. Ignored if substrate_reactions is a dictionary.1057 max_our: Max oxygen uptake rate.1058 id_style: 'cobrapy' or 'simpheny'.1059 1060 """"""1061 if id_style=='cobrapy': o2 = 'EX_o2_e'1062 elif id_style=='simpheny': o2 = 'EX_o2(e)'1063 else: raise Exception('Invalid id_style')1064 1065 if isinstance(substrate_reactions, dict):1066 for r, v in six.iteritems(substrate_reactions):1067 model.reactions.get_by_id(r).lower_bound = -abs(v)1068 elif isinstance(substrate_reactions, list):1069 for r in substrate_reactions:1070 model.reactions.get_by_id(r).lower_bound = -abs(sur)1071 elif isinstance(substrate_reactions, str):1072 model.reactions.get_by_id(substrate_reactions).lower_bound = -abs(sur)1073 else: raise Exception('bad substrate_reactions argument')1074 1075 if aerobic:1076 model.reactions.get_by_id(o2).lower_bound = -abs(max_our)1077 else:1078 model.reactions.get_by_id(o2).lower_bound = 01079 1080 # model specific setup1081 if str(model)=='iJO1366' and aerobic==False:1082 for r in ['CAT', 'SPODM', 'SPODMpp']:1083 model.reactions.get_by_id(r).lower_bound = 01084 model.reactions.get_by_id(r).upper_bound = 01085 if fix_iJO1366 and str(model)=='iJO1366':1086 for r in ['ACACT2r']:1087 model.reactions.get_by_id(r).upper_bound = 01088 print('made ACACT2r irreversible')1089 1090 # TODO hydrogen reaction for ijo1091 1092 if str(model)=='iMM904' and aerobic==False:1093 necessary_ex = ['EX_ergst(e)', 'EX_zymst(e)', 'EX_hdcea(e)',1094 'EX_ocdca(e)', 'EX_ocdcea(e)', 'EX_ocdcya(e)']1095 for r in necessary_ex:1096 rxn = model.reactions.get_by_id(r)1097 rxn.lower_bound = -10001098 rxn.upper_bound = 10001099 1100 return model1101 1102def turn_on_subsystem(model, subsystem):1103 raise NotImplementedError()1104 for reaction in model.reactions:1105 if reaction.subsystem.strip('_') == subsystem.strip('_'):1106 reaction.lower_bound = -1000 if reaction.reversibility else 01107 reaction.upper_bound = 10001108 return model1109 1110def carbons_for_exchange_reaction(reaction):1111 if len(reaction._metabolites) > 1:1112 raise Exception('%s not an exchange reaction' % str(reaction))1113 1114 metabolite = next(reaction._metabolites.iterkeys())1115 try:1116 return metabolite.formula.elements['C']1117 except KeyError:1118 return 01119 # match = re.match(r'C([0-9]+)', str(metabolite.formula))1120 # try:1121 # return int(match.group(1))1122 # except AttributeError:1123 # return 01124 1125def fix_legacy_id(id, use_hyphens=False):1126 id = id.replace('_DASH_', '__')1127 id = id.replace('_FSLASH_', '/')1128 id = id.replace('_BSLASH_', ""\\"")1129 id = id.replace('_LPAREN_', '(')1130 id = id.replace('_LSQBKT_', '[')1131 id = id.replace('_RSQBKT_', ']')1132 id = id.replace('_RPAREN_', ')')1133 id = id.replace('_COMMA_', ',')1134 id = id.replace('_PERIOD_', '.')1135 id = id.replace('_APOS_', ""'"")1136 id = id.replace('&', '&')1137 id = id.replace('<', '<')1138 id = id.replace('>', '>')1139 id = id.replace('"', '""')1140 if use_hyphens:1141 id = id.replace('__', '-')1142 else:1143 id = id.replace(""-"", ""__"")1144 return id1145 1146def split_compartment(component_id):1147 """"""Split the metabolite bigg_id into a metabolite and a compartment id.1148 1149 Arguments1150 ---------1151 1152 component_id: the bigg_id of the metabolite.1153 1154 """"""1155 match = re.search(r'_[a-z][a-z0-9]?$', component_id)1156 if match is None:1157 raise NotFoundError(""No compartment found for %s"" % component_id)1158 met = component_id[0:match.start()]1159 compartment = component_id[match.start()+1:]1160 return met, compartment1161","Python"
1162"Metabolic","SBRG/cobradb","cobradb/component_loading.py",".py","11307","317","# -*- coding: utf-8 -*-1163 1164from cobradb.models import *1165from cobradb import settings1166from cobradb.util import (scrub_gene_id, get_or_create_data_source,1167 get_or_create, timing)1168 1169import sys, os, math, re1170from os.path import basename1171from warnings import warn1172from sqlalchemy import text, or_, and_, func1173import logging1174import six1175import itertools as it1176 1177 1178class BadGenomeError(Exception):1179 pass1180 1181 1182def _load_gb_file(genbank_file_handle):1183 """"""Load the Genbank file.1184 1185 Arguments1186 ---------1187 1188 genbank_file_handle: The handle to the genbank file.1189 1190 """"""1191 # imports1192 from Bio import SeqIO1193 1194 # load the genbank file1195 logging.debug('Loading file: %s' % genbank_file_handle.name)1196 try:1197 gb_file = SeqIO.read(genbank_file_handle, 'gb')1198 except IOError:1199 raise BadGenomeError(""File '%s' not found"" % genbank_file_handle.name)1200 except Exception as e: