Skip to content

Commit

Permalink
Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
forus committed Dec 11, 2024
1 parent 8da3e6c commit 3e0cda7
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 85 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.cbioportal.file.export;

import org.cbioportal.file.model.CancerStudyMetadata;
import org.cbioportal.file.model.GenericDatatypeMetadata;
import org.cbioportal.file.model.GenericProfileDatatypeMetadata;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;

/**
* A serializer for file model records that serializes them the cBioPortal key-value metadata format.
* e.g. meta_study.txt
*/
public class KeyValueMetadataWriter {
private final Writer writer;

/**
* @param writer - the writer to write the key-value metadata to
* e.g. StringWriter, FileWriter
*/
public KeyValueMetadataWriter(Writer writer) {
this.writer = writer;
}

/**
* Write a cancer study metadata to the writer
*/
public void write(CancerStudyMetadata cancerStudyMetadata) {
LinkedHashMap<String, String> metdata = new LinkedHashMap<>();
metdata.put("type_of_cancer", cancerStudyMetadata.typeOfCancer());
metdata.put("cancer_study_identifier", cancerStudyMetadata.cancerStudyIdentifier());
metdata.put("name", cancerStudyMetadata.name());
metdata.put("description", cancerStudyMetadata.description());
cancerStudyMetadata.citation().ifPresent(citation -> metdata.put("citation", citation));
cancerStudyMetadata.pmid().ifPresent(pmid -> metdata.put("pmid", pmid));
cancerStudyMetadata.groups().ifPresent(groups -> metdata.put("groups", groups));
cancerStudyMetadata.addGlobalCaseList().ifPresent(addGlobalCaseList -> metdata.put("add_global_case_list", addGlobalCaseList.toString()));
cancerStudyMetadata.tagsFile().ifPresent(tagsFile -> metdata.put("tags_file", tagsFile));
cancerStudyMetadata.referenceGenome().ifPresent(referenceGenome -> metdata.put("reference_genome", referenceGenome));
write(metdata);
}

/**
* Write a generic datatype metadata to the writer
*/
public void write(GenericDatatypeMetadata genericDatatypeMetadata) {
LinkedHashMap<String, String> metadata = new LinkedHashMap<>();
metadata.put("cancer_study_identifier", genericDatatypeMetadata.cancerStudyIdentifier());
metadata.put("generic_alteration_type", genericDatatypeMetadata.geneticAlterationType());
metadata.put("datatype", genericDatatypeMetadata.datatype());
metadata.put("data_filename", genericDatatypeMetadata.dataFilename());
write(metadata);
}

/**
* Write a generic profile datatype metadata to the writer
*/
public void write(GenericProfileDatatypeMetadata genericProfileDatatypeMetadata) {
write((GenericDatatypeMetadata) genericProfileDatatypeMetadata);
LinkedHashMap<String, String> metadata = new LinkedHashMap<>();
metadata.put("stable_id", genericProfileDatatypeMetadata.stableId());
metadata.put("show_profile_in_analysis_tab", genericProfileDatatypeMetadata.showProfileInAnalysisTab().toString().toLowerCase());
metadata.put("profile_name", genericProfileDatatypeMetadata.profileName());
metadata.put("profile_description", genericProfileDatatypeMetadata.profileDescription());
genericProfileDatatypeMetadata.genePanel().ifPresent(genePanel -> metadata.put("gene_panel", genePanel));
write(metadata);
}

private void write(LinkedHashMap<String, String> metadata) {
metadata.forEach((key, value) -> {
try {
writer.write(composeKeyValueLine(key, value));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private static String composeKeyValueLine(String key, String value) {
return key + ": " + value + "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Represents metadata for a cancer study.
*/
public record CancerStudyMetadata (
public record CancerStudyMetadata(
/**
* The cancer type abbreviation, e.g., "brca". This should be the same cancer type as specified in the meta_cancer_type.txt file, if available. The type can be "mixed" for studies with multiple cancer types.
*/
Expand Down Expand Up @@ -55,4 +55,4 @@ public record CancerStudyMetadata (
* The study reference genome (e.g. hg19, hg38). Without specifying this property, the study will be assigned to the reference genome specified in application.properties (property ucsc.build).
*/
Optional<String> referenceGenome
) implements StudyRelated {}
) implements StudyRelatedMetadata {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public record ClinicalSampleAttributesMetadata(
String cancerStudyIdentifier,
String dataFilename
) implements GenericDatatypeFileMetadata {
) implements GenericDatatypeMetadata {
public String geneticAlterationType() {
return "CLINICAL";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.cbioportal.file.model;

public interface GenericDatatypeFileMetadata extends StudyRelated {
public interface GenericDatatypeMetadata extends StudyRelatedMetadata {
String geneticAlterationType();
String datatype();
String dataFilename();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Optional;

public interface GenericProfileDatatypeFileMetadata extends GenericDatatypeFileMetadata {
public interface GenericProfileDatatypeMetadata extends GenericDatatypeMetadata {
String stableId();
Boolean showProfileInAnalysisTab();
String profileName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record MutationMetadata(
String profileName,
String profileDescription,
Optional<String> genePanel
) implements GenericProfileDatatypeFileMetadata {
) implements GenericProfileDatatypeMetadata {
public String geneticAlterationType() {
return "MUTATION_EXTENDED";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.cbioportal.file.model;

public interface StudyRelated {
public interface StudyRelatedMetadata {
String cancerStudyIdentifier();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class MetadataWriterTest {

StringWriter output = new StringWriter();
KeyValueConfigurationWriter writer = new KeyValueConfigurationWriter(output);
KeyValueMetadataWriter writer = new KeyValueMetadataWriter(output);

@Test
public void testCancerStudyMetadataWriter() {
Expand Down

0 comments on commit 3e0cda7

Please sign in to comment.