use of org.cytoscape.tableimport.internal.reader.NetworkTableMappingParameters in project cytoscape-impl by cytoscape.
the class ImportTablePanel method getNetworkTableMappingParameters.
public NetworkTableMappingParameters getNetworkTableMappingParameters() throws Exception {
final String sourceName = getPreviewPanel().getSourceName();
final String[] attrNames = getPreviewPanel().getAttributeNames();
final SourceColumnSemantic[] types = getPreviewPanel().getTypes();
if (!isAttributeNamesValid(attrNames, types))
return null;
final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);
final AttributeDataType[] dataTypes = getPreviewPanel().getDataTypes();
final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);
final String[] listDelimiters = getPreviewPanel().getListDelimiters();
final String[] listDelimitersCopy = Arrays.copyOf(listDelimiters, listDelimiters.length);
int startLineNumber = getStartLineNumber();
String commentChar = null;
if (!getCommentLinePrefix().isEmpty())
commentChar = getCommentLinePrefix();
final int sourceColumnIndex = getPreviewPanel().getColumnIndex(SOURCE);
final int targetColumnIndex = getPreviewPanel().getColumnIndex(TARGET);
final int interactionColumnIndex = getPreviewPanel().getColumnIndex(INTERACTION);
final String defaultInteraction = defaultInteractionTextField.getText();
// Build mapping parameter object.
final List<String> del = checkDelimiter();
final NetworkTableMappingParameters mapping = new NetworkTableMappingParameters(sourceName, del, listDelimitersCopy, attrNames, dataTypesCopy, typesCopy, sourceColumnIndex, targetColumnIndex, interactionColumnIndex, defaultInteraction, startLineNumber, commentChar);
return mapping;
}
use of org.cytoscape.tableimport.internal.reader.NetworkTableMappingParameters in project cytoscape-impl by cytoscape.
the class LoadNetworkReaderTask method run.
@Override
public void run(final TaskMonitor tm) throws Exception {
tm.setTitle("Loading network from table");
tm.setProgress(0.0);
tm.setStatusMessage("Loading network...");
taskMonitor = tm;
final List<String> attrNameList = new ArrayList<>();
int colCount;
String[] attributeNames;
final CyNetworkReaderManager networkReaderManager = serviceRegistrar.getService(CyNetworkReaderManager.class);
if (is != null)
netReader = networkReaderManager.getReader(is, inputName);
if (netReader == null)
netReader = networkReaderManager.getReader(uri, inputName);
if (netReader instanceof CombineReaderAndMappingTask) {
Workbook workbook = null;
// Load Spreadsheet data for preview.
if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
try {
workbook = WorkbookFactory.create(new FileInputStream(tempFile));
} catch (InvalidFormatException e) {
// e.printStackTrace();
throw new IllegalArgumentException("Could not read Excel file. Maybe the file is broken?", e);
} finally {
}
}
netReader = null;
if (startLoadRow > 0)
startLoadRow--;
final int startLoadRowTemp = firstRowAsColumnNames ? 0 : startLoadRow;
previewPanel.updatePreviewTable(workbook, fileType, tempFile.getAbsolutePath(), new FileInputStream(tempFile), delimiters.getSelectedValues(), null, startLoadRowTemp);
colCount = previewPanel.getPreviewTable().getColumnModel().getColumnCount();
Object curName = null;
if (firstRowAsColumnNames) {
previewPanel.setFirstRowAsColumnNames();
startLoadRow++;
}
final SourceColumnSemantic[] types = previewPanel.getTypes();
for (int i = 0; i < colCount; i++) {
curName = previewPanel.getPreviewTable().getColumnModel().getColumn(i).getHeaderValue();
if (attrNameList.contains(curName)) {
int dupIndex = 0;
for (int idx = 0; idx < attrNameList.size(); idx++) {
if (curName.equals(attrNameList.get(idx))) {
dupIndex = idx;
break;
}
}
if (!TypeUtil.allowsDuplicateName(ImportType.NETWORK_IMPORT, types[i], types[dupIndex])) {
// TODO add message to user (Duplicate Column Name Found)
return;
}
}
if (curName == null)
attrNameList.add("Column " + i);
else
attrNameList.add(curName.toString());
}
attributeNames = attrNameList.toArray(new String[attrNameList.size()]);
final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);
final AttributeDataType[] dataTypes = previewPanel.getDataTypes();
final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);
AttributeDataType[] tunableDataTypes = null;
if (dataTypeList != null && !dataTypeList.trim().isEmpty())
tunableDataTypes = TypeUtil.parseDataTypeList(dataTypeList);
if (tunableDataTypes != null && tunableDataTypes.length > 0)
System.arraycopy(tunableDataTypes, 0, dataTypesCopy, 0, Math.min(tunableDataTypes.length, dataTypesCopy.length));
SourceColumnSemantic[] tunableColumnTypes = null;
if (columnTypeList != null && !columnTypeList.trim().isEmpty()) {
tunableColumnTypes = TypeUtil.parseColumnTypeList(columnTypeList);
}
if (tunableColumnTypes != null && tunableColumnTypes.length > 0) {
System.arraycopy(tunableColumnTypes, 0, typesCopy, 0, Math.min(tunableColumnTypes.length, typesCopy.length));
// Set the source and target interaction columns
int index = 1;
for (SourceColumnSemantic scs : tunableColumnTypes) {
if (scs.equals(SourceColumnSemantic.SOURCE))
indexColumnSourceInteraction = index;
else if (scs.equals(SourceColumnSemantic.TARGET))
indexColumnTargetInteraction = index;
index++;
}
}
if (nogui) {
// Handle the validation
nogui = false;
ValidationState state = getValidationState(new StringBuffer(80));
switch(state) {
case INVALID:
tm.showMessage(TaskMonitor.Level.ERROR, "Source column must be specified");
return;
case REQUEST_CONFIRMATION:
tm.showMessage(TaskMonitor.Level.WARN, "Target column is not specified. No edges will be created");
}
nogui = true;
}
String[] listDelimiters = previewPanel.getListDelimiters();
if (listDelimiters == null || listDelimiters.length == 0) {
listDelimiters = new String[dataTypes.length];
if (delimitersForDataList.getSelectedValue() != null)
Arrays.fill(listDelimiters, delimitersForDataList.getSelectedValue());
}
if (indexColumnSourceInteraction > 0)
indexColumnSourceInteraction--;
if (indexColumnTargetInteraction > 0)
indexColumnTargetInteraction--;
if (indexColumnTypeInteraction > 0)
indexColumnTypeInteraction--;
networkName = previewPanel.getSourceName();
ntmp = new NetworkTableMappingParameters(networkName, delimiters.getSelectedValues(), listDelimiters, attributeNames, dataTypesCopy, typesCopy, indexColumnSourceInteraction, indexColumnTargetInteraction, indexColumnTypeInteraction, defaultInteraction, startLoadRow, null);
try {
if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {
final Sheet sheet = workbook.getSheet(networkName);
reader = new ExcelNetworkSheetReader(networkName, sheet, ntmp, nMap, rootNetwork, serviceRegistrar);
} else {
networkName = this.inputName;
reader = new NetworkTableReader(networkName, new FileInputStream(tempFile), ntmp, nMap, rootNetwork, serviceRegistrar);
}
} catch (Exception ioe) {
tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read network: " + ioe.getMessage());
return;
}
loadNetwork(tm);
tm.setProgress(1.0);
} else {
networkName = this.inputName;
insertTasksAfterCurrentTask(netReader);
}
}
Aggregations