diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/Main.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/Main.java index 2aea0c6..726c6d0 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/Main.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/Main.java @@ -2,15 +2,12 @@ import org.monarchinitiative.phenopacket2prompt.cmd.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import picocli.CommandLine; import java.util.concurrent.Callable; @CommandLine.Command(name = "phenopacket2prompt", mixinStandardHelpOptions = true, version = "0.2.0", description = "Convert phenopacket to prompt for GPT") public class Main implements Callable { - private static final Logger logger = LoggerFactory.getLogger(Main.class); public static void main(String[] args){ if (args.length == 0) { @@ -21,6 +18,7 @@ public static void main(String[] args){ CommandLine cline = new CommandLine(new Main()) .addSubcommand("batch", new GbtTranslateBatchCommand()) .addSubcommand("download", new DownloadCommand()) + .addSubcommand("prompt", new PromptCommand()) .addSubcommand("mine", new TextMineCommand()) .addSubcommand("translate", new GptTranslateCommand()) ; diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/cmd/PromptCommand.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/cmd/PromptCommand.java new file mode 100644 index 0000000..4db463c --- /dev/null +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/cmd/PromptCommand.java @@ -0,0 +1,57 @@ +package org.monarchinitiative.phenopacket2prompt.cmd; + + +import org.monarchinitiative.phenol.base.PhenolRuntimeException; +import org.monarchinitiative.phenol.io.OntologyLoader; +import org.monarchinitiative.phenol.ontology.data.Ontology; +import org.monarchinitiative.phenopacket2prompt.model.PpktIndividual; +import org.monarchinitiative.phenopacket2prompt.output.PromptGenerator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.Callable; + +@CommandLine.Command(name = "prompt", aliases = {"P"}, + mixinStandardHelpOptions = true, + description = "Create a prompt from one phenopacket") +public class PromptCommand implements Callable { + private final static Logger LOGGER = LoggerFactory.getLogger(PromptCommand.class); + + + @CommandLine.Option(names = {"--hp"}, + description = "path to HP json file") + private String hpoJsonPath = "data/hp.json"; + + + @CommandLine.Option(names = {"-p", "--ppkt"}, description = "Path to JSON phenopacket file", required = true) + private String ppkt; + + @CommandLine.Option(names = {"-o", "--out"}, description = "Name of output prompt file") + private String outfile = null; + + + + @Override + public Integer call() throws Exception { + File hpJsonFile = new File(hpoJsonPath); + if (! hpJsonFile.isFile()) { + throw new PhenolRuntimeException("Could not find hp.json at " + hpJsonFile.getAbsolutePath()); + } + Ontology hpo = OntologyLoader.loadOntology(hpJsonFile); + LOGGER.info("HPO version {}", hpo.version().orElse("n/a")); + PromptGenerator generator = PromptGenerator.english(); + PpktIndividual individual = PpktIndividual.fromFile(new File(ppkt)); + String prompt = generator.createPrompt(individual); + System.out.println(prompt); + if (outfile != null) { + LOGGER.trace("Writing prompt to {}", outfile); + Path path = Path.of(outfile); + Files.writeString(path, prompt); + } + return 0; + } +} diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/AgeNotSpecified.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/AgeNotSpecified.java index 5720146..2f37003 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/AgeNotSpecified.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/AgeNotSpecified.java @@ -28,6 +28,11 @@ public boolean isInfant() { return false; } + @Override + public boolean isNeonate() { + return false; + } + @Override public boolean isFetus() { return false; diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/HpoOnsetAge.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/HpoOnsetAge.java index da8073f..ec252d8 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/HpoOnsetAge.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/HpoOnsetAge.java @@ -38,23 +38,41 @@ public final class HpoOnsetAge implements PhenopacketAge { private final static Set fetalIds = Set.of(TermId.of(" HP:0030674"), TermId.of("HP:0011461"), TermId.of("HP:0034199"), TermId.of("HP:0034197"), TermId.of("HP:0034198"), TermId.of("HP:0011460*")); - /** - * Childhood onset - */ - private final static TermId childhoodOnset = TermId.of("HP:0011463"); - - private final static TermId juvenileOnset = TermId.of("HP:0003621"); + public static HpoOnsetAge congenital() { + return new HpoOnsetAge(congenitalOnset.getValue(), "Congenital onset"); + } + public static HpoOnsetAge infantile() { + return new HpoOnsetAge(infantileOnset.getValue(), "Infantile onset"); + } + public static HpoOnsetAge childhood() { + return new HpoOnsetAge(childhoodOnset.getValue(), "Childhood onset"); + } + public static HpoOnsetAge juvenile() { + return new HpoOnsetAge(juvenileOnset.getValue(), "Juvenile onset"); + } - /** - * Infantile onset - */ - private final static TermId infantileOnset = TermId.of("HP:0003593"); - /** - * Congenital onset - */ + private final static TermId neonatalOnset = TermId.of("HP:0003623"); private final static TermId congenitalOnset = TermId.of("HP:0003577"); - + private final static TermId infantileOnset = TermId.of("HP:0003593"); + private final static TermId childhoodOnset = TermId.of("HP:0011463"); + private final static TermId juvenileOnset = TermId.of("HP:0003621"); + /** Adult onset*/ + private final static TermId adultOnset = TermId.of("HP:0003581"); + /** Young adult onset HP:0011462 */ + private final static TermId youngAdultOnset = TermId.of("HP:0011462"); + /** Early young adult onset HP:0025708*/ + private final static TermId earlyYoungAdultAnset = TermId.of("HP:0025708"); + /** Intermediate young adult onset HP:0025709 */ + private final static TermId intermediateYoungAdultOnset = TermId.of("HP:0025709"); + /** Late young adult onset HP:0025710 */ + private final static TermId lateYoungAdultOnset = TermId.of("HP:0025710"); + /** Middle age onset HP:0003596 */ + private final static TermId middleAgeOnset = TermId.of("HP:0003596"); + /** Late onset HP:0003584 */ + private final static TermId lateOnset = TermId.of("HP:0003584"); + private final static Set adultTermIds = Set.of(adultOnset, youngAdultOnset, earlyYoungAdultAnset, + intermediateYoungAdultOnset, lateYoungAdultOnset, middleAgeOnset, lateOnset); public HpoOnsetAge(String id, String label) { this.tid = TermId.of(id); @@ -66,7 +84,6 @@ public HpoOnsetAge(String id, String label) { } else { totalDays = Integer.MAX_VALUE; } - } @Override @@ -83,24 +100,29 @@ public PhenopacketAgeType ageType() { public boolean isJuvenile() { return tid.equals(juvenileOnset); } - - @Override public boolean isChild() { return tid.equals(childhoodOnset); } - @Override public boolean isInfant() { return tid.equals(infantileOnset); } + @Override + public boolean isNeonate() { + return tid.equals(neonatalOnset); + } + @Override public boolean isCongenital() { return tid.equals(congenitalOnset); } @Override + public boolean isAdult() { + return adultTermIds.contains(tid); + public boolean isYoungAdult() { return youngAdultIds.contains(tid); } @@ -115,17 +137,10 @@ public boolean isLateAdultAge() { return tid.equals(lateAgeOnset); } - @Override - public boolean isAdult() { - return youngAdultIds.contains(tid) || otherAdult.contains(tid); - } - @Override public boolean isFetus() { return fetalIds.contains(tid); } - - @Override public int totalDays() { return totalDays; @@ -136,23 +151,6 @@ public TermId getTid() { } - public static HpoOnsetAge childhood() { - return new HpoOnsetAge(childhoodOnset.getValue(), "Childhood onset"); - } - - public static HpoOnsetAge juvenile() { - return new HpoOnsetAge(juvenileOnset.getValue(), "Juvenile onset"); - } - - - public static HpoOnsetAge infantile() { - return new HpoOnsetAge(infantileOnset.getValue(), "Infantile onset"); - } - - - public static HpoOnsetAge congenital() { - return new HpoOnsetAge(congenitalOnset.getValue(), "Congenital onset"); - } @Override public int hashCode() { diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/Iso8601Age.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/Iso8601Age.java index a5ad088..f8771cd 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/Iso8601Age.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/Iso8601Age.java @@ -78,7 +78,7 @@ public PhenopacketAgeType ageType() { @Override public boolean isJuvenile() { - return years >= 6 && years < 18; + return years >= 6 && years < 16; } @Override @@ -91,6 +91,11 @@ public boolean isInfant() { return years < 1; } + @Override + public boolean isNeonate() { + return years == 0 && months < 1; + } + @Override public boolean isFetus() { // always false because we cannot express prenatal ages with iso diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/PhenopacketAge.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/PhenopacketAge.java index 0110700..5de104b 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/model/PhenopacketAge.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/model/PhenopacketAge.java @@ -11,6 +11,8 @@ public sealed interface PhenopacketAge permits AgeNotSpecified, HpoOnsetAge, Iso boolean isInfant(); + boolean isNeonate(); + boolean isFetus(); boolean isCongenital(); diff --git a/src/main/java/org/monarchinitiative/phenopacket2prompt/output/impl/english/PpktIndividualEnglish.java b/src/main/java/org/monarchinitiative/phenopacket2prompt/output/impl/english/PpktIndividualEnglish.java index 5de6984..67c7f7b 100644 --- a/src/main/java/org/monarchinitiative/phenopacket2prompt/output/impl/english/PpktIndividualEnglish.java +++ b/src/main/java/org/monarchinitiative/phenopacket2prompt/output/impl/english/PpktIndividualEnglish.java @@ -116,9 +116,13 @@ private String onsetTermAtAgeOf(HpoOnsetAge hpoOnsetTermAge) { } else if (hpoOnsetTermAge.isChild()) { return buildBlocks.inChildhood(); } else if (hpoOnsetTermAge.isJuvenile()) { - return buildBlocks.asAdolescent(); + return "as an adolescent"; + } else if (hpoOnsetTermAge.isNeonate()){ + return "as a neonate"; + } else if (hpoOnsetTermAge.isAdult()){ + return "in adulthood"; } else { - return buildBlocks.inAdulthoold(); + throw new PhenolRuntimeException("Could not identify onset age for HpoOnsetAge " + hpoOnsetTermAge); } }