Skip to content

Commit

Permalink
error messages and messages to show files are imported #1
Browse files Browse the repository at this point in the history
  • Loading branch information
herme committed Feb 8, 2024
1 parent 5fa458a commit 7b9472f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.cytoscape.application.CyApplicationManager;
Expand Down Expand Up @@ -71,15 +72,12 @@ public void run(TaskMonitor taskMonitor) {
mggManager.setMetadataJsonObject(jsonResult);


// Show the JSON data in a panel if showJSONInPanel
// if (showMetaDataInPanel ) {
// SwingUtilities.invokeLater(() -> showDataInPanel(jsonResult));
// }




taskMonitor.setStatusMessage("Metadata file imported successfully.");
taskMonitor.setProgress(1.0);
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(null,
"Metadata file loaded correctly", "Information", JOptionPane.INFORMATION_MESSAGE));



Expand Down Expand Up @@ -107,15 +105,15 @@ private JSONObject processCSVDataToJson(List<String[]> csvData) {
JSONArray mainArray = new JSONArray();

if (!csvData.isEmpty()) {
// First, add the headers
// adding the headers
String[] headers = csvData.remove(0);
JSONArray headerArray = new JSONArray();
for (String header : headers) {
headerArray.add(header);
}
mainArray.add(headerArray);

// Then, add each row of data
// adding each row of data
for (String[] row : csvData) {
JSONArray rowArray = new JSONArray();
for (String cell : row) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public TaskIterator createTaskIterator() {

return new TaskIterator(new ImportMetadataTask(filePath, mggManager));
} else if (option == JFileChooser.CANCEL_OPTION) {
// if the file selection is cancelled, return an empty TaskIterator
// if file selection is cancelled, return empty TaskIterator
return new TaskIterator();
} else {
// no file was selected or error

String errorMessage = "Error selecting file";
JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
// Return an empty TaskIterator
Expand Down
202 changes: 131 additions & 71 deletions src/main/java/be/kuleuven/mgG/internal/tasks/ImportNetworkData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.swing.CySwingApplication;
import org.cytoscape.model.CyEdge;
Expand Down Expand Up @@ -37,107 +40,164 @@ public void run(TaskMonitor taskMonitor) throws Exception {

CyNetwork currentNetwork=mggManager.getCurrentNetwork();

if(currentNetwork==null) {
taskMonitor.showMessage(TaskMonitor.Level.ERROR, " No Network is currently loaded)");
return;
}

//edgetable of the network
CyTable edgetable=currentNetwork.getDefaultEdgeTable();

if(currentNetwork==null) {

taskMonitor.showMessage(TaskMonitor.Level.ERROR, " No Network is currently loaded)");
return;

// Initialize JSONArray for network data
}


JSONArray networkData = new JSONArray();


CyTable edgeTable = currentNetwork.getDefaultEdgeTable();


boolean weightColumnExists = edgeTable.getColumn("microbetag::weight") != null;
boolean hasWeights = false;

// are weights present?
if (weightColumnExists) {
for (CyEdge edge : currentNetwork.getEdgeList()) {
Double weight = edgeTable.getRow(edge.getSUID()).get("microbetag::weight", Double.class);
if (weight != null) {
hasWeights = true;
break;
}
}
}

// Add headers
JSONArray headers = new JSONArray();
headers.add("Edge ID");

headers.add("Source Node");
headers.add("Target Node");

if(hasWeights) {

headers.add("weight");
}
networkData.add(headers);


// Get list of edges
List<CyEdge> edges = currentNetwork.getEdgeList();

for (CyEdge edge : edges) {
JSONArray edgeArray = new JSONArray();

Long edgeId = edge.getSUID();
CyNode sourceNode = edge.getSource();
CyNode targetNode = edge.getTarget();

// Get source and target node IDs or names
String sourceNodeId = getNodeIdentifier(currentNetwork, sourceNode);
String targetNodeId = getNodeIdentifier(currentNetwork, targetNode);

edgeArray.add(edgeId);
edgeArray.add(sourceNodeId);
edgeArray.add(targetNodeId);

networkData.add(edgeArray);

for (CyEdge edge : currentNetwork.getEdgeList()) {

JSONArray edgeArray = new JSONArray();


String sourceNodeId = currentNetwork.getRow(edge.getSource()).get("name", String.class);
String targetNodeId = currentNetwork.getRow(edge.getTarget()).get("name", String.class);



Double weight = edgeTable.getRow(edge.getSUID()).get("microbetag::weight", Double.class);


if (hasWeights && weight!=null) {


edgeArray.add(sourceNodeId);
edgeArray.add(targetNodeId);
edgeArray.add(weight);

networkData.add(edgeArray);

} else if(!hasWeights) {



taskMonitor.showMessage(TaskMonitor.Level.ERROR ,"Network data has no microbetag::weight value,"
+ "network data will not import");
}

}


// Creating a new JSONObject for the network
// JSONObject for the network
JSONObject networkJsonObject = new JSONObject();
networkJsonObject.put("network", networkData);

// JSONObject for abudance data
JSONObject datajsonObject=new JSONObject();
datajsonObject=mggManager.getJsonObject();






// Check if source and target node names are in dataJsonObject
if (areNodesInDataJson(networkJsonObject, datajsonObject)) {
// If all nodes are present, set the network object


if (hasWeights && areNodesInDataJson(networkJsonObject, datajsonObject)) {
// If all are present
mggManager.setNetworkObject(networkJsonObject);
taskMonitor.setTitle("Uploading Network");
taskMonitor.showMessage(TaskMonitor.Level.INFO ,"Network data loaded correctly");
} else {
// If not all nodes are present, set the network object to null
mggManager.setNetworkObject(null);
taskMonitor.showMessage(TaskMonitor.Level.WARN,
"Node names in the network are not the same with the names in the abundance table -> network data can't be uploaded");
}


// Send the network object to mggManager
// mggManager.setNetworkObject(networkJsonObject);
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(null,
"Network data loaded correctly", "Information", JOptionPane.INFORMATION_MESSAGE));

// taskMonitor.showMessage(TaskMonitor.Level.INFO ,"Network Object" +networkJsonObject.toString());
// taskMonitor.showMessage(TaskMonitor.Level.INFO ,"Data object" +datajsonObject.toString());
}
else {
// If not all are present
mggManager.setNetworkObject(null);
taskMonitor.showMessage(TaskMonitor.Level.ERROR,
"Node ids of the network are not present with sequence identifiers of "
+ " abudance table -> network data can't be imported");

// SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(null,
// "Node ids of the network are not present with sequence identifiers of " +
// "the abundance table -> network data can't be imported", "Error", JOptionPane.ERROR_MESSAGE));

}
}


//taskMonitor.setStatusMessage("NetworkArray" + networkData.toString());
//taskMonitor.setStatusMessage("networkJsonObjec" + networkJsonObject.toString());

// Sending the structured network object to mggManager
//mggManager.setNetworkObject(networkJsonObject);
}


private String getNodeIdentifier(CyNetwork network, CyNode node) {
// Example: Getting the name attribute of the node
// Replace "name" with the actual column name for your node names
return network.getRow(node).get("name", String.class);
}

private boolean areNodesInDataJson(JSONObject networkJsonObject, JSONObject dataJsonObject) {
// Extract node names from the networkJsonObject
JSONArray networkData = (JSONArray) networkJsonObject.get("network");
Set<String> nodeNamesInNetwork = new HashSet<>();
for (int i = 1; i < networkData.size(); i++) { // skip headers
JSONArray edge = (JSONArray) networkData.get(i);
nodeNamesInNetwork.add((String) edge.get(1)); // Source Node
nodeNamesInNetwork.add((String) edge.get(2)); // Target Node
}

// Check if these node names are in the first row of dataJsonObject
if (dataJsonObject.containsKey("data")) {
JSONArray data = (JSONArray) dataJsonObject.get("data");
JSONArray headerRow = (JSONArray) data.get(0); // First row with column names
Set<String> columnNamesInData = new HashSet<>(headerRow);




return columnNamesInData.containsAll(nodeNamesInNetwork);
}
return false;
}


JSONArray networkData = (JSONArray) networkJsonObject.get("network");
Set<String> nodeNamesInNetwork = new HashSet<>();
for (int i = 1; i < networkData.size(); i++) {
JSONArray edge = (JSONArray) networkData.get(i);
nodeNamesInNetwork.add((String) edge.get(0));
nodeNamesInNetwork.add((String) edge.get(1));
}


Set<String> nodeNamesInData = new HashSet<>();


if (dataJsonObject.containsKey("data")) {
JSONArray data = (JSONArray) dataJsonObject.get("data");

for (int i = 1; i < data.size(); i++) {
JSONArray row = (JSONArray) data.get(i);

String nodeName = (String) row.get(0);
nodeNamesInData.add(nodeName);
}
}


return nodeNamesInData.containsAll(nodeNamesInNetwork);
}


}





0 comments on commit 7b9472f

Please sign in to comment.