Skip to content

Commit 7ce7bba

Browse files
committed
Add implementation of makeValidForR for string vectors
1 parent 4a5efcb commit 7ce7bba

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

src/ubic/basecode/util/StringUtil.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.io.IOException;
2929
import java.io.StringReader;
3030
import java.util.Collection;
31+
import java.util.HashMap;
32+
import java.util.Map;
3133

3234
/**
3335
* @author pavlidis
@@ -190,28 +192,52 @@ public static boolean isLatinLetter( char c ) {
190192
};
191193

192194
/**
193-
* Mimics the make.names method in R (character.c) to make valid variables names; we use this for column headers in
194-
* some output files. This doesn't give the exact sames results as R; we avoid repeated '.'.
195+
* Mimics the {@code make.names} method in R (character.c) to make valid variables names; we use this for column
196+
* headers in some output files.
197+
* <p>
198+
* This was modified in 1.1.26 to match the behavior of R more closely, if not exactly.
195199
*
196-
* @param s
200+
* @param s a string to be made valid for R
197201
* @return modified string
198202
* @author paul
199203
*/
200-
public static String makeValidForR( String colName ) {
201-
if ( colName == null ) {
204+
public static String makeValidForR( String s ) {
205+
if ( s == null ) {
202206
return "NA";
203207
}
204-
if ( colName.isEmpty()
208+
if ( s.isEmpty()
205209
// starts with a non-letter or non-dot
206-
|| ( !Character.isAlphabetic( colName.charAt( 0 ) ) && colName.charAt( 0 ) != '.' )
210+
|| ( !Character.isAlphabetic( s.charAt( 0 ) ) && s.charAt( 0 ) != '.' )
207211
// 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._]", "." );
212+
|| ( s.charAt( 0 ) == '.' && s.length() > 1 && Character.isDigit( s.charAt( 1 ) ) ) ) {
213+
return "X" + s.replaceAll( "[^A-Za-z0-9._]", "." );
210214
}
211-
if ( StringUtils.equalsAny( colName, R_RESERVED_WORDS ) ) {
212-
return colName + ".";
215+
if ( StringUtils.equalsAny( s, R_RESERVED_WORDS ) ) {
216+
return s + ".";
213217
}
214-
return colName.replaceAll( "[^A-Za-z0-9._]", "." );
218+
return s.replaceAll( "[^A-Za-z0-9._]", "." );
219+
}
220+
221+
/**
222+
* Mimics the {@code make.names} method in R when using with a vector of strings and the unique argument set to TRUE.
223+
* @author poirigui
224+
*/
225+
public static String[] makeValidForR( String[] strings ) {
226+
String[] result = new String[strings.length];
227+
Map<String, Integer> counts = new HashMap<>();
228+
for ( int i = 0; i < strings.length; i++ ) {
229+
String s = strings[i];
230+
String rs = StringUtil.makeValidForR( s );
231+
if ( counts.containsKey( rs ) ) {
232+
int count = counts.get( rs );
233+
result[i] = rs + "." + count;
234+
counts.put( rs, count + 1 );
235+
} else {
236+
result[i] = rs;
237+
counts.put( rs, 1 );
238+
}
239+
}
240+
return result;
215241
}
216242

217243
/**

test/ubic/basecode/util/StringUtilTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public void testCommonSuffixNone() {
7272
test.add( "aaaab" );
7373
test.add( "aaaacb" );
7474
String suf = StringUtil.commonSuffix( test );
75-
assertEquals( null, suf );
75+
assertNull( suf );
7676
}
7777

7878
@Test
7979
public void testMakeRnames() {
8080
assertFalse( Character.isDigit( '.' ) );
81-
assertEquals( "NA", StringUtil.makeValidForR( null ) );
81+
assertEquals( "NA", StringUtil.makeValidForR( ( String ) null ) );
8282
assertEquals( "test", StringUtil.makeValidForR( "test" ) );
8383
assertEquals( "X", StringUtil.makeValidForR( "X" ) );
8484
assertEquals( "X123", StringUtil.makeValidForR( "123" ) );
@@ -94,6 +94,7 @@ public void testMakeRnames() {
9494
assertEquals( "...f33oo", StringUtil.makeValidForR( "...f33oo" ) );
9595
assertEquals( "X1foo.dd....f..a", StringUtil.makeValidForR( "1foo dd . [f] a" ) );
9696
assertEquals( "X.1foo.dd....f..a", StringUtil.makeValidForR( ".1foo dd . [f] a" ) );
97+
assertArrayEquals( new String[] { "foo", "foo.1", "foo.2", "bar" }, StringUtil.makeValidForR( new String[] { "foo", "foo", "foo", "bar" } ) );
9798
}
9899

99100
}

0 commit comments

Comments
 (0)