Skip to content

Commit

Permalink
Short syntax for same basic anchor point gene features. This fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolotin committed Sep 25, 2015
1 parent 622b80f commit 7143bfb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
75 changes: 50 additions & 25 deletions src/main/java/com/milaboratory/mixcr/reference/GeneFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,35 +615,52 @@ private static GeneFeature parseSingle(String string) {
return new GeneFeature(ReferencePoint.parse(fromTo[0]), ReferencePoint.parse(fromTo[1]));
} else { // feature by name CDR2(-2,3)
int br = string.indexOf('(');
GeneFeature base;
if (br == -1)

if (br == -1) {
GeneFeature base;
base = getFeatureByName(string);
else
base = getFeatureByName(string.substring(0, br));
if (base == null)
throw new IllegalArgumentException("Unknown feature: " + string);

if (br == -1) return base;

int offset1, offset2;
String[] offsets = string.substring(br + 1, string.length() - 1).split(",");
try {
offset1 = Integer.parseInt(offsets[0].trim());
offset2 = Integer.parseInt(offsets[1].trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Incorrect input: " + string);
if (base == null)
throw new IllegalArgumentException("Unknown feature: " + string);
return base;
} else {
if (string.charAt(string.length() - 1) != ')')
throw new IllegalArgumentException("Wrong syntax: " + string);

Object base;

String baseName = string.substring(0, br);

base = getFeatureByName(baseName);

if (base == null)
base = ReferencePoint.getPointByName(baseName);

if (base == null)
throw new IllegalArgumentException("Unknown feature / anchor point: " + baseName);

int offset1, offset2;
String[] offsets = string.substring(br + 1, string.length() - 1).split(",");
try {
offset1 = Integer.parseInt(offsets[0].trim());
offset2 = Integer.parseInt(offsets[1].trim());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Incorrect input: " + string);
}
if (base instanceof GeneFeature)
return new GeneFeature((GeneFeature) base, offset1, offset2);
else
return new GeneFeature((ReferencePoint) base, offset1, offset2);
}
return new GeneFeature(base, offset1, offset2);
}
}

public static String encode(GeneFeature feature) {
return encode(feature, true);
}

static String encode(GeneFeature feature, boolean shortNameForKnownFeatures) {
static String encode(GeneFeature feature, boolean copact) {
ensureInitialized();
if (shortNameForKnownFeatures) {
if (copact) {
String s = nameByFeature.get(feature);
if (s != null)
return s;
Expand All @@ -653,16 +670,24 @@ static String encode(GeneFeature feature, boolean shortNameForKnownFeatures) {
out:
for (int i = 0; i < encodes.length; ++i) {
ReferenceRange region = feature.regions[i];
if (shortNameForKnownFeatures)
if (copact) {
String base = null;
for (GeneFeature a : available)
if (match(region, a)) {
GeneFeature known = new GeneFeature(region.getWithoutOffset());
String base = getNameByFeature(known);
if (region.hasOffsets())
base += "(" + region.begin.offset + ", " + region.end.offset + ")";
encodes[i] = base;
continue out;
base = getNameByFeature(known);
}

if (region.begin.basicPoint == region.end.basicPoint)
base = ReferencePoint.encode(region.begin.getWithoutOffset(), true);

if (base != null) {
if (region.hasOffsets())
base += "(" + region.begin.offset + ", " + region.end.offset + ")";
encodes[i] = base;
continue out;
}
}
encodes[i] = "{" + ReferencePoint.encode(region.begin, true) + ":" + ReferencePoint.encode(region.end, false) + "}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ public void testParse2() throws Exception {
assertEncode("{FR1Begin(-33):FR3End(3)}+JRegion+CExon1(-3,12)");
}

@Test
public void testParse3() throws Exception {
assertEncode("CDR3Begin(0, 10)");
assertEncode("V5UTRBeginTrimmed(0, 10)");
}

@Test(expected = IllegalArgumentException.class)
public void testParse4() throws Exception {
GeneFeature.parse("CDR3Begin");
}

@Test
public void testContains1() throws Exception {
assertTrue(CDR3.contains(VJJunction));
Expand Down

0 comments on commit 7143bfb

Please sign in to comment.