Skip to content

Commit

Permalink
WRP-510 WRP-550 Fix to Algorithm 3 and add another to match where gro…
Browse files Browse the repository at this point in the history
…up composition changes
  • Loading branch information
pgwilliams committed Jun 23, 2015
1 parent 3a075ca commit c7f4ee3
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 26 deletions.
3 changes: 2 additions & 1 deletion replace-redundant-relationships.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ then
exit -1
fi
set -x;
java -Xms4g -Xmx5g -classpath ${executable} org.ihtsdo.snomed.util.rf2.RelationshipProcessor ${sourceDir}/sct2_StatedRelationship_Snapshot_INT_${effectiveTime}.txt ${sourceDir}/sct2_Relationship_Snapshot_INT_${effectiveTime}.txt target/sct2_StatedRelationship_Snapshot_INT_{effectiveTime}.txt
today=`date +%Y%m%d`
java -Xms4g -Xmx5g -classpath ${executable} org.ihtsdo.snomed.util.rf2.RelationshipProcessor ${sourceDir}/sct2_StatedRelationship_Snapshot_INT_${effectiveTime}.txt ${sourceDir}/sct2_Relationship_Snapshot_INT_${effectiveTime}.txt target/sct2_StatedRelationship_Delta_INT_${today}.txt


84 changes: 80 additions & 4 deletions src/main/java/org/ihtsdo/snomed/util/rf2/Concept.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Concept implements Comparable<Concept> {
}

Set<Concept> parents = new TreeSet<Concept>();
Set<Relationship> attributes = new HashSet<Relationship>();
TreeSet<Relationship> attributes = new TreeSet<Relationship>();

public Concept(Long id) {
this.id = id;
Expand Down Expand Up @@ -145,7 +145,45 @@ public List<Relationship> findMatchingRelationships(Long typeId, int group, Conc
return secondPassMatches;
}

private boolean hasParent(Concept targetConcept) {
public List<Relationship> findMatchingRelationships(Long typeId, Concept statedDestinationConcept) {
// find relationships of this concept with the same type and group, and where the destination has the
// statedDestinationConcept as a parent.

// lets match on type first since it's cheap
List<Relationship> firstPassMatches = findMatchingRelationships(typeId);

// Now we'll try for an exact match with the destination concept, and if not found,
// run again looking for more proximate children
List<Relationship> secondPassMatches = new ArrayList<Relationship>();
for (Relationship thisRelationship : firstPassMatches) {
if (thisRelationship.getDestinationConcept().equals(statedDestinationConcept)) {
secondPassMatches.add(thisRelationship);
}
}

if (secondPassMatches.size() == 0) {
for (Relationship thisRelationship : firstPassMatches) {
if (thisRelationship.getDestinationConcept().hasParent(statedDestinationConcept)) {
secondPassMatches.add(thisRelationship);
}
}
}

return secondPassMatches;
}

private List<Relationship> findMatchingRelationships(Long typeId) {
// find relationships of this concept with the same type
List<Relationship> matches = new ArrayList<Relationship>();
for (Relationship thisRelationship : attributes) {
if (thisRelationship.isType(typeId)) {
matches.add(thisRelationship);
}
}
return matches;
}

public boolean hasParent(Concept targetConcept) {
// Recurse through my parents to find if one of them is the targetConcept
// Will stop at the root concept, returning false, as it has no parents
for (Concept thisParent : parents) {
Expand Down Expand Up @@ -178,12 +216,15 @@ public List<Relationship> findMatchingRelationships(Long typeId, Long destinatio
return matches;
}

public List<Relationship> findMatchingRelationships(int group) {
public List<Relationship> findMatchingRelationships(int group, boolean filterIsAs) {
// find relationships of this concept with the group
List<Relationship> matches = new ArrayList<Relationship>();
for (Relationship thisRelationship : attributes) {
if (thisRelationship.matchesGroup(group)) {
matches.add(thisRelationship);
// Are we filtering out Is A relationships?
if (!filterIsAs || (filterIsAs && !thisRelationship.isType(Relationship.ISA_ID))) {
matches.add(thisRelationship);
}
}
}
return matches;
Expand Down Expand Up @@ -219,4 +260,39 @@ public List<Relationship> findMatchingRelationships(String triplesHash, Relation
return null;
}

/**
* @return a list of relationships in groups where the group contains at least ALL the given relationship types
*/
public List<Relationship> findMatchingRelationships(List<Long> groupTypes) {
List<Relationship> allGroupRelationships = new ArrayList<Relationship>();
// Work through all the groups
for (int groupId = 1; groupId <= this.maxGroupId; groupId++) {
boolean allMatch = true;
List<Relationship> thisGroupRelationships = findMatchingRelationships(groupId, false);
// Work through all the types and make sure each one is represented
second_loop:
for (Long thisType : groupTypes) {
for (Relationship thisRelationship : thisGroupRelationships) {
if (thisRelationship.isType(thisType)) {
continue second_loop; // Move on to next type
}
}
// If we've not broken out of the loop by here, then we've failed to find a match for this Type
allMatch = false;
// No need to carry on through other types if we can't match this one
break;
}

// If all types are represented, then add these relationships to our list of potential matches
if (allMatch) {
allGroupRelationships.addAll(thisGroupRelationships);
}
}
return allGroupRelationships;
}

public TreeSet<Relationship> getAttributes() {
return attributes;
}

}
72 changes: 66 additions & 6 deletions src/main/java/org/ihtsdo/snomed/util/rf2/Relationship.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import org.ihtsdo.snomed.util.Type5UuidFactory;

public class Relationship {
public class Relationship implements Comparable<Relationship> {

// id effectiveTime active moduleId sourceId destinationId relationshipGroup typeId characteristicTypeId modifierId
public static final long ISA_ID = 116680003;
public static final String CHARACTERISTIC_STATED_SCTID = "900000000000010007";
public static final String FIELD_DELIMITER = "\t";
public static final String LINE_DELIMITER = "\r\n";
public static final String ACTIVE_FLAG = "1";
public static final String INACTIVE_FLAG = "0";
public static final String HEADER_ROW = "id\teffectiveTime\tactive\tmoduleId\tsourceId\tdestinationId\trelationshipGroup\ttypeId\tcharacteristicTypeId\tmodifierId\r\n";

public static enum CHARACTERISTIC {
STATED, INFERRED
Expand All @@ -27,12 +31,12 @@ public static enum CHARACTERISTIC {
Concept sourceConcept;
Concept destinationConcept;

Long typeId;
String uuid;
int group;
Relationship replacement = null;
private Long typeId;
private String uuid;
private int group;
private Relationship replacement = null;

boolean needsReplaced = false;
private boolean needsReplaced = false;

public static final int IDX_ID = 0;
public static final int IDX_EFFECTIVETIME = 1;
Expand All @@ -44,6 +48,8 @@ public static enum CHARACTERISTIC {
public static final int IDX_TYPEID = 7;
public static final int IDX_CHARACTERISTICTYPEID = 8;
public static final int IDX_MODIFIERID = 9;
public static final int MAX_COLUMN = 9;


private String[] lineValues;

Expand Down Expand Up @@ -137,4 +143,58 @@ public boolean matchesGroup(int group) {
return (this.group == group);
}

public boolean isType(Long thisType) {
return this.typeId.equals(thisType);
}

public String toString() {
return "[S: " + lineValues[IDX_SOURCEID] + ", D: " + lineValues[IDX_DESTINATIONID] + ", T: " + lineValues[IDX_TYPEID] + ", G: "
+ lineValues[IDX_RELATIONSHIPGROUP] + "] ";
}

public String getRF2(String effectiveTime, String activeFlag, String chacteristicTypeId) {
StringBuffer sb = new StringBuffer();
// Output all columns, replacing effectiveTime, active and chacteristicTypeId to passed in values
for (int columnIdx = 0; columnIdx <= MAX_COLUMN; columnIdx++) {
if (columnIdx == IDX_EFFECTIVETIME) {
sb.append(effectiveTime);
} else if (columnIdx == IDX_ACTIVE) {
sb.append(activeFlag);
} else if (columnIdx == IDX_CHARACTERISTICTYPEID) {
sb.append(chacteristicTypeId);
} else {
sb.append(lineValues[columnIdx]);
}

if (columnIdx < MAX_COLUMN) {
sb.append(FIELD_DELIMITER);
}
}

sb.append(LINE_DELIMITER);
return sb.toString();
}

public Relationship getReplacement() {
return replacement;
}

@Override
public int compareTo(Relationship other) {
// Sort on source sctid, group, type, destination
int i = this.getSourceId().compareTo(other.getSourceId());
if (i != 0)
return i;

i = this.getGroup() - other.getGroup();
if (i != 0)
return i;

i = this.getTypeId().compareTo(other.getTypeId());
if (i != 0)
return i;

return this.getDestinationId().compareTo(other.getDestinationId());
}

}
Loading

0 comments on commit c7f4ee3

Please sign in to comment.