001/*
002 $Id: SearchModification.java 4485 2013-06-10 15:00:28Z fredrik $
003
004 */
005package org.proteios.core;
006
007import org.proteios.core.data.SearchModificationData;
008import org.proteios.core.query.Hql;
009import org.proteios.core.query.Restrictions;
010
011import java.util.ArrayList;
012import java.util.Iterator;
013import java.util.List;
014import java.util.Set;
015
016/**
017 * This class represent searchModifications.
018 * 
019 * @author Fredrik
020 * @version 2.0
021 */
022public class SearchModification
023                extends Modification<SearchModificationData>
024{
025        /**
026         * The type of item represented by this class.
027         * 
028         * @see Item#PROTEIOS_SEARCHMODIFICATION
029         * @see #getType()
030         */
031        public static final Item TYPE = Item.PROTEIOS_SEARCHMODIFICATION;
032
033
034        /**
035         * Get a query that returns searchModification items.
036         * 
037         * @return An {@link ItemQuery} object.
038         */
039        public static ItemQuery<SearchModification> getQuery()
040        {
041                return new ItemQuery<SearchModification>(SearchModification.class);
042        }
043
044
045        SearchModification(SearchModificationData searchModificationData)
046        {
047                super(searchModificationData);
048        }
049
050
051        /*
052         * From the Identifiable interface
053         * -------------------------------------------
054         */
055        public Item getType()
056        {
057                return TYPE;
058        }
059
060
061        // -------------------------------------------
062        /**
063         * Always null.
064         */
065        public Set<Annotatable> getAnnotatableParents()
066                        throws BaseException
067        {
068                return null;
069        }
070
071
072        /*
073         * From the BasicItem class -------------------------------------------
074         */
075        /**
076         * Check that:
077         * <ul>
078         * <li>no {@link Sample} has been created from this searchModification
079         * </ul>
080         */
081        @Override
082        public boolean isUsed()
083                        throws BaseException
084        {
085                return false;
086        }
087
088
089        /**
090         * Get the {@link SpectrumSearch} this SearchModification is associated
091         * with.
092         * 
093         * @return The <code>SpectrumSearch</code> item or null if not known
094         * @throws BaseException If there is another error
095         */
096        public SpectrumSearch getSpectrumSearch()
097                        throws BaseException
098        {
099                return getDbControl().getItem(SpectrumSearch.class,
100                        getData().getSpectrumSearch());
101        }
102
103
104        /**
105         * Set the {@link SpectrumSearch} this SearchModification is associated
106         * with.
107         * 
108         * @param spectrumSearch The new <code>SpectrumSearch</code> item
109         * @throws BaseException If there is another error
110         */
111        public void setSpectrumSearch(SpectrumSearch spectrumSearch)
112                        throws BaseException
113        {
114                getData().setSpectrumSearch(spectrumSearch.getData());
115        }
116
117        /**
118         * This enum is used to verify terminal specificities. In the data layer the
119         * stored values are int values representing the ordinal in the enum.
120         * Additions of new values to this list has to be at the end of the link, or
121         * existing database entries will turn incorrect.
122         */
123        public enum TerminalSpecificity
124        {
125                none,
126                peptide_N_terminus,
127                peptide_C_terminus,
128                protein_N_terminus,
129                protein_C_terminus
130        }
131
132
133        /**
134         * The enum representation of the int value in the database is returned. The
135         * String representation of the TerminalSpecificity can be obtained by
136         * toString().
137         * 
138         * @return terminalSpecificity The terminal specificity
139         */
140        public TerminalSpecificity getTerminalSpecificity()
141        {
142                TerminalSpecificity retVal = TerminalSpecificity.none;
143                int ordinal = getData().getTerminalSpecificity();
144                for (TerminalSpecificity t : TerminalSpecificity.values())
145                {
146                        if (t.ordinal() == ordinal)
147                                retVal = t;
148                }
149                return retVal;
150        }
151
152
153        /**
154         * The terminalSpecificity is converted to the ordinal in the enum before it
155         * is sent to the data layer.
156         * 
157         * @param terminalSpecificity The terminalSpecificity to set.
158         */
159        public void setTerminalSpecificity(TerminalSpecificity terminalSpecificity)
160        {
161                this.getData().setTerminalSpecificity(terminalSpecificity.ordinal());
162        }
163
164
165        /**
166         * @return Returns the fixed modififcation flag
167         */
168        public boolean isFixed()
169        {
170                return getData().isFixed();
171        }
172
173
174        /**
175         * @param fixed Whether the modification is static.
176         */
177        public void setFixed(boolean fixed)
178        {
179                getData().setFixed(fixed);
180        }
181
182        /**
183         * The maximum length of the amino acid specficity that can be stored in the
184         * database.
185         * 
186         * @see #setAminoAcidSpecificity(String)
187         */
188        public static final int MAX_AASPECIFICITY_LENGTH = SearchModificationData.MAX_AASPECIFICITY_LENGTH;
189
190
191        /**
192         * Get the amino acid specificity of the modification. Should be amino acid
193         * one letter codes
194         * 
195         * @return The amino acid for the modification
196         */
197        public String getAminoAcidSpecificity()
198        {
199                return getData().getAminoAcidSpecificity();
200        }
201
202
203        /**
204         * Set the amino acid specificities of the Modification. The amino acids
205         * should be gven as single letter values. The value may be null but must
206         * not be longer than the value specified by the
207         * {@link #MAX_AASPECIFICITY_LENGTH} constant.
208         * 
209         * @param aminoAcidSpecificity The amino acid specificities
210         * @throws PermissionDeniedException If the logged in user doesn't have
211         *         write permission
212         * @throws InvalidDataException If the string is too long
213         */
214        public void setAminoAcidSpecificity(String aminoAcidSpecificity)
215                        throws PermissionDeniedException, InvalidDataException
216        {
217                checkPermission(Permission.WRITE);
218                getData().setAminoAcidSpecificity(
219                        StringUtil.setNullableString(aminoAcidSpecificity,
220                                "aminoAcidSpecificity", MAX_AASPECIFICITY_LENGTH));
221        }
222
223
224        /**
225         * This function will return fixed search modifications for search results
226         * file (if it has been imported)
227         */
228        public static List<SearchModification> getFixedMods(File resultsFile,
229                        DbControl dc, ItemFactory factory)
230        {
231                List<SearchModification> fixedMods = new ArrayList<SearchModification>();
232                ItemQuery<SpectrumSearch> ssQuery = SpectrumSearch.getQuery();
233                ssQuery.restrict(Restrictions.eq(Hql.property("resultFile"),
234                        Hql.entity(resultsFile)));
235                ssQuery.setMaxResults(1);
236                ItemResultIterator<SpectrumSearch> ssit = ssQuery.iterate(dc);
237                if (ssit.hasNext())
238                {
239                        SpectrumSearch ss = (SpectrumSearch) ssit.next();
240                        Iterator<SearchModification> smit = ss.getSearchModifications()
241                                .iterator();
242                        while (smit.hasNext())
243                        {
244                                SearchModification sm = smit.next();
245                                if (sm.isFixed())
246                                {
247                                        SearchModification newMod = factory
248                                                .create(SearchModification.class);
249                                        newMod.setMonoisotopicDeltaMass(sm
250                                                .getMonoisotopicDeltaMass());
251                                        newMod
252                                                .setAminoAcidSpecificity(sm.getAminoAcidSpecificity());
253                                        newMod.setTerminalSpecificity(sm.getTerminalSpecificity());
254                                        newMod.setDiffFormula(sm.getDiffFormula());
255                                        newMod.setFixed(true);
256                                        newMod.setName(sm.getName());
257                                        if (sm.getName().contains("@"))
258                                        {
259                                                newMod.setName(getModNameFromTandem(sm.getName()));
260                                        }
261                                        else if (sm.getName().contains(" "))
262                                        {
263                                                newMod.setName(sm.getName().substring(0,
264                                                        sm.getName().indexOf(" ")));
265                                        }
266                                        fixedMods.add(newMod);
267                                }
268                        }
269                }
270                return fixedMods;
271        }
272}