Skip to content

Commit

Permalink
Some minor fixes and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
imedina committed Sep 11, 2015
1 parent d93ad1e commit c1b3031
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.opencb.bionetdb.core.models.Network;
import org.opencb.bionetdb.core.neo4j.Neo4JNetworkDBAdaptor;
import org.opencb.commons.utils.FileUtils;
import org.opencb.datastore.core.QueryOptions;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -35,13 +36,16 @@ public void execute() {
ExpressionParser expressionParser = new ExpressionParser(metadata);
NetworkDBAdaptor networkDBAdaptor = new Neo4JNetworkDBAdaptor(expressionCommandOptions.database);

QueryOptions options = new QueryOptions();
options.put("addNodes", this.expressionCommandOptions.add);

Map<String, Map<String, String>> allExpressionFiles = expressionParser.getMyFiles();
for (String tissue : allExpressionFiles.keySet()) {
for (String timeseries : allExpressionFiles.get(tissue).keySet()) {
if ((expressionCommandOptions.tissues == null || expressionCommandOptions.tissues.contains(tissue)) &&
(expressionCommandOptions.timeseries == null || expressionCommandOptions.timeseries.contains(timeseries))) {
List<Expression> myExpression = expressionParser.parse(tissue, timeseries);
networkDBAdaptor.addExpressionData(tissue, timeseries, myExpression, expressionCommandOptions.add);
networkDBAdaptor.addExpressionData(tissue, timeseries, myExpression, options);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions bionetdb-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<groupId>org.opencb.datastore</groupId>
<artifactId>datastore-core</artifactId>
</dependency>
<dependency>
<groupId>org.opencb.commons</groupId>
<artifactId>commons-lib</artifactId>
</dependency>

<dependency>
<groupId>org.neo4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.opencb.bionetdb.core.api;

import org.opencb.bionetdb.core.exceptions.DBException;
import org.opencb.bionetdb.core.exceptions.NetworkDBException;
import org.opencb.bionetdb.core.models.Expression;
import org.opencb.bionetdb.core.models.Network;
import org.opencb.bionetdb.core.models.Xref;
Expand Down Expand Up @@ -45,18 +45,18 @@ enum NetworkQueryParams implements QueryParam {
}


void insert(Network network, QueryOptions queryOptions) throws DBException;
void insert(Network network, QueryOptions queryOptions) throws NetworkDBException;

void addXrefs(String nodeID, List<Xref> xref_list);

/**
*
* @param tissue Tissue of the current expression experiment
* @param timeseries Timeseries of the current expression experiment
* @param timeSeries Timeseries of the current expression experiment
* @param myExpression List of expression data to be add in the database
* @param add Boolean to know if nodes not found in the database have to be created and insert their expression or not
* @param options Boolean to know if nodes not found in the database have to be created and insert their expression or not
*/
void addExpressionData(String tissue, String timeseries, List<Expression> myExpression, boolean addNodes);
void addExpressionData(String tissue, String timeSeries, List<Expression> myExpression, QueryOptions options);

//TODO: To remove
//public QueryResult getXrefs(String idNode);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.opencb.bionetdb.core.exceptions;

/**
* Created by pfurio on 10/9/15.
*/
public class NetworkDBException extends Exception {

public NetworkDBException(String message) {
super(message);
}

public NetworkDBException(String message, Throwable cause) {
super(message, cause);
}

public NetworkDBException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.opencb.bionetdb.core.io;

import org.opencb.bionetdb.core.models.Expression;
import org.opencb.commons.utils.FileUtils;

import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.nio.file.Files.readAllLines;

/**
* Created by pfurio on 8/9/15.
*/
Expand All @@ -19,81 +21,103 @@ public class ExpressionParser {
private Map<String, Map<String, String>> myFiles;

public ExpressionParser(Path metadata) throws IOException {
FileUtils.checkFile(metadata);

myFiles = new HashMap<>();
List<String> allLines = readAllLines(metadata);
List<String> allLines = Files.readAllLines(metadata);
for (String line : allLines) {
String[] fields = line.split("\t");
Map<String, String> timeseries;
if (myFiles.containsKey(fields[0]))
timeseries = myFiles.get(fields[0]);
else
timeseries = new HashMap<>();
timeseries.put(fields[1], metadata.getParent().toString() + "/" + fields[2]);
Map<String, String> timeSeries;
if (myFiles.containsKey(fields[0])) {
timeSeries = myFiles.get(fields[0]);
} else {
timeSeries = new HashMap<>();
}
timeSeries.put(fields[1], metadata.getParent().toString() + "/" + fields[2]);
if (!myFiles.containsKey(fields[0]))
myFiles.put(fields[0], timeseries);
myFiles.put(fields[0], timeSeries);
}
}

public Map<String, Map<String, String>> getMyFiles() {
return myFiles;
}

public List<Expression> parse(String tissue, String time) throws IOException {

String expressionFile = myFiles.get(tissue).get(time);
List<Expression> myExpressionList = new ArrayList<>();

// Open expression file
BufferedReader br = new BufferedReader(new FileReader(expressionFile));
String currentLine = br.readLine();
int colId = -1;
int colExpr = -1;
int colPval = -1;
int colOdds = -1;
int colUpreg = -1;

int col_id, col_expr, col_pval, col_odds, col_upreg;
col_id = col_expr = col_pval = col_odds = col_upreg = -1;
String[] headers = currentLine.split("\t", -1);
if (headers.length == 2 || headers.length == 5) {
// Open expression file, this can be gzipped
Path expressionFilePath = Paths.get(expressionFile);
BufferedReader br = FileUtils.newBufferedReader(expressionFilePath);

// Finding the index position for each column
String currentLine = br.readLine();
String[] headers = currentLine.split("\t", -1);
if (headers.length > 1) {
// Get the column where we can find each of the columns
for (int i = 0; i < headers.length; i++) {
switch(headers[i]) {
case "ID":
col_id = i;
colId = i;
break;
case "Expression":
col_expr = i;
colExpr = i;
break;
case "Pvalue":
col_pval = i;
colPval = i;
break;
case "Odds":
col_odds = i;
colOdds = i;
break;
case "Upregulated":
col_upreg = i;
colUpreg = i;
break;
default:
break;
}
}

// It we have the geneID at least and some expression, pvalues, odds or upregulations...
if (col_id != -1 && (col_expr != -1 ||col_odds != -1 ||col_pval != -1 ||col_upreg != -1)) {
if (colId != -1 && (colExpr != -1 ||colOdds != -1 ||colPval != -1 ||colUpreg != -1)) {
while ((currentLine = br.readLine()) != null) {
String[] line_spl = currentLine.split("\t", -1);
Expression myexpr = new Expression(line_spl[col_id]);
if (col_expr != -1)
myexpr.setExpression(Double.parseDouble(line_spl[col_expr]));
if (col_odds != -1)
myexpr.setOdds(Double.parseDouble(line_spl[col_odds]));
if (col_pval != -1)
myexpr.setPvalue(Double.parseDouble(line_spl[col_pval]));
if (col_upreg != -1)
myexpr.setUpregulated(Integer.parseInt(line_spl[col_upreg]));
myExpressionList.add(myexpr);
Expression myExpr = new Expression(line_spl[colId]);
if (colExpr != -1) {
myExpr.setExpression(Double.parseDouble(line_spl[colExpr]));
}
if (colOdds != -1) {
myExpr.setOdds(Double.parseDouble(line_spl[colOdds]));
}
if (colPval != -1) {
myExpr.setPvalue(Double.parseDouble(line_spl[colPval]));
}
if (colUpreg != -1) {
myExpr.setUpregulated(Integer.parseInt(line_spl[colUpreg]));
}
myExpressionList.add(myExpr);
}
}
}

return myExpressionList;
}

@Override
public String toString() {
return "ExpressionParser{" +
"myFiles=" + myFiles +
'}';
}

public Map<String, Map<String, String>> getMyFiles() {
return myFiles;
}

public void setMyFiles(Map<String, Map<String, String>> myFiles) {
this.myFiles = myFiles;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class XrefAnnotationParser {
* Unknown info is represented as an empty string.
*/
public Map<String, Xref> parseXrefAnnotationFile(Path path) throws IOException {

// Reading GZip input stream
InputStream inputStream;
if (path.toFile().getName().endsWith(".gz")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Expression {
private double odds;
private int upregulated;

public Expression() {
this("");
}

public Expression(String id) {
this.id = id;
this.expression = -1;
Expand All @@ -21,40 +25,55 @@ public Expression(String id) {
this.upregulated = -1;
}

public void setExpression(double expression) {
this.expression = expression;
}

public void setPvalue(double pvalue) {
this.pvalue = pvalue;
}

public void setOdds(double odds) {
this.odds = odds;
}

public void setUpregulated(int upregulated) {
this.upregulated = upregulated;
@Override
public String toString() {
return "Expression{" +
"id='" + id + '\'' +
", expression=" + expression +
", pvalue=" + pvalue +
", odds=" + odds +
", upregulated=" + upregulated +
'}';
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public double getExpression() {
return expression;
}

public void setExpression(double expression) {
this.expression = expression;
}

public double getPvalue() {
return pvalue;
}

public void setPvalue(double pvalue) {
this.pvalue = pvalue;
}

public double getOdds() {
return odds;
}

public void setOdds(double odds) {
this.odds = odds;
}

public int getUpregulated() {
return upregulated;
}

public void setUpregulated(int upregulated) {
this.upregulated = upregulated;
}

}
Loading

0 comments on commit c1b3031

Please sign in to comment.