Skip to content

Commit 4a5efcb

Browse files
committed
Improve makeValidForR and make it considerably closed to make.names() from R
1 parent 4635670 commit 4a5efcb

2 files changed

Lines changed: 41 additions & 30 deletions

File tree

src/ubic/basecode/util/StringUtil.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ public static boolean isLatinLetter( char c ) {
184184
return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' );
185185
}
186186

187+
private static final String[] R_RESERVED_WORDS = {
188+
"if", "else", "repeat", "while", "function", "for", "in", "next", "break",
189+
"TRUE", "FALSE", "NULL", "Inf", "NaN", "NA", "NA_integer_", "NA_real_", "NA_character_", "NA_complex_",
190+
};
191+
187192
/**
188193
* Mimics the make.names method in R (character.c) to make valid variables names; we use this for column headers in
189194
* some output files. This doesn't give the exact sames results as R; we avoid repeated '.'.
@@ -192,17 +197,21 @@ public static boolean isLatinLetter( char c ) {
192197
* @return modified string
193198
* @author paul
194199
*/
195-
public static String makeValidForR( String s ) {
196-
197-
// If string starts with a digit or "." and then a digit, prepend an X.
198-
if ( s.matches( "^\\.?[0-9].+" ) ) {
199-
s = "X" + s;
200+
public static String makeValidForR( String colName ) {
201+
if ( colName == null ) {
202+
return "NA";
200203
}
201-
202-
// TODO: check for reserved words. https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html
203-
204-
// no dashes or white space or other punctuation. '.' is okay and so is "_", now.
205-
return s.replaceAll( "[\\W]+", "." );
204+
if ( colName.isEmpty()
205+
// starts with a non-letter or non-dot
206+
|| ( !Character.isAlphabetic( colName.charAt( 0 ) ) && colName.charAt( 0 ) != '.' )
207+
// dot followed by a digit
208+
|| ( colName.charAt( 0 ) == '.' && colName.length() > 1 && Character.isDigit( colName.charAt( 1 ) ) ) ) {
209+
return "X" + colName.replaceAll( "[^A-Za-z0-9._]", "." );
210+
}
211+
if ( StringUtils.equalsAny( colName, R_RESERVED_WORDS ) ) {
212+
return colName + ".";
213+
}
214+
return colName.replaceAll( "[^A-Za-z0-9._]", "." );
206215
}
207216

208217
/**

test/ubic/basecode/util/StringUtilTest.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2006 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
@@ -18,17 +18,16 @@
1818
*/
1919
package ubic.basecode.util;
2020

21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertNull;
21+
import org.junit.Test;
2322

2423
import java.util.Collection;
2524
import java.util.HashSet;
2625

27-
import org.junit.Test;
26+
import static org.junit.Assert.*;
2827

2928
/**
3029
* @author pavlidis
31-
*
30+
*
3231
*/
3332
public class StringUtilTest {
3433

@@ -78,20 +77,23 @@ public void testCommonSuffixNone() {
7877

7978
@Test
8079
public void testMakeRnames() {
81-
String actual = StringUtil.makeValidForR( "f33oo dd . [f] a" );
82-
assertEquals( "f33oo.dd.f.a", actual );
83-
84-
actual = StringUtil.makeValidForR( ".f33oo" );
85-
assertEquals( ".f33oo", actual );
86-
87-
actual = StringUtil.makeValidForR( "...f33oo" );
88-
assertEquals( ".f33oo", actual );
89-
90-
actual = StringUtil.makeValidForR( "1foo dd . [f] a" );
91-
assertEquals( "X1foo.dd.f.a", actual );
92-
93-
actual = StringUtil.makeValidForR( ".1foo dd . [f] a" );
94-
assertEquals( "X.1foo.dd.f.a", actual );
80+
assertFalse( Character.isDigit( '.' ) );
81+
assertEquals( "NA", StringUtil.makeValidForR( null ) );
82+
assertEquals( "test", StringUtil.makeValidForR( "test" ) );
83+
assertEquals( "X", StringUtil.makeValidForR( "X" ) );
84+
assertEquals( "X123", StringUtil.makeValidForR( "123" ) );
85+
assertEquals( "X", StringUtil.makeValidForR( "" ) );
86+
assertEquals( "X..", StringUtil.makeValidForR( " " ) );
87+
assertEquals( "if.", StringUtil.makeValidForR( "if" ) );
88+
assertEquals( "TRUE.", StringUtil.makeValidForR( "TRUE" ) );
89+
assertEquals( "...", StringUtil.makeValidForR( "..." ) );
90+
assertEquals( "..", StringUtil.makeValidForR( ". " ) );
91+
assertEquals( "X.2way", StringUtil.makeValidForR( ".2way" ) );
92+
assertEquals( "f33oo.dd....f..a", StringUtil.makeValidForR( "f33oo dd . [f] a" ) );
93+
assertEquals( ".f33oo", StringUtil.makeValidForR( ".f33oo" ) );
94+
assertEquals( "...f33oo", StringUtil.makeValidForR( "...f33oo" ) );
95+
assertEquals( "X1foo.dd....f..a", StringUtil.makeValidForR( "1foo dd . [f] a" ) );
96+
assertEquals( "X.1foo.dd....f..a", StringUtil.makeValidForR( ".1foo dd . [f] a" ) );
9597
}
9698

9799
}

0 commit comments

Comments
 (0)