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

Improvement/more fields refractor some fields #3

Merged
merged 7 commits into from
Oct 30, 2018
Merged
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
18 changes: 10 additions & 8 deletions src/main/conf/import.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
{
"key": "delim",
"title": "Delimiter Character",
"default": ",",
"maximum": 1,
"type": "string",
"description": "Single character used to separate fields within a record. Leave blank to read whitespace-separated columns."
},
{
"key": "col_types",
"title": "Column Types",
"key": "na",
"type": "string",
"format": "textarea",
"default": ".default=\"?\"",
"description": "A compact string representation where each character represents one column: c = character, i = integer, n = number, d = double, l = logical, D = date, T = date time, t = time, ? = guess, or _/- to skip the column."
"title": "Missing Values",
"description": "A comma separated list of values to be considered as missing."
},
{
"key": "is_col_types_subset",
"type": "boolean",
"title": "Subset of Columns"
"key": "skip",
"type": "integer",
"default": 0,
"title": "Skip",
"description": "Number of lines to skip before reading data."
},
{
"key": "locale",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ public class DataReadROperation extends AbstractROperation {

private final String delimiter;

private final String columnSpecification;
private final String missingValuesCharacters;

private final boolean columnSpecificationForSubset;
private final int numberOfRecordsToSkip;

public DataReadROperation(String symbol, String source, String delimiter, String columnSpecification, boolean columnSpecificationForSubset) {
private final String locale;

public DataReadROperation(String symbol,
String source, String delimiter, String missingValuesCharacters, int numberOfRecordsToSkip, String locale) {
this.symbol = symbol;
this.source = source;
this.delimiter = delimiter;
this.columnSpecification = columnSpecification;
this.columnSpecificationForSubset = columnSpecificationForSubset;
this.missingValuesCharacters = missingValuesCharacters;
this.numberOfRecordsToSkip = numberOfRecordsToSkip;
this.locale = locale;
}

@Override
Expand All @@ -38,18 +42,23 @@ private String getCommand() {
}

private String readWithDelimiter() {
return String.format("read_delim('%s', delim = '%s'%s)", source, delimiter, columnTypes());
return String.format("read_delim('%s', delim = '%s'%s%s%s)", source, delimiter, missingValues(), numberOfRecordsToSkipValue(), localeValue());
}

private String readWithTable() {
return String.format("read_table('%s'%s)", source, columnTypes());
return String.format("read_table('%s'%s%s%s)", source, missingValues(), numberOfRecordsToSkipValue(), localeValue());
}

private String missingValues() {
return Strings.isNullOrEmpty(missingValuesCharacters) ? ", na = c(\"\", \"NA\")" : String.format(", na = c(%s)", missingValuesCharacters);
}

private String numberOfRecordsToSkipValue() {
return ", skip = " + numberOfRecordsToSkip;
}

private String columnTypes() {
if (Strings.isNullOrEmpty(columnSpecification)) {
return "";
}
return ", col_types = " + String.format(columnSpecificationForSubset ? "cols_only(%s)" : "cols(%s)", columnSpecification);
private String localeValue() {
return String.format(", locale = locale(\"%s\")", locale);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import javax.validation.constraints.NotNull;

import com.google.common.base.Strings;

import org.json.JSONObject;
import org.obiba.magma.Datasource;
import org.obiba.magma.DatasourceFactory;
Expand Down Expand Up @@ -33,14 +35,16 @@ protected Datasource internalCreate() {
File file = resolvePath(parameters.optString("file"));

String delimiter = parameters.optString("delim");
String columnTypes = parameters.optString("col_types");
boolean columnSpecificationForSubset = parameters.optBoolean("is_col_types_subset");
String missingValuesCharacters = parameters.optString("na");
String locale = parameters.optString("locale");
int skip = parameters.optInt("skip");

String symbol = getSymbol(file);
// copy file to the R session
prepareFile(file);
execute(new DataReadROperation(symbol, file.getName(), delimiter, columnTypes, columnSpecificationForSubset));
return new RDatasource(getName(), getRSessionHandler(), symbol, parameters.optString("entity_type"), parameters.optString("id"));
execute(new DataReadROperation(symbol, file.getName(), delimiter, missingValuesCharacters, skip, Strings.isNullOrEmpty(locale) ? "en" : locale));
return new RDatasource(getName(), getRSessionHandler(), symbol, parameters.optString("entity_type"),
parameters.optString("id"));
}
};
factory.setRSessionHandler(getRSessionHandler());
Expand Down