diff --git a/src/main/java/org/cbioportal/file/export/KeyValueConfigurationWriter.java b/src/main/java/org/cbioportal/file/export/KeyValueConfigurationWriter.java deleted file mode 100644 index dfa795f6b03..00000000000 --- a/src/main/java/org/cbioportal/file/export/KeyValueConfigurationWriter.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.cbioportal.file.export; - -import org.cbioportal.file.model.CancerStudyMetadata; -import org.cbioportal.file.model.GenericDatatypeFileMetadata; -import org.cbioportal.file.model.GenericProfileDatatypeFileMetadata; - -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 configuration format. - * e.g. meta_study.txt - */ -public class KeyValueConfigurationWriter { - private final Writer writer; - - /** - * @param writer - the writer to write the key-value configuration to - * e.g. StringWriter, FileWriter - */ - public KeyValueConfigurationWriter(Writer writer) { - this.writer = writer; - } - - /** - * Write a cancer study metadata to the output writer - */ - public void write(CancerStudyMetadata cancerStudyMetadata) { - LinkedHashMap config = new LinkedHashMap<>(); - config.put("type_of_cancer", cancerStudyMetadata.typeOfCancer()); - config.put("cancer_study_identifier", cancerStudyMetadata.cancerStudyIdentifier()); - config.put("name", cancerStudyMetadata.name()); - config.put("description", cancerStudyMetadata.description()); - cancerStudyMetadata.citation().ifPresent(citation -> config.put("citation", citation)); - cancerStudyMetadata.pmid().ifPresent(pmid -> config.put("pmid", pmid)); - cancerStudyMetadata.groups().ifPresent(groups -> config.put("groups", groups)); - cancerStudyMetadata.addGlobalCaseList().ifPresent(addGlobalCaseList -> config.put("add_global_case_list", addGlobalCaseList.toString())); - cancerStudyMetadata.tagsFile().ifPresent(tagsFile -> config.put("tags_file", tagsFile)); - cancerStudyMetadata.referenceGenome().ifPresent(referenceGenome -> config.put("reference_genome", referenceGenome)); - write(config); - } - - public void write(GenericDatatypeFileMetadata genericDatatypeFileMetadata) { - LinkedHashMap config = new LinkedHashMap<>(); - config.put("cancer_study_identifier", genericDatatypeFileMetadata.cancerStudyIdentifier()); - config.put("generic_alteration_type", genericDatatypeFileMetadata.geneticAlterationType()); - config.put("datatype", genericDatatypeFileMetadata.datatype()); - config.put("data_filename", genericDatatypeFileMetadata.dataFilename()); - write(config); - } - - public void write(GenericProfileDatatypeFileMetadata genericProfileDatatypeFileMetadata) { - write((GenericDatatypeFileMetadata) genericProfileDatatypeFileMetadata); - LinkedHashMap config = new LinkedHashMap<>(); - config.put("stable_id", genericProfileDatatypeFileMetadata.stableId()); - config.put("show_profile_in_analysis_tab", genericProfileDatatypeFileMetadata.showProfileInAnalysisTab().toString().toLowerCase()); - config.put("profile_name", genericProfileDatatypeFileMetadata.profileName()); - config.put("profile_description", genericProfileDatatypeFileMetadata.profileDescription()); - genericProfileDatatypeFileMetadata.genePanel().ifPresent(genePanel -> config.put("gene_panel", genePanel)); - write(config); - } - - private void write(LinkedHashMap config) { - config.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"; - } -} diff --git a/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java b/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java new file mode 100644 index 00000000000..6d751092d86 --- /dev/null +++ b/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java @@ -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 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 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 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 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"; + } +} diff --git a/src/main/java/org/cbioportal/file/model/CancerStudyMetadata.java b/src/main/java/org/cbioportal/file/model/CancerStudyMetadata.java index 7f06a9d6475..5be9d7660fe 100644 --- a/src/main/java/org/cbioportal/file/model/CancerStudyMetadata.java +++ b/src/main/java/org/cbioportal/file/model/CancerStudyMetadata.java @@ -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. */ @@ -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 referenceGenome -) implements StudyRelated {} \ No newline at end of file +) implements StudyRelatedMetadata {} \ No newline at end of file diff --git a/src/main/java/org/cbioportal/file/model/ClinicalSampleAttributesMetadata.java b/src/main/java/org/cbioportal/file/model/ClinicalSampleAttributesMetadata.java index ad33a9bee42..f08ee6334c4 100644 --- a/src/main/java/org/cbioportal/file/model/ClinicalSampleAttributesMetadata.java +++ b/src/main/java/org/cbioportal/file/model/ClinicalSampleAttributesMetadata.java @@ -3,7 +3,7 @@ public record ClinicalSampleAttributesMetadata( String cancerStudyIdentifier, String dataFilename -) implements GenericDatatypeFileMetadata { +) implements GenericDatatypeMetadata { public String geneticAlterationType() { return "CLINICAL"; } diff --git a/src/main/java/org/cbioportal/file/model/GenericDatatypeFileMetadata.java b/src/main/java/org/cbioportal/file/model/GenericDatatypeMetadata.java similarity index 63% rename from src/main/java/org/cbioportal/file/model/GenericDatatypeFileMetadata.java rename to src/main/java/org/cbioportal/file/model/GenericDatatypeMetadata.java index b6debe4cece..f84efea8fc0 100644 --- a/src/main/java/org/cbioportal/file/model/GenericDatatypeFileMetadata.java +++ b/src/main/java/org/cbioportal/file/model/GenericDatatypeMetadata.java @@ -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(); diff --git a/src/main/java/org/cbioportal/file/model/GenericProfileDatatypeFileMetadata.java b/src/main/java/org/cbioportal/file/model/GenericProfileDatatypeMetadata.java similarity index 71% rename from src/main/java/org/cbioportal/file/model/GenericProfileDatatypeFileMetadata.java rename to src/main/java/org/cbioportal/file/model/GenericProfileDatatypeMetadata.java index ad63d488e17..8914c24d467 100644 --- a/src/main/java/org/cbioportal/file/model/GenericProfileDatatypeFileMetadata.java +++ b/src/main/java/org/cbioportal/file/model/GenericProfileDatatypeMetadata.java @@ -2,7 +2,7 @@ import java.util.Optional; -public interface GenericProfileDatatypeFileMetadata extends GenericDatatypeFileMetadata { +public interface GenericProfileDatatypeMetadata extends GenericDatatypeMetadata { String stableId(); Boolean showProfileInAnalysisTab(); String profileName(); diff --git a/src/main/java/org/cbioportal/file/model/MutationMetadata.java b/src/main/java/org/cbioportal/file/model/MutationMetadata.java index 7d9d8e4b434..35af33adb88 100644 --- a/src/main/java/org/cbioportal/file/model/MutationMetadata.java +++ b/src/main/java/org/cbioportal/file/model/MutationMetadata.java @@ -8,7 +8,7 @@ public record MutationMetadata( String profileName, String profileDescription, Optional genePanel -) implements GenericProfileDatatypeFileMetadata { +) implements GenericProfileDatatypeMetadata { public String geneticAlterationType() { return "MUTATION_EXTENDED"; } diff --git a/src/main/java/org/cbioportal/file/model/StudyRelated.java b/src/main/java/org/cbioportal/file/model/StudyRelatedMetadata.java similarity index 64% rename from src/main/java/org/cbioportal/file/model/StudyRelated.java rename to src/main/java/org/cbioportal/file/model/StudyRelatedMetadata.java index 9210dce6390..fd07ea46bfc 100644 --- a/src/main/java/org/cbioportal/file/model/StudyRelated.java +++ b/src/main/java/org/cbioportal/file/model/StudyRelatedMetadata.java @@ -1,5 +1,5 @@ package org.cbioportal.file.model; -public interface StudyRelated { +public interface StudyRelatedMetadata { String cancerStudyIdentifier(); } diff --git a/src/test/java/org/cbioportal/file/export/MetadataWriterTest.java b/src/test/java/org/cbioportal/file/export/MetadataWriterTest.java index 4e0363b6d9a..b755c749a7b 100644 --- a/src/test/java/org/cbioportal/file/export/MetadataWriterTest.java +++ b/src/test/java/org/cbioportal/file/export/MetadataWriterTest.java @@ -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() {