Commit 9aefbcb4 authored by Cossin Sebastien's avatar Cossin Sebastien
Browse files

WIP Java program

parent fad2de6e
package fr.erias.frenchdrugs.comparators;
import fr.erias.frenchdrugs.models.ClinicalDrug;
public abstract class CDcomp {
protected final ClinicalDrug cd;
public CDcomp(ClinicalDrug cd) {
this.cd = cd;
}
public ClinicalDrug getCD() {
return(this.cd);
}
public abstract boolean isEquals(Object o);
protected boolean isSameIngredientSize(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof CDcomp)) {
return false;
}
CDcomp c = (CDcomp) o;
// compare number of ClinicalDrugComponent
int size1 = cd.getCDCs().size();
int size2 = c.getCD().getCDCs().size();
if (size1 != size2) {
return(false);
}
return(true);
}
public abstract int getHashCode();
@Override
public boolean equals(Object o) {
return(isEquals(o));
}
@Override
public int hashCode() {
return(getHashCode());
}
}
package fr.erias.frenchdrugs.comparators;
import fr.erias.frenchdrugs.models.ClinicalDrug;
import fr.erias.frenchdrugs.models.ClinicalDrugComponent;
public class CDcompIN extends CDcomp {
public CDcompIN(ClinicalDrug cd) {
super(cd);
}
public static boolean checkSameIngredients(ClinicalDrug cd1, ClinicalDrug cd2) {
// compare number of ClinicalDrugComponent
int size1 = cd1.getCDCs().size();
int size2 = cd2.getCDCs().size();
if (size1 != size2) {
return(false);
}
// compare the substance:
for (int i = 0; i<size1; i++) {
ClinicalDrugComponent cdc1 = cd1.getCDCs().get(i);
String substanceId1 = cdc1.getSubstanceStrength().getSubstanceId();
ClinicalDrugComponent cdc2 = cd2.getCDCs().get(i);
String substanceId2 = cdc2.getSubstanceStrength().getSubstanceId();
// they are ordered by substanceId
if (!substanceId1.equals(substanceId2)) {
return(false);
}
}
return(true);
}
@Override
public boolean isEquals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof CDcompIN)) {
return false;
}
CDcompIN c = (CDcompIN) o;
return(checkSameIngredients(this.cd, c.getCD()));
}
@Override
public int getHashCode() {
int result = 17;
for (ClinicalDrugComponent cdc1 : cd.getCDCs()) {
result = result + 31 * cdc1.getSubstanceStrength().getSubstanceId().hashCode();
}
return result;
}
}
package fr.erias.frenchdrugs.comparators;
import fr.erias.frenchdrugs.models.ClinicalDrug;
import fr.erias.frenchdrugs.models.ClinicalDrugComponent;
import fr.erias.frenchdrugs.models.DoseForm;
public class CDcompINDoseForm extends CDcomp {
public CDcompINDoseForm(ClinicalDrug cd) {
super(cd);
}
public static boolean checkDoseForm(ClinicalDrug cd1, ClinicalDrug cd2) {
DoseForm df1 = cd1.getDoseForm();
DoseForm df2 = cd2.getDoseForm();
if (!df1.equals(df2)) {
return(false);
} else {
return(true);
}
}
@Override
public boolean isEquals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof CDcompINDoseForm)) {
return false;
}
CDcompINDoseForm c = (CDcompINDoseForm) o;
// check same ingredient:
CDcompIN.checkSameIngredients(this.getCD(), c.getCD());
// check same dose form:
if (!CDcompINDoseForm.checkDoseForm(c.getCD(), this.getCD())) {
return(false);
}
return(true);
}
@Override
public int getHashCode() {
int result = 17;
for (ClinicalDrugComponent cdc1 : cd.getCDCs()) {
result = result + 31 * cdc1.getSubstanceStrength().getSubstanceId().hashCode();
}
result = result + this.cd.getDoseForm().hashCode();
return result;
}
}
package fr.erias.frenchdrugs.comparators;
import fr.erias.frenchdrugs.models.ClinicalDrug;
import fr.erias.frenchdrugs.models.ClinicalDrugComponent;
import fr.erias.frenchdrugs.models.ISubstanceStrength;
public class CDcompINstrength extends CDcomp {
public CDcompINstrength(ClinicalDrug cd) {
super(cd);
}
public static boolean checkSubstanceStrength(ClinicalDrug cd1, ClinicalDrug cd2) {
// compare number of ClinicalDrugComponent
int size1 = cd1.getCDCs().size();
int size2 = cd2.getCDCs().size();
if (size1 != size2) {
return(false);
}
// compare the substance:
for (int i = 0; i<size1; i++) {
ClinicalDrugComponent cdc1 = cd1.getCDCs().get(i);
ISubstanceStrength substanceStrength1 = cdc1.getSubstanceStrength();
ClinicalDrugComponent cdc2 = cd2.getCDCs().get(i);
ISubstanceStrength substanceStrength2 = cdc2.getSubstanceStrength();
// they are ordered by substanceId
if (!substanceStrength1.equals(substanceStrength2)) {
return(false);
}
}
return(true);
}
@Override
public boolean isEquals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof CDcompINstrength)) {
return false;
}
CDcompINstrength c = (CDcompINstrength) o;
if (!CDcompINstrength.checkSubstanceStrength(c.getCD(), this.getCD())) {
return(false);
}
return(true);
}
@Override
public int getHashCode() {
int result = 17;
for (ClinicalDrugComponent cdc1 : cd.getCDCs()) {
result = result + 31 * cdc1.getSubstanceStrength().hashCode();
}
return result;
}
}
package fr.erias.frenchdrugs.load;
public class Compo {
private int cis;
private String dosageform;
private int typecode;
private String sacode;
private String sastrength;
private String sawikidata;
private float saamount;
private String saucumunit;
private String ftcode;
private String ftstrength;
private String ftwikidata;
private float ftamount;
private String ftucumunit;
public Compo(int cis, String dosageform, int typecode, String sacode) {
this.cis = cis;
this.dosageform = dosageform;
}
public static void main(String[] args) {
}
}
package fr.erias.frenchdrugs.load;
public class InvalidFileException extends Exception {
public InvalidFileException(String string) {
super(string);
}
}
package fr.erias.frenchdrugs.load;
import java.io.IOException;
public class LoadCompo extends LoadFile {
public LoadCompo() {
this.filename = "drug_composition.tsv";
this.header = "cis dosageform typecode sacode sastrength sawikidata saamount saucumunit ftcode ftstrength ftwikidata ftamount ftucumunit";
this.nColumns = 13;
this.sep = "\t";
}
@Override
public void handleColumns(String[] columns) {
}
public static void main(String[] args) throws IOException, InvalidFileException {
new LoadCompo().loadFile();
}
}
package fr.erias.frenchdrugs.load;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public abstract class LoadFile {
protected String filename = "some_file_name";
protected String sep = "\t";
protected String header = "col1 col2 col3";
protected int nColumns = 3;
public void loadFile() throws IOException, InvalidFileException {
FileInputStream in = new FileInputStream(filename);
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String header = br.readLine();
checkHeader(header);
int lineNumber = 1;
while ((line = br.readLine()) != null) {
String[] columns = getColumns(line, lineNumber);
handleColumns(columns);
lineNumber += 1;
}
br.close();
}
public abstract void handleColumns(String[] columns);
public void checkHeader(String header) throws InvalidFileException {
if (!header.equals(this.header)) {
throw new InvalidFileException("header doesn't match");
}
}
public String[] getColumns(String line, int lineNumber) {
String[] columns = line.split(sep, -1);
if (columns.length != nColumns) {
String error = String.format("Incorrect number of columns %d:"
+ "\nline %d: ----> %s", columns.length, lineNumber, line);
throw new ArrayIndexOutOfBoundsException(error);
}
return(columns);
}
}
package fr.erias.frenchdrugs.models;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
/**
* Ingredient + Strength + Dose Form ; ex: Fluoxetine 4 MG/ML Oral Solution
* It may contain multiple substance
* @author Sebastien Cossin
*
*/
public class ClinicalDrug {
/**
* Sorted arrayList by substance name
*/
private final ArrayList<ClinicalDrugComponent> cdcs;
private final DoseForm df;
public ClinicalDrug(Collection<ClinicalDrugComponent> cdcs, DoseForm df) {
this.cdcs = new ArrayList<ClinicalDrugComponent>();
for (ClinicalDrugComponent cdc : cdcs) {
this.cdcs.add(cdc);
}
// sort by substanceId
Collections.sort(this.cdcs,new Comparator<ClinicalDrugComponent>() {
@Override
public int compare(ClinicalDrugComponent o1, ClinicalDrugComponent o2) {
String substance1 = o1.getSubstanceStrength().getSubstanceId();
String substance2 = o2.getSubstanceStrength().getSubstanceId();
return substance1.compareTo(substance2);
}
});
this.df = df;
}
public ArrayList<ClinicalDrugComponent> getCDCs() {
return(this.cdcs);
}
public HashSet<ClinicalDrugComponent> getSetCDCs() {
return(this.getSetCDCs());
}
public DoseForm getDoseForm() {
return(this.df);
}
/**
* copy the arraylist to sort for the toString method
* @return
*/
private ArrayList<ClinicalDrugComponent> getCopyArrayList() {
ArrayList<ClinicalDrugComponent> newArrayList = new ArrayList<ClinicalDrugComponent>();
for (ClinicalDrugComponent cdc : this.cdcs) {
newArrayList.add(cdc);
}
return(newArrayList);
}
public String toString() {
// sort alphabetic substance name order
ArrayList<ClinicalDrugComponent> newArrayList = getCopyArrayList();
Collections.sort(newArrayList,new Comparator<ClinicalDrugComponent>() {
@Override
public int compare(ClinicalDrugComponent o1, ClinicalDrugComponent o2) {
String prefLabel1 = o1.getSubstanceStrength().terminology();
String prefLabel2 = o2.getSubstanceStrength().terminology();
return prefLabel1.compareTo(prefLabel2);
}
});
StringBuilder sb = new StringBuilder();
for (ClinicalDrugComponent cdc : newArrayList) {
sb.append(cdc.toString());
sb.append(" / ");
}
sb.setLength(sb.length() - 2); // remove the "/ " before inserting the dose form
sb.append(df.toString());
return(sb.toString());
}
}
package fr.erias.frenchdrugs.models;
import fr.erias.frenchdrugs.solvent.Concentration;
import fr.erias.frenchdrugs.solvent.ISolvent;
/**
* Clinical Drug Component. Ex: Fluoxetine 4 MG/ML
* {@link IN} + {@link Strength}
* @author Sebastien Cossin
*
*/
public class ClinicalDrugComponent {
private final ISubstanceStrength substanceStrength;
private final ISolvent solvent;
public ClinicalDrugComponent(ISubstanceStrength substanceStrength, ISolvent solvent) {
this.substanceStrength = substanceStrength;
this.solvent = solvent;
}
public ISubstanceStrength getSubstanceStrength() {
return substanceStrength;
}
public ISolvent getSolvent() {
return solvent;
}
public String toString() {
Concentration con = new Concentration(substanceStrength.getStrength(), this.solvent);
StringBuilder sb = new StringBuilder();
sb.append(substanceStrength.terminology().trim());
sb.append(" ");
sb.append(con.getConcentration());
return(sb.toString());
}
}
package fr.erias.frenchdrugs.models;
/**
* Dose Form. Ex: Oral Solution
* @author Sebastien Cossin
*
*/
public class DoseForm {
private final String doseFormId;
private final String prefLabel;
public DoseForm(String id, String prefLabel) {
this.doseFormId = id;
this.prefLabel = prefLabel;
}
public String getDoseFormId() {
return doseFormId;
}
public String getPrefLabel() {
return prefLabel;
}
public String toString() {
return(this.prefLabel);
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof DoseForm)) {
return false;
}
DoseForm c = (DoseForm) o;
return this.doseFormId.equals(c.getDoseFormId());
}
@Override
public int hashCode() {
return(this.doseFormId.hashCode());
}
}
package fr.erias.frenchdrugs.models;
/**
* Ingredient. Ex: Fluoxetine
* A compound or moiety that gives the drug its distinctive clinical properties. Ingredients generally use the United States Adopted Name (USAN).
* @author Sebastien Cossin
*
*/
public class IN implements ISubstance {
private final String substanceId;
private final String prefLabel;
public IN(String substanceId, String prefLabel) {
this.substanceId = substanceId;
this.prefLabel = prefLabel;
}
public String getSubstanceId() {
return substanceId;
}
public String terminology() {
return prefLabel;
}
/**
* return true if same substanceId
*/
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof ISubstance)) {
return false;
}
ISubstance c = (ISubstance) o;
return this.substanceId.equals(c.getSubstanceId());
}
@Override
public int hashCode() {
return(this.substanceId.hashCode());
}
}
package fr.erias.frenchdrugs.models;
public class INStrength implements ISubstanceStrength {
private final IN in;
private final Strength strength;
public INStrength(IN in, Strength strength) {