Skip to content

Commit

Permalink
models: add write method to Network in order to save it into a file i…
Browse files Browse the repository at this point in the history
…n JSON format based, #58
  • Loading branch information
jtarraga committed Nov 30, 2020
1 parent abba9da commit bb63017
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
package org.opencb.bionetdb.app.cli.admin.executors;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import htsjdk.samtools.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.opencb.bionetdb.app.cli.CommandExecutor;
import org.opencb.bionetdb.app.cli.admin.AdminCliOptionsParser;
import org.opencb.bionetdb.core.exceptions.BioNetDBException;
import org.opencb.bionetdb.core.io.BioPaxParser;
import org.opencb.bionetdb.core.io.SbmlParser;
import org.opencb.bionetdb.core.io.SifParser;
import org.opencb.bionetdb.core.models.network.Network;
import org.opencb.bionetdb.core.models.network.Node;
import org.opencb.bionetdb.core.models.network.Relation;
import org.opencb.bionetdb.lib.BioNetDbManager;
import org.opencb.bionetdb.lib.utils.Builder;
import org.opencb.commons.utils.FileUtils;
import org.opencb.commons.utils.ListUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
* Created by imedina on 05/08/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opencb.bionetdb.app;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
Expand All @@ -9,7 +10,7 @@
import org.opencb.bionetdb.core.models.network.Relation;

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class BioNetDBMainTest {
Expand All @@ -20,7 +21,8 @@ public void createCsvClinicalAnalysis() {
String cmdLine = "~/appl/bionetdb/build/bin/bionetdb.sh create-csv -i " + caPath + "/input/ -o csv/ --clinical-analysis";
}

private void createNetworks() {

public void createNetworks() {
long uid = 0;

ObjectMapper mapper = new ObjectMapper();
Expand All @@ -43,12 +45,17 @@ private void createNetworks() {
Relation.Type.GENE__DRUG);
network.getRelations().add(relation1);

File file = new File("/tmp/network1.json");
try {
mapper.writer().writeValue(new File("/tmp/network1.json"), network);
} catch (IOException e) {
network.write(file);
} catch (FileNotFoundException | JsonProcessingException e) {
e.printStackTrace();
}

// try {
// mapper.writer().writeValue(, network);
// } catch (IOException e) {
// e.printStackTrace();
// }

network = new Network("net2", "net2", "Network #2");
network.setNodes(new ArrayList<>());
Expand All @@ -67,11 +74,17 @@ private void createNetworks() {
Relation.Type.GENE__DRUG);
network.getRelations().add(relation3);

file = new File("/tmp/network2.json");
try {
mapper.writer().writeValue(new File("/tmp/network2.json"), network);
} catch (IOException e) {
network.write(file);
} catch (FileNotFoundException | JsonProcessingException e) {
e.printStackTrace();
}
// try {
// mapper.writer().writeValue(new File("/tmp/network2.json"), network);
// } catch (IOException e) {
// e.printStackTrace();
// }

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.opencb.bionetdb.core.models.network;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -14,6 +22,9 @@ public class Network extends Graph {

private Map<String, Object> attributes;

private long numNodes;
private long numRelations;

public Network() {
this("", "", "");
}
Expand All @@ -27,6 +38,32 @@ public Network(String id, String name, String description) {
attributes = new HashMap<>();
}

public void write(File file) throws FileNotFoundException, JsonProcessingException {
Network network = new Network(id, name, description);
network.setAttributes(attributes);
network.setNumNodes(nodes.size());
network.setNumRelations(relations.size());

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true);

PrintWriter pw = new PrintWriter(file);

pw.println(mapper.writer().writeValueAsString(network));
for (Node node: nodes) {
pw.println(mapper.writer().writeValueAsString(node));
}
for (Relation relation: relations) {
pw.println(mapper.writer().writeValueAsString(relation));
}

pw.close();


}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Network{");
Expand Down Expand Up @@ -75,4 +112,14 @@ public Network setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
return this;
}

public Network setNumNodes(long numNodes) {
this.numNodes = numNodes;
return this;
}

public Network setNumRelations(long numRelations) {
this.numRelations = numRelations;
return this;
}
}

0 comments on commit bb63017

Please sign in to comment.