SiLeBAT/FSK-Lab

View on GitHub
de.bund.bfr.knime.pmm.nodes/src/de/bund/bfr/knime/pmm/common/writer/TableReader.java

Summary

Maintainability
D
1 day
Test Coverage
package de.bund.bfr.knime.pmm.common.writer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLStreamException;

import org.sbml.jsbml.Model;
import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.UnitDefinition;

import de.bund.bfr.knime.pmm.common.CatalogModelXml;
import de.bund.bfr.knime.pmm.common.DepXml;
import de.bund.bfr.knime.pmm.common.IndepXml;
import de.bund.bfr.knime.pmm.common.ParamXml;
import de.bund.bfr.knime.pmm.common.PmmXmlDoc;
import de.bund.bfr.knime.pmm.common.PmmXmlElementConvertable;
import de.bund.bfr.knime.pmm.common.generictablemodel.KnimeTuple;
import de.bund.bfr.knime.pmm.common.math.MathUtilities;
import de.bund.bfr.knime.pmm.common.pmmtablemodel.Model1Schema;
import de.bund.bfr.knime.pmm.common.pmmtablemodel.Model2Schema;
import de.bund.bfr.knime.pmm.common.pmmtablemodel.TimeSeriesSchema;
import de.bund.bfr.knime.pmm.common.units.Categories;
import de.bund.bfr.knime.pmm.common.units.Category;
import de.bund.bfr.knime.pmm.common.units.ConvertException;
import de.bund.bfr.pmfml.PMFUtil;
import de.bund.bfr.pmfml.sbml.PMFUnitDefinition;

public class TableReader {
    public final static int LEVEL = 3;
    public final static int VERSION = 1;

    public static void renameLog(KnimeTuple tuple) {
        PmmXmlDoc modelXml = tuple.getPmmXml(Model1Schema.ATT_MODELCATALOG);
        CatalogModelXml model = (CatalogModelXml) modelXml.get(0);

        model.formula = MathUtilities.replaceVariable(model.formula, "log", "log10");
        tuple.setValue(Model1Schema.ATT_MODELCATALOG, modelXml);
    }

    public static void replaceCelsiusAndFahrenheit(KnimeTuple tuple) {
        final String CELSIUS = "°C";
        final String FAHRENHEIT = "°F";
        final String KELVIN = "K";

        PmmXmlDoc indepXml = tuple.getPmmXml(Model1Schema.ATT_INDEPENDENT);
        PmmXmlDoc modelXml = tuple.getPmmXml(Model1Schema.ATT_MODELCATALOG);
        CatalogModelXml model = (CatalogModelXml) modelXml.get(0);
        Category temp = Categories.getTempCategory();

        for (PmmXmlElementConvertable el : indepXml.getElementSet()) {
            IndepXml indep = (IndepXml) el;

            if (CELSIUS.equals(indep.unit)) {
                try {
                    String replacement = "(" + temp.getConversionString(indep.name, KELVIN, CELSIUS) + ")";

                    model.formula = MathUtilities.replaceVariable(model.formula, indep.name, replacement);
                    indep.unit = KELVIN;
                    indep.min = temp.convert(indep.min, CELSIUS, KELVIN);
                    indep.max = temp.convert(indep.max, CELSIUS, KELVIN);
                } catch (ConvertException e) {
                    e.printStackTrace();
                }
            } else if (FAHRENHEIT.equals(indep.unit)) {
                try {
                    String replacement = "(" + temp.getConversionString(indep.name, KELVIN, FAHRENHEIT) + ")";

                    model.formula = MathUtilities.replaceVariable(model.formula, indep.name, replacement);
                    indep.unit = FAHRENHEIT;
                    indep.min = temp.convert(indep.min, FAHRENHEIT, KELVIN);
                    indep.max = temp.convert(indep.max, FAHRENHEIT, KELVIN);
                } catch (ConvertException e) {
                    e.printStackTrace();
                }
            }
        }

        tuple.setValue(Model1Schema.ATT_INDEPENDENT, indepXml);
        tuple.setValue(Model1Schema.ATT_MODELCATALOG, modelXml);
    }

    public static void addNamespaces(SBMLDocument doc) {
        doc.addDeclaredNamespace("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        doc.addDeclaredNamespace("xmlns:pmml", "http://www.dmg.org/PMML-4_2");
        doc.addDeclaredNamespace("xmlns:pmf", "http://sourceforge.net/projects/microbialmodelingexchange/files/PMF-ML");
        doc.addDeclaredNamespace("xmlns:dc", "http://purl.org/dc/elements/1.1");
        doc.addDeclaredNamespace("xmlns:dcterms", "http://purl.org/dc/terms/");
        doc.addDeclaredNamespace("xmlns:pmmlab",
                "http://sourceforge.net/projects/microbialmodelingexchange/files/PMF-ML");
        doc.addDeclaredNamespace("xmlns:numl", "http://www.numl.org/numl/level1/version1");
        doc.addDeclaredNamespace("xmlns:xlink", "http//www.w3.org/1999/xlink");
    }

    public static void addUnitDefinitions(Model model, DepXml depXml, List<IndepXml> indepXmls,
            List<ParamXml> constXmls) throws XMLStreamException {
        // Get units from dep, indeps and consts
        HashSet<String> units = new HashSet<>();
        if (depXml.unit != null) {
            units.add(depXml.unit);
        }

        for (IndepXml indepXml : indepXmls) {
            if (indepXml.unit != null) {
                units.add(indepXml.unit);
            }
        }

        for (ParamXml paramXml : constXmls) {
            if (paramXml.unit != null) {
                units.add(paramXml.unit);
            }
        }

        // Creates and adds unit definitions for the units present in DB.
        // Missing units in DB will not be retrievable and thus will lack a list
        // of units
        for (String unit : units) {
            PMFUnitDefinition unitDefinition = WriterUtils.createUnitFromDB(unit);
            
            // If unitDefinition could not be retrieved from DB then create a new one.
            if (unitDefinition == null) {
                String unitID = PMFUtil.createId(unit);
                if (!model.hasUnit(unitID)) {
                    UnitDefinition ud = model.createUnitDefinition(PMFUtil.createId(unit));
                    ud.setName(unit);
                }
            }
            // Otherwise add it
            else {
                model.addUnitDefinition(unitDefinition.getUnitDefinition());
            }
        }
    }

    public static Map<Integer, Map<Integer, List<KnimeTuple>>> sortGlobalModels(List<KnimeTuple> tuples) {
        // Sort tertiary models
        Map<Integer, Map<Integer, List<KnimeTuple>>> gms = new HashMap<>();
        for (KnimeTuple tuple : tuples) {
            Integer gmID = tuple.getInt(Model2Schema.ATT_GLOBAL_MODEL_ID);
            Integer condID = tuple.getInt(TimeSeriesSchema.ATT_CONDID);

            // global model is in globalModels
            if (gms.containsKey(gmID)) {
                // Get global model
                Map<Integer, List<KnimeTuple>> gm = gms.get(gmID);
                // globalModel has tertiary model with condID => Add tuple to
                // this tertiary model
                if (gm.containsKey(condID)) {
                    gm.get(condID).add(tuple);
                }
                // Otherwise, create a tertiary model with condID and add it the
                // current tuple
                else {
                    LinkedList<KnimeTuple> tertiaryModel = new LinkedList<>();
                    tertiaryModel.add(tuple);
                    gm.put(condID, tertiaryModel);
                }
            }

            // else, create tertiary model with condID and add it to new global
            // model
            else {
                // Create new global model
                HashMap<Integer, List<KnimeTuple>> gm = new HashMap<>();

                // Create tertiary model and add it to new global model
                LinkedList<KnimeTuple> tertiaryModel = new LinkedList<>();
                tertiaryModel.add(tuple);
                gm.put(condID, tertiaryModel);

                // Add new global model
                gms.put(gmID, gm);
            }
        }
        return gms;
    }

}