From 845c275e2ef4f547c92bb699502eb650c5e0e868 Mon Sep 17 00:00:00 2001 From: Matt Prestegaard Date: Mon, 29 May 2017 04:41:53 -0500 Subject: [PATCH] Fixed CWD logic and now makes use of IMGT files automatically --- .../ars/AntigenRecognitionSiteLoader.java | 4 +- .../valid/cwd/CommonWellDocumentedLoader.java | 123 ++++++++++-------- .../org/dash/valid/gl/GLStringUtilities.java | 31 +---- .../report/CommonWellDocumentedWriter.java | 14 +- .../LinkageDisequilibriumAnalyzerTest.java | 20 +-- .../cwd/CommonWellDocumentedLoaderTest.java | 17 +-- 6 files changed, 90 insertions(+), 119 deletions(-) diff --git a/ld-validation/src/main/java/org/dash/valid/ars/AntigenRecognitionSiteLoader.java b/ld-validation/src/main/java/org/dash/valid/ars/AntigenRecognitionSiteLoader.java index e6b8893..56c568c 100644 --- a/ld-validation/src/main/java/org/dash/valid/ars/AntigenRecognitionSiteLoader.java +++ b/ld-validation/src/main/java/org/dash/valid/ars/AntigenRecognitionSiteLoader.java @@ -84,7 +84,9 @@ public static AntigenRecognitionSiteLoader getInstance() throws IOException, Inv catch (IOException | ParserConfigurationException | SAXException e) { LOGGER.info("Couldn't find IMGT file in the correct format for hladb: " + hladb); instance.init(); - System.setProperty(GLStringConstants.HLADB_PROPERTY, "Default"); + + // TODO: Make final determination - commenting this in messes up the CWD logic currently + //System.setProperty(GLStringConstants.HLADB_PROPERTY, "Default"); } } diff --git a/ld-validation/src/main/java/org/dash/valid/cwd/CommonWellDocumentedLoader.java b/ld-validation/src/main/java/org/dash/valid/cwd/CommonWellDocumentedLoader.java index f091e70..6db1d15 100644 --- a/ld-validation/src/main/java/org/dash/valid/cwd/CommonWellDocumentedLoader.java +++ b/ld-validation/src/main/java/org/dash/valid/cwd/CommonWellDocumentedLoader.java @@ -25,42 +25,32 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; -import java.util.ArrayList; +import java.net.URL; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; +import java.util.zip.ZipInputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import org.dash.valid.gl.GLStringConstants; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; public class CommonWellDocumentedLoader { private static final Logger LOGGER = Logger.getLogger(CommonWellDocumentedLoader.class.getName()); - private static final String NOT_APPLICABLE = "NA"; private static CommonWellDocumentedLoader instance = null; private Set cwdAlleles; - - private HashMap cwdByAccession; - private HashMap> hlaDbByAccession; - - public HashMap getCwdByAccession() { - return cwdByAccession; - } - - private void setCwdByAccession(HashMap cwdByAccession) { - this.cwdByAccession = cwdByAccession; - } - - public HashMap> getHlaDbByAccession() { - return hlaDbByAccession; - } - - private void setHlaDbByAccession(HashMap> hlaDbByAccession) { - this.hlaDbByAccession = hlaDbByAccession; - } + private HashMap accessionMap; private CommonWellDocumentedLoader(String hladb) { init(hladb); @@ -85,29 +75,67 @@ private void init(String hladb) { } - public void loadCommonWellDocumentedAlleles(String hladb) throws IOException, FileNotFoundException { - String filename = "reference/CWD.txt"; + public HashMap loadFromIMGT(String hladb) throws IOException, ParserConfigurationException, SAXException { + HashMap accessionMap = new HashMap(); + + if (hladb == null) hladb = GLStringConstants.LATEST_HLADB; + URL url = new URL("https://raw.githubusercontent.com/ANHIG/IMGTHLA/" + hladb.replace(GLStringConstants.PERIOD, GLStringConstants.EMPTY_STRING) + "/xml/hla.xml.zip"); + + ZipInputStream zipStream = new ZipInputStream(url.openStream()); + zipStream.getNextEntry(); + BufferedReader reader = new BufferedReader(new InputStreamReader(zipStream)); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(reader); + Document doc = builder.parse(is); + + String name; + String accession; + + NodeList nList = doc.getElementsByTagName("allele"); + for (int i=0;i cwdSet = new HashSet(); + HashMap accessionMap = null; + boolean accessionLoaded = false; if (hladb == null) hladb = GLStringConstants.LATEST_HLADB; + + try { + accessionMap = loadFromIMGT(hladb); + } + catch (IOException | ParserConfigurationException | SAXException e) { + LOGGER.info("Could not load file from IMGT for hladb: " + hladb); + } - HashMap cwdByAccession = new HashMap(); - HashMap> hlaDbByAccession = new HashMap>(); - List hladbs; - Set cwdSet = new HashSet(); + if (accessionMap != null && accessionMap.size() > 0) { + accessionLoaded = true; + } + else { + accessionMap = new HashMap(); + } + + String filename = "reference/CWD.txt"; BufferedReader reader = new BufferedReader(new InputStreamReader(CommonWellDocumentedLoader.class.getClassLoader().getResourceAsStream(filename))); String row; String[] columns; - List headers = null; int idx = 0; int hladbIdx = -1; - + List headers = null; + while ((row = reader.readLine()) != null) { - hladbs = new ArrayList(); columns = row.split(GLStringConstants.TAB); - cwdByAccession.put(columns[0], GLStringConstants.HLA_DASH + columns[1]); - + if (idx < 1) { headers = Arrays.asList(columns); @@ -119,22 +147,17 @@ public void loadCommonWellDocumentedAlleles(String hladb) throws IOException, Fi } } else { - cwdSet.add(GLStringConstants.HLA_DASH + columns[hladbIdx]); - } - - for (int i=0;i cwdAlleles) { this.cwdAlleles = cwdAlleles; } - public String getAccessionByAllele(String allele) { - if (!getCwdByAccession().containsValue(allele)) { - return null; - } - - for (String key : getCwdByAccession().keySet()) { - if (getCwdByAccession().get(key).equals(allele)) { - return key; - } - } - - return null; + public HashMap getAccessionMap() { + return this.accessionMap; } - public List getHlaDbsByAccession(String accession) { - return getHlaDbByAccession().get(accession); + private void setAccessionMap(HashMap accessionMap) { + this.accessionMap = accessionMap; } } diff --git a/ld-validation/src/main/java/org/dash/valid/gl/GLStringUtilities.java b/ld-validation/src/main/java/org/dash/valid/gl/GLStringUtilities.java index e907264..2034ee1 100644 --- a/ld-validation/src/main/java/org/dash/valid/gl/GLStringUtilities.java +++ b/ld-validation/src/main/java/org/dash/valid/gl/GLStringUtilities.java @@ -166,44 +166,27 @@ public static boolean validateGLStringFormat(String glString) { public static Set checkCommonWellDocumented(String glString) { Set notCommon = new HashSet(); + + CommonWellDocumentedLoader loader = CommonWellDocumentedLoader.getInstance(); + + HashMap accessionMap = loader.getAccessionMap(); - Set cwdAlleles = CommonWellDocumentedLoader.getInstance() - .getCwdAlleles(); + Set cwdAlleles = loader.getCwdAlleles(); StringTokenizer st = new StringTokenizer(glString, GL_STRING_DELIMITER_REGEX); String token; while (st.hasMoreTokens()) { token = st.nextToken(); - - if (!checkCommonWellDocumented(cwdAlleles, token)) { + + if (!cwdAlleles.contains(accessionMap.get(token))) { notCommon.add(token); } - } return notCommon; } - /** - * @param cwdAlleles - * @param token - */ - private static boolean checkCommonWellDocumented(Set cwdAlleles, - String allele) { - if (cwdAlleles.contains(allele)) { - return true; - } - - for (String cwdAllele : cwdAlleles) { - if (allele.equals(cwdAllele)) { - return true; - } - } - - return false; - } - public static boolean fieldLevelComparison(String allele, String referenceAllele) { if (allele == null || referenceAllele == null) { diff --git a/ld-validation/src/main/java/org/dash/valid/report/CommonWellDocumentedWriter.java b/ld-validation/src/main/java/org/dash/valid/report/CommonWellDocumentedWriter.java index 077d978..237b87a 100644 --- a/ld-validation/src/main/java/org/dash/valid/report/CommonWellDocumentedWriter.java +++ b/ld-validation/src/main/java/org/dash/valid/report/CommonWellDocumentedWriter.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.util.logging.Logger; -import org.dash.valid.cwd.CommonWellDocumentedLoader; import org.dash.valid.gl.GLStringConstants; import org.dash.valid.handler.CommonWellDocumentedFileHandler; @@ -63,18 +62,13 @@ public static String formatCommonWellDocumented( StringBuffer sb = new StringBuffer("Id: " + findings.getGLId() + GLStringConstants.NEWLINE + "GL String: " + findings.getGLString()); sb.append(GLStringConstants.NEWLINE + GLStringConstants.NEWLINE + "HLA DB Version: " + findings.getHladb() + GLStringConstants.NEWLINE); - CommonWellDocumentedLoader loader = CommonWellDocumentedLoader.getInstance(); - String accession; - for (String allele : findings.getNonCWDAlleles()) { - sb.append("WARNING - Allele: " + allele + " not in the CWD list for HLA DB: " + findings.getHladb()); - accession = loader.getAccessionByAllele(allele); - if (accession != null) { - sb.append(" (Found under accession: " + accession + " in these HLA DBs: " + - loader.getHlaDbsByAccession(accession) + ")"); - } + sb.append("WARNING - Allele: " + allele + " not in the CWD list. "); sb.append(GLStringConstants.NEWLINE); } + + sb.append(GLStringConstants.NEWLINE); + return sb.toString(); } } diff --git a/ld-validation/src/test/java/org/dash/LinkageDisequilibriumAnalyzerTest.java b/ld-validation/src/test/java/org/dash/LinkageDisequilibriumAnalyzerTest.java index 11adb88..7276cd3 100644 --- a/ld-validation/src/test/java/org/dash/LinkageDisequilibriumAnalyzerTest.java +++ b/ld-validation/src/test/java/org/dash/LinkageDisequilibriumAnalyzerTest.java @@ -21,14 +21,7 @@ */ package org.dash; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.ProtocolException; -import java.net.URL; import java.util.LinkedHashMap; import java.util.List; @@ -42,7 +35,6 @@ import org.dash.valid.gl.LinkageDisequilibriumGenotypeList; import org.dash.valid.gl.haplo.Haplotype; import org.dash.valid.report.DetectedLinkageFindings; -import org.junit.Ignore; import org.junit.Test; import org.nmdp.gl.MultilocusUnphasedGenotype; @@ -110,15 +102,11 @@ public void testPhasedGenotypeList() throws IOException { @Test public void testLinkageReportingInlineGLString() throws IOException { - //String fullyQualified = GLStringUtilities.fullyQualifyGLString("HLA-A*11:01:01+HLA-A*24:02:01:01/HLA-A*24:02:01:02L/HLA-A*24:02:01:03^HLA-B*18:01:01:01/HLA-B*18:01:01:02/HLA-B*18:51+HLA-B*53:01:01^HLA-C*04:01:01:01/HLA-C*04:01:01:02/HLA-C*04:01:01:03/HLA-C*04:01:01:04/HLA-C*04:01:01:05/HLA-C*04:20/HLA-C*04:117+HLA-C*12:03:01:01/HLA-C*12:03:01:02/HLA-C*12:34^HLA-DPA1*01:03:01:01/HLA-DPA1*01:03:01:02/HLA-DPA1*01:03:01:03/HLA-DPA1*01:03:01:04/HLA-DPA1*01:03:01:05+HLA-DPA1*02:01:01^HLA-DPB1*02:01:02+HLA-DPB1*09:01^HLA-DQA1*01:02:01:01/HLA-DQA1*01:02:01:02/HLA-DQA1*01:02:01:03/HLA-DQA1*01:02:01:04/HLA-DQA1*01:11+HLA-DQA1*03:01:01^HLA-DQB1*03:05:01+HLA-DQB1*06:09^HLA-DRB1*11:04:01+HLA-DRB1*13:02:01^HLA-DRB3*02:02:01:01/HLA-DRB3*02:02:01:02+HLA-DRB3*03:01:01"); - - String fullyQualified = GLStringUtilities.fullyQualifyGLString("HLA-A*24:02:01:01~HLA-C*04:01:01:06~HLA-B*35:02:01~HLA-DRB3*02:02:01:02~HLA-DRB1*11:01:01:01~HLA-DQA1*05:05:01:01/HLA-DQA1*05:05:01:02~HLA-DQB1*03:01:01:03~HLA-DPA1*01:03:01:01~HLA-DPB1*05:01:01+HLA-A*11:01:01:01~HLA-C*12:03:01:01~HLA-B*35:03:01~HLA-DRB3*02:02:01:01~HLA-DRB1*13:01:01:01/HLA-DRB1*13:01:01:02~HLA-DQA1*01:03:01:02~HLA-DQB1*06:03:01~HLA-DPA1*02:01:01:01~HLA-DPB1*13:01:01/HLA-DPB1*107:01"); - LinkageDisequilibriumGenotypeList genotypeList = new LinkageDisequilibriumGenotypeList("SBCFMW0003", fullyQualified); - //MultilocusUnphasedGenotype mug = GLStringUtilities.convertToMug(fullyQualified); - //DetectedLinkageFindings findings = LinkageDisequilibriumAnalyzer.detectLinkages(mug); - - DetectedLinkageFindings findings = LinkageDisequilibriumAnalyzer.detectLinkages(genotypeList); + String fullyQualified = GLStringUtilities.fullyQualifyGLString("HLA-A*11:01:01+HLA-A*24:02:01:01/HLA-A*24:02:01:02L/HLA-A*24:02:01:03^HLA-B*18:01:01:01/HLA-B*18:01:01:02/HLA-B*18:51+HLA-B*53:01:01^HLA-C*04:01:01:01/HLA-C*04:01:01:02/HLA-C*04:01:01:03/HLA-C*04:01:01:04/HLA-C*04:01:01:05/HLA-C*04:20/HLA-C*04:117+HLA-C*12:03:01:01/HLA-C*12:03:01:02/HLA-C*12:34^HLA-DPA1*01:03:01:01/HLA-DPA1*01:03:01:02/HLA-DPA1*01:03:01:03/HLA-DPA1*01:03:01:04/HLA-DPA1*01:03:01:05+HLA-DPA1*02:01:01^HLA-DPB1*02:01:02+HLA-DPB1*09:01^HLA-DQA1*01:02:01:01/HLA-DQA1*01:02:01:02/HLA-DQA1*01:02:01:03/HLA-DQA1*01:02:01:04/HLA-DQA1*01:11+HLA-DQA1*03:01:01^HLA-DQB1*03:05:01+HLA-DQB1*06:09^HLA-DRB1*11:04:01+HLA-DRB1*13:02:01^HLA-DRB3*02:02:01:01/HLA-DRB3*02:02:01:02+HLA-DRB3*03:01:01"); + MultilocusUnphasedGenotype mug = GLStringUtilities.convertToMug(fullyQualified); + DetectedLinkageFindings findings = LinkageDisequilibriumAnalyzer.detectLinkages(mug); + assertNotNull(findings); } } diff --git a/ld-validation/src/test/java/org/dash/valid/cwd/CommonWellDocumentedLoaderTest.java b/ld-validation/src/test/java/org/dash/valid/cwd/CommonWellDocumentedLoaderTest.java index 351cd9c..54a9055 100644 --- a/ld-validation/src/test/java/org/dash/valid/cwd/CommonWellDocumentedLoaderTest.java +++ b/ld-validation/src/test/java/org/dash/valid/cwd/CommonWellDocumentedLoaderTest.java @@ -23,12 +23,11 @@ import java.io.FileNotFoundException; import java.io.IOException; -import java.util.List; - -import junit.framework.TestCase; import org.junit.Test; +import junit.framework.TestCase; + public class CommonWellDocumentedLoaderTest extends TestCase { private static final String DQA10111 = "HLA-DQA1*01:11"; private static final String HLA08433 = "HLA08433"; @@ -42,16 +41,8 @@ public void test() { @Test public void testLoadAllCWD() throws FileNotFoundException, IOException { CommonWellDocumentedLoader cwdLoader = CommonWellDocumentedLoader.getInstance(); - List hladbs; - assertTrue(cwdLoader.getCwdByAccession().containsValue(DQA10111)); - for (String key : cwdLoader.getCwdByAccession().keySet()) { - if (cwdLoader.getCwdByAccession().get(key).equals(DQA10111)) { - assertTrue(key.equals(HLA08433)); - hladbs = cwdLoader.getHlaDbByAccession().get(key); - assertNotNull(hladbs); - break; - } - } + assertTrue(cwdLoader.getAccessionMap().containsKey(DQA10111)); + assertTrue(cwdLoader.getAccessionMap().get(DQA10111).equals(HLA08433)); } }