|
| 1 | +package chebi.service; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.FileWriter; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.io.OutputStreamWriter; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Iterator; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Properties; |
| 20 | + |
| 21 | +import uk.ac.ebi.chebi.webapps.chebiWS.client.ChebiWebServiceClient; |
| 22 | +import uk.ac.ebi.chebi.webapps.chebiWS.model.ChebiWebServiceFault_Exception; |
| 23 | +import uk.ac.ebi.chebi.webapps.chebiWS.model.*; |
| 24 | + |
| 25 | +public class GetChEBIbyNames { |
| 26 | + |
| 27 | + private static String absPath = System.getProperty("user.dir"); |
| 28 | + |
| 29 | + public static void main(String[] args) { |
| 30 | + |
| 31 | + GetChEBIbyNames chebi = new GetChEBIbyNames(); |
| 32 | + |
| 33 | + String configFilePath = absPath + "/mappings/PT-UNII-ChEBI-mapping/ChEBIJavaClient/mappings/config.properties"; |
| 34 | + |
| 35 | + try { |
| 36 | + |
| 37 | + HashMap<String, String> properties = chebi.readConfigs(configFilePath); |
| 38 | + String inputPath = absPath +"/"+ properties.get("input"); |
| 39 | + String outputPath = absPath +"/"+ properties.get("output"); |
| 40 | + |
| 41 | + List<String> drugL = readDrugListFile(inputPath); |
| 42 | + Map<String, String> mappingD = getMappingsD(drugL); |
| 43 | + printToFile(mappingD, outputPath); |
| 44 | + |
| 45 | + } catch (IOException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public static Map<String, String> getMappingsD(List<String> drugL) { |
| 52 | + Map<String, String> mappingD = new HashMap<String, String>(); |
| 53 | + |
| 54 | + for (String drug : drugL) { |
| 55 | + if (!drug.isEmpty()) { |
| 56 | + System.out.println(drug); |
| 57 | + String chebiURI = getChEBIByName(drug); |
| 58 | + if (!chebiURI.isEmpty()) |
| 59 | + mappingD.put(drug.trim(), ("http://purl.obolibrary.org/obo/"+chebiURI.replace(":","_"))); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return mappingD; |
| 64 | + } |
| 65 | + |
| 66 | + public HashMap<String, String> readConfigs(String propFilePath) throws IOException { |
| 67 | + |
| 68 | + try { |
| 69 | + HashMap<String, String> result = new HashMap<String, String>(); |
| 70 | + |
| 71 | + File file = new File(propFilePath); |
| 72 | + FileInputStream fileInput = new FileInputStream(file); |
| 73 | + Properties properties = new Properties(); |
| 74 | + properties.load(fileInput); |
| 75 | + fileInput.close(); |
| 76 | + |
| 77 | + String input = properties.getProperty("input"); |
| 78 | + String output = properties.getProperty("output"); |
| 79 | + |
| 80 | + if (!input.isEmpty() && !output.isEmpty()) { |
| 81 | + |
| 82 | + result.put("input", input); |
| 83 | + result.put("output", output); |
| 84 | + |
| 85 | + return result; |
| 86 | + } else { |
| 87 | + System.out.println("Property no found - input: " + input + " | output: " + output); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + } catch (Exception e) { |
| 92 | + System.out.println("Exception: " + e); |
| 93 | + } |
| 94 | + return null; |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public static String getChEBIByName(String drugname) { |
| 99 | + |
| 100 | + String chebi = ""; |
| 101 | + |
| 102 | + try { |
| 103 | + |
| 104 | + ChebiWebServiceClient client = new ChebiWebServiceClient(); |
| 105 | + LiteEntityList entities = client.getLiteEntity(drugname, SearchCategory.ALL, 50, StarsCategory.ALL); |
| 106 | + List<LiteEntity> resultList = entities.getListElement(); |
| 107 | + |
| 108 | + for (LiteEntity liteEntity : resultList) { |
| 109 | + if (drugname.toLowerCase().equals(liteEntity.getChebiAsciiName().toLowerCase())) { |
| 110 | + chebi = liteEntity.getChebiId(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + } catch (ChebiWebServiceFault_Exception e) { |
| 115 | + System.err.println(e.getMessage()); |
| 116 | + } |
| 117 | + return chebi; |
| 118 | + } |
| 119 | + |
| 120 | + public static List<String> readDrugListFile(String filePath) { |
| 121 | + |
| 122 | + List<String> drugL = new ArrayList<String>(); |
| 123 | + |
| 124 | + BufferedReader br; |
| 125 | + try { |
| 126 | + br = new BufferedReader(new FileReader(filePath)); |
| 127 | + |
| 128 | + while (true) { |
| 129 | + |
| 130 | + String line = br.readLine(); |
| 131 | + |
| 132 | + if (line == null) { |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + if (!line.isEmpty()) |
| 137 | + drugL.add(line.trim()); |
| 138 | + } |
| 139 | + br.close(); |
| 140 | + |
| 141 | + } catch (Exception e) { |
| 142 | + e.printStackTrace(); |
| 143 | + } |
| 144 | + return drugL; |
| 145 | + } |
| 146 | + |
| 147 | + // write string into file of specified format |
| 148 | + public static void printToFile(Map<String, String> mappingD, String outputPath) { |
| 149 | + File file = new File(outputPath); |
| 150 | + |
| 151 | + try { |
| 152 | + |
| 153 | + if (!file.exists()) { |
| 154 | + file.createNewFile(); |
| 155 | + } |
| 156 | + |
| 157 | + FileOutputStream fos = new FileOutputStream(file); |
| 158 | + |
| 159 | + Iterator it = mappingD.entrySet().iterator(); |
| 160 | + |
| 161 | + FileWriter fw = new FileWriter(file); |
| 162 | + BufferedWriter bw = new BufferedWriter(fw); |
| 163 | + |
| 164 | + while (it.hasNext()) { |
| 165 | + Map.Entry pair = (Map.Entry) it.next(); |
| 166 | + bw.write(pair.getKey() + "\t" + pair.getValue() + "\n"); |
| 167 | + } |
| 168 | + bw.close(); |
| 169 | + } catch (IOException e) { |
| 170 | + e.printStackTrace(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments