|
| 1 | +package life.qbic.io; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 6 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.FileReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
| 17 | +import life.qbic.model.petab.MetaInformation; |
| 18 | +import life.qbic.model.petab.PetabMetadata; |
| 19 | + |
| 20 | +public class PetabParser { |
| 21 | + |
| 22 | + private final String META_INFO_YAML = "metaInformation.yaml"; |
| 23 | + |
| 24 | + public PetabMetadata parse(String dataPath) { |
| 25 | + |
| 26 | + File directory = new File(dataPath); |
| 27 | + List<String> sourcePetabReferences = new ArrayList<>(); |
| 28 | + |
| 29 | + File yaml = findYaml(directory); |
| 30 | + if (yaml != null) { |
| 31 | + BufferedReader reader = null; |
| 32 | + try { |
| 33 | + reader = new BufferedReader(new FileReader(yaml)); |
| 34 | + while (true) { |
| 35 | + String line = reader.readLine(); |
| 36 | + if (line == null) { |
| 37 | + break; |
| 38 | + } |
| 39 | + if (line.startsWith("openbisID")) { |
| 40 | + String datasetCode = line.strip().split(": ")[1]; |
| 41 | + sourcePetabReferences.add(datasetCode); |
| 42 | + } |
| 43 | + } |
| 44 | + reader.close(); |
| 45 | + } catch (IOException e) { |
| 46 | + throw new RuntimeException(e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return new PetabMetadata(sourcePetabReferences); |
| 51 | + } |
| 52 | + |
| 53 | + private File findYaml(File directory) { |
| 54 | + for (File file : Objects.requireNonNull(directory.listFiles())) { |
| 55 | + if (file.isFile() && file.getName().equals(META_INFO_YAML)) { |
| 56 | + return file; |
| 57 | + } |
| 58 | + if (file.isDirectory()) { |
| 59 | + return findYaml(file); |
| 60 | + } |
| 61 | + } |
| 62 | + System.out.println(META_INFO_YAML + " not found"); |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * adds key-value pairs to the bottom of a petab.yaml found below the provided path |
| 68 | + * |
| 69 | + * @param outputPath the path of the PEtab |
| 70 | + * @param properties map of properties to add |
| 71 | + */ |
| 72 | + public void addParameters(String outputPath, Map<String, String> properties) { |
| 73 | + |
| 74 | + File directory = new File(outputPath); |
| 75 | + |
| 76 | + File yaml = findYaml(directory); |
| 77 | + if (yaml != null) { |
| 78 | + FileOutputStream fos = null; |
| 79 | + try { |
| 80 | + fos = new FileOutputStream(yaml.getPath(), true); |
| 81 | + for (Map.Entry<String, String> entry : properties.entrySet()) { |
| 82 | + String line = entry.getKey() + ": " + entry.getValue() + "\n"; |
| 83 | + fos.write(line.getBytes()); |
| 84 | + } |
| 85 | + fos.close(); |
| 86 | + } catch (IOException e) { |
| 87 | + throw new RuntimeException(e); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) throws IOException { |
| 93 | + File testfile = new File("/Users/afriedrich/git/openbis-20-scripts/example_petab/metaInformation.yaml"); |
| 94 | + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); |
| 95 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 96 | + MetaInformation metaInfo = mapper.readValue(testfile, MetaInformation.class); |
| 97 | + System.err.println(metaInfo); |
| 98 | + metaInfo.getUnits().setOpenbisParentIds(Arrays.asList("1","2")); |
| 99 | + metaInfo.getUnits().setOpenbisId("3"); |
| 100 | + mapper.writeValue(new File("/Users/afriedrich/git/openbis-20-scripts/Output.yaml"), metaInfo); |
| 101 | + |
| 102 | + |
| 103 | + ObjectMapper mapper2 = new YAMLMapper(); |
| 104 | + MetaInformation metaInfo2 = mapper2.readValue(testfile, MetaInformation.class); |
| 105 | + System.err.println(metaInfo2); |
| 106 | + |
| 107 | + /* |
| 108 | +MetaInformation{ |
| 109 | + units=Units{ |
| 110 | + measurement='some technique', |
| 111 | + time='min, |
| 112 | + t0 = timepoint of first intervention', |
| 113 | + stimulus='something', |
| 114 | + medium=Medium{type='DMEM', volume=1.5, unit='ml'}, |
| 115 | + ncells=CellCountInfo{seeded=0.0, count='null', unit='null'}, |
| 116 | + measurement_technique='immunublotting', |
| 117 | + openBISId='null', openBISParentIds=null, |
| 118 | + dateOfExperiment=[2024-04-30] |
| 119 | + }, |
| 120 | + preprocessingInformation=null, |
| 121 | + measurementData=MeasurementData{ |
| 122 | + measurement=Measurement{ |
| 123 | + unit='intensity (a.u.)', lloq='null'}, |
| 124 | + time=Time{unit='min'}, |
| 125 | + replicateId=IdWithPattern{name='null', pattern='date_gel_replicate'} |
| 126 | + }, |
| 127 | + experimentalCondition=ExperimentalCondition{conditionId=IdWithPattern{name='null', pattern='condition'}, |
| 128 | + conditions=null}} |
| 129 | +
|
| 130 | +
|
| 131 | + ExperimentInformation: |
| 132 | +units: |
| 133 | + measurement: some technique |
| 134 | + time: min, t0 = timepoint of first intervention |
| 135 | + treatment: |
| 136 | + stimulus: something |
| 137 | + medium: |
| 138 | + type: DMEM |
| 139 | + volume: 1.5 |
| 140 | + unit: ml |
| 141 | + ncells: |
| 142 | + seeded: 0.4 |
| 143 | + ncellsCount: ~ |
| 144 | + unit: mio |
| 145 | + measurement_technique: immunublotting |
| 146 | + openBISId: ~ |
| 147 | + dateOfExperiment: |
| 148 | + - 2024-04-30 |
| 149 | +PreprocessingInformation: |
| 150 | + normalizationStatus: Raw (data on linear scale) |
| 151 | + preprocessing: |
| 152 | + method: normalize by blotIt |
| 153 | + arguments: |
| 154 | + housekeeperObservableIds: |
| 155 | + description: |
| 156 | +measurementData: |
| 157 | + measurement: |
| 158 | + unit: intensity (a.u.) |
| 159 | + lloq: ~ |
| 160 | + time: |
| 161 | + unit: min |
| 162 | + replicateId: |
| 163 | + pattern: date_gel_replicate |
| 164 | +experimentalCondition: |
| 165 | + conditionId: |
| 166 | + pattern: condition |
| 167 | + TGFb: |
| 168 | + unit: ng/ul |
| 169 | + GAS6: |
| 170 | + unit: ug/ml |
| 171 | +
|
| 172 | + */ |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments