001/**
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Sep 15, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.align.gui.autosuggest;
026
027import org.biojava.nbio.structure.scop.ScopDatabase;
028import org.biojava.nbio.structure.scop.ScopDescription;
029import org.biojava.nbio.structure.scop.ScopDomain;
030import org.biojava.nbio.structure.scop.ScopFactory;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.Vector;
035import java.util.concurrent.atomic.AtomicBoolean;
036
037public class SCOPAutoSuggestProvider implements AutoSuggestProvider{
038
039        boolean DEBUG = false;
040
041        int maxResults = 20;
042
043        AtomicBoolean stop = new AtomicBoolean(false);
044
045        @Override
046        public Vector<String> getSuggestion(String userInput) {
047
048                long timeS = System.currentTimeMillis();
049
050                List<ScopDomain> domains = new ArrayList<ScopDomain>();
051
052                domains = getPossibleScopDomains(userInput);
053
054
055
056                // convert domains to Strings
057
058                Vector<String> v=new Vector<String>();
059
060                int counter = 0;
061                for ( ScopDomain d : domains){
062                        counter ++;
063
064                        String scopId = d.getScopId();
065                        v.add(scopId);
066
067
068                        if ( counter > maxResults)
069                                break;
070                }
071
072                long timeE = System.currentTimeMillis();
073
074                if ( DEBUG)
075                        System.out.println("ScopAutoSuggestProvider took " + (timeE - timeS) + " ms. to get " + v.size() + " suggestions");
076
077                return v;
078
079        }
080
081
082
083        private List<ScopDomain> getPossibleScopDomains(String userInput) {
084
085                List<ScopDomain> domains = new ArrayList<ScopDomain>();
086
087                ScopDatabase scop = ScopFactory.getSCOP();
088
089                if (userInput.length() ==5 && userInput.startsWith("d") && (! userInput.contains("."))) {
090                        userInput = userInput.substring(1);
091                }
092
093                if ( userInput.length() ==4){
094                        domains = scop.getDomainsForPDB(userInput);
095
096                } else {
097                        int suni = -1;
098
099                        try {
100                                suni = Integer.parseInt(userInput);
101                        } catch (NumberFormatException e){
102                                //supress
103                        }
104
105                        if ( stop.get())
106                                return domains;
107
108                        if ( suni != -1)
109                                domains = scop.getScopDomainsBySunid(suni);
110
111                        if ( stop.get())
112                                return domains;
113
114                        if ( domains == null || domains.size() < 1){
115
116                                if ( userInput.length() > 5){
117                                        // e.g. d4hhba
118
119                                        domains.addAll(scop.filterByDomainName(userInput));
120
121                                }
122                        }
123
124                        if ( stop.get())
125                                return domains;
126
127                        if (DEBUG)
128                                System.out.println("domains: " + domains);
129
130                        if ( domains == null || domains.size() < 1) {
131                                if ( userInput.length() > 0 ){
132                                        List<ScopDescription> descs = scop.filterByClassificationId(userInput);
133
134                                        if ( descs == null || descs.size() < 1){
135                                                descs = scop.filterByDescription(userInput);
136                                        }
137
138
139                                        for (ScopDescription d : descs){
140                                                domains.addAll(scop.getScopDomainsBySunid(d.getSunID()));
141                                                if ( domains.size()> maxResults){
142                                                        break;
143                                                }
144
145                                                if ( stop.get())
146                                                        return domains;
147                                        }
148                                }
149
150                        }
151
152                }
153
154                return domains;
155        }
156
157
158
159
160
161        @Override
162        public void setMaxNrSuggestions(int maxNrSuggestions) {
163                maxResults = maxNrSuggestions;
164
165        }
166
167        @Override
168        public int getMaxNrSuggestions() {
169                return maxResults;
170        }
171
172        @Override
173        public void clear() {
174                // TODO Auto-generated method stub
175
176        }
177
178        @Override
179        public void stop() {
180                stop.set(true);
181                if (DEBUG)
182                        System.out.println("ScopAutoSuggestProvider got signal stop");
183
184        }
185
186
187
188}