/** * The Uniprot mappings to other database identifiers for this sequence * * @return * @throws Exception */ public LinkedHashMap<String, ArrayList<DBReferenceInfo>> getDatabaseReferences() throws Exception { LinkedHashMap<String, ArrayList<DBReferenceInfo>> databaseReferencesHashMap = new LinkedHashMap<String, ArrayList<DBReferenceInfo>>(); if (uniprotDoc == null) { return databaseReferencesHashMap; } Element uniprotElement = uniprotDoc.getDocumentElement(); Element entryElement = XMLHelper.selectSingleElement(uniprotElement, "entry"); ArrayList<Element> dbreferenceElementList = XMLHelper.selectElements(entryElement, "dbReference"); for (Element element : dbreferenceElementList) { String type = element.getAttribute("type"); String id = element.getAttribute("id"); ArrayList<DBReferenceInfo> idlist = databaseReferencesHashMap.get(type); if (idlist == null) { idlist = new ArrayList<DBReferenceInfo>(); databaseReferencesHashMap.put(type, idlist); } DBReferenceInfo dbreferenceInfo = new DBReferenceInfo(type, id); ArrayList<Element> propertyElementList = XMLHelper.selectElements(element, "property"); for (Element propertyElement : propertyElementList) { String propertyType = propertyElement.getAttribute("type"); String propertyValue = propertyElement.getAttribute("value"); dbreferenceInfo.addProperty(propertyType, propertyValue); } idlist.add(dbreferenceInfo); } return databaseReferencesHashMap; }
/** * @param uniprotDoc * @return * @throws Exception */ private String getSequence(Document uniprotDoc) throws Exception { Element uniprotElement = uniprotDoc.getDocumentElement(); Element entryElement = XMLHelper.selectSingleElement(uniprotElement, "entry"); Element sequenceElement = XMLHelper.selectSingleElement(entryElement, "sequence"); String seqdata = sequenceElement.getTextContent(); return seqdata; }
/** * Pull uniprot key words which is a mixed bag of words associated with this sequence * * @return * @throws Exception */ public ArrayList<String> getKeyWords() throws Exception { ArrayList<String> keyWordsList = new ArrayList<String>(); if (uniprotDoc == null) { return keyWordsList; } Element uniprotElement = uniprotDoc.getDocumentElement(); Element entryElement = XMLHelper.selectSingleElement(uniprotElement, "entry"); ArrayList<Element> keyWordElementList = XMLHelper.selectElements(entryElement, "keyword"); for (Element element : keyWordElementList) { keyWordsList.add(element.getTextContent()); } return keyWordsList; }
/** @return */ public AccessionID getAccession() { AccessionID accessionID = new AccessionID(); if (uniprotDoc == null) { return accessionID; } try { Element uniprotElement = uniprotDoc.getDocumentElement(); Element entryElement = XMLHelper.selectSingleElement(uniprotElement, "entry"); Element nameElement = XMLHelper.selectSingleElement(entryElement, "name"); accessionID = new AccessionID(nameElement.getTextContent(), DataSource.UNIPROT); } catch (Exception e) { logger.error("Exeception: ", e); } return accessionID; }
/** * Get the organism name assigned to this sequence * * @return * @throws Exception */ public String getOrganismName() throws Exception { if (uniprotDoc == null) { return ""; } Element uniprotElement = uniprotDoc.getDocumentElement(); Element entryElement = XMLHelper.selectSingleElement(uniprotElement, "entry"); Element organismElement = XMLHelper.selectSingleElement(entryElement, "organism"); if (organismElement == null) { return ""; } Element nameElement = XMLHelper.selectSingleElement(organismElement, "name"); if (nameElement == null) { return ""; } return nameElement.getTextContent(); }
/** * @param accession * @return * @throws Exception */ private Document getUniprotXML(String accession) throws Exception { int index = accession.lastIndexOf("."); String key = accession; if (index != -1) { key = accession.substring(0, index); } StringBuilder sb = new StringBuilder(); File f = null; if (uniprotDirectoryCache != null && uniprotDirectoryCache.length() > 0) { f = new File(uniprotDirectoryCache + File.separatorChar + key + ".xml"); if (f.exists()) { FileReader fr = new FileReader(f); int size = (int) f.length(); char[] data = new char[size]; fr.read(data); fr.close(); sb.append(data); index = sb.indexOf("xmlns="); // strip out name space stuff to make it easier on xpath if (index != -1) { int lastIndex = sb.indexOf(">", index); sb.replace(index, lastIndex, ""); } } } // http://www.uniprot.org/uniprot/?query=SORBIDRAFT_03g027040&format=xml if (sb.length() == 0) { String uniprotURL = getUniprotbaseURL() + "/uniprot/?query=" + key + "&format=xml"; // String uniprotURL = getUniprotbaseURL() + "/uniprot/" + key + ".xml"; logger.info("Loading: {}", uniprotURL); URL uniprot = new URL(uniprotURL); URLConnection uniprotConnection = uniprot.openConnection(); uniprotConnection.setRequestProperty("User-Agent", "BioJava"); BufferedReader in = new BufferedReader(new InputStreamReader(uniprotConnection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } in.close(); index = sb.indexOf("xmlns="); // strip out name space stuff to make it easier on xpath if (index != -1) { int lastIndex = sb.indexOf(">", index); sb.replace(index, lastIndex, ""); } if (f != null) { FileWriter fw = new FileWriter(f); fw.write(sb.toString()); fw.close(); } } logger.info("Load complete"); try { // logger.debug(sb.toString()); Document document = XMLHelper.inputStreamToDocument(new ByteArrayInputStream(sb.toString().getBytes())); return document; } catch (Exception e) { logger.error("Exception on xml parse of: {}", sb.toString()); } return null; }