Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.urls

File IdentifiersUrlProvider.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
13% of files have more coverage

Code metrics

30
66
13
1
266
187
29
0.44
5.08
13
2.23

Classes

Class Line # Actions
IdentifiersUrlProvider 50 66 29
0.85321185.3%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21   
22    package jalview.urls;
23   
24    import static jalview.util.UrlConstants.DB_ACCESSION;
25    import static jalview.util.UrlConstants.DELIM;
26    import static jalview.util.UrlConstants.SEP;
27   
28    import jalview.util.JSONUtils;
29    import jalview.util.UrlLink;
30   
31    import java.io.FileReader;
32    import java.io.IOException;
33    import java.util.ArrayList;
34    import java.util.HashMap;
35    import java.util.Iterator;
36    import java.util.List;
37    import java.util.Map;
38    import java.util.StringTokenizer;
39   
40    import org.json.simple.parser.ParseException;
41   
42    /**
43    *
44    * Implements the UrlProviderI interface for a UrlProvider object which serves
45    * URLs from identifiers.org
46    *
47    * @author $author$
48    * @version $Revision$
49    */
 
50    public class IdentifiersUrlProvider extends UrlProviderImpl
51    {
52   
53    private static final String LOCAL_KEY = "Local";
54   
55    private static final String ID_ORG_KEY = "identifiers.org";
56   
57    // map of string ids to urls
58    private HashMap<String, UrlLink> urls;
59   
60    // list of selected urls
61    private ArrayList<String> selectedUrls;
62   
 
63  42 toggle public IdentifiersUrlProvider(String cachedUrlList)
64    {
65  42 urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
66  42 selectedUrls = new ArrayList<>();
67  42 checkSelectionMatchesUrls(cachedUrlList);
68    }
69   
70    /**
71    * Read data from an identifiers.org download file
72    *
73    * @param idFileName
74    * name of identifiers.org download file
75    * @return hashmap of identifiers.org data, keyed by MIRIAM id
76    */
 
77  42 toggle @SuppressWarnings("unchecked")
78    private HashMap<String, UrlLink> readIdentifiers(String idFileName)
79    {
80    // identifiers.org data
81  42 HashMap<String, UrlLink> idData = new HashMap<>();
82   
83  42 String errorMessage = null;
84  42 try
85    {
86    // NOTE: THIS WILL FAIL IN SWINGJS BECAUSE IT INVOLVES A FILE READER
87  42 FileReader reader = new FileReader(idFileName);
88  42 String key = "";
89  42 Map<String, Object> obj = (Map<String, Object>) JSONUtils
90    .parse(reader);
91  42 if (obj.containsKey(ID_ORG_KEY))
92    {
93  0 key = ID_ORG_KEY;
94    }
95  42 else if (obj.containsKey(LOCAL_KEY))
96    {
97  42 key = LOCAL_KEY;
98    }
99    else
100    {
101  0 jalview.bin.Console.outPrintln(
102    "Unexpected key returned from identifiers jalview service");
103  0 return idData;
104    }
105   
106  42 List<Object> jsonarray = (List<Object>) obj.get(key);
107   
108    // loop over each entry in JSON array and build HashMap entry
109  16960 for (int i = 0; i < jsonarray.size(); i++)
110    {
111  16918 Map<String, Object> item = (Map<String, Object>) jsonarray.get(i);
112   
113  16918 String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
114    + DELIM;
115  16918 UrlLink link = new UrlLink((String) item.get("name"), url,
116    (String) item.get("prefix"));
117  16918 idData.put((String) item.get("id"), link);
118    }
119    } catch (IOException | ParseException e)
120    {
121  0 errorMessage = e.toString();
122  0 idData.clear();
123    }
124    // BH 2018 -- added more valuable report
125  42 if (errorMessage != null)
126    {
127  0 jalview.bin.Console.errPrintln("IdentifiersUrlProvider: cannot read "
128    + idFileName + ": " + errorMessage);
129    }
130  42 return idData;
131    }
132   
 
133  42 toggle private void checkSelectionMatchesUrls(String cachedUrlList)
134    {
135  42 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
136  310 while (st.hasMoreElements())
137    {
138  268 String id = st.nextToken();
139   
140  268 if (isMiriamId(id))
141    {
142    // this is an identifiers.org MIRIAM id
143  44 if (urls.containsKey(id))
144    {
145  44 selectedUrls.add(id);
146    }
147    }
148    }
149   
150    // reset defaultUrl in case it is no longer selected
151  42 setPrimaryUrl(primaryUrl);
152    }
153   
 
154  81 toggle @Override
155    public boolean setPrimaryUrl(String id)
156    {
157  81 if (urls.containsKey(id))
158    {
159  2 primaryUrl = id;
160    }
161    else
162    {
163  79 primaryUrl = null;
164    }
165   
166  81 return urls.containsKey(id);
167    }
168   
 
169  5 toggle @Override
170    public String writeUrlsAsString(boolean selected)
171    {
172  5 if (!selected)
173    {
174  2 return ""; // we don't cache unselected identifiers.org urls
175    }
176   
177  3 StringBuffer links = new StringBuffer();
178  3 if (!selectedUrls.isEmpty())
179    {
180  3 for (String k : selectedUrls)
181    {
182  9 links.append(k);
183  9 links.append(SEP);
184    }
185    // remove last SEP
186  3 links.setLength(links.length() - 1);
187    }
188  3 return links.toString();
189    }
190   
 
191  41 toggle @Override
192    public List<String> getLinksForMenu()
193    {
194  41 List<String> links = new ArrayList<>();
195  41 for (String key : selectedUrls)
196    {
197  15 links.add(urls.get(key).toStringWithTarget());
198    }
199  41 return links;
200    }
201   
 
202  22 toggle @Override
203    public List<UrlLinkDisplay> getLinksForTable()
204    {
205  22 return super.getLinksForTable(urls, selectedUrls, false);
206    }
207   
 
208  9 toggle @Override
209    public void setUrlData(List<UrlLinkDisplay> links)
210    {
211  9 selectedUrls = new ArrayList<>();
212   
213  9 Iterator<UrlLinkDisplay> it = links.iterator();
214  99 while (it.hasNext())
215    {
216  90 UrlLinkDisplay link = it.next();
217   
218    // Handle links with MIRIAM ids only
219  90 if (isMiriamId(link.getId()))
220    {
221    // select/deselect links accordingly and set default url
222  35 if (urls.containsKey(link.getId()))
223    {
224  35 if (link.getIsSelected())
225    {
226  26 selectedUrls.add(link.getId());
227    }
228  35 if (link.getIsPrimary())
229    {
230  0 setPrimaryUrl(link.getId());
231    }
232    }
233    }
234    }
235    }
236   
 
237  7 toggle @Override
238    public String getPrimaryUrl(String seqid)
239    {
240  7 return super.getPrimaryUrl(seqid, urls);
241    }
242   
 
243  5 toggle @Override
244    public String getPrimaryUrlId()
245    {
246  5 return primaryUrl;
247    }
248   
 
249  0 toggle @Override
250    public String getPrimaryTarget(String seqid)
251    {
252  0 return null;
253    }
254   
 
255  1 toggle @Override
256    public String choosePrimaryUrl()
257    {
258  1 return null;
259    }
260   
 
261  38 toggle @Override
262    public boolean contains(String id)
263    {
264  38 return (urls.containsKey(id));
265    }
266    }