Skip to content

Commit

Permalink
- export model as cps / sbml
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergmann committed Apr 17, 2024
1 parent a0f728e commit b77b6cc
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/assets/python/export_current_as_sbml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import basico
import js

version = int(js.window.sbml_export_version)
level = int(js.window.sbml_export_level)


sbml_export = basico.save_model_to_string(type='sbml', sbml_level=level, sbml_version=version)
105 changes: 105 additions & 0 deletions src/components/Landing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@ import stateService from '@/services/StateService'
/* @ts-ignore */
import Markdown from 'vue3-markdown-it'
const sbmlExport = ref([
{
label: 'Level 3 Version 1',
command: () => {
exportSbmlLV(3, 1)
}
},
{
label: 'Level 3 Version 2',
command: () => {
exportSbmlLV(3, 2)
}
},
{
separator: true
},
{
label: 'Level 2 Version 4',
command: () => {
exportSbmlLV(2, 4)
}
},
{
label: 'Level 2 Version 1',
command: () => {
exportSbmlLV(2, 1)
}
},
{
separator: true
},
{
label: 'Level 1 Version 2',
command: () => {
exportSbmlLV(1, 2)
}
}
]);
const state: State | undefined = inject('$state')
const baseUrl = import.meta.env.BASE_URL
Expand All @@ -31,6 +70,65 @@ const loadFile = (files: Array<File>) => {
reader.readAsText(file)
}
const exportCopasi = () =>
{
if (state?.copasi == null) {
return
}
const blob = new Blob([state.copasi], { type: 'application/x-copasi' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('hidden', '')
a.setAttribute('href', url)
a.setAttribute('download', state.modelName +'.cps')
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
const exportSBML = () =>
{
if (state?.sbml == null) {
return
}
const blob = new Blob([state.sbml], { type: 'application/sbml+xml' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('hidden', '')
a.setAttribute('href', url)
a.setAttribute('download', state.modelName +'.xml')
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
const exportSbmlLV = (level: number, version:number) =>
{
if (state?.pyodide == null) {
return
}
const model : string = stateService.exportSBML(state, window, level, version)
const blob = new Blob([model], { type: 'application/sbml+xml' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.setAttribute('hidden', '')
a.setAttribute('href', url)
// create filename including sbml.modelName + Level + Version converting
// level and version to string
const filename = state.modelName + '_L' + level.toString() + 'V' + version.toString() + '.xml'
a.setAttribute('download', filename)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
const loadDiseaseModel = (model: string) => {
if (state == null) {
return
Expand Down Expand Up @@ -243,6 +341,13 @@ const diseaseModels = ref([
<template v-if="state.modelDescription != ''">
<Markdown :source="state.modelDescription" />
</template>

<div class="col-12">
<span class="p-buttonset">
<Button icon="pi pi-arrow-circle-down" title="Download current COPASI file" label="COPASI" class="mr-2 mb-2" @click="exportCopasi()"> </Button>
<SplitButton icon="pi pi-arrow-circle-down" :model="sbmlExport" title="Download current file as SBML" label="SBML" class="p-button-secondary mr-2 mb-2" @click="exportSBML()"></SplitButton>
</span>
</div>
</div>
</div>

Expand Down
16 changes: 16 additions & 0 deletions src/services/StateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { State } from '../components/Types'
import load_model from '../assets/python/load_model.py?raw'
import simulate_model from '../assets/python/simulate_model.py?raw'
import steady_steate from '../assets/python/steady_state.py?raw'
import export_current_as_sbml from '../assets/python/export_current_as_sbml.py?raw'

export default class StateService {
/**
Expand Down Expand Up @@ -43,6 +44,21 @@ export default class StateService {
state.timeCourseResult = null
}

public static exportSBML(state: State, window: Window, level : number,version:number) {
if (!state.pyodide) {
return
}

/* @ts-ignore */
window.sbml_export_version = version
/* @ts-ignore */
window.sbml_export_level = level

state.pyodide.runPython(export_current_as_sbml)

return state.pyodide.globals.get('sbml_export')
}

public static loadModelFromContent(state: State, window: Window, content: string) {
if (!state.pyodide) {
return
Expand Down

0 comments on commit b77b6cc

Please sign in to comment.