Skip to content

Commit a3b0572

Browse files
committed
petab model
1 parent 68a1e95 commit a3b0572

22 files changed

+687
-77
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
<artifactId>picocli</artifactId>
9595
<version>4.6.2</version>
9696
</dependency>
97+
<dependency>
98+
<groupId>com.fasterxml.jackson.dataformat</groupId>
99+
<artifactId>jackson-dataformat-yaml</artifactId>
100+
<version>2.17.2</version>
101+
</dependency>
97102
<dependency>
98103
<groupId>jline</groupId>
99104
<artifactId>jline</artifactId>
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}

src/main/java/life/qbic/io/commandline/DownloadDatasetCommand.java

-68
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package life.qbic.io.commandline;
2+
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
4+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import life.qbic.App;
9+
import life.qbic.io.PetabParser;
10+
import life.qbic.model.DatasetWithProperties;
11+
import life.qbic.model.download.OpenbisConnector;
12+
import picocli.CommandLine.Command;
13+
import picocli.CommandLine.Mixin;
14+
import picocli.CommandLine.Parameters;
15+
16+
@Command(name = "download-petab",
17+
description = "Downloads PEtab dataset and stores some additional information from openbis in the petab.yaml")
18+
public class DownloadPetabCommand implements Runnable {
19+
20+
@Parameters(arity = "1", paramLabel = "dataset id", description = "The code of the dataset to download. Can be found via list-data.")
21+
private String datasetCode;
22+
@Parameters(arity = "1", paramLabel = "download path", description = "The local path where to store the downloaded data")
23+
private String outputPath;
24+
@Mixin
25+
AuthenticationOptions auth = new AuthenticationOptions();
26+
27+
@Override
28+
public void run() {
29+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS(), auth.getDSS());
30+
OpenbisConnector openbis = new OpenbisConnector(authentication);
31+
32+
List<DataSet> datasets = openbis.findDataSets(Collections.singletonList(datasetCode));
33+
34+
if(datasets.isEmpty()) {
35+
System.out.println(datasetCode+" not found");
36+
return;
37+
}
38+
DatasetWithProperties result = new DatasetWithProperties(datasets.get(0));
39+
Optional<String> patientID = openbis.findPropertyInSampleHierarchy("PATIENT_DKFZ_ID",
40+
result.getExperiment().getIdentifier());
41+
patientID.ifPresent(s -> result.addProperty("patientID", s));
42+
result.addProperty("openbisID", datasetCode);
43+
44+
System.out.println("Found dataset, downloading.");
45+
System.out.println();
46+
47+
openbis.downloadDataset(outputPath, datasetCode);
48+
49+
System.out.println("Adding additional information to petab.yaml");
50+
System.out.println();
51+
52+
PetabParser parser = new PetabParser();
53+
parser.addParameters(outputPath, result.getProperties());
54+
System.out.println("Done");
55+
}
56+
57+
}

src/main/java/life/qbic/io/commandline/UploadDatasetCommand.java

-5
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,4 @@ private boolean pathValid(String dataPath) {
7070
return new File(dataPath).exists();
7171
}
7272

73-
private String getTimeStamp() {
74-
final String PATTERN_FORMAT = "YYYY-MM-dd_HHmmss";
75-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
76-
return LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
77-
}
7873
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package life.qbic.io.commandline;
2+
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
4+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId;
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import life.qbic.App;
10+
import life.qbic.io.PetabParser;
11+
import life.qbic.model.download.OpenbisConnector;
12+
import picocli.CommandLine.Command;
13+
import picocli.CommandLine.Mixin;
14+
import picocli.CommandLine.Option;
15+
import picocli.CommandLine.Parameters;
16+
17+
@Command(name = "upload-petab-result",
18+
description = "uploads a petab based on other PETab downloaded from openbis and attaches it to a provided experiment and any datasets referenced in the PETab metadata.")
19+
public class UploadPetabResultCommand implements Runnable {
20+
21+
@Parameters(arity = "1", paramLabel = "file/folder", description = "The path to the file or folder to upload")
22+
private String dataPath;
23+
@Parameters(arity = "1", paramLabel = "experiment ID", description = "The full identifier of the experiment the data should be attached to. "
24+
+ "The identifier must be of the format: /space/project/experiment")
25+
private String experimentID;
26+
@Option(arity = "1..*", paramLabel = "<parent_datasets>", description = "Optional list of dataset codes to act"
27+
+ " as parents for the upload. E.g. when this dataset has been generated using these datasets as input.", names = {"-pa", "--parents"})
28+
private List<String> parents = new ArrayList<>();
29+
@Mixin
30+
AuthenticationOptions auth = new AuthenticationOptions();
31+
32+
private OpenbisConnector openbis;
33+
private PetabParser petabParser = new PetabParser();
34+
35+
@Override
36+
public void run() {
37+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS(), auth.getDSS());
38+
openbis = new OpenbisConnector(authentication);
39+
40+
if(!pathValid(dataPath)) {
41+
System.out.printf("Path %s could not be found%n", dataPath);
42+
return;
43+
}
44+
if(!experimentExists(experimentID)) {
45+
System.out.printf("Experiment %s could not be found%n", experimentID);
46+
return;
47+
}
48+
parents = petabParser.parse(dataPath).getSourcePetabReferences();
49+
if(!datasetsExist(parents)) {
50+
System.out.printf("One or more datasets %s could not be found%n", parents);
51+
return;
52+
}
53+
System.out.println();
54+
System.out.println("Parameters verified, uploading dataset...");
55+
System.out.println();//TODO copy and remove source references
56+
DataSetPermId result = openbis.registerDataset(Path.of(dataPath), experimentID, parents);
57+
System.out.printf("Dataset %s was successfully created%n", result.getPermId());
58+
}
59+
60+
private boolean datasetsExist(List<String> datasetCodes) {
61+
return openbis.findDataSets(datasetCodes).size() == datasetCodes.size();
62+
}
63+
64+
private boolean experimentExists(String experimentID) {
65+
return openbis.experimentExists(experimentID);
66+
}
67+
68+
private boolean pathValid(String dataPath) {
69+
return new File(dataPath).exists();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)