Skip to content

Commit

Permalink
test and finish update method
Browse files Browse the repository at this point in the history
  • Loading branch information
wow-such-code committed Oct 21, 2024
1 parent c9447ab commit 4e48a4b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 36 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,29 @@ least one sample attribute is different in openBIS and SEEK
4. assets attached to the experiment or samples will be created, if they are missing from this assay
5. no existing sample or assets are deleted from SEEK, even if they are missing from openBIS

**Example command:**

`java -jar target/openbis-scripts-1.0.0-jar-with-dependencies.jar openbis-to-seek /MYSPACE/PROJECTY/00_P_INFO_691 mystudy -d -config config.txt --openbis-pw --seek-pw`

**Example output:**

Transfer openBIS -> SEEK started.
Provided openBIS object: /MYSPACE/PROJECTY/00_P_INFO_691
Provided SEEK study title: mystudy
No SEEK project title provided, will search config file.
Transfer datasets to SEEK? true
Update existing nodes if found? true
Connecting to openBIS...
Searching for specified object in openBIS...
Search successful.
Connecting to SEEK...
Collecting information from openBIS...
Translating openBIS property codes to SEEK names...
Creating SEEK structure...
Trying to find existing corresponding assay in SEEK...
Found assay with id 64
Updating nodes...
Mismatch found in Gender attribute of /MYSPACE/PROJECTY/00_P_INFO_691. Sample will be updated.
http://localhost:3000/assays/64 was successfully updated.

## Caveats and Future Options
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import life.qbic.model.OpenbisExperimentWithDescendants;
import life.qbic.model.OpenbisSeekTranslator;
import life.qbic.model.download.SEEKConnector.SeekStructurePostRegistrationInformation;
import life.qbic.model.isa.NodeType;
import life.qbic.model.isa.SeekStructure;
import life.qbic.model.download.OpenbisConnector;
import life.qbic.model.download.SEEKConnector;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void run() {
System.out.printf("No SEEK project title provided, will search config file.%n");
}
System.out.printf("Transfer datasets to SEEK? %s%n", transferData);
System.out.printf("Update existing assay if found? %s%n", !noUpdate);
System.out.printf("Update existing nodes if found? %s%n", !noUpdate);
if(blacklistFile!=null && !blacklistFile.isBlank()) {
System.out.printf("File with datasets codes that won't be transferred: %s%n", blacklistFile);
}
Expand All @@ -100,28 +101,20 @@ public void run() {

this.openbis = new OpenbisConnector(authentication);

boolean isSample = false;
boolean isDataSet = false;

System.out.println("Searching for specified object in openBIS...");

boolean isExperiment = experimentExists(objectID);

if (isExperiment && (studyTitle == null || studyTitle.isBlank())) {
System.out.printf(
"No SEEK study title was provided. This is mandatory if an openBIS experiment is to be transferred%n");
return;
}
NodeType nodeType = NodeType.ASSAY;

if (!isExperiment && sampleExists(objectID)) {
isSample = true;
nodeType = NodeType.SAMPLE;
}

if (!isExperiment && !isSample && datasetsExist(Arrays.asList(objectID))) {
isDataSet = true;
if (!isExperiment && !nodeType.equals(NodeType.SAMPLE) && datasetsExist(Arrays.asList(objectID))) {
nodeType = NodeType.ASSET;
}

if (!isSample && !isExperiment && !isDataSet) {
if (nodeType.equals(NodeType.ASSAY) && !isExperiment) {
System.out.printf(
"%s could not be found in openBIS. Make sure you either specify an experiment, sample or dataset%n",
objectID);
Expand Down Expand Up @@ -150,16 +143,20 @@ public void run() {
OpenbisExperimentWithDescendants structure;
try {
System.out.println("Collecting information from openBIS...");
if (isExperiment) {
structure = openbis.getExperimentWithDescendants(objectID);
postRegInfo = handleExperimentTransfer(structure);
} else if (isSample) {
structure = openbis.getExperimentAndDataFromSample(objectID);
postRegInfo = handleExperimentTransfer(structure);
} else {
structure = openbis.getExperimentStructureFromDataset(objectID);
postRegInfo = handleExperimentTransfer(structure);
switch (nodeType) {
case ASSAY:
structure = openbis.getExperimentWithDescendants(objectID);
break;
case SAMPLE:
structure = openbis.getExperimentAndDataFromSample(objectID);
break;
case ASSET:
structure = openbis.getExperimentStructureFromDataset(objectID);
break;
default:
throw new RuntimeException("Handling of node type " + nodeType + " is not supported.");
}
postRegInfo = handleExperimentTransfer(structure, nodeType);
} catch (URISyntaxException | IOException | InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -171,7 +168,7 @@ public void run() {
}

private SeekStructurePostRegistrationInformation handleExperimentTransfer(
OpenbisExperimentWithDescendants experiment)
OpenbisExperimentWithDescendants experiment, NodeType nodeType)
throws URISyntaxException, IOException, InterruptedException {
Set<String> blacklist = parseBlackList(blacklistFile);
System.out.println("Translating openBIS property codes to SEEK names...");
Expand Down Expand Up @@ -302,8 +299,9 @@ private Set<String> parseBlackList(String blacklistFile) {
}
}

private SeekStructurePostRegistrationInformation updateAssayStructure(SeekStructure nodeWithChildren,
String assayID) throws URISyntaxException, IOException, InterruptedException {
private SeekStructurePostRegistrationInformation updateAssayStructure(
SeekStructure nodeWithChildren, String assayID) throws URISyntaxException,
IOException, InterruptedException {
SeekStructurePostRegistrationInformation postRegInfo = seek.updateAssayNode(nodeWithChildren,
assayID);
List<AssetToUpload> assetsToUpload = postRegInfo.getAssetsToUpload();
Expand Down Expand Up @@ -381,7 +379,7 @@ private Optional<String> getAssayIDForOpenBISExperiment(Experiment experiment)
// because if a perm id is found in the wrong SEEK node, meta-information in SEEK could be
// overwritten or samples/data added to the wrong assay.
String permID = experiment.getPermId().getPermId();
List<String> assayIDs = seek.searchAssaysContainingKeyword(permID);
List<String> assayIDs = seek.searchAssaysInStudyContainingKeyword(permID);
if(assayIDs.isEmpty()) {
return Optional.empty();
}
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/life/qbic/model/download/SEEKConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class SEEKConnector {
private byte[] credentials;
private OpenbisSeekTranslator translator;
private final String DEFAULT_PROJECT_ID;
private String currentStudy;
private final List<String> ASSET_TYPES = new ArrayList<>(Arrays.asList("data_files", "models",
"sops", "documents", "publications"));

Expand All @@ -75,7 +76,8 @@ public void setDefaultInvestigation(String investigationTitle)

public void setDefaultStudy(String studyTitle)
throws URISyntaxException, IOException, InterruptedException {
translator.setDefaultStudy(searchNodeWithTitle("studies", studyTitle));
this.currentStudy = searchNodeWithTitle("studies", studyTitle);
translator.setDefaultStudy(currentStudy);
}

/**
Expand Down Expand Up @@ -500,18 +502,24 @@ private String searchNodeWithTitle(String nodeType, String title)
* @throws IOException
* @throws InterruptedException
*/
public List<String> searchAssaysContainingKeyword(String searchTerm)
public List<String> searchAssaysInStudyContainingKeyword(String searchTerm)
throws URISyntaxException, IOException, InterruptedException {

JsonNode result = genericSearch("assays", "*"+searchTerm+"*");

JsonNode hits = result.path("data");
List<String> assayIDs = new ArrayList<>();
List<String> assayIDsInStudy = new ArrayList<>();
for (Iterator<JsonNode> it = hits.elements(); it.hasNext(); ) {
JsonNode hit = it.next();
assayIDs.add(hit.get("id").asText());
String assayID = hit.get("id").asText();
JsonNode assayData = fetchAssayData(assayID).get("data");
JsonNode relationships = assayData.get("relationships");
String studyID = relationships.get("study").get("data").get("id").asText();
if(studyID.equals(currentStudy)) {
assayIDsInStudy.add(assayID);
}
}
return assayIDs;
return assayIDsInStudy;
}

/**
Expand Down Expand Up @@ -556,8 +564,10 @@ public List<String> searchAssetsContainingKeyword(String searchTerm)
/**
* Updates information of an existing assay, its samples and attached assets. Missing samples and
* assets are created, but nothing missing from the new structure is deleted from SEEK.
*
* @param nodeWithChildren the translated Seek structure as it should be once the update is done
* @param assayID the assay id of the existing assay, that should be compared to the new structure
* @param assayID the assay id of the existing assay, that should be compared to the new
* structure
* @return information necessary to make post registration updates in openBIS and upload missing
* data to newly created assets. In the case of the update use case, only newly created objects
* will be contained in the return object.
Expand Down Expand Up @@ -585,9 +595,9 @@ public SeekStructurePostRegistrationInformation updateAssayNode(SeekStructure no

boolean oldEmpty = oldValue == null || oldValue.toString().isEmpty();
boolean newEmpty = newValue == null || newValue.toString().isEmpty();
if ((!oldEmpty && !newEmpty) && !newValue.equals(oldValue)) {
System.out.printf("Mismatch found in attributes of %s. Sample will be updated.%n",
openBisID);
if ((!oldEmpty && !newEmpty) && !newValue.toString().equals(oldValue.toString())) {
System.out.printf("Mismatch found in %s attribute of %s. Sample will be updated.%n",
key, openBisID);
newSample.setAssayIDs(List.of(assayID));
updateSample(newSample, existingSample.getSeekID());
}
Expand Down Expand Up @@ -840,6 +850,7 @@ public SeekStructurePostRegistrationInformation createNode(SeekStructure nodeWit

Pair<ISAAssay, String> assayIDPair = nodeWithChildren.getAssayWithOpenBISReference().get();

System.out.println("Creating assay...");
String assayID = addAssay(assayIDPair.getKey());
String assayEndpoint = apiURL+"/assays/"+assayID;
Pair<String, String> experimentIDWithEndpoint =
Expand All @@ -850,6 +861,9 @@ public SeekStructurePostRegistrationInformation createNode(SeekStructure nodeWit

Map<String, String> sampleIDsWithEndpoints = new HashMap<>();
Map<ISASample, String> samplesWithReferences = nodeWithChildren.getSamplesWithOpenBISReference();
if(!samplesWithReferences.isEmpty()) {
System.out.println("Creating samples...");
}
for(ISASample sample : samplesWithReferences.keySet()) {
sample.setAssayIDs(Collections.singletonList(assayID));
String sampleEndpoint = createSample(sample);
Expand All @@ -858,6 +872,10 @@ public SeekStructurePostRegistrationInformation createNode(SeekStructure nodeWit

Map<GenericSeekAsset, DataSetFile> isaToFileMap = nodeWithChildren.getISAFileToDatasetFiles();

if(!isaToFileMap.isEmpty()) {
System.out.println("Creating assets...");
}

List<AssetToUpload> assetsToUpload = createAssetsForAssays(isaToFileMap,
Collections.singletonList(assayID));

Expand Down

0 comments on commit 4e48a4b

Please sign in to comment.