diff --git a/console/src/main/java/lcsb/mapviewer/run/BellExport.java b/console/src/main/java/lcsb/mapviewer/run/BellExport.java deleted file mode 100644 index f3c63406f72f52f727ab78bb90300bd7ef08caae..0000000000000000000000000000000000000000 --- a/console/src/main/java/lcsb/mapviewer/run/BellExport.java +++ /dev/null @@ -1,137 +0,0 @@ -package lcsb.mapviewer.run; - -import org.apache.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; - -import lcsb.mapviewer.annotation.services.ModelAnnotator; -import lcsb.mapviewer.common.IProgressUpdater; -import lcsb.mapviewer.converter.ConverterParams; -import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.species.Species; -import lcsb.mapviewer.persist.ApplicationContextLoader; -import lcsb.mapviewer.persist.DbUtils; -import lcsb.mapviewer.services.interfaces.IExporterService; -import lcsb.mapviewer.services.interfaces.IExporterService.ExporterParameters; -import lcsb.mapviewer.services.utils.data.ExportFileType; - -/** - * This class prepare data for Bell format (further transformation is done by - * Marek). - * - * @author Piotr Gawron - * - */ -public class BellExport { - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(BellExport.class.getName()); - - /** - * Object that performs different types of export transformation. - */ - @Autowired - private IExporterService exporter; - - /** - * Object used to annotate the model. - */ - @Autowired - private ModelAnnotator modelAnnotator; - - /** - * Object used to annotate the model. - */ - @Autowired - private DbUtils dbUtils; - - /** - * Static main method used to run this stand alone code. - * - * @param args - * command line arguments - */ - public static void main(String[] args) { - ApplicationContextLoader.loadApplicationContext("consoleApplicationContext.xml"); - BellExport main = new BellExport(); - ApplicationContextLoader.injectDependencies(main); - main.run(); - } - - /** - * Default constructor. - */ - public BellExport() { - - } - - /** - * Method that export pd map into file that can be exported to bell (by - * Marek). - * - */ - private void run() { - CellDesignerXmlParser p = new CellDesignerXmlParser(); - try { - String filename = PdMapAnnotations.getLastPdFilename(); - String version = PdMapAnnotations.getLastPdVersion(); - - // filename = "testFiles/other_full/ASTHMAP18000X8000V1_38.xml"; - // version = "1.1.38"; - dbUtils.createSessionForCurrentThread(); - Model model = p.createModel(new ConverterParams().filename(filename)); - - modelAnnotator.performAnnotations(model, new IProgressUpdater() { - @Override - public void setProgress(double progress) { - } - }); - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).// - fileName("out/bell/" + version + "-reactions.txt").// - fileType(ExportFileType.TAB_SEPARATED).// - type(Species.class).// - moleculeEdges(false); - - exporter.exportReactions(params); - - params = new IExporterService.ExporterParameters().model(model).// - fileName("out/bell/" + version + "-species.txt").// - fileType(ExportFileType.TAB_SEPARATED).// - type(Species.class).// - // column(ExportColumn.FULL).// - moleculeEdges(false); - exporter.exportSpecies(params); - - params = new IExporterService.ExporterParameters().model(model).// - fileName("out/bell/" + version + "-compartments.txt").// - fileType(ExportFileType.TAB_SEPARATED).// - type(Object.class); // - // column(ExportColumn.FULL); - exporter.exportCompartments(params); - - } catch (Exception e) { - logger.error(e.getMessage(), e); - } finally { - dbUtils.closeSessionForCurrentThread(); - } - } - - /** - * @return the modelAnnotator - * @see #modelAnnotator - */ - public ModelAnnotator getModelAnnotator() { - return modelAnnotator; - } - - /** - * @param modelAnnotator - * the modelAnnotator to set - * @see #modelAnnotator - */ - public void setModelAnnotator(ModelAnnotator modelAnnotator) { - this.modelAnnotator = modelAnnotator; - } -} diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java b/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java deleted file mode 100644 index a2a369d7114ec1686ddf67be0b749ebaced353d9..0000000000000000000000000000000000000000 --- a/service/src/main/java/lcsb/mapviewer/services/impl/ExporterService.java +++ /dev/null @@ -1,1088 +0,0 @@ -package lcsb.mapviewer.services.impl; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; - -import lcsb.mapviewer.common.exception.InvalidArgumentException; -import lcsb.mapviewer.converter.model.celldesigner.reaction.ReactionLineData; -import lcsb.mapviewer.model.map.MiriamData; -import lcsb.mapviewer.model.map.MiriamType; -import lcsb.mapviewer.model.map.compartment.Compartment; -import lcsb.mapviewer.model.map.compartment.PathwayCompartment; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.reaction.Modifier; -import lcsb.mapviewer.model.map.reaction.Product; -import lcsb.mapviewer.model.map.reaction.Reactant; -import lcsb.mapviewer.model.map.reaction.Reaction; -import lcsb.mapviewer.model.map.reaction.ReactionNode; -import lcsb.mapviewer.model.map.species.AntisenseRna; -import lcsb.mapviewer.model.map.species.Complex; -import lcsb.mapviewer.model.map.species.Degraded; -import lcsb.mapviewer.model.map.species.Drug; -import lcsb.mapviewer.model.map.species.Element; -import lcsb.mapviewer.model.map.species.Gene; -import lcsb.mapviewer.model.map.species.Ion; -import lcsb.mapviewer.model.map.species.Phenotype; -import lcsb.mapviewer.model.map.species.Protein; -import lcsb.mapviewer.model.map.species.Rna; -import lcsb.mapviewer.model.map.species.SimpleMolecule; -import lcsb.mapviewer.model.map.species.Species; -import lcsb.mapviewer.model.map.species.Unknown; -import lcsb.mapviewer.model.map.species.field.AntisenseRnaRegion; -import lcsb.mapviewer.model.map.species.field.ModificationResidue; -import lcsb.mapviewer.model.map.species.field.RnaRegion; -import lcsb.mapviewer.services.interfaces.IExporterService; -import lcsb.mapviewer.services.utils.data.ExportColumn; -import lcsb.mapviewer.services.utils.data.ExportFileType; -import lcsb.mapviewer.services.view.PubmedAnnotatedElementsView; -import lcsb.mapviewer.services.view.PubmedAnnotatedElementsViewFactory; - -/** - * Implementation of {@link IExporterService} interface that allows to export - * {@link Model}. - * - * @author Piotr Gawron - * - */ -public class ExporterService implements IExporterService { - - /** - * Default class logger. - */ - private static Logger logger = Logger.getLogger(ExporterService.class); - - /** - * List of publications by {@link Model#getId() model id}. This is used for - * caching, as obtaining this list on runtime is quite expensive. - */ - private static Map<Integer, List<PubmedAnnotatedElementsView>> pubmedPublicationsForModel = new HashMap<>(); - - /** - * Factory used to create {@link PubmedAnnotatedElementsView publication - * summary} elements. - */ - @Autowired - private PubmedAnnotatedElementsViewFactory pubmedAnnotatedElementsViewFactory; - - @Override - public void exportSpecies(ExporterParameters parameters) throws IOException { - String content = getExportSpeciesString(parameters); - writeToFile(content, parameters.getFileName()); - } - - @Override - public String getExportSpeciesString(ExporterParameters parameters) { - Set<String> uniqeLines = new HashSet<String>(); - StringBuilder builder = new StringBuilder(); - for (ExportColumn column : parameters.getColumns()) { - builder.append(column.getTitle() + "\t"); - } - builder.append("\n"); - for (Model model : parameters.getModels()) { - for (Element alias : model.getElements()) { - if (alias instanceof Species) { - Species speciesAlias = (Species) alias; - boolean toBeProcessed = false; - // check type - for (Class<?> type : parameters.getTypes()) { - if (type.isAssignableFrom(speciesAlias.getClass())) { - toBeProcessed = true; - break; - } - } - if (!toBeProcessed && parameters.getTypes().size() > 0) { - continue; - } - - // check where it lays - toBeProcessed = (parameters.getIncludedAliases().size() == 0); - for (Compartment ca : parameters.getIncludedAliases()) { - if (ca.cross(alias)) { - toBeProcessed = true; - } - } - for (Compartment ca : parameters.getExcludedAliases()) { - if (ca.cross(alias)) { - toBeProcessed = false; - } - } - if (toBeProcessed) { - List<String> elements = getExportStringForOneSpecies(speciesAlias, parameters); - for (String string : elements) { - uniqeLines.add(string); - } - } - } - } - } - List<String> sortedElements = new ArrayList<String>(); - sortedElements.addAll(uniqeLines); - Collections.sort(sortedElements); - for (String string : sortedElements) { - builder.append(string); - builder.append("\n"); - } - return builder.toString(); - } - - /** - * Returns strings with species alias exported using given parameters. Many - * lines can be exported because one species alias can belong to many - * pathways. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @param species - * export string are generated for this alias - * @return list of strings representing exported data - */ - protected List<String> getExportStringForOneSpecies(Species species, ExporterParameters parameters) { - if (parameters.getFileType() != ExportFileType.TAB_SEPARATED) { - throw new InvalidArgumentException("Unhandled file type: " + parameters.getFileType()); - } - - List<Compartment> aliases = getComponentList(species); - List<String> result = new ArrayList<String>(); - - Compartment smallestCompartment = getCompartment(species); - - List<ExportColumn> processedColumns = new ArrayList<ExportColumn>(); - for (ExportColumn column : parameters.getColumns()) { - if (column.getClazz().isAssignableFrom(Species.class)) { - processedColumns.add(column); - } - } - - for (Compartment compartment : aliases) { - String separator = ""; - StringBuilder line = new StringBuilder(); - for (ExportColumn column : processedColumns) { - line.append(separator); - separator = "\t"; - if (column == ExportColumn.COMPARTMENT_NAME) { - if (smallestCompartment == null) { - line.append("null"); - } else { - line.append(smallestCompartment.getName()); - } - } else if (column == ExportColumn.POSITION_TO_COMPARTMENT) { - line.append(species.getPositionToCompartment()); - } else if (column == ExportColumn.COMPONENT_NAME) { - line.append(compartment.getName()); - } else if (column == ExportColumn.NAME) { - if (species instanceof Complex) { - if (!parameters.isComplexElementsName()) { - line.append(species.getName()); - } else { - line.append(getComplexNameWithElements((Complex) species, parameters)); - } - } else { - line.append(species.getName()); - } - } else if (column == ExportColumn.ID) { - line.append(getId(species)); - } else if (column == ExportColumn.TYPE) { - line.append(species.getClass().getSimpleName()); - } else if (column == ExportColumn.SUBMODEL) { - line.append(species.getModelData().getName()); - } else if (column == ExportColumn.COMPLEX) { - String complex = null; - if (species.getComplex() != null) { - complex = species.getComplex().getName(); - - complex += " [id=" + getId(species.getComplex()) + "]"; - } - line.append(complex); - } else { - throw new InvalidArgumentException("Unhandled column type: " + column); - } - } - for (MiriamType mt : parameters.getMiriamTypes()) { - line.append(separator); - String types = ""; - for (MiriamData md : species.getMiriamData()) { - if (md.getDataType().equals(mt)) { - if (!types.isEmpty()) { - types += ","; - } - types += md.getResource(); - } - } - if (types.equals("")) { - line.append(mt.toString() + ": N/A"); - } else { - line.append(mt.toString() + ": " + types); - } - } - result.add(line.toString().replaceAll("[\n\r]+", " ")); - } - return result; - } - - /** - * Returns {@link Compartment} to which element belongs to. - * - * @param element - * alias of the element - * @return {@link Compartment} to which element belongs to - */ - private Compartment getCompartment(Species element) { - Compartment smallestAlias = null; - List<Compartment> compartmentAliases = new ArrayList<Compartment>(); - - for (Compartment alias : element.getModel().getCompartments()) { - if (alias.cross(element)) { - if (!(alias instanceof PathwayCompartment)) { - compartmentAliases.add(alias); - } - } - } - - if (compartmentAliases.size() > 0) { - smallestAlias = compartmentAliases.get(0); - } - for (Compartment compartmentAlias : compartmentAliases) { - if (compartmentAlias.getSize() < smallestAlias.getSize()) { - smallestAlias = compartmentAlias; - } - } - return smallestAlias; - } - - /** - * Returns a list of pathways to which element belongs to. - * - * @param element - * alias of the element - * @return list of {@link Compartment} representing pathways - */ - private List<Compartment> getComponentList(Species element) { - Model model = element.getModel(); - - List<Compartment> aliases = new ArrayList<Compartment>(); - for (Compartment alias : model.getCompartments()) { - if (alias instanceof PathwayCompartment) { - if (alias.cross(element)) { - aliases.add(alias); - } - } - } - - // in case the element is outside any component then we have to crate - // artifical null component - if (aliases.size() == 0) { - PathwayCompartment nullAlias = new PathwayCompartment("default"); - nullAlias.setName("null"); - aliases.add(nullAlias); - } - - return aliases; - } - - /** - * Returns name of a complex with the names of all species included in this - * complex. - * - * @param complex - * complex for which name is computed - * @param parameters - * parameter limits the species that will be included in generated - * name - * @return name of a complex with the names of all species included in this - * complex - */ - private String getComplexNameWithElements(Complex complex, ExporterParameters parameters) { - StringBuilder result = new StringBuilder(); - String separator = ""; - for (Element species2 : complex.getAllSimpleChildren()) { - boolean add = false; - if (parameters.getTypes().size() == 0) { - add = true; - } - for (Class<?> class1 : parameters.getTypes()) { - if (class1.isAssignableFrom(species2.getClass())) { - add = true; - } - } - if (add) { - result.append(separator); - result.append(species2.getName()); - separator = ":"; - } - - } - if (result.length() == 0) { - return complex.getName(); - } else { - return result.toString(); - } - } - - /** - * Writes content into a file. - * - * @param content - * content that should be written into the file. - * @param fileName - * file name - * @throws IOException - * thrown when there is a problem with accessing file - */ - protected void writeToFile(String content, String fileName) throws IOException { - logger.debug("Save file: " + fileName); - File file = new File(fileName); - - // if file doesnt exists, then create it - if (!file.exists()) { - // first create directory structure - file.getParentFile().mkdirs(); - file.createNewFile(); - } - - FileWriter fw = new FileWriter(file.getAbsoluteFile()); - BufferedWriter bw = new BufferedWriter(fw); - bw.write(content); - bw.close(); - } - - @Override - public String getExportInteractionString(ExporterParameters parameters) { - Set<String> uniqueLines = new HashSet<String>(); - if (parameters.getFileType() != ExportFileType.SIF && parameters.getFileType() != ExportFileType.TAB_SEPARATED) { - throw new InvalidArgumentException("Unsupported file format: " + parameters.getFileType()); - } - - for (Model model : parameters.getModels()) { - for (Reaction reaction : model.getReactions()) { - int counter = 0; - for (ReactionNode node : reaction.getReactionNodes()) { - Element element = node.getElement(); - for (Class<?> type : parameters.getTypes()) { - if (type.isAssignableFrom(element.getClass())) { - counter++; - break; - } - } - } - if (counter > 1) { - uniqueLines.addAll(getExportSingleInteractionString(reaction, parameters)); - } - } - } - if (parameters.isMoleculeEdges()) { - for (Model model : parameters.getModels()) { - for (Element element : model.getElements()) { - if (element instanceof Species) { - if (element instanceof SimpleMolecule) { - boolean ok = false; - for (Compartment alias : parameters.getIncludedAliases()) { - if (alias.cross(element)) { - ok = true; - } - } - for (Compartment alias : parameters.getExcludedAliases()) { - if (alias.cross(element)) { - ok = false; - } - } - if (ok) { - String string = getExportSingleInteractionStringFromSpecies((SimpleMolecule) element, parameters); - if (parameters.getFileType() == ExportFileType.SIF) { - if (!uniqueLines.contains(string.replace("molecule_type", "reaction_type"))) { - uniqueLines.add(string); - } - } else { - uniqueLines.add(string); - } - } - } - } - } - } - } - List<String> sortedLines = new ArrayList<String>(); - sortedLines.addAll(uniqueLines); - Collections.sort(sortedLines); - StringBuilder result = new StringBuilder(); - for (String string : sortedLines) { - if (!string.equals("")) { - result.append(string); - result.append("\n"); - } - } - - return result.toString(); - } - - /** - * Creates interaction from molecule (molecule is used as extension of - * reaction between different types of elements). - * - * @param species - * molecule which is treaded as a reaction element - * @param parameters - * {@link ExporterParameters export parameters} - * @return string representing exported reaction - */ - protected String getExportSingleInteractionStringFromSpecies(SimpleMolecule species, ExporterParameters parameters) { - if (parameters.getFileType() != ExportFileType.SIF) { - throw new InvalidArgumentException("Unsupported file format: " + parameters.getFileType()); - } - StringBuilder result = new StringBuilder(); - Set<String> elements = new HashSet<String>(); - for (Model model : parameters.getModels()) { - for (Reaction reaction : model.getReactions()) { - boolean goodReaction = false; - for (ReactionNode node : reaction.getReactionNodes()) { - if (node.getElement() instanceof SimpleMolecule) { - Element species2 = node.getElement(); - if (species.getName().equalsIgnoreCase(species2.getName())) { - goodReaction = true; - } - } - } - if (!goodReaction) { - continue; - } - for (ReactionNode node : reaction.getReactionNodes()) { - boolean ok = false; - if (parameters.getIncludedAliases().size() == 0) { - ok = true; - } - for (Compartment alias2 : parameters.getIncludedAliases()) { - if (alias2.cross(node.getElement())) { - ok = true; - break; - } - } - for (Compartment alias2 : parameters.getExcludedAliases()) { - if (alias2.cross(node.getElement())) { - ok = false; - break; - } - } - if (ok) { - Element element = node.getElement(); - for (Class<?> type : parameters.getTypes()) { - if (type.isAssignableFrom(element.getClass())) { - if (element instanceof Complex) { - if (!parameters.isComplexElementsName()) { - elements.add(((Species) element).getName()); - } else { - String name = getComplexNameWithElements((Complex) element, parameters); - elements.add(name); - } - } else { - elements.add(((Species) element).getName()); - } - break; - } - } - } - } - } - } - if (elements.size() <= 1) { - return ""; - } - List<String> list = new ArrayList<String>(); - list.addAll(elements); - result.append(list.get(0)); - result.append("\tmolecule_type"); - for (int i = 1; i < list.size(); i++) { - result.append("\t"); - result.append(list.get(i)); - } - return result.toString(); - - } - - /** - * Returns strings with reactions exported using given parameters. Many lines - * can be exported because one species alias can belong to many pathways. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @param reaction - * export string are generated for this reaction - * @return list of strings representing exported data - */ - protected List<String> getExportSingleInteractionString(Reaction reaction, ExporterParameters parameters) { - List<String> result = new ArrayList<String>(); - if (parameters.getFileType() == ExportFileType.SIF) { - StringBuilder builder = new StringBuilder(); - Set<Element> elements = new HashSet<>(); - for (ReactionNode node : reaction.getReactionNodes()) { - boolean ok = false; - if (parameters.getIncludedAliases().size() == 0) { - ok = true; - } - for (Compartment alias2 : parameters.getIncludedAliases()) { - if (alias2.cross(node.getElement())) { - ok = true; - break; - } - } - for (Compartment alias2 : parameters.getExcludedAliases()) { - if (alias2.cross(node.getElement())) { - ok = false; - break; - } - } - if (ok) { - Element element = node.getElement(); - if (parameters.getTypes().size() == 0) { - elements.add(element); - } else { - for (Class<?> type : parameters.getTypes()) { - if (type.isAssignableFrom(element.getClass())) { - elements.add(element); - break; - } - } - } - } - } - Set<String> elementsNames = new HashSet<>(); - for (Element element : elements) { - if (element instanceof Complex) { - if (!parameters.isComplexElementsName()) { - elementsNames.add(((Species) element).getName()); - } else { - String name = getComplexNameWithElements((Complex) element, parameters); - elementsNames.add(name); - } - - } else { - elementsNames.add(((Species) element).getName()); - } - } - - if (elementsNames.size() <= 1) { - return result; - } - List<String> list = new ArrayList<String>(); - list.addAll(elementsNames); - builder.append(list.get(0)); - builder.append("\treaction_type"); - for (int i = 1; i < list.size(); i++) { - builder.append("\t"); - builder.append(list.get(i)); - } - result.add(builder.toString()); - } else if (parameters.getFileType() == ExportFileType.TAB_SEPARATED) { - if (parameters.getColumns() == null || parameters.getColumns().size() == 0) { - result.add(getOldFormatString(reaction, parameters)); - } else { - result.addAll(getNewFormatString(reaction, parameters)); - } - } else { - throw new InvalidArgumentException("Unsupported file format: " + parameters.getFileType()); - } - return result; - } - - /** - * Creates tab separated string for reaction in old format... - * - * @param reaction - * reaction to transform - * @param parameters - * {@link ExporterParameters export parameters} - * @return export string representation of the reaction - */ - private String getOldFormatString(Reaction reaction, ExporterParameters parameters) { - StringBuilder result = new StringBuilder(); - result.append(reaction.getIdReaction()); - result.append("\t"); - result.append(ReactionLineData.getByReactionType(reaction.getClass())); - result.append("\t"); - result.append("reversible=" + reaction.isReversible()); - result.append("\t"); - if (reaction.getMiriamData().size() > 0) { - result.append("REACTION_MIRIAM_DATA"); - result.append("\t"); - for (MiriamData md : reaction.getMiriamData()) { - result.append("data_type=" + md.getDataType().getUris().get(0)); - result.append("\t"); - result.append("relation_type=" + md.getRelationType().getStringRepresentation()); - result.append("\t"); - result.append("resource=" + md.getResource()); - result.append("\t"); - } - } - for (Reactant reactant : reaction.getReactants()) { - result.append("REACTANT"); - result.append("\t"); - result.append(getExportSingleSpecies((Species) reactant.getElement(), parameters)); - result.append("\t"); - } - for (Product product : reaction.getProducts()) { - result.append("PRODUCT"); - result.append("\t"); - result.append(getExportSingleSpecies((Species) product.getElement(), parameters)); - result.append("\t"); - } - for (Modifier modifier : reaction.getModifiers()) { - result.append("MODIFIER"); - result.append("\t"); - result.append("type=" + modifier.getClass().getSimpleName()); - result.append("\t"); - result.append(getExportSingleSpecies((Species) modifier.getElement(), parameters)); - result.append("\t"); - } - return result.toString(); - } - - /** - * Creates tab separated string for reaction in new format... - * - * @param reaction - * reaction to transform - * @param parameters - * {@link ExporterParameters export parameters} - * @return export string representation of the reaction - */ - private List<String> getNewFormatString(Reaction reaction, ExporterParameters parameters) { - List<String> result = new ArrayList<String>(); - StringBuilder builder = new StringBuilder(); - boolean splitReaction = false; - for (ExportColumn column : parameters.getColumns()) { - if (column.getClazz().isAssignableFrom(Reaction.class)) { - if (column.equals(ExportColumn.REACTION_ID)) { - builder.append(reaction.getIdReaction()); - } else if (column.equals(ExportColumn.REACTION_REVERSIBLE)) { - builder.append(reaction.isReversible()); - } else if (column.equals(ExportColumn.REACTION_TYPE)) { - builder.append(ReactionLineData.getByReactionType(reaction.getClass())); - } else if (column.equals(ExportColumn.REACTION_TWO_ELEMENT)) { - splitReaction = true; - continue; - } else { - throw new InvalidArgumentException("Unknown column type: " + column); - } - } - builder.append("\t"); - } - for (MiriamType mt : parameters.getMiriamTypes()) { - String types = ""; - for (MiriamData md : reaction.getMiriamData()) { - if (md.getDataType().equals(mt)) { - if (!types.isEmpty()) { - types += ","; - } - types += md.getResource(); - } - } - if (types.equals("")) { - builder.append(mt.toString() + ": N/A"); - } else { - builder.append(mt.toString() + ": " + types); - } - builder.append("\t"); - } - - int counter = 0; - - if (splitReaction) { - String base = builder.toString(); - Set<Element> inputs = new HashSet<Element>(); - Set<Element> outputs = new HashSet<Element>(); - for (Reactant reactant : reaction.getReactants()) { - if (!isAssignable(parameters, reactant.getElement())) { - continue; - } - if (reactant.getElement() instanceof Complex) { - inputs.addAll(((Complex) (reactant.getElement())).getAllChildren()); - } else { - inputs.add(reactant.getElement()); - } - } - for (Product product : reaction.getProducts()) { - if (!isAssignable(parameters, product.getElement())) { - continue; - } - if (product.getElement() instanceof Complex) { - outputs.addAll(((Complex) (product.getElement())).getAllChildren()); - } else { - outputs.add(product.getElement()); - } - } - for (Modifier modifier : reaction.getModifiers()) { - if (!isAssignable(parameters, modifier.getElement())) { - continue; - } - if (modifier.getElement() instanceof Complex) { - inputs.addAll(((Complex) (modifier.getElement())).getAllChildren()); - } else { - inputs.add(modifier.getElement()); - } - } - for (Element input : inputs) { - if (!isAssignable(parameters, input)) { - continue; - } - for (Element output : outputs) { - if (!isAssignable(parameters, output)) { - continue; - } - String line = base + "\t" + getId(input) + "\t" + getId(output); - result.add(line); - } - } - } else { - - for (Reactant reactant : reaction.getReactants()) { - if (!isAssignable(parameters, reactant.getElement())) { - continue; - } - counter++; - - builder.append("REACTANT"); - builder.append("\t"); - builder.append(getId(reactant.getElement())); - builder.append("\t"); - } - for (Product product : reaction.getProducts()) { - if (!isAssignable(parameters, product.getElement())) { - continue; - } - counter++; - - builder.append("PRODUCT"); - builder.append("\t"); - builder.append(getId(product.getElement())); - builder.append("\t"); - } - for (Modifier modifier : reaction.getModifiers()) { - if (!isAssignable(parameters, modifier.getElement())) { - continue; - } - counter++; - - builder.append("MODIFIER"); - builder.append("\t"); - builder.append(getId(modifier.getElement())); - builder.append("\t"); - } - if (counter > 1) { - result.add(builder.toString()); - } - } - return result; - } - - /** - * Checks if element is acceptable by {@link ExporterParameters export - * parameters}. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @param element - * element to check - * @return true if element can exist in export string, <code>false</code> - * otherwise - */ - private boolean isAssignable(ExporterParameters parameters, Element element) { - boolean ok = false; - for (Class<?> clazz : parameters.getTypes()) { - if (clazz.isAssignableFrom(element.getClass())) { - ok = true; - } - } - return ok; - } - - /** - * Creates tab separated string for species for full mode. - * - * @param alias - * alias of the element to transform - * @param parameters - * {@link ExporterParameters export parameters} - * @return export string representation of the element - */ - private String getExportSingleSpecies(Species alias, ExporterParameters parameters) { - if (parameters.getFileType() != ExportFileType.TAB_SEPARATED) { - throw new InvalidArgumentException("Unsupported file format: " + parameters.getFileType()); - } - StringBuilder sb = new StringBuilder(); - sb.append(alias.getClass().getSimpleName() + "\t"); - sb.append("id=" + getId(alias) + "\t"); - sb.append("name=" + alias.getName() + "\t"); - sb.append("fullName=" + alias.getFullName() + "\t"); - sb.append("charge=" + alias.getCharge() + "\t"); - sb.append("initialAmount=" + alias.getInitialAmount() + "\t"); - sb.append("initialConcentration=" + alias.getInitialConcentration() + "\t"); - String compartment = null; - if (alias.getCompartment() != null) { - compartment = alias.getCompartment().getName(); - } - sb.append("compartment=" + compartment); - sb.append("\t"); - String complexId = null; - if (alias.getComplex() != null) { - complexId = getId(alias.getComplex()); - } - sb.append("complex=" + complexId); - sb.append("\t"); - sb.append("hasOnlySubstanceUnits=" + alias.hasOnlySubstanceUnits()); - sb.append("\t"); - sb.append("positionToCompartment=" + alias.getPositionToCompartment()); - sb.append("\t"); - - if (alias instanceof Protein) { - Protein protein = (Protein) alias; - sb.append("dimer=" + protein.getHomodimer()); - sb.append("\t"); - sb.append("structuralState=" + protein.getStructuralState()); - sb.append("\t"); - sb.append("hypothetical=" + protein.isHypothetical()); - sb.append("\t"); - for (ModificationResidue mr : protein.getModificationResidues()) { - sb.append("MODIFICATION RESIDUE"); - sb.append("\t"); - sb.append("angle=" + mr.getAngle()); - sb.append("\t"); - sb.append("name=" + mr.getName()); - if (mr.getState() != null) { - sb.append("\t"); - sb.append("shortName=" + mr.getState().getAbbreviation()); - } - sb.append("\t"); - sb.append("side=" + mr.getSide()); - sb.append("\t"); - sb.append("size=" + mr.getSize()); - sb.append("\t"); - if (mr.getState() != null) { - sb.append("state=" + mr.getState().getFullName() + "\t"); - } - } - } else if (alias instanceof Gene) { - Gene gene = (Gene) alias; - for (ModificationResidue mr : gene.getModificationResidues()) { - sb.append("MODIFICATION RESIDUE"); - sb.append("\t"); - sb.append("angle=" + mr.getAngle()); - sb.append("\t"); - sb.append("name=" + mr.getName()); - sb.append("\t"); - if (mr.getState() != null) { - sb.append("shortName=" + mr.getState().getAbbreviation() + "\t"); - } - sb.append("side=" + mr.getSide()); - sb.append("\t"); - sb.append("size=" + mr.getSize()); - sb.append("\t"); - if (mr.getState() != null) { - sb.append("state=" + mr.getState().getFullName() + "\t"); - } - } - } else if (alias instanceof Complex) { - Complex complex = (Complex) alias; - sb.append("dimer=" + complex.getHomodimer()); - sb.append("\t"); - sb.append("hypothetical=" + complex.isHypothetical()); - sb.append("\t"); - } else if (alias instanceof SimpleMolecule) { - SimpleMolecule molecule = (SimpleMolecule) alias; - sb.append("dimer=" + molecule.getHomodimer()); - sb.append("\t"); - } else if (alias instanceof Ion) { - sb.append(""); - } else if (alias instanceof Phenotype) { - sb.append(""); - } else if (alias instanceof Drug) { - sb.append(""); - - } else if (alias instanceof Rna) { - Rna rna = (Rna) alias; - for (RnaRegion region : rna.getRegions()) { - sb.append("RNA_REGION"); - sb.append("\t"); - sb.append("type=" + region.getType()); - sb.append("\t"); - if (region.getState() != null) { - sb.append("state=" + region.getState().getFullName() + "\t"); - } - sb.append("\t"); - sb.append("size=" + region.getSize()); - sb.append("\t"); - sb.append("pos=" + region.getPos()); - sb.append("\t"); - } - } else if (alias instanceof AntisenseRna) { - AntisenseRna antisenseRna = (AntisenseRna) alias; - for (AntisenseRnaRegion region : antisenseRna.getRegions()) { - sb.append("RNA_REGION" + "\t"); - if (region.getType() != null) { - sb.append("type=" + region.getType().getName() + "\t"); - } - sb.append("name=" + region.getName() + "\t"); - sb.append("size=" + region.getSize() + "\t"); - sb.append("pos=" + region.getPos() + "\t"); - } - } else if (alias instanceof Unknown) { - sb.append(""); - } else if (alias instanceof Degraded) { - sb.append(""); - } else { - throw new InvalidArgumentException("Invalid class type: " + alias.getClass()); - } - if (alias.getMiriamData().size() > 0) { - sb.append("MIRIAM_DATA"); - sb.append("\t"); - for (MiriamData md : alias.getMiriamData()) { - sb.append("data_type=" + md.getDataType().getUris().get(0)); - sb.append("\t"); - sb.append("relation_type=" + md.getRelationType().getStringRepresentation()); - sb.append("\t"); - sb.append("resource=" + md.getResource()); - sb.append("\t"); - } - } - return sb.toString(); - } - - @Override - public void exportReactions(ExporterParameters parameters) throws IOException { - String content = getExportInteractionString(parameters); - writeToFile(content, parameters.getFileName()); - } - - @Override - public void exportCompartments(ExporterParameters parameters) throws IOException { - String content = getExportCompartmentsString(parameters); - writeToFile(content, parameters.getFileName()); - } - - @Override - public String getExportCompartmentsString(ExporterParameters parameters) { - Set<String> uniqeLines = new HashSet<>(); - StringBuilder builder = new StringBuilder(); - for (Model model : parameters.getModels()) { - for (Compartment alias : model.getCompartments()) { - - // check where it lays - boolean toBeProcessed = (parameters.getIncludedAliases().size() == 0); - for (Compartment ca : parameters.getIncludedAliases()) { - if (ca.cross(alias)) { - toBeProcessed = true; - } - } - for (Compartment ca : parameters.getExcludedAliases()) { - if (ca.cross(alias)) { - toBeProcessed = false; - } - } - if (toBeProcessed) { - List<String> elements = getExportStringForOneCompartment(alias, parameters); - for (String string : elements) { - uniqeLines.add(string); - } - } - } - } - List<String> sortedElements = new ArrayList<>(); - sortedElements.addAll(uniqeLines); - Collections.sort(sortedElements); - for (String string : sortedElements) { - builder.append(string); - builder.append("\n"); - } - return builder.toString(); - } - - /** - * Exports compartments into set of text lines defining a compartment. - * - * @param compartment - * alias of the compartment to export - * @param parameters - * parameters used in export - * @return string srepresenting compartment in export format - */ - private List<String> getExportStringForOneCompartment(Compartment compartment, ExporterParameters parameters) { - List<String> result = new ArrayList<String>(); - if (parameters.getFileType() != ExportFileType.TAB_SEPARATED) { - throw new InvalidArgumentException("Unhandled file type: " + parameters.getFileType()); - } - StringBuilder sb = new StringBuilder(); - sb.append(getId(compartment) + "\t"); - sb.append("name=" + compartment.getName() + "\t"); - sb.append("fullname=" + compartment.getFullName() + "\t"); - if (compartment.getMiriamData().size() > 0) { - sb.append("MIRIAM_DATA"); - sb.append("\t"); - for (MiriamData md : compartment.getMiriamData()) { - sb.append("data_type=" + md.getDataType().getUris().get(0)); - sb.append("\t"); - sb.append("relation_type=" + md.getRelationType().getStringRepresentation()); - sb.append("\t"); - sb.append("resource=" + md.getResource()); - sb.append("\t"); - } - } - result.add(sb.toString()); - return result; - } - - /** - * Returns identifier that should be used for a given alias. - * - * @param alias - * alis for which identifier will be returned - * @return identifier that should be used for a given alias - */ - private String getId(Element alias) { - return alias.getElementId(); - } - - @Override - public List<PubmedAnnotatedElementsView> getPublicationModelSummary(Model model) { - List<PubmedAnnotatedElementsView> result = pubmedPublicationsForModel.get(model.getId()); - if (result != null) { - return result; - } else { - result = new ArrayList<>(); - pubmedPublicationsForModel.put(model.getId(), result); - } - List<Model> models = new ArrayList<>(); - models.add(model); - models.addAll(model.getSubmodels()); - Set<MiriamData> miriamDatas = new HashSet<>(); - for (Model m : models) { - for (Element element : m.getElements()) { - for (MiriamData md : element.getMiriamData()) { - if (MiriamType.PUBMED.equals(md.getDataType())) { - miriamDatas.add(md); - } - } - } - for (Reaction reaction : m.getReactions()) { - for (MiriamData md : reaction.getMiriamData()) { - if (MiriamType.PUBMED.equals(md.getDataType())) { - miriamDatas.add(md); - } - } - } - } - for (MiriamData miriamData : miriamDatas) { - result.add(pubmedAnnotatedElementsViewFactory.create(miriamData, model)); - } - return result; - } - -} diff --git a/service/src/main/java/lcsb/mapviewer/services/interfaces/IExporterService.java b/service/src/main/java/lcsb/mapviewer/services/interfaces/IExporterService.java deleted file mode 100644 index 1895e4acd5a37704bd29d8fb4bedbda470430366..0000000000000000000000000000000000000000 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/IExporterService.java +++ /dev/null @@ -1,557 +0,0 @@ -package lcsb.mapviewer.services.interfaces; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import lcsb.mapviewer.model.map.MiriamType; -import lcsb.mapviewer.model.map.compartment.Compartment; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.services.utils.data.ExportColumn; -import lcsb.mapviewer.services.utils.data.ExportFileType; -import lcsb.mapviewer.services.view.PubmedAnnotatedElementsView; - -/** - * Interface used for simple export of the {@link Model}. - * - * @author Piotr Gawron - * - */ -public interface IExporterService { - /** - * Class that contains parameters used for exporting data. - * - * @author Piotr Gawron - * - */ - class ExporterParameters { - /** - * File where the export data should be saved. - */ - private String fileName = null; - /** - * List of models from which data is exported. - */ - private List<Model> models = new ArrayList<Model>(); - /** - * List of compartment names that should be included in export (empty list - * means that everything should be included). - */ - private List<String> included = new ArrayList<String>(); - /** - * List of compartment names that should be excluded in export (empty list - * means that anything should be excluded). - */ - private List<String> excluded = new ArrayList<String>(); - /** - * Type of file to which export should be performed. - */ - private ExportFileType fileType = null; - /** - * Elements that should be exported. - */ - private List<Class<?>> types = new ArrayList<Class<?>>(); - /** - * Columns that should be included in the export data. - */ - private List<ExportColumn> columns = new ArrayList<ExportColumn>(); - /** - * Miriam annotations that should be included in the export data. - */ - private List<MiriamType> miriamTypes = new ArrayList<MiriamType>(); - /** - * List of compartment aliases that should be included in export (empty list - * means that everything should be included). - */ - private Set<Compartment> includedAliases = new HashSet<Compartment>(); - /** - * List of compartment aliases that should be excluded in export (empty list - * means that anything should be excluded). - */ - private Set<Compartment> excludedAliases = new HashSet<Compartment>(); - /** - * If false default complex name will be used for exporting complexes. If - * true then the name will be a concatenated string of all complex - * subspecies. - */ - private boolean complexElementsName = false; - - /** - * Should the molecules be considered as edges. Might be usefull for <a - * href= - * "http://en.wikipedia.org/wiki/Protein%E2%80%93protein_interaction">PPI - * </a>. - */ - private boolean moleculeEdges = false; - - /** - * - */ - public ExporterParameters() { - } - - /** - * Sets {@link #fileName}. - * - * @param fileName - * new {@link #fileName} - * @return object with all parameters set - */ - public ExporterParameters fileName(String fileName) { - this.fileName = fileName; - return this; - } - - /** - * Sets {@link #model}. - * - * @param model - * new {@link #model} - * @return object with all parameters set - */ - public ExporterParameters model(Model model) { - this.models = new ArrayList<Model>(); - models.add(model); - for (Model m : model.getSubmodels()) { - models.add(m); - } - - for (Model modelElement : models) { - includedAliases.clear(); - for (String string : included) { - for (Compartment alias : modelElement.getCompartments()) { - if (alias.getName().equalsIgnoreCase(string)) { - includedAliases.add(alias); - } - } - } - - excludedAliases.clear(); - for (String string : excluded) { - for (Compartment alias : modelElement.getCompartments()) { - if (alias.getName().equalsIgnoreCase(string)) { - excludedAliases.add(alias); - } - } - } - } - return this; - } - - /** - * Adds element to {@link #included}. - * - * @param included - * new element in {@link #included} - * @return object with all parameters set - */ - public ExporterParameters included(String included) { - this.included.add(included); - - if (models.size() > 0) { - for (Model model : models) { - for (Compartment alias : model.getCompartments()) { - if (alias.getName().equalsIgnoreCase(included)) { - includedAliases.add(alias); - } - } - } - } - return this; - } - - /** - * Adds elements to {@link #included}. - * - * @param included - * list of elements to add to {@link #included} - * @return object with all parameters set - */ - public ExporterParameters included(List<String> included) { - for (String string : included) { - included(string); - } - return this; - } - - /** - * Adds element to {@link #excluded}. - * - * @param excluded - * new element in {@link #excluded} - * @return object with all parameters set - */ - public ExporterParameters excluded(String excluded) { - this.excluded.add(excluded); - - if (models.size() > 0) { - for (Model model : models) { - for (Compartment alias : model.getCompartments()) { - if (alias.getName().equalsIgnoreCase(excluded)) { - excludedAliases.add(alias); - } - } - } - } - return this; - } - - /** - * Adds element to {@link #excluded}. - * - * @param excluded - * new element in {@link #excluded} - * @return object with all parameters set - */ - public ExporterParameters excluded(Compartment excluded) { - this.excluded.add(excluded.getName()); - - excludedAliases.add(excluded); - return this; - } - - /** - * Adds elements to {@link #excluded}. - * - * @param excluded - * list of elements to add to {@link #excluded} - * @return object with all parameters set - */ - public ExporterParameters excluded(List<String> excluded) { - for (String string : excluded) { - excluded(string); - } - return this; - } - - /** - * Adds elements to {@link #excluded}. - * - * @param excluded - * list of elements to add to {@link #excluded} - * @return object with all parameters set - */ - public ExporterParameters excluded(Collection<Compartment> excluded) { - for (Compartment string : excluded) { - excluded(string); - } - return this; - } - - /** - * Sets {@link #fileType}. - * - * @param fileType - * new {@link #fileType} - * @return object with all parameters set - */ - public ExporterParameters fileType(ExportFileType fileType) { - this.fileType = fileType; - return this; - } - - /** - * Adds type to {@link #types}. - * - * @param type - * new element in {@link #types} - * @return object with all parameters set - */ - public ExporterParameters type(Class<?> type) { - this.types.add(type); - return this; - } - - /** - * Adds types to {@link #types}. - * - * @param types - * new elements in {@link #types} - * @return object with all parameters set - */ - public ExporterParameters type(List<Class<?>> types) { - this.types.addAll(types); - return this; - } - - /** - * Adds column to {@link #columns}. - * - * @param column - * new element in {@link #columns} - * @return object with all parameters set - */ - public ExporterParameters column(ExportColumn column) { - this.columns.add(column); - return this; - } - - /** - * Adds columns to {@link #columns}. - * - * @param columns - * new elements in {@link #columns} - * @return object with all parameters set - */ - public ExporterParameters column(List<ExportColumn> columns) { - this.columns.addAll(columns); - return this; - } - - /** - * Adds {@link MiriamType} to {@link #miriamTypes}. - * - * @param mt - * new element in {@link #miriamTypes} - * @return object with all parameters set - */ - public ExporterParameters miriamType(MiriamType mt) { - this.miriamTypes.add(mt); - return this; - } - - /** - * Adds {@link MiriamType miriam types} to {@link #miriamTypes list}. - * - * @param mts - * new elements in {@link #miriamTypes} - * @return object with all parameters set - */ - public ExporterParameters miriamTypes(List<MiriamType> mts) { - this.miriamTypes.addAll(mts); - return this; - } - - /** - * Adds columns to {@link #columns}. - * - * @param columns2 - * new elements in {@link #columns} - * @return object with all parameters set - */ - public ExporterParameters column(ExportColumn[] columns2) { - for (ExportColumn exportColumn : columns2) { - column(exportColumn); - } - return this; - } - - /** - * Sets {@link #moleculeEdges}. - * - * @param moleculeEdges - * new {@link #moleculeEdges} - * @return object with all parameters set - */ - public ExporterParameters moleculeEdges(boolean moleculeEdges) { - this.moleculeEdges = moleculeEdges; - return this; - } - - /** - * Adds elements to {@link #included}. - * - * @param included - * list of elements to add to {@link #included} - * @return object with all parameters set - */ - public ExporterParameters included(String[] included) { - for (String string : included) { - included(string); - } - return this; - } - - /** - * Adds elements to {@link #excluded}. - * - * @param excluded - * list of elements to add to {@link #excluded} - * @return object with all parameters set - */ - public ExporterParameters excluded(String[] excluded) { - for (String string : excluded) { - excluded(string); - } - return this; - } - - /** - * Sets {@link #complexElementsName}. - * - * @param complexElementsName - * new {@link #complexElementsName} - * @return object with all parameters set - */ - public ExporterParameters complexElementsName(boolean complexElementsName) { - this.complexElementsName = complexElementsName; - return this; - } - - /** - * @return the fileName - * @see #fileName - */ - public String getFileName() { - return fileName; - } - - /** - * @return the model - * @see #model - */ - public List<Model> getModels() { - return models; - } - - /** - * @return the included - * @see #included - */ - public List<String> getIncluded() { - return included; - } - - /** - * @return the excluded - * @see #excluded - */ - public List<String> getExcluded() { - return excluded; - } - - /** - * @return the fileType - * @see #fileType - */ - public ExportFileType getFileType() { - return fileType; - } - - /** - * @return the types - * @see #types - */ - public List<Class<?>> getTypes() { - return types; - } - - /** - * @return the columns - * @see #columns - */ - public List<ExportColumn> getColumns() { - return columns; - } - - /** - * @return the includedAliases - * @see #includedAliases - */ - public Set<Compartment> getIncludedAliases() { - return includedAliases; - } - - /** - * @return the excludedAliases - * @see #excludedAliases - */ - public Set<Compartment> getExcludedAliases() { - return excludedAliases; - } - - /** - * @return the complexElementsName - * @see #complexElementsName - */ - public boolean isComplexElementsName() { - return complexElementsName; - } - - /** - * @return the moleculeEdges - * @see #moleculeEdges - */ - public boolean isMoleculeEdges() { - return moleculeEdges; - } - - /** - * @return the miriamTypes - * @see #miriamTypes - */ - public List<MiriamType> getMiriamTypes() { - return miriamTypes; - } - - } - - /** - * Export species using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @throws IOException - * thrown when there is a problem with accessing output file - */ - void exportSpecies(ExporterParameters parameters) throws IOException; - - /** - * Returns string with all species exported using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @return String representing exported data - */ - String getExportSpeciesString(ExporterParameters parameters); - - /** - * Returns string with all compartments exported using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @return String representing exported data - */ - String getExportCompartmentsString(ExporterParameters parameters); - - /** - * Export compartments using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @throws IOException - * thrown when there is a problem with accessing output file - */ - void exportCompartments(ExporterParameters parameters) throws IOException; - - /** - * Export reactions using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @throws IOException - * thrown when there is a problem with accessing output file - */ - void exportReactions(ExporterParameters parameters) throws IOException; - - /** - * Returns string with all reactions exported using given parameters. - * - * @param parameters - * {@link ExporterParameters export parameters} - * @return String representing exported data - */ - String getExportInteractionString(ExporterParameters parameters); - - /** - * Returns list of publciations for given model. - * - * @param model - * model for which list of publication will be returned - * @return list of publciations for given model - */ - List<PubmedAnnotatedElementsView> getPublicationModelSummary(Model model); -} diff --git a/service/src/main/resources/applicationContext-service.xml b/service/src/main/resources/applicationContext-service.xml index 08cd8531e2246247b3f680f3332ab4f3cda736a6..45127552d31965209f09675d98bacc199f830e7b 100644 --- a/service/src/main/resources/applicationContext-service.xml +++ b/service/src/main/resources/applicationContext-service.xml @@ -19,8 +19,6 @@ <bean id="DrugService" class="lcsb.mapviewer.services.search.db.drug.DrugService"/> - <bean id="ExporterService" class="lcsb.mapviewer.services.impl.ExporterService"/> - <bean id="ExternalServicesService" class="lcsb.mapviewer.services.impl.ExternalServicesService"/> <bean id="LayoutService" class="lcsb.mapviewer.services.impl.LayoutService"/> diff --git a/service/src/test/java/lcsb/mapviewer/services/impl/AllImplServiceTests.java b/service/src/test/java/lcsb/mapviewer/services/impl/AllImplServiceTests.java index c190d180cf2a44b141055a62e0e74d30f093ea28..a5f016714de7a25ea45263a08df7156d9e3993eb 100644 --- a/service/src/test/java/lcsb/mapviewer/services/impl/AllImplServiceTests.java +++ b/service/src/test/java/lcsb/mapviewer/services/impl/AllImplServiceTests.java @@ -9,7 +9,6 @@ import org.junit.runners.Suite.SuiteClasses; ConfigurationServiceTest.class, // DataMiningServiceTest.class, // DataMiningServiceTest2.class, // - ExporterServiceTest.class, // ExternalServicesServiceTest.class, // LayoutServiceTest.class, // Md5PasswordEncoderTest.class, // diff --git a/service/src/test/java/lcsb/mapviewer/services/impl/ExporterServiceTest.java b/service/src/test/java/lcsb/mapviewer/services/impl/ExporterServiceTest.java deleted file mode 100644 index e65556f1044b7cf57849214923809461a2670647..0000000000000000000000000000000000000000 --- a/service/src/test/java/lcsb/mapviewer/services/impl/ExporterServiceTest.java +++ /dev/null @@ -1,540 +0,0 @@ -package lcsb.mapviewer.services.impl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.apache.log4j.Logger; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -import lcsb.mapviewer.commands.CreateHierarchyCommand; -import lcsb.mapviewer.converter.model.celldesigner.reaction.ReactionLineData; -import lcsb.mapviewer.model.map.compartment.Compartment; -import lcsb.mapviewer.model.map.model.Model; -import lcsb.mapviewer.model.map.reaction.Reaction; -import lcsb.mapviewer.model.map.reaction.ReactionNode; -import lcsb.mapviewer.model.map.species.Complex; -import lcsb.mapviewer.model.map.species.GenericProtein; -import lcsb.mapviewer.model.map.species.Protein; -import lcsb.mapviewer.model.map.species.Species; -import lcsb.mapviewer.services.ServiceTestFunctions; -import lcsb.mapviewer.services.interfaces.IExporterService; -import lcsb.mapviewer.services.interfaces.IExporterService.ExporterParameters; -import lcsb.mapviewer.services.utils.data.ExportColumn; -import lcsb.mapviewer.services.utils.data.ExportFileType; -import lcsb.mapviewer.services.view.PubmedAnnotatedElementsView; - -public class ExporterServiceTest extends ServiceTestFunctions { - Logger logger = Logger.getLogger(ExporterServiceTest.class); - @Autowired - IExporterService exporter2; - - ExporterService exporter; - - @Before - public void setUp() throws Exception { - exporter = (ExporterService) exporter2; - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testGetExportSpeciesStringForTabSeparatedFile() throws Exception { - try { - Model model = getModelForFile("testFiles/export_with_artifitial_comp.xml", false); - ExportColumn columns[] = { ExportColumn.NAME, ExportColumn.ID, ExportColumn.COMPONENT_NAME, - ExportColumn.COMPARTMENT_NAME, ExportColumn.TYPE }; - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED); - - // full export (all elements) - String string = exporter.getExportSpeciesString(params); - String lines[] = string.split("\n"); - assertTrue("Not enough elements in the result file", lines.length > 1); - - String response = lines[0]; - String textColumns[] = response.split("\t"); - assertEquals(5, textColumns.length); - - // only proteins for self-made modules - new CreateHierarchyCommand(model, 8, 80).execute(); - string = exporter.getExportSpeciesString(params.type(GenericProtein.class)); - lines = string.split("\n"); - assertTrue(lines.length > 1); - - boolean differenceBetweenComponentCompartmnet = false; - int lineCount = 0; - for (String string2 : lines) { - if (lineCount == 0) { - textColumns = string2.split("\t"); - assertEquals(ExportColumn.TYPE.getTitle(), textColumns[4]); - } else { - textColumns = string2.split("\t"); - assertEquals(GenericProtein.class.getSimpleName(), textColumns[4]); - if (!textColumns[2].equalsIgnoreCase(textColumns[3])) { - differenceBetweenComponentCompartmnet = true; - } - } - lineCount++; - } - assertTrue(differenceBetweenComponentCompartmnet); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testDuplicatesInGetExportSpeciesString() throws Exception { - try { - Model model = getModelForFile("testFiles/export_duplicate_elements.xml", false); - ExportColumn columns[] = { ExportColumn.NAME, ExportColumn.COMPONENT_NAME, ExportColumn.TYPE }; - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED); - - // full export (all elements) - String string = exporter.getExportSpeciesString(params); - - String lines[] = string.split("\n"); - Set<String> uniqueNames = new HashSet<String>(); - for (String string2 : lines) { - uniqueNames.add(string2); - } - assertEquals(uniqueNames.size(), lines.length); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetExportStringForOneSpecies_Complex() { - try { - Model model = getModelForFile("testFiles/export/hsp70.xml", true); - - Species alias = (Species) model.getElementByElementId("csa1"); - ExportColumn columns[] = { ExportColumn.NAME }; - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).// - column(columns).// - complexElementsName(false).// - fileType(ExportFileType.TAB_SEPARATED); - - List<String> elements = exporter.getExportStringForOneSpecies(alias, params); - assertTrue(elements.contains("HSP70:chaperon")); - - // now the results are concatenated children - params.complexElementsName(true); - - elements = exporter.getExportStringForOneSpecies(alias, params); - assertTrue(elements.size() > 0); - String string = elements.get(0); - assertFalse(string.equals("HSP70:chaperon")); - assertTrue(string.contains("HSP70")); - assertTrue(string.contains("STUB1")); - assertTrue(string.contains("DNAJB2")); - assertTrue(string.contains("BAG1")); - - // and now check complexes with mix of proteins and molecules - alias = (Species) model.getElementByElementId("csa3"); - - elements = exporter.getExportStringForOneSpecies(alias, params); - assertTrue(elements.size() > 0); - string = elements.get(0); - assertTrue(string.contains("UBA1")); - assertTrue(string.contains("UBA6")); - assertTrue(string.contains("AMP")); - - elements = exporter.getExportStringForOneSpecies(alias, params.type(Protein.class)); - assertTrue(elements.size() > 0); - string = elements.get(0); - assertTrue(string.contains("UBA1")); - assertTrue(string.contains("UBA6")); - assertFalse(string.contains("AMP")); - - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occured"); - } - } - - @Test - public void testGetExportStringForOneReaction() throws Exception { - try { - Model model = getModelForFile("testFiles/export/reaction.xml", false); - Set<Compartment> aliases = new HashSet<>(); - for (Compartment alias : model.getCompartments()) { - if (alias.getName().equalsIgnoreCase("Dopamine loaded synaptic vesicle")) - aliases.add(alias); - } - - Reaction reaction = model.getReactionByReactionId("re2"); - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).// - fileType(ExportFileType.SIF).// - type(Protein.class).// - type(Complex.class).// - fileType(ExportFileType.SIF).// - complexElementsName(false).// - excluded(aliases); - - String string = exporter.getExportSingleInteractionString(reaction, params).get(0); - assertNotNull(string); - assertTrue(string.contains("Calcium channel:RIMBP:RIM:MUNC13:RAB")); - assertTrue(string.contains("CSP:Hsc70:SGT")); - assertTrue(string.contains("SNCA")); - assertTrue(string.contains("trans-SNARE complex")); - assertFalse(string.contains("VAMP2")); - - params = new IExporterService.ExporterParameters().model(model).fileType(ExportFileType.SIF).type(Complex.class) - .type(Protein.class); - string = exporter.getExportSingleInteractionString(reaction, params).get(0); - assertTrue(string.contains("VAMP2")); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - - } - - @Test - public void testGetExportStringForOneReactionInGeneral() { - try { - Model model = getModelForFile("testFiles/export/reaction.xml", true); - Reaction reaction = model.getReactionByReactionId("re2"); - ExporterParameters params = new IExporterService.ExporterParameters().model(model).// - fileType(ExportFileType.SIF).// - complexElementsName(false).// - type(Complex.class).type(Protein.class); - String string = exporter.getExportSingleInteractionString(reaction, params).get(0); - assertNotNull(string); - assertTrue(string.contains("Calcium channel:RIMBP:RIM:MUNC13:RAB")); - assertTrue(string.contains("CSP:Hsc70:SGT")); - assertTrue(string.contains("SNCA")); - assertTrue(string.contains("trans-SNARE complex")); - assertTrue(string.contains("SNAP25")); - assertFalse(string.contains("RAB3A")); - - params.complexElementsName(true); - string = exporter.getExportSingleInteractionString(reaction, params).get(0); - assertFalse(string.contains("trans-SNARE complex")); - assertTrue(string.contains("RAB3A")); - - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occured"); - } - - } - - @Test - public void testGetExportStringForOneReactionInGeneralWithOnlyOneElement() throws Exception { - try { - Model model = getModelForFile("testFiles/export/reaction.xml", false); - Reaction reaction = model.getReactionByReactionId("re11"); - model.removeReactions(model.getReactions()); - model.addReaction(reaction); - Set<Compartment> aliases = new HashSet<>(); - aliases.add((Compartment) model.getElementByElementId("ca1")); - ExporterParameters params = new IExporterService.ExporterParameters().model(model).fileType(ExportFileType.SIF) - .included("dopamine loaded synaptic vesicle"); - assertEquals(0, exporter.getExportSingleInteractionString(reaction, params).size()); - - params = new IExporterService.ExporterParameters().model(model).fileType(ExportFileType.SIF); - assertEquals(1, exporter.getExportSingleInteractionString(reaction, params).size()); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetExportSingleInteractionString_text() { - try { - Model model = getModelForFile("testFiles/export_with_artifitial_comp.xml", false); - for (Reaction reaction : model.getReactions()) { - String str = exporter - .getExportSingleInteractionString(reaction, - new IExporterService.ExporterParameters().fileType(ExportFileType.TAB_SEPARATED).type(Species.class)) - .get(0); - - List<String> strings = new ArrayList<String>(); - strings.add(ReactionLineData.getByReactionType(reaction.getClass()).getCellDesignerString()); - strings.add(reaction.getIdReaction()); - for (ReactionNode node : reaction.getReactionNodes()) { - strings.add(node.getElement().getName()); - - } - for (String string : strings) { - assertTrue( - "Description \"" + str + "\" doesn't contain " + string + ". reactionId=" + reaction.getIdReaction(), - str.contains(string)); - } - } - - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occured"); - } - } - - @Test - public void testGetExportComponentAndCompartments() throws Exception { - try { - Model model = getModelForFile("testFiles/export/export_model.xml", false); - new CreateHierarchyCommand(model, 7, 80).execute(); - ExporterParameters params = new IExporterService.ExporterParameters().model(model) - .fileType(ExportFileType.TAB_SEPARATED).column(ExportColumn.NAME).column(ExportColumn.COMPONENT_NAME) - .column(ExportColumn.COMPARTMENT_NAME); - - List<String> list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa1"), params); - String desc = list.get(0); - String compartment = desc.split("\t")[2]; - assertEquals("c1", compartment); - String component = desc.split("\t")[1]; - assertEquals("Test3", component); - - list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa2"), params); - desc = null; - for (String string : list) { - if (string.contains("Test 1")) { - desc = string; - } - } - compartment = desc.split("\t")[2]; - assertEquals("c2", compartment); - component = desc.split("\t")[1]; - assertEquals("Test 1", component); - - for (String string : list) { - if (string.contains("Test2")) { - desc = string; - } - } - compartment = desc.split("\t")[2]; - assertEquals("c2", compartment); - component = desc.split("\t")[1]; - assertEquals("Test2", component); - - for (String string : list) { - if (string.contains("Test3")) { - desc = string; - } - } - compartment = desc.split("\t")[2]; - assertEquals("c2", compartment); - component = desc.split("\t")[1]; - assertEquals("Test3", component); - - list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa3"), params); - boolean ok = false; - for (String string : list) { - if (string.split("\t")[2].equals("null") && string.split("\t")[1].equals("Test2")) { - ok = true; - } - } - assertTrue(ok); - - list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa4"), params); - desc = list.get(0); - compartment = desc.split("\t")[2]; - assertEquals("null", compartment); - component = desc.split("\t")[1]; - assertEquals("Test3", component); - - list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa5"), params); - desc = list.get(0); - compartment = desc.split("\t")[2]; - assertEquals("c3", compartment); - component = desc.split("\t")[1]; - assertEquals("null", component); - - list = exporter.getExportStringForOneSpecies((Species) model.getElementByElementId("sa6"), params); - desc = list.get(0); - compartment = desc.split("\t")[2]; - assertEquals("null", compartment); - component = desc.split("\t")[1]; - assertEquals("null", component); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetExportSpeciesStringForTabSeparatedFile2() throws Exception { - try { - Model model = getModelForFile("testFiles/export_with_artifitial_comp.xml", false); - ExportColumn columns[] = { ExportColumn.NAME, ExportColumn.ID, ExportColumn.COMPONENT_NAME, - ExportColumn.COMPARTMENT_NAME, ExportColumn.TYPE }; - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED).type(Object.class); - - // full export (all elements) - String string = exporter.getExportSpeciesString(params); - String lines[] = string.split("\n"); - assertTrue("Not enough elements in the result file", lines.length > 1); - - String response = lines[0]; - String textColumns[] = response.split("\t"); - assertEquals(columns.length, textColumns.length); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetExportCompartmentsString() throws Exception { - try { - Model model = getModelForFile("testFiles/export/export_compartments.xml", false); - - ExporterParameters params = new IExporterService.ExporterParameters().model(model) - .fileType(ExportFileType.TAB_SEPARATED).type(Object.class); - - // full export (all compartments) - String string = exporter.getExportCompartmentsString(params); - String lines[] = string.split("\n"); - assertTrue("Not enough elements in the result file", lines.length >= 2); - - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAllExportColumnsSpecies() throws Exception { - try { - Model model = getModelForFile("testFiles/export_with_artifitial_comp.xml", false); - List<ExportColumn> columns = new ArrayList<ExportColumn>(); - for (ExportColumn column : ExportColumn.values()) { - if (column.getClazz().isAssignableFrom(Species.class)) { - columns.add(column); - } - } - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED); - - // full export (all elements) - String string = exporter.getExportSpeciesString(params); - assertNotNull(string); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testAllExportColumnsReaction() throws Exception { - try { - Model model = getModelForFile("testFiles/export_with_artifitial_comp.xml", false); - List<ExportColumn> columns = new ArrayList<ExportColumn>(); - for (ExportColumn column : ExportColumn.values()) { - if (column.getClazz().isAssignableFrom(Reaction.class)) { - columns.add(column); - } - } - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED); - - // full export (all elements) - String string = exporter.getExportInteractionString(params); - assertNotNull(string); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testGetTabSeparatedReaction() throws Exception { - try { - Model model = getModelForFile("testFiles/export/reaction.xml", false); - Set<Compartment> aliases = new HashSet<>(); - for (Compartment alias : model.getCompartments()) { - if (alias.getName().equalsIgnoreCase("Dopamine loaded synaptic vesicle")) - aliases.add(alias); - } - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).// - fileType(ExportFileType.TAB_SEPARATED).// - column(ExportColumn.REACTION_ID).// - column(ExportColumn.REACTION_TYPE).// - type(Species.class); - - String string = exporter.getExportInteractionString(params); - assertTrue(string.indexOf("STATE_TRANSITION REACTANT") >= 0); - assertTrue(string.indexOf("sa7") >= 0); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - - } - - @Test - public void test() throws Exception { - try { - Model model = getModelForFile("testFiles/export/new_line_file.xml", false); - List<ExportColumn> columns = new ArrayList<ExportColumn>(); - for (ExportColumn column : ExportColumn.values()) { - if (column.getClazz().isAssignableFrom(Species.class)) { - columns.add(column); - } - } - - ExporterParameters params = new IExporterService.ExporterParameters().model(model).column(columns) - .fileType(ExportFileType.TAB_SEPARATED); - - // full export (all elements) - String string = exporter.getExportSpeciesString(params); - String tmp[] = string.split("\n"); - for (String string2 : tmp) { - assertTrue(string2, columns.size() <= string2.split("\t", -1).length); - } - assertNotNull(string); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } - - @Test - public void testPubmedSummary() throws Exception { - try { - Model model = getModelForFile("testFiles/export/reaction.xml", false); - List<PubmedAnnotatedElementsView> result = exporter.getPublicationModelSummary(model); - assertNotNull(result); - for (PubmedAnnotatedElementsView pubmedAnnotatedElementsView : result) { - assertNotNull(pubmedAnnotatedElementsView.getArticle()); - assertNotNull(pubmedAnnotatedElementsView.getArticle().getTitle()); - assertTrue(pubmedAnnotatedElementsView.getElements().size() > 0); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - } -}