001package org.biopax.paxtools.pattern;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.Named;
005
006import java.util.ArrayList;
007import java.util.Collections;
008import java.util.List;
009
010/**
011 * A pattern match is an array of biopax elements that satisfies the list of mapped constraints in a
012 * pattern.
013 *
014 * @author Ozgun Babur
015 */
016public class Match implements Cloneable
017{
018        /**
019         * Array of variables.
020         */
021        private BioPAXElement[] variables;
022
023        /**
024         * Constructor with size.
025         * @param size array size
026         */
027        public Match(int size)
028        {
029                this.variables = new BioPAXElement[size];
030        }
031
032        /**
033         * Getter for the element array.
034         * @return element array
035         */
036        public BioPAXElement[] getVariables()
037        {
038                return variables;
039        }
040
041        /**
042         * Gets element at the index.
043         * @param index index of the element to get
044         * @return element at the index
045         */
046        public BioPAXElement get(int index)
047        {
048                return variables[index];
049        }
050
051        /**
052         * Gets element corresponding to the given label in the pattern.
053         * @param label label of the element in the pattern
054         * @param p related pattern
055         * @return element of the given label
056         * @throws IllegalArgumentException if the label not in the pattern
057         */
058        public BioPAXElement get(String label, Pattern p)
059        {
060                return variables[p.indexOf(label)];
061        }
062
063        /**
064         * Gets elements corresponding to the given labels in the pattern.
065         * @param label labels of the element in the pattern
066         * @param p related pattern
067         * @return elements of the given label
068         * @throws IllegalArgumentException if one of the labels not in the pattern
069         */
070        public List<BioPAXElement> get(String[] label, Pattern p)
071        {
072                if (label == null) return Collections.emptyList();
073
074                List<BioPAXElement> list = new ArrayList<BioPAXElement>(label.length);
075                for (String lab : label)
076                {
077                        list.add(variables[p.indexOf(lab)]);
078                }
079                return list;
080        }
081
082        /**
083         * Gets first element of the match
084         * @return first element
085         */
086        public BioPAXElement getFirst()
087        {
088                return variables[0];
089        }
090
091        /**
092         * Gets last element of the match.
093         * @return last element
094         */
095        public BioPAXElement getLast()
096        {
097                return variables[variables.length - 1];
098        }
099
100        /**
101         * Gets the array size.
102         * @return array size
103         */
104        public int varSize()
105        {
106                return variables.length;
107        }
108
109        /**
110         * Sets the given element to the given index.
111         * @param ele element to set
112         * @param index index to set
113         */
114        public void set(BioPAXElement ele, int index)
115        {
116                variables[index] = ele;
117        }
118
119        /**
120         * Checks if all given indices are assigned.
121         * @param ind indices to check
122         * @return true if none of them are null
123         */
124        public boolean varsPresent(int ... ind)
125        {
126                for (int i : ind)
127                {
128                        if (variables[i] == null) return false;
129                }
130                return true;
131        }
132
133        /**
134         * Clones a match.
135         * @return clone of the match
136         */
137        @Override
138        public Object clone()
139        {
140                Match m = null;
141                try
142                {
143                        m = (Match) super.clone();
144                        m.variables = new BioPAXElement[variables.length];
145                        System.arraycopy(variables, 0, m.variables, 0, variables.length);
146                        return m;
147                }
148                catch (CloneNotSupportedException e)
149                {
150                        throw new RuntimeException("super.clone() not supported.");
151                }
152        }
153
154        /**
155         * Gets name of variables.
156         * @return names of variables
157         */
158        @Override
159        public String toString()
160        {
161                String s = "";
162
163                int  i = 0;
164                for (BioPAXElement ele : variables)
165                {
166                        if (ele != null) s += i + " - " + getAName(ele) + "\n";
167                        i++;
168                }
169                return s;
170        }
171
172        /**
173         * Finds a name for the variable.
174         * @param ele element to check
175         * @return a name
176         */
177        public String getAName(BioPAXElement ele)
178        {
179                String name = null;
180                
181                if (ele instanceof Named)
182                {
183                        Named n = (Named) ele;
184                        if (n.getDisplayName() != null && n.getDisplayName().length() > 0) 
185                                name = n.getDisplayName();
186                        else if (n.getStandardName() != null && n.getStandardName().length() > 0) 
187                                name = n.getStandardName();
188                        else if (!n.getName().isEmpty() && n.getName().iterator().next().length() > 0)
189                                name = n.getName().iterator().next();
190                }
191                if (name == null ) name = ele.getRDFId();
192                
193                return name + " (" + ele.getModelInterface().getName().substring(
194                        ele.getModelInterface().getName().lastIndexOf(".") + 1) + ")";
195        }
196}