Search in sources :

Example 1 with FileListParameter

use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.

the class ParamHandler method createPort.

/**
 * Convert the current element into a Port and the respective
 * IFileParameter.
 *
 * @param paramName
 *            The name of the Parameter.
 * @param attributes
 *            Attributes of the Parameter.
 */
private void createPort(String paramName, Attributes attributes, boolean isList) {
    // check if we want to create this port
    if (BLACKLIST.contains(paramName)) {
        LOG.setLevel(Level.ALL);
        LOG.info("Ignoring port: " + paramName);
        return;
    }
    Port p = new Port();
    p.setName(m_currentPath + paramName);
    p.setMultiFile(isList);
    p.setOptional(isOptional(attributes));
    p.setActive(true);
    List<String> mimetypes = extractMIMETypes(attributes);
    for (String mt : mimetypes) {
        p.addMimeType(mt);
    }
    String attr_type = attributes.getValue(ATTR_TYPE);
    boolean isInputPort = TYPE_INPUT_FILE.equals(attr_type) || getTags(attributes).contains(INPUTFILE_TAG) || TYPE_INPUT_PREFIX.equals(attr_type);
    // to save/load settings.
    if (p.isOptional() && !isInputPort) {
        p.addMimeType("Inactive");
    }
    String description = attributes.getValue(ATTR_DESCRIPTION);
    p.setDescription(description);
    m_currentParameter = null;
    // create port parameter
    if (isList) {
        m_currentParameter = new FileListParameter(paramName, new ArrayList<String>());
        ((FileListParameter) m_currentParameter).setPort(p);
        ((FileListParameter) m_currentParameter).setDescription(p.getDescription());
        ((FileListParameter) m_currentParameter).setIsOptional(p.isOptional());
    // Values will be filled at the end of the ITEMLIST tag.
    } else {
        m_currentParameter = new FileParameter(paramName, "");
        ((FileParameter) m_currentParameter).setPort(p);
        ((FileParameter) m_currentParameter).setDescription(p.getDescription());
        ((FileParameter) m_currentParameter).setIsOptional(p.isOptional());
        // Fills parameter with default value
        ((FileParameter) m_currentParameter).setValue(attributes.getValue(ATTR_VALUE));
    }
    p.setIsPrefix(TYPE_OUTPUT_PREFIX.equals(attr_type) || TYPE_INPUT_PREFIX.equals(attr_type));
    if (isInputPort) {
        m_inputPorts.add(p);
    } else {
        m_outputPorts.add(p);
    }
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) Port(com.genericworkflownodes.knime.port.Port) ArrayList(java.util.ArrayList) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter)

Example 2 with FileListParameter

use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.

the class GenericKnimeNodeModel method getOutputBaseNames.

/**
 * Tries to guess the optimal output file names given all the input edges.
 * The file names will be extracted from the configuration, hence the file
 * names need to be transferred into config prior to using this method. See
 * {@link GenericKnimeNodeModel#transferIncomingPorts2Config(PortObject[])}.
 *
 * @return A list of base names for the output files.
 * @throws Exception
 */
private List<String> getOutputBaseNames() throws Exception {
    // 1. we select always the list with the highest number of files.
    // 2. we prefer lists over files (independent of the number of
    // elements).
    // 3. we prefer files over prefixes since we assume that prefixes are
    // often indices or reference data
    // 4. ignore optional parameters
    List<String> basenames = new ArrayList<String>();
    // find the port
    int naming_port = 0;
    int max_size = -1;
    boolean seen_prefix = false;
    boolean isFileParameter = false;
    for (int i = 0; i < m_nodeConfig.getInputPorts().size(); ++i) {
        Port port = m_nodeConfig.getInputPorts().get(i);
        String name = port.getName();
        Parameter<?> p = m_nodeConfig.getParameter(name);
        // we don't assume that optional ports are naming relevant
        if (p.isOptional()) {
            continue;
        }
        if (p instanceof FileListParameter) {
            FileListParameter flp = (FileListParameter) p;
            if (max_size == -1 || (isFileParameter && (max_size <= flp.getValue().size()))) {
                max_size = flp.getValue().size();
                naming_port = i;
            } else if (flp.getValue().size() != max_size) {
                throw new Exception("The number of output files cannot be determined since multiple input file lists with disagreeing numbers exist.");
            }
        } else if (max_size == -1 || seen_prefix) {
            // is a regular incoming port but we have no better option
            max_size = 1;
            naming_port = i;
            // indicating that we have (for now) selected a file parameter
            // which will be overruled by any FileListParameter
            isFileParameter = true;
            seen_prefix = port.isPrefix();
        }
    }
    if (m_nodeConfig.getInputPorts().size() > 0) {
        // generate the filenames if there are input ports
        // without ports, the names are set in transferOutgoingPorts2Config
        Port port = m_nodeConfig.getInputPorts().get(naming_port);
        String name = port.getName();
        Parameter<?> p = m_nodeConfig.getParameter(name);
        if (p instanceof FileListParameter) {
            // we have multiple base names
            FileListParameter flp = (FileListParameter) p;
            for (String fName : flp.getValue()) {
                basenames.add(FilenameUtils.getBaseName(fName));
            }
        } else {
            // we only have a single basename
            // FilenameUtils.getBaseName()
            basenames.add(FilenameUtils.getBaseName(((FileParameter) p).getValue()));
        }
    }
    return basenames;
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) Port(com.genericworkflownodes.knime.port.Port) ArrayList(java.util.ArrayList) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidParameterValueException(com.genericworkflownodes.knime.parameter.InvalidParameterValueException) UnknownToolExecutorException(com.genericworkflownodes.knime.execution.UnknownToolExecutorException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoBinaryAvailableException(com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException) UnknownCommandGeneratorException(com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException)

Example 3 with FileListParameter

use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.

the class CTDConfigurationReaderTest method testRead.

@Test
public void testRead() throws Exception {
    CTDConfigurationReader reader = new CTDConfigurationReader();
    assertNotNull(reader);
    INodeConfiguration config = reader.read(TestDataSource.class.getResourceAsStream("FeatureLinkerUnlabeled.ctd"));
    assertEquals("Map Alignment", config.getCategory());
    assertNotNull(config.getParameter("FeatureLinkerUnlabeled.1.in"));
    assertTrue(config.getParameter("FeatureLinkerUnlabeled.1.in") instanceof FileListParameter);
    assertNotNull(config.getInputPortByName("FeatureLinkerUnlabeled.1.in"));
    assertEquals("1.11.0", config.getVersion());
    FileListParameter flp = (FileListParameter) config.getParameter("FeatureLinkerUnlabeled.1.in");
    assertEquals(0, flp.getValue().size());
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) TestDataSource(com.genericworkflownodes.knime.test.data.TestDataSource) INodeConfiguration(com.genericworkflownodes.knime.config.INodeConfiguration) Test(org.junit.Test)

Example 4 with FileListParameter

use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.

the class CTDConfigurationReaderTest method testSeqAnCTD.

@Test
public void testSeqAnCTD() throws Exception {
    CTDConfigurationReader reader = new CTDConfigurationReader();
    assertNotNull(reader);
    INodeConfiguration config = reader.read(TestDataSource.class.getResourceAsStream("sam2matrix.ctd"));
    assertEquals("Metagenomics", config.getCategory());
    assertEquals("http://www.seqan.de", config.getDocUrl());
    // <ITEM name="out" value="" type="output-file"
    // description="Output file." supported_formats="*.tsv" required="true"
    // advanced="false" />
    FileParameter fp_out = (FileParameter) config.getParameter("sam2matrix.out");
    assertNotNull(fp_out);
    assertEquals("Output file.", fp_out.getDescription());
    assertEquals(1, fp_out.getPort().getMimeTypes().size());
    assertEquals("tsv", fp_out.getPort().getMimeTypes().get(0));
    assertFalse(fp_out.isOptional());
    assertFalse(fp_out.isAdvanced());
    // <ITEMLIST name="mapping" type="input-file"
    // description="File containing the mappings." supported_formats="*.sam"
    // required="true" advanced="false" >
    FileListParameter flp_mapping = (FileListParameter) config.getParameter("sam2matrix.mapping");
    assertNotNull(flp_mapping);
    assertEquals("File containing the mappings.", flp_mapping.getDescription());
    assertEquals(1, flp_mapping.getPort().getMimeTypes().size());
    assertEquals("sam", flp_mapping.getPort().getMimeTypes().get(0));
    assertFalse(flp_mapping.isOptional());
    assertFalse(flp_mapping.isAdvanced());
    // <ITEM name="reads" value="" type="input-file"
    // description="File containing the reads contained in the mapping file(s)."
    // supported_formats="*.fa,*.fasta,*.fq,*.fastq" required="true"
    // advanced="false" />
    FileParameter fp_reads = (FileParameter) config.getParameter("sam2matrix.reads");
    assertNotNull(fp_reads);
    assertEquals("File containing the reads contained in the mapping file(s).", fp_reads.getDescription());
    assertEquals(4, fp_reads.getPort().getMimeTypes().size());
    assertEquals("fa", fp_reads.getPort().getMimeTypes().get(0));
    assertEquals("fasta", fp_reads.getPort().getMimeTypes().get(1));
    assertEquals("fq", fp_reads.getPort().getMimeTypes().get(2));
    assertEquals("fastq", fp_reads.getPort().getMimeTypes().get(3));
    assertFalse(fp_reads.isOptional());
    assertFalse(fp_reads.isAdvanced());
    // <ITEMLIST name="reference" type="string"
    // description="Name of the file used as reference of the corresponding sam file. If not specified the names of the mapping files are taken"
    // required="false" advanced="false" >
    StringListParameter slp_reference = (StringListParameter) config.getParameter("sam2matrix.reference");
    assertNotNull(slp_reference);
    assertEquals("Name of the file used as reference of the corresponding sam file. If not specified the names of the mapping files are taken", slp_reference.getDescription());
    assertTrue(slp_reference.isOptional());
    assertFalse(slp_reference.isAdvanced());
    // the parameter following parameters were tagged with gkn-ignore and
    // hence should not be parsed
    assertNull(config.getParameter("sam2matrix.write-ctd-file-ext"));
    assertNull(config.getParameter("sam2matrix.out-file-ext"));
    assertNull(config.getParameter("sam2matrix.mapping-file-ext"));
    assertNull(config.getParameter("sam2matrix.reads-file-ext"));
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) StringListParameter(com.genericworkflownodes.knime.parameter.StringListParameter) TestDataSource(com.genericworkflownodes.knime.test.data.TestDataSource) INodeConfiguration(com.genericworkflownodes.knime.config.INodeConfiguration) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) Test(org.junit.Test)

Example 5 with FileListParameter

use of com.genericworkflownodes.knime.parameter.FileListParameter in project GenericKnimeNodes by genericworkflownodes.

the class DynamicGenericNodeModel method transferOutgoingPorts2Config.

/**
 * Creates a list of lists of output files (as {@link URI}s) pointing to the
 * files that will be generated by the executed tool.
 *
 * @param jobdir
 *            The working directory of the executable.
 * @param inData
 *            The input data as {@link PortObject} array
 * @return A list of lists of output files
 * @throws Exception
 *             If the input has an invalid configuration.
 */
private List<PortObject> transferOutgoingPorts2Config(final File jobdir, PortObject[] inData, ExecutionContext exec) throws Exception {
    final int nOut = m_nodeConfig.getOutputPorts().size();
    List<PortObject> outPorts = new ArrayList<PortObject>(nOut);
    for (int i = 0; i < nOut; i++) {
        Port port = m_nodeConfig.getOutputPorts().get(i);
        String name = port.getName();
        String ext = "";
        boolean isPrefix = port.isPrefix();
        if (!isPrefix) {
            ext = getOutputType(i);
        }
        Parameter<?> p = m_nodeConfig.getParameter(name);
        // basenames and number of output files guessed from input
        List<String> basenames = getOutputBaseNames();
        if (p instanceof FileListParameter && port.isMultiFile()) {
            // we currently do not support lists of prefixes
            if (isPrefix) {
                throw new InvalidSettingsException("GKN currently does not support lists of prefixes as output.");
            }
            FileListParameter flp = (FileListParameter) p;
            List<String> fileNames = new ArrayList<String>();
            if (basenames.size() == 0) {
                throw new Exception("Cannot determine number of output files if no input file is given.");
            }
            // if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
            PortObject fsupo;
            if (!ext.equals("Inactive")) {
                fsupo = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
                for (int f = 0; f < basenames.size(); ++f) {
                    // create basename: <base_name>_<port_nr>_<outfile_nr>
                    String file_basename = String.format("%s_%d", basenames.get(f), f);
                    File file = ((FileStoreURIPortObject) fsupo).registerFile(file_basename + "." + ext);
                    fileNames.add(file.getAbsolutePath());
                }
            } else {
                fsupo = InactiveBranchPortObject.INSTANCE;
            }
            // add filled portobject
            outPorts.add(fsupo);
            // overwrite existing settings with new values generated by the
            // stash
            flp.setValue(fileNames);
        } else if (p instanceof FileParameter && !port.isMultiFile()) {
            // if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
            PortObject po;
            if (!ext.equals("Inactive")) {
                // if we have no basename to use (e.g., Node without input-file)
                // we use the nodename
                String basename;
                if (basenames.isEmpty()) {
                    basename = m_nodeConfig.getName();
                } else {
                    basename = basenames.get(0);
                }
                // create basename: <base_name>_<outfile_nr>
                String fileName = basename;
                if (!isPrefix) {
                    fileName += '.' + ext;
                }
                if (isPrefix) {
                    po = new FileStorePrefixURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i), fileName);
                    ((FileParameter) p).setValue(((FileStorePrefixURIPortObject) po).getPrefix());
                    LOGGER.debug("> setting param " + name + "->" + ((FileStorePrefixURIPortObject) po).getPrefix());
                } else {
                    po = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
                    // we do not append the file extension if we have a prefix
                    File file = ((FileStoreURIPortObject) po).registerFile(fileName);
                    ((FileParameter) p).setValue(file.getAbsolutePath());
                    LOGGER.debug("> setting param " + name + "->" + file);
                }
            } else {
                po = InactiveBranchPortObject.INSTANCE;
            }
            // remember output file
            outPorts.add(po);
        } else {
            throw new Exception("Invalid connection between ports and parameters.");
        }
    }
    return outPorts;
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) Port(com.genericworkflownodes.knime.port.Port) ArrayList(java.util.ArrayList) FileStorePrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidParameterValueException(com.genericworkflownodes.knime.parameter.InvalidParameterValueException) ExecutionFailedException(com.genericworkflownodes.knime.generic_node.ExecutionFailedException) UnknownToolExecutorException(com.genericworkflownodes.knime.execution.UnknownToolExecutorException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoBinaryAvailableException(com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException) UnknownCommandGeneratorException(com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) IPrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject) FileStorePrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject) PortObject(org.knime.core.node.port.PortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) File(java.io.File)

Aggregations

FileListParameter (com.genericworkflownodes.knime.parameter.FileListParameter)8 FileParameter (com.genericworkflownodes.knime.parameter.FileParameter)7 ArrayList (java.util.ArrayList)6 Port (com.genericworkflownodes.knime.port.Port)5 NoBinaryAvailableException (com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException)4 UnknownCommandGeneratorException (com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException)4 UnknownToolExecutorException (com.genericworkflownodes.knime.execution.UnknownToolExecutorException)4 IFileParameter (com.genericworkflownodes.knime.parameter.IFileParameter)4 InvalidParameterValueException (com.genericworkflownodes.knime.parameter.InvalidParameterValueException)4 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 FileStorePrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject)2 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)2 IPrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject)2 INodeConfiguration (com.genericworkflownodes.knime.config.INodeConfiguration)2 ExecutionFailedException (com.genericworkflownodes.knime.generic_node.ExecutionFailedException)2 TestDataSource (com.genericworkflownodes.knime.test.data.TestDataSource)2 File (java.io.File)2 Test (org.junit.Test)2