Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created September 2, 2018 22:00
Show Gist options
  • Save Midnighter/7a50faa976d34cfbd59397c3152bc97e to your computer and use it in GitHub Desktop.
Save Midnighter/7a50faa976d34cfbd59397c3152bc97e to your computer and use it in GitHub Desktop.
Perform a single gene deletion study using COBRApy and record the flux distributions.
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
cobra = "*"
python-libsbml = "*"
optlang = "*"
pandas = "*"
tqdm = "*"
[dev-packages]
{
"_meta": {
"hash": {
"sha256": "198c2466447616b9f325ee547e789de0e6c8ad89622722ad752cb82609309110"
},
"pipfile-spec": 6,
"requires": {},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"cobra": {
"hashes": [
"sha256:17d0fbdafadabacd9ddc728dc6494839b84f01db3f319d9cfcd1a6486199de17",
"sha256:90f1d498543f615b53357269a2ae5fd6aa27daaf3de8cdcc6fbdac4ec48ad58c"
],
"index": "pypi",
"version": "==0.13.4"
},
"depinfo": {
"hashes": [
"sha256:1798f577a61249b04d672cec8d97143542ca24b524bf8abd2a22bc7d0598f678",
"sha256:33eb3f43d82e198bb5c6164472a108a66dc969aca930ccbab4480f469af3def8"
],
"markers": "python_version != '3.0.*' and python_version != '3.2.*' and python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.3.*'",
"version": "==1.4.0"
},
"future": {
"hashes": [
"sha256:e39ced1ab767b5936646cedba8bcce582398233d6a627067d4c6a454c90cfedb"
],
"version": "==0.16.0"
},
"mpmath": {
"hashes": [
"sha256:04d14803b6875fe6d69e6dccea87d5ae5599802e4b1df7997bddd2024001050c"
],
"version": "==1.0.0"
},
"numpy": {
"hashes": [
"sha256:1c362ad12dd09a43b348bb28dd2295dd9cdf77f41f0f45965e04ba97f525b864",
"sha256:2156a06bd407918df4ac0122df6497a9c137432118f585e5b17d543e593d1587",
"sha256:24e4149c38489b51fc774b1e1faa9103e82f73344d7a00ba66f6845ab4769f3f",
"sha256:340ec1697d9bb3a9c464028af7a54245298502e91178bddb4c37626d36e197b7",
"sha256:35db8d419345caa4eeaa65cd63f34a15208acd87530a30f0bc25fc84f55c8c80",
"sha256:361370e9b7f5e44c41eee29f2bb5cb3b755abb4b038bce6d6cbe08db7ff9cb74",
"sha256:36e8dcd1813ca92ce7e4299120cee6c03adad33d89b54862c1b1a100443ac399",
"sha256:378378973546ecc1dfaf9e24c160d683dd04df871ecd2dcc86ce658ca20f92c0",
"sha256:419e6faee16097124ee627ed31572c7e80a1070efa25260b78097cca240e219a",
"sha256:4287104c24e6a09b9b418761a1e7b1bbde65105f110690ca46a23600a3c606b8",
"sha256:549f3e9778b148a47f4fb4682955ed88057eb627c9fe5467f33507c536deda9d",
"sha256:5e359e9c531075220785603e5966eef20ccae9b3b6b8a06fdfb66c084361ce92",
"sha256:5ee7f3dbbdba0da75dec7e94bd7a2b10fe57a83e1b38e678200a6ad8e7b14fdc",
"sha256:62d55e96ec7b117d3d5e618c15efcf769e70a6effaee5842857b64fb4883887a",
"sha256:719b6789acb2bc86ea9b33a701d7c43dc2fc56d95107fd3c5b0a8230164d4dfb",
"sha256:7a70f2b60d48828cba94a54a8776b61a9c2657a803d47f5785f8062e3a9c7c55",
"sha256:7b9e37f194f8bcdca8e9e6af92e2cbad79e360542effc2dd6b98d63955d8d8a3",
"sha256:83b8fc18261b70f45bece2d392537c93dc81eb6c539a16c9ac994c47fc79f09a",
"sha256:9473ad28375710ab18378e72b59422399b27e957e9339c413bf00793b4b12df0",
"sha256:95b085b253080e5d09f7826f5e27dce067bae813a132023a77b739614a29de6e",
"sha256:98b86c62c08c2e5dc98a9c856d4a95329d11b1c6058cb9b5191d5ea6891acd09",
"sha256:a3bd01d6d3ed3d7c06d7f9979ba5d68281f15383fafd53b81aa44b9191047cf8",
"sha256:c81a6afc1d2531a9ada50b58f8c36197f8418ef3d0611d4c1d7af93fdcda764f",
"sha256:ce75ed495a746e3e78cfa22a77096b3bff2eda995616cb7a542047f233091268",
"sha256:dae8618c0bcbfcf6cf91350f8abcdd84158323711566a8c5892b5c7f832af76f",
"sha256:df0b02c6705c5d1c25cc35c7b5d6b6f9b3b30833f9d178843397ae55ecc2eebb",
"sha256:e3660744cda0d94b90141cdd0db9308b958a372cfeee8d7188fdf5ad9108ea82",
"sha256:f2362d0ca3e16c37782c1054d7972b8ad2729169567e3f0f4e5dd3cdf85f188e"
],
"markers": "python_version != '3.0.*' and python_version != '3.2.*' and python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.3.*'",
"version": "==1.15.1"
},
"optlang": {
"hashes": [
"sha256:2ceb3bf9b6cbc697c640920c599fdfdf0e45398c7aef2fbbf531ea54f7425579",
"sha256:a56cd33516aef427ccc4a914fe003fe83c4cf0c6884bd8efc1b7197e6ff039f0"
],
"index": "pypi",
"version": "==1.4.3"
},
"pandas": {
"hashes": [
"sha256:11975fad9edbdb55f1a560d96f91830e83e29bed6ad5ebf506abda09818eaf60",
"sha256:12e13d127ca1b585dd6f6840d3fe3fa6e46c36a6afe2dbc5cb0b57032c902e31",
"sha256:1c87fcb201e1e06f66e23a61a5fea9eeebfe7204a66d99df24600e3f05168051",
"sha256:242e9900de758e137304ad4b5663c2eff0d798c2c3b891250bd0bd97144579da",
"sha256:26c903d0ae1542890cb9abadb4adcb18f356b14c2df46e4ff657ae640e3ac9e7",
"sha256:2e1e88f9d3e5f107b65b59cd29f141995597b035d17cc5537e58142038942e1a",
"sha256:31b7a48b344c14691a8e92765d4023f88902ba3e96e2e4d0364d3453cdfd50db",
"sha256:4fd07a932b4352f8a8973761ab4e84f965bf81cc750fb38e04f01088ab901cb8",
"sha256:5b24ca47acf69222e82530e89111dd9d14f9b970ab2cd3a1c2c78f0c4fbba4f4",
"sha256:647b3b916cc8f6aeba240c8171be3ab799c3c1b2ea179a3be0bd2712c4237553",
"sha256:66b060946046ca27c0e03e9bec9bba3e0b918bafff84c425ca2cc2e157ce121e",
"sha256:6efa9fa6e1434141df8872d0fa4226fc301b17aacf37429193f9d70b426ea28f",
"sha256:be4715c9d8367e51dbe6bc6d05e205b1ae234f0dc5465931014aa1c4af44c1ba",
"sha256:bea90da782d8e945fccfc958585210d23de374fa9294a9481ed2abcef637ebfc",
"sha256:d785fc08d6f4207437e900ffead930a61e634c5e4f980ba6d3dc03c9581748c7",
"sha256:de9559287c4fe8da56e8c3878d2374abc19d1ba2b807bfa7553e912a8e5ba87c",
"sha256:f4f98b190bb918ac0bc0e3dd2ab74ff3573da9f43106f6dba6385406912ec00f",
"sha256:f71f1a7e2d03758f6e957896ed696254e2bc83110ddbc6942018f1a232dd9dad",
"sha256:fb944c8f0b0ab5c1f7846c686bc4cdf8cde7224655c12edcd59d5212cd57bec0"
],
"index": "pypi",
"version": "==0.23.4"
},
"pipdeptree": {
"hashes": [
"sha256:013d343fb0305e95f33a81329a30277fcaac45f78ccea90bcfcdb7dbb9d13da2",
"sha256:2cdd29356c9e3a0cab60d1b20571de713abca031a87f4685c31fc0cab3295d19",
"sha256:a2774940d77fa11c1fb275c350080e75c592d1db5ff5679e0be5e566239de83a"
],
"markers": "python_version != '3.0.*' and python_version != '3.2.*' and python_version != '3.1.*' and python_version >= '2.7' and python_version != '3.3.*'",
"version": "==0.13.0"
},
"python-dateutil": {
"hashes": [
"sha256:1adb80e7a782c12e52ef9a8182bebeb73f1d7e24e374397af06fb4956c8dc5c0",
"sha256:e27001de32f627c22380a688bcc43ce83504a7bc5da472209b4c70f02829f0b8"
],
"version": "==2.7.3"
},
"python-libsbml": {
"hashes": [
"sha256:100e320fe0cfd53deca92360a3b283fe43aac3f9d256ddd47df5c0f80f37e414",
"sha256:1399f7433dd55206fbbe953f1bafe59de59eec5d10dc19fda66ab8bac4296df5",
"sha256:192b4ef9638f3d7fc004a75ceb7d2a3477e02f31819ae0b180ce0404ebc3d329",
"sha256:1ada08745099179688b01b5fa0d0fbfe2928759098c65252fe02c3871375d2b9",
"sha256:1e9b64036bf3a7bfc0d1908799de42a9ce69a4c773f9f9c5e28d3a47e305635d",
"sha256:31097631bc3960e36e7e8ba2be5224409300169d441634ca6b187d0c89b3204c",
"sha256:3174b68588a395e9365fff9bcb19692d583525980bc21f33ccee9f3cd45cecfe",
"sha256:3d50351c146a5a9223487e5d697b451d2e2fa50bdc04f969448351df547dbffa",
"sha256:483a6101a7fa99e6873352228b24c7035b4bd24e7fbc10cb9b10e3e9ea047355",
"sha256:4d188b936abd0bd06bfac8a9554051113b26a53d4210a2bf59cc1fcdbe5fb4c0",
"sha256:53c017e0b767ea0c3cabf2d542da7d9c13b33603f800b3abbec0b3fc74134d0c",
"sha256:5b5893eed6c6033075986f0f825e10246a7b91dc5807346239b04879674917ea",
"sha256:717ecedb25a2ffeb4027e071beda6114f58216a24719c57c45e5469aa5a32af1",
"sha256:80c5384dfbef68b03b7ea0df8d3e1764e001b3a08d51f03aaab1c9100e743135",
"sha256:98a5271cb87595e0617ebb2ac869642e5eee8d3470938edf7ec19d863cec60fd",
"sha256:9fd9a61a471ee11fbadbb4fc07ffcdb7b147b9f71977b87c9547e956297999c3",
"sha256:a8446cc54bf12b9939c1bb38dede1d0676ef11660705a958fabf55b90f609ac0",
"sha256:abda6e7f2770fc3128f184c1d24ccfea3156f7c65a9f0673cef2bfc56819631d",
"sha256:dcb3ad314b281c9dffbab23c374486a30e318f24aa04d6adeced402ad66d7e22",
"sha256:e2eb0a2eede4930ecbfddd28cd6b1a3da2710f106113415217d9437fbea64c41",
"sha256:ed28a35dc101a951684919aa961eba7e957b8952187bd4dcbbb7845c669b1f21",
"sha256:f779520fad40feaf37c21ac0da323f252f2f9270ba27d33c8fca4a1447805407"
],
"index": "pypi",
"version": "==5.17.0"
},
"pytz": {
"hashes": [
"sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053",
"sha256:ffb9ef1de172603304d9d2819af6f5ece76f2e85ec10692a524dd876e72bf277"
],
"version": "==2018.5"
},
"ruamel.yaml": {
"hashes": [
"sha256:36f26a9788f005b70e5f8dd4b42588032f3290884955a19ea21339dd7a2a91bd",
"sha256:37dc87cd78bb6160606dfbd2b015015cf1441872133950ecb4a837a90d694670",
"sha256:3a2ae6ac6f1cd7e26dd1c7a5249080ff269a903e43c237e575239bc42c85d65c",
"sha256:42765a674a5ab4dd7bd6de837c93f2911f6114bb040a07a71379b1577b3b2e36",
"sha256:57f7578af7a71a562d64f78c4a72589d55d471650c19f0fc54e0d2e6c7e5f1f5",
"sha256:66692fc9608c3d7002d5502dea45d3c81fb6158781f65de4d984af41a2e800c8",
"sha256:6719c43f0fb41436759823a7c01258152d18e5f8368e89cd5b1eda5e03734403",
"sha256:6cc5d81f3dac5bf0d387df68dd973bee80ded42f1f83474e59e948450646a805",
"sha256:7a42648c994e99a0b59237f1c10eafeb08475db09ecabf92cd1176693782cd23",
"sha256:7d276cfbbf2a373d6cd02263509b1435ecb7725143a7d3e0bf627f9f3de39bc7",
"sha256:8bd4c97e44514f1d977576799fdd8e8f2b2654a4cd52c0d91c8b7e1c6bcea9ed",
"sha256:aa0ca65adf6dfb511e1de9adb04d64a65b7bf2cc007cd942caaa9a004b43dfeb",
"sha256:aec20e594f604f13e6bd44fff42bfa865ff04b22e93722fc4f744a97bb0bc9cf",
"sha256:aef5a5816680f4c77f872f7f805bbbafc813e9c9cb3405cc9b7507c1680fe7d3",
"sha256:b7fbb5574c8e87e5acc4cb201ca84a1cfb8b967543dd7861fb20743977ea0701",
"sha256:cdce01701ad52a3771393db7feac7d41caa083ba683bd3f16f6647347cab2387",
"sha256:ebbb6527bf12df4835cf7ab001736ba7af657cbf8814354e36e0b8267fb433ca",
"sha256:f1c55e2422ff879dbc6751fbd05a38455a7f870351afff29bcc38df336c3b86c",
"sha256:f6c372edc30b25b884b4bd7556c4eb5b426516e44bf5856601ec5642ffa36905",
"sha256:f9e07c9ba68a94e8b075e1ecf004c211db864246fc65ddf66976b38e82a5242b",
"sha256:fa77530ae18c8e4f1b6596eb77a1cdcbac16f400ef6b48ada0c803533deb5c60",
"sha256:fae2d856c8542a5b5684f1c8f36c538a965068dda39d25a3a82373f05fcf2983"
],
"version": "==0.15.64"
},
"six": {
"hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
"sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
],
"version": "==1.11.0"
},
"swiglpk": {
"hashes": [
"sha256:15e837d292ac8f41ba008b89816d34d5d4ddd4b2b3909ee49316e50a3ee8419a",
"sha256:1b693a5116a5ce83c2e99ed05564000f676ebe01ec19d0aa27dd2f5f319acc24",
"sha256:200710813be665a1dfde57b423a034d661011b6acfc9140388e1bf4bc2653182",
"sha256:20265377d9cc7d7d1ef1ebc4517b8f5308e8c1cfb2b2a36185ae2217df7216fe",
"sha256:20d5fdf8a7452adef816b9d3fb56dcae431bbcee5f1243f248ffef740effa0a7",
"sha256:22eb7634b59104beed3ab329c0b2cf0d4295a6392a1ee5ee4dd6261bfd48a34a",
"sha256:4063beec2c7563979157c040d2809866f9a042c6a7315c5cf9525b648eed9a2e",
"sha256:4671d10b3e1ea287e9e2e43adc31408f8e79566adc988a3992481cc2738869b5",
"sha256:482377bd4c1cb56f097e21c73a9873b410bf245ccc964f593799ed7eba6bd154",
"sha256:4924ae1661cd8c319c1d11d4d06517bc4ac775ff6a148312b5f3594a0b3cd132",
"sha256:553b153e9c83d9e62ab35ca29a636517cccc9a65d45798a97024a5a9796f7cfb",
"sha256:5b3254df3059b6575f07c3538a645cb8d6a05e9c0a1ba27f5b95d406faf5b79d",
"sha256:6f26e1e5ff3f0ca2bb12cfc6cc0324f9d867e5d8dfa237fd6270476441132154",
"sha256:82f23004ccdcb3198ea615130a4d41e1b3c64768ed444bdbce3e9bfadabb0b60",
"sha256:83219003025aaff567c74e8847c6f8087b06d89db6c8c43f0f166520d4200fb7",
"sha256:834953a005121efb9d59453dab29465cac041486fc262a1e1f1d736fd00f1123",
"sha256:8d9b252012b6f5d3e5865f61a3c05ddb257d559c2117a0c0198a8525de7076f3",
"sha256:999caaeec0cb68b99d7a34225bc7f03fbdb772ee40535913c9ed89886d167e30",
"sha256:9ef4f006a419eae76bcf446a948a2d748004f3e6d947cc4181904578ea1529b0",
"sha256:b4e81ea6331227e76cbc2905e91c645dc76ae25729143ae6d111ebbd68265ede",
"sha256:b554ddc9840e0863cbe528797f4ebf3fb29925965e7135440bf284504a7bcd2f",
"sha256:c16ae393de23e161dcbb20066e230d75c6bf4ad2b3b7f46906de9432ca6bbfac",
"sha256:cc4f07a5a793975baef42243955a0766f3c1d2a12b5b52ddf174adcbae66c969",
"sha256:d78dd0cd56015a00f34a023eac8dd668c29ece03a3146bdd08ead21c9d877546",
"sha256:d9573d143da84d6089d4db2e2902334c2751bb2ea1f23b60bae96ea10d563b8c",
"sha256:daab24791eccd8d943de721ebdfc61aea21ad73dad549e971000b9d0f56feaef",
"sha256:f0c985cad21bb2202b04a6a45f7d46849997cf4731321811f9db276a9ab0c2c8",
"sha256:ff5868082ac5a2f6a1efa58a8d18d323c57cd4008b9454d872c295f302539faf"
],
"version": "==1.5.1"
},
"sympy": {
"hashes": [
"sha256:286ca070d72e250861dea7a21ab44f541cb2341e8268c70264cf8642dbd9225f"
],
"version": "==1.2"
},
"tabulate": {
"hashes": [
"sha256:e4ca13f26d0a6be2a2915428dc21e732f1e44dad7f76d7030b2ef1ec251cf7f2"
],
"version": "==0.8.2"
},
"tqdm": {
"hashes": [
"sha256:5ef526702c0d265d5a960a3b27f3971fac13c26cf0fb819294bfa71fc6026c88",
"sha256:a3364bd83ce4777320b862e3c8a93d7da91e20a95f06ef79bed7dd71c654cafa"
],
"index": "pypi",
"version": "==4.25.0"
}
},
"develop": {}
}
# -*- coding: utf-8 -*-
# Copyright 2018 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Perform single deletion studies in parallel."""
from __future__ import absolute_import
import logging
import multiprocessing
import sys
from cobra.core import Solution
from cobra.exceptions import OptimizationError
from cobra.flux_analysis import moma, pfba, room
from cobra.io import read_sbml_model
from optlang.exceptions import SolverError
from pandas import Series
from tqdm import tqdm
LOGGER = logging.getLogger()
def _init_worker(model, reference):
global _model
global _reference
_model = model
_reference = reference
def _pfba_gene_deletion_study(model, gene):
with model:
model_gene = model.genes.get_by_id(gene)
model_gene.knock_out()
try:
sol = pfba(model)
except (OptimizationError, SolverError) as err:
sol = Solution(objective_value=None, status=str(err),
fluxes=Series([]))
return gene, sol
def _lmoma_gene_deletion_study(model, gene, reference):
with model:
model_gene = model.genes.get_by_id(gene)
model_gene.knock_out()
try:
sol = moma(model, solution=reference, linear=True)
except (OptimizationError, SolverError) as err:
sol = Solution(objective_value=None, status=str(err),
fluxes=Series([]))
return gene, sol
def _room_gene_deletion_study(model, gene, reference):
with model:
model_gene = model.genes.get_by_id(gene)
model_gene.knock_out()
try:
sol = room(model, solution=reference)
except (OptimizationError, SolverError) as err:
sol = Solution(objective_value=None, status=str(err),
fluxes=Series([]))
return gene, sol
def _pfba_worker(gene):
global _model
return _pfba_gene_deletion_study(_model, gene)
def _lmoma_worker(gene):
global _model
global _reference
return _lmoma_gene_deletion_study(_model, gene, _reference)
def _room_worker(gene):
global _model
global _reference
return _room_gene_deletion_study(_model, gene, _reference)
def single_gene_deletion_study(model, method, processes=1):
reference = pfba(model)
genes = [g.id for g in model.genes]
if len(genes) == 0:
LOGGER.info("Everything complete.")
return
if method == "linear moma":
worker = _lmoma_worker
elif method == "room":
worker = _room_worker
elif method == "pfba":
worker = _pfba_worker
else:
raise ValueError("Unknown method '{}'.".format(method))
# Compute the results potentially using multiprocessing.
LOGGER.info("Calculating knock-out predictions.")
if processes > 1:
LOGGER.debug("Using multiprocessing.")
chunk_size = max(len(genes) // processes, 1)
pool = multiprocessing.Pool(
processes, initializer=_init_worker, initargs=(model, reference)
)
results = pool.imap_unordered(worker, genes, chunksize=chunk_size)
pool.close()
else:
_init_worker(model, reference)
results = map(worker, genes)
# Iterate over the results and store the flux distributions.
for gene, sol in tqdm(results, total=len(genes),
desc="{} - Genes".format(method)):
if sol.status != "optimal":
LOGGER.warning(
"'%s' knock-out caused a non-optimal solver status: %s.",
gene, sol.status)
# TODO: Do something useful with the results here.
sol.fluxes.to_csv("{}.csv".format(gene), header=True)
if processes > 1:
pool.join()
def main(argv):
model = read_sbml_model(argv[0])
single_gene_deletion_study(model, argv[1], int(argv[2]))
if __name__ == "__main__":
logging.basicConfig(level="INFO",
format="[%(levelname)s] %(message)s")
if len(sys.argv) != 4:
LOGGER.critical("Usage:\n%s <model> <method> <processes>",
sys.argv[0])
sys.exit(2)
else:
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment