Skip to content

Commit

Permalink
Merge pull request #32 from monarch-initiative/update_docs
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
pnrobinson authored Jun 22, 2024
2 parents 8d966fa + 5d0d376 commit 7080c98
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> {
private static final Logger logger = LoggerFactory.getLogger(Main.class);

public static void main(String[] args){
if (args.length == 0) {
Expand All @@ -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())
;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public boolean isInfant() {
return false;
}

@Override
public boolean isNeonate() {
return false;
}

@Override
public boolean isFetus() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,41 @@ public final class HpoOnsetAge implements PhenopacketAge {
private final static Set<TermId> 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<TermId> adultTermIds = Set.of(adultOnset, youngAdultOnset, earlyYoungAdultAnset,
intermediateYoungAdultOnset, lateYoungAdultOnset, middleAgeOnset, lateOnset);

public HpoOnsetAge(String id, String label) {
this.tid = TermId.of(id);
Expand All @@ -66,7 +84,6 @@ public HpoOnsetAge(String id, String label) {
} else {
totalDays = Integer.MAX_VALUE;
}

}

@Override
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public PhenopacketAgeType ageType() {

@Override
public boolean isJuvenile() {
return years >= 6 && years < 18;
return years >= 6 && years < 16;
}

@Override
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public sealed interface PhenopacketAge permits AgeNotSpecified, HpoOnsetAge, Iso

boolean isInfant();

boolean isNeonate();

boolean isFetus();

boolean isCongenital();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 7080c98

Please sign in to comment.