001package org.biopax.paxtools.pattern.miner;
002
003import org.biopax.paxtools.controller.PathAccessor;
004import org.biopax.paxtools.model.BioPAXElement;
005import org.biopax.paxtools.model.level3.SimplePhysicalEntity;
006import org.biopax.paxtools.model.level3.SmallMoleculeReference;
007import org.biopax.paxtools.pattern.Match;
008import org.biopax.paxtools.pattern.Pattern;
009import org.biopax.paxtools.pattern.constraint.PathConstraint;
010import org.biopax.paxtools.pattern.constraint.Size;
011
012import java.io.IOException;
013import java.io.OutputStream;
014import java.util.List;
015import java.util.Map;
016
017/**
018 * Miner for getting ubiquitous small molecules in a model.
019 * @author Ozgun Babur
020 */
021public class UbiquitousIDMiner extends MinerAdapter
022{
023        /**
024         * Constructor that sets name and description.
025         */
026        public UbiquitousIDMiner()
027        {
028                super("ubiquitous-molecule-lister", "Finds small molecules that participate in at least " +
029                        "50 Conversions. Writes down IDs of these molecules to the output file, one ID per " +
030                        "line.");
031        }
032
033        /**
034         * Constructs the pattern.
035         * @return pattern
036         */
037        @Override
038        public Pattern constructPattern()
039        {
040                Pattern p = new Pattern(SmallMoleculeReference.class, "SMR");
041                p.add(new Size(
042                        new PathConstraint("SmallMoleculeReference/entityReferenceOf/participantOf:Conversion"),
043                        50, Size.Type.GREATER_OR_EQUAL), "SMR");
044                return p;
045        }
046
047        /**
048         * Writes the result as "A B", where A and B are gene symbols, and whitespace is tab.
049         * @param matches pattern search result
050         * @param out output stream
051         */
052        @Override
053        public void writeResult(Map<BioPAXElement, List<Match>> matches, OutputStream out)
054                throws IOException
055        {
056                writeResultDetailed(matches, out, 1);
057        }
058
059        /**
060         * Gets header of the result file.
061         * @return header
062         */
063        @Override
064        public String getHeader()
065        {
066                return "IDs of ubiquitous elements";
067        }
068
069        /**
070         * Gets the ids of the small molecule reference and its physical entities.
071         * @param m current match
072         * @param col current column
073         * @return ubique IDs
074         */
075        @Override
076        public String getValue(Match m, int col)
077        {
078                assert col == 0;
079
080                return getRelatedIDs((SmallMoleculeReference) m.get("SMR", getPattern()));
081        }
082
083        /**
084         * Gets IDs of the small molecule reference and its physical entities.
085         * @param smr small molecule reference
086         * @return related IDs
087         */
088        private String getRelatedIDs(SmallMoleculeReference smr)
089        {
090                String ids = smr.getRDFId();
091
092                for (Object o : new PathAccessor(
093                        "SmallMoleculeReference/entityReferenceOf").getValueFromBean(smr))
094                {
095                        SimplePhysicalEntity spe = (SimplePhysicalEntity) o;
096                        ids += "\n" + spe.getRDFId();
097                }
098                return ids;
099        }
100}