use of net.sf.mzmine.parameters.UserParameter in project mzmine2 by mzmine.
the class ProjectParametersImporter method importParameters.
public boolean importParameters() {
// Let user choose a CSV file for importing
File parameterFile = chooseFile();
if (parameterFile == null) {
logger.info("Parameter importing cancelled.");
return false;
}
// Read and interpret parameters
UserParameter<?, ?>[] parameters = processParameters(parameterFile);
if (parameters == null)
return false;
// TODO: Show a dialog for selecting which parameters to import and edit
// their types
// Read values of parameters and store them in the project
processParameterValues(parameterFile, parameters);
return true;
}
use of net.sf.mzmine.parameters.UserParameter in project mzmine2 by mzmine.
the class ProjectParametersImporter method processParameters.
private UserParameter<?, ?>[] processParameters(File parameterFile) {
ArrayList<UserParameter<?, ?>> parameters = new ArrayList<UserParameter<?, ?>>();
// Open reader
BufferedReader parameterFileReader;
try {
parameterFileReader = new BufferedReader(new FileReader(parameterFile));
// Read column headers which are used as parameter names
String firstRow = parameterFileReader.readLine();
StringTokenizer st = new StringTokenizer(firstRow, ",");
// Assume first column contains file names
st.nextToken();
ArrayList<String> parameterNames = new ArrayList<String>();
Hashtable<String, ArrayList<String>> parameterValues = new Hashtable<String, ArrayList<String>>();
while (st.hasMoreTokens()) {
String paramName = st.nextToken();
if (parameterValues.containsKey(paramName)) {
logger.severe("Did not import parameters because of a non-unique parameter name: " + paramName);
desktop.displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Could not open file " + parameterFile);
parameterFileReader.close();
return null;
}
parameterNames.add(paramName);
parameterValues.put(paramName, new ArrayList<String>());
}
// Read rest of the rows which contain file name in the first column
// and parameter values in the rest of the columns
String nextRow = parameterFileReader.readLine();
int rowNumber = 2;
while (nextRow != null) {
st = new StringTokenizer(nextRow, ",");
// Skip first column (File name)
if (st.hasMoreTokens())
st.nextToken();
Iterator<String> parameterNameIterator = parameterNames.iterator();
while (st.hasMoreTokens()) {
if (st.hasMoreTokens() ^ parameterNameIterator.hasNext()) {
logger.severe("Incorrect number of parameter values on row " + rowNumber);
desktop.displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Incorrect number of parameter values on row " + rowNumber);
parameterFileReader.close();
return null;
}
parameterValues.get(parameterNameIterator.next()).add(st.nextToken());
}
nextRow = parameterFileReader.readLine();
rowNumber++;
}
// Decide parameter types (all numeric => Double, all unique string
// => String, at least one duplicate string => Object with possible
// values
Iterator<String> parameterNameIterator = parameterNames.iterator();
while (parameterNameIterator.hasNext()) {
String name = parameterNameIterator.next();
ArrayList<String> vals = parameterValues.get(name);
// Test for all numeric
Iterator<String> valIterator = vals.iterator();
boolean isAllNumeric = true;
while (valIterator.hasNext()) {
try {
Double.valueOf(valIterator.next());
} catch (NumberFormatException ex) {
isAllNumeric = false;
break;
}
}
if (isAllNumeric) {
parameters.add(new DoubleParameter(name, null));
continue;
}
// Test for "set of values"
ArrayList<String> uniqueValues = new ArrayList<String>();
valIterator = vals.iterator();
while (valIterator.hasNext()) {
String val = valIterator.next();
if (!uniqueValues.contains(val))
uniqueValues.add(val);
}
if (uniqueValues.size() < vals.size()) {
parameters.add(new ComboParameter<String>(name, null, uniqueValues.toArray(new String[0])));
continue;
}
// Otherwise it is a free text parameter
parameters.add(new StringParameter(name, null));
}
// Close reader
parameterFileReader.close();
} catch (IOException ex) {
logger.severe("Could not read file " + parameterFile);
desktop.displayErrorMessage(MZmineCore.getDesktop().getMainWindow(), "Could not open file " + parameterFile);
return null;
}
return parameters.toArray(new UserParameter[0]);
}
use of net.sf.mzmine.parameters.UserParameter in project mzmine2 by mzmine.
the class ProjectParametersSetupDialog method copyParameterValuesToRawDataFiles.
private void copyParameterValuesToRawDataFiles() {
MZmineProject currentProject = MZmineCore.getProjectManager().getCurrentProject();
// Remove all previous parameters from project
UserParameter<?, ?>[] parameters = currentProject.getParameters();
for (UserParameter<?, ?> parameter : parameters) {
currentProject.removeParameter(parameter);
}
// Add new parameters
parameters = parameterValues.keySet().toArray(new UserParameter[0]);
for (UserParameter<?, ?> parameter : parameters) {
currentProject.addParameter(parameter);
}
// Set values for new parameters
for (int columnIndex = 0; columnIndex < parameterValues.keySet().size(); columnIndex++) {
UserParameter<?, ?> parameter = tablemodelParameterValues.getParameter(columnIndex + 1);
for (int dataFileIndex = 0; dataFileIndex < dataFiles.length; dataFileIndex++) {
RawDataFile file = dataFiles[dataFileIndex];
Object value = tablemodelParameterValues.getValueAt(dataFileIndex, columnIndex + 1);
if (parameter instanceof DoubleParameter) {
Double doubleValue = null;
if (value instanceof Double)
doubleValue = (Double) value;
if (value instanceof String)
doubleValue = Double.parseDouble((String) value);
currentProject.setParameterValue(parameter, file, doubleValue);
}
if (parameter instanceof StringParameter) {
if (value == null)
value = "";
currentProject.setParameterValue(parameter, file, (String) value);
}
if (parameter instanceof ComboParameter) {
if (value == null)
value = "";
currentProject.setParameterValue(parameter, file, (String) value);
}
}
}
}
use of net.sf.mzmine.parameters.UserParameter in project mzmine2 by mzmine.
the class ProjectParametersSetupDialog method copyParameterValuesFromRawDataFiles.
private void copyParameterValuesFromRawDataFiles() {
MZmineProject currentProject = MZmineCore.getProjectManager().getCurrentProject();
for (int dataFileIndex = 0; dataFileIndex < dataFiles.length; dataFileIndex++) {
RawDataFile file = dataFiles[dataFileIndex];
UserParameter<?, ?>[] parameters = currentProject.getParameters();
// Loop through all parameters defined for this file
for (UserParameter<?, ?> p : parameters) {
// Check if this parameter has been seen before?
Object[] values;
if (!(parameterValues.containsKey(p))) {
// No, initialize a new array of values for this parameter
values = new Object[dataFiles.length];
for (int i = 0; i < values.length; i++) values[i] = p.getValue();
parameterValues.put(p, values);
} else {
values = parameterValues.get(p);
}
// Set value of this parameter for the current raw data file
values[dataFileIndex] = currentProject.getParameterValue(p, file);
}
}
}
use of net.sf.mzmine.parameters.UserParameter in project mzmine2 by mzmine.
the class UserParameterSaveHandler method saveParameters.
/**
* Function which creates an XML file with user parameters
*/
void saveParameters() throws SAXException, IOException, TransformerConfigurationException {
logger.info("Saving user parameters");
StreamResult streamResult = new StreamResult(finalStream);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
hd.setResult(streamResult);
hd.startDocument();
UserParameter<?, ?>[] projectParameters = project.getParameters();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", UserParameterElementName.COUNT.getElementName(), "CDATA", String.valueOf(projectParameters.length));
hd.startElement("", "", UserParameterElementName.PARAMETERS.getElementName(), atts);
atts.clear();
// <PARAMETER>
for (UserParameter<?, ?> parameter : project.getParameters()) {
if (canceled)
return;
logger.finest("Saving user parameter " + parameter.getName());
atts.addAttribute("", "", UserParameterElementName.NAME.getElementName(), "CDATA", parameter.getName());
atts.addAttribute("", "", UserParameterElementName.TYPE.getElementName(), "CDATA", parameter.getClass().getSimpleName());
hd.startElement("", "", UserParameterElementName.PARAMETER.getElementName(), atts);
atts.clear();
fillParameterElement(parameter, hd);
hd.endElement("", "", UserParameterElementName.PARAMETER.getElementName());
completedParameters++;
}
hd.endElement("", "", UserParameterElementName.PARAMETERS.getElementName());
hd.endDocument();
}
Aggregations