Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent splitting commas in SMARTS expressions #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.ipbhalle.metfraglib.parameter;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Level;

Expand Down Expand Up @@ -264,15 +266,44 @@ public static Object getParameter(String parameter, String parameterName) throws
values[i] = Double.parseDouble(tmp[i]);
return values;
}
if(type.equals("String[]")) {
String[] tmp = parameter.split(",");
for(int i = 0; i < tmp.length; i++)
tmp[i] = tmp[i].trim();
return tmp;
if (type.equals("String[]")) {
if (parameterName.contains("Smarts")) {
return splitSmartsString(parameter);
} else {
String[] tmp = parameter.split(",");
for (int i = 0; i < tmp.length; i++)
tmp[i] = tmp[i].trim();
return tmp;
}
}
return parameter;
}


public static String[] splitSmartsString(String input) {
List<String> substrings = new ArrayList<>();
int start = 0;
int squareBracketCount = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '[') {
squareBracketCount++;
} else if (c == ']') {
squareBracketCount--;
} else if (c == ',' && squareBracketCount == 0) {
String substring = input.substring(start, i).trim();
if (!substring.isEmpty()) {
substrings.add(substring);
}
start = i + 1;
}
}
String lastSubstring = input.substring(start).trim();
if (!lastSubstring.isEmpty()) {
substrings.add(lastSubstring);
}
return substrings.toArray(new String[substrings.size()]);
}

public static String getType(String parameterName) {
if(!parameterDatatypes.containsKey(parameterName)) return "";
return parameterDatatypes.get(parameterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ public void test() {
}
}

@Test
public void testSplitSmartsList() {
/*
* check SMARTS list string splitting
*/
try {
String[] smartsListExpected = {
"C(=O)-O", // no comma in SMARTS
"[C,O]-c1ccccc1", // comma in a pair of square brackets
"[$(c1(-[O,N])ccccc1),$(c1c(-[O,N])cccc1)]-C(=O)-O", // comma in nested square brackets
};
String smartsListString = String.join(",", smartsListExpected);
String[] smartsListActual = (String[]) ParameterDataTypes.getParameter(smartsListString,
VariableNames.PRE_CANDIDATE_FILTER_SMARTS_INCLUSION_LIST_NAME);
assertArrayEquals("\"" + smartsListString + "\" " + VariableNames.PRE_CANDIDATE_FILTER_SMARTS_INCLUSION_LIST_NAME + " was wrongly split",
smartsListExpected,
smartsListActual);
} catch (ParameterNotKnownException e) {
fail(e.getMessage());
}
}

/**
* check exception
*
Expand Down