Skip to content

Commit b783627

Browse files
committed
Fix for parsing Jronn model*.rec on Windows.
1 parent 5602c57 commit b783627

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

biojava-protein-disorder/src/main/java/org/biojava/nbio/ronn/ModelLoader.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,25 @@ void loadModels() throws NumberFormatException, IOException {
170170
"model" + i + ".rec"),
171171
"ISO-8859-1"));
172172
String line = null;
173-
final Scanner scan = new Scanner(bfr);
174-
scan.useDelimiter(System.getProperty("line.separator"));
175-
final int numberOfSeqs = scan.nextInt();
173+
line = bfr.readLine().trim();
174+
final int numberOfSeqs = Integer.parseInt(line);
176175
final Model model = new Model(i, numberOfSeqs);
177176
// ignore this one, its always 19 defined in RonnConstrain
178-
scan.nextInt();
177+
line = bfr.readLine();
179178
for (int j = 0; j < numberOfSeqs; j++) {
180-
line = scan.next();
181-
final char[] dbseq = line.trim().toCharArray();
182-
assert dbseq.length < Short.MAX_VALUE;
183-
model.Length[j] = (short) dbseq.length;
184-
for (int dResidue = 0; dResidue < dbseq.length; dResidue++) {
185-
model.dbAA[j][dResidue] = RonnConstraint.INDEX[dbseq[dResidue] - 'A'];
186-
assert !((model.dbAA[j][dResidue] < 0) || (model.dbAA[j][dResidue] > 19));
187-
}
188-
model.W[j] = scan.nextFloat();
189-
}
179+
line = bfr.readLine();
180+
final char[] dbseq = line.trim().toCharArray();
181+
assert dbseq.length < Short.MAX_VALUE;
182+
model.Length[j] = (short) dbseq.length;
183+
for (int dResidue = 0; dResidue < dbseq.length; dResidue++) {
184+
model.dbAA[j][dResidue] = RonnConstraint.INDEX[dbseq[dResidue] - 'A'];
185+
assert !((model.dbAA[j][dResidue] < 0) || (model.dbAA[j][dResidue] > 19));
186+
}
187+
line = bfr.readLine().trim();
188+
model.W[j] = Float.parseFloat(line);
189+
}
190190
ModelLoader.models.put(model.modelNum, model);
191191
bfr.close();
192-
scan.close();
193192
}
194193
}
195194

biojava-protein-disorder/src/test/java/org/biojava/nbio/ronn/JronnTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public class JronnTest {
3131

32-
@Test
32+
// Implemented with two platform checks.
3333
public void verifyRanges() {
3434

3535
Range[] ranges = Jronn.getDisorder(new FastaSequence("name", "LLRGRHLMNGTMIMRPWNFLNDHHFPKFFPHLIEQQAIWLADWWRKKHC" +
@@ -51,4 +51,52 @@ public void verifyRanges() {
5151
assertEquals(313, ranges[3].to);
5252
//System.out.println(Arrays.toString(ranges));
5353
}
54+
55+
/**
56+
* Jronn was breaking on Windows platform due to the different System.getProperty("line.separator") values
57+
* (CRLF vs LF). This wraps the existing unit testing to show that it works on windows or unix.
58+
*/
59+
@Test
60+
public void checkJronn() {
61+
// Windows CRLF
62+
ScopedProperty lineSepProp = new ScopedProperty("line.separator", "\r\n");
63+
verifyRanges();
64+
lineSepProp.close();
65+
66+
// UNIX LF
67+
ScopedProperty lineSepPropUnix = new ScopedProperty("line.separator", "\n");
68+
verifyRanges();
69+
lineSepPropUnix.close();
70+
}
71+
72+
/**
73+
* A scoped property helper class to check with Windows style CRLF.
74+
* Credit Thomas Klambauer, but here have removed the implement of
75+
* AutoCloseable (Java 7) for BioJava support of Java 6.
76+
*/
77+
public class ScopedProperty {
78+
79+
private final String key;
80+
private final String oldValue;
81+
82+
/**
83+
*
84+
* @param key The System.setProperty key
85+
* @param value The System.setProperty value to switch to.
86+
*/
87+
public ScopedProperty(final String key, final String value) {
88+
this.key = key;
89+
oldValue = System.setProperty(key, value);
90+
}
91+
92+
public void close() {
93+
// Can't use setProperty(key, null) -> Throws NullPointerException.
94+
if( oldValue == null ) {
95+
// Previously there was no entry.
96+
System.clearProperty(key);
97+
} else {
98+
System.setProperty(key, oldValue);
99+
}
100+
}
101+
}
54102
}

0 commit comments

Comments
 (0)