Git Lab CI for docker build enabled! You can enable it using .gitlab-ci.yml in your project. Check file template at https://gitlab.bio.di.uminho.pt/snippets/5

Generate gene reaction rules v1

parent f1fd994c
......@@ -10,13 +10,14 @@ import java.util.Random;
import pt.uminho.ceb.biosystems.merlin.bigg.blast.Properties;
import pt.uminho.ceb.biosystems.merlin.bigg.blast.ProvideBiggReactionsToGenes;
import pt.uminho.ceb.biosystems.merlin.bigg.data.BiggDataCenter;
import pt.uminho.ceb.biosystems.merlin.bigg.data.BiggUtils;
import pt.uminho.ceb.biosystems.merlin.utilities.io.FileUtils;
public class BiggMain {
public static void main(String[] args) throws InterruptedException, IOException {
public static void main(String[] args) throws Exception {
String workFolderID = "/workdir/"; //Docker (/workdir/)
String paramsPath = workFolderID.concat("workerSubmissions/params.txt");
......@@ -24,11 +25,12 @@ public class BiggMain {
List<String> listArgs = readParamsFile(paramsPath);
//MUDAR PATHS CONFIGS E MAIN
Properties properties = new Properties();
properties = validateProperties(properties, listArgs);
//BiggDataCenter.genesReactionsRuleFile(workFolderID);
String filePath = workFolderID.concat("workerSubmissions/queryGenome.faa");
......
......@@ -26,7 +26,7 @@ public class Properties {
private static final Logger logger = LoggerFactory.getLogger(Properties.class);
public Properties() {
String propertiesFilePath = "/workdir/configs/" + "configurations.txt";
String propertiesFilePath = "/workdir/" + "configurations.txt";
allProperties = FileUtils.readPropertiesFile(false,propertiesFilePath);
boolean go = assignVariables();
......
......@@ -17,6 +17,9 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
......@@ -25,20 +28,26 @@ import pt.uminho.ceb.biosystems.merlin.bigg.data.BiggDataCenter;
public class ProvideBiggReactionsToGenes {
private Map<String,Set<String>> blastResults;
private String sequenceIdsGenesRelation="";
private static Map<String, List<String>> seqIdReactionRelation = new HashMap<>();
private String queryPath;
private Map<String, List<String>> gprsMap = new HashMap<String,List<String>>();
private String workFolderID;
public ProvideBiggReactionsToGenes(String workFolderID, String queryPathParameters, Properties properties, int option, String items) {
this.queryPath = queryPathParameters;
this.workFolderID = workFolderID;
try {
Blast blast = new Blast(workFolderID, queryPath, properties, option, items);
blastResults = blast.getResults();
getReactionIds(properties, option, workFolderID);
maiscenas(workFolderID);
System.out.println("BLAST Finished");
}
......@@ -48,7 +57,7 @@ public class ProvideBiggReactionsToGenes {
}
private void getReactionIds(Properties properties, int option, String workFolderID) throws IOException {
String sequenceIdsGenesRelation="";
switch (option) {
case 1:
......@@ -134,7 +143,158 @@ public class ProvideBiggReactionsToGenes {
}
}
private void maiscenas(String workFolderID) throws Exception {
//Initialize script enginte to perform boolean validations
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Map<String,String> genesReactionRuleMap = BiggDataCenter.readGenesReactionsRuleFile(workFolderID); //Get all reaction rules
Map<String,String> queryGeneBiggGeneMap = queryGeneBiggGeneRetrieverOther(); //get bigg genes associated with the query gene
Map<String,String> finalMap = new HashMap<String,String>();
for(Entry<String,String> entry : genesReactionRuleMap.entrySet()) { //for each reaction
String reaction = entry.getKey();
if(!reaction.equals("ABCDE") && !reaction.equals("HEYYY") && !reaction.equals("AKSHF") && !reaction.equals("akjshas") )
continue;
String ruleInit = entry.getValue(); //get the rule with the original values
String rule = ruleInit.replace("and", "&&").replace("or", "||").replace("(", "( ").replace(")", " )"); // rule with the logical symbols
//Lists
List<String> allElements = Arrays.asList(rule.split(" "));
List<String> allElementsValue = new ArrayList<String>();
Map<String,String> elementsValue = new HashMap<String,String>();
for (String element : allElements) {
if(!element.equals("&&") && !element.equals("||") && !element.equals("(") && !element.equals(")")) { //if it's a gene
if(queryGeneBiggGeneMap.containsKey(element)) {//check if the blast results contains this gene
allElementsValue.add("true");
elementsValue.put(element, queryGeneBiggGeneMap.get(element));
}
else {
allElementsValue.add("false");
}
}
else
allElementsValue.add(element);
}
//join the list elements value
StringBuilder allElementsValueString = new StringBuilder();
for (String s : allElementsValue)
{
allElementsValueString.append(s);
}
Object result = engine.eval(allElementsValueString.toString());
Boolean boolResult = Boolean.parseBoolean(result.toString());
//If the result is true, make the rule. Otherwise, just ignore.
if(boolResult == false)
continue;
//Remove the "false" values, because they are not similar. The expressions returned true, even though that values were false
String finalExpression = "";
for(String elmnt : allElements) {//for each element
if(!elmnt.equals("&&") && !elmnt.equals("||") && !elmnt.equals("(") && !elmnt.equals(")")) { //if it's a gene
int index = allElements.indexOf(elmnt); //get the index of this element to check in the values list if its true or false
boolean val = Boolean.parseBoolean(allElementsValue.get(index));
if(val != false) {
String queryGeneRelated = elementsValue.get(elmnt); //get the related queryGene
finalExpression += queryGeneRelated;
}
}
else
finalExpression += elmnt;
}
if (finalExpression.startsWith(" ||") || finalExpression.startsWith(" &&")) //remove starting || or &&
finalExpression.substring(1);
if (finalExpression.endsWith("||") || finalExpression.endsWith("&&")) //remove ending || or &&
finalExpression.substring(0, finalExpression.length() - 1);
String finalRule = finalExpression.replace("&&", " and ").replace("||", " or ");
finalMap.put(reaction, finalRule);
System.out.println();
}
System.out.println("FINALE");
}
private Map<String, List<String>> queryGeneBiggGeneRetriever() throws IOException{
Map<String,String> seqIDGenesMap = BiggDataCenter.readSeqIDGenesRelationFile(sequenceIdsGenesRelation, workFolderID);
Map<String,List<String>> map = new HashMap<String,List<String>>();
for (Entry<String,Set<String>> result : blastResults.entrySet()) {
List<String> biggGenesList = new ArrayList<String>();
String biggGenes="";
for(String gene : result.getValue()) {
biggGenes = biggGenes + seqIDGenesMap.get(gene) + ";";
}
biggGenesList = Arrays.asList(biggGenes.split(";"));
map.put(result.getKey(), biggGenesList);
}
return map;
}
private Map<String, String> queryGeneBiggGeneRetrieverOther() throws IOException{
Map<String,String> seqIDGenesMap = BiggDataCenter.readSeqIDGenesRelationFile(sequenceIdsGenesRelation, workFolderID);
Map<String,String> map = new HashMap<String,String>();
for (Entry<String,Set<String>> result : blastResults.entrySet()) {
for(String gene : result.getValue()) {
String biggGenes = seqIDGenesMap.get(gene);
List<String> biggGenesList = new ArrayList<String>();
biggGenesList = Arrays.asList(biggGenes.split(";"));
for(String geneListElement : biggGenesList) {
String geneFinal = geneListElement.trim();
map.put(geneFinal, result.getKey());
}
}
}
return map;
}
......
......@@ -42,7 +42,6 @@ public class BiggDataCenter {
public BiggDataCenter(String model) {
try {
setCompartments();
this.setModel(model);
......@@ -1044,6 +1043,129 @@ public class BiggDataCenter {
}
public static Map<String,String> genesReactionsRuleFile(String path) throws Exception {
Map<String,String> geneReactionRuleMap = new HashMap<String, String>();
Map<String,List<String>> geneReactionRuleMapList = new HashMap<String,List<String>>();
try {
List<String> modelFiles = new ArrayList<String>();
File folder = new File(path+"Bigg_Files/BiggData/");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
modelFiles.add(listOfFiles[i].getName().replace(".txt", ""));
}
for (String model : modelFiles) {
File f = new File(path+"Bigg_Files/BiggData/"+model+".txt");
if(!f.exists())
continue;
String file = BiggUtils.readFile(path+"Bigg_Files/BiggData/"+model+".txt", StandardCharsets.UTF_8);
System.out.println(model);
JSONObject modelDetails = new JSONObject(file);
modelDetails = modelDetails.getJSONObject(model);
BiggModels biggModel = new BiggModels();
//model genes
biggModel.setReactions(readModelReactions(modelDetails.getJSONObject("reaction"))); //Get all the reaction
for(BiggReactions reaction : biggModel.getReactions()) {
List<String> reactions = new ArrayList<String>();
List<BiggReactionResult> results = reaction.getResults();
for(BiggReactionResult result: results) {
if(result.getGeneReactionRule().equals(null) || result.getGeneReactionRule().equals(""))
continue;
if(!geneReactionRuleMap.containsKey(reaction.getBiggId())) {//If the key (Gene ID) doesn't exist already
if(reactions.isEmpty()) {//If it's the first result, just uses the value
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), result.getGeneReactionRule());
}
else { //Else, checks if the value is different and updates
if(reactions.contains(result.getGeneReactionRule()))
continue;
else {
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), geneReactionRuleMap.get(reaction.getBiggId()) + " or " + result.getGeneReactionRule());
}
}
geneReactionRuleMapList.put(reaction.getBiggId(), reactions);
}
else {//If already exists
reactions = geneReactionRuleMapList.get(reaction.getBiggId());
if(reactions.contains(result.getGeneReactionRule()))
continue;
else {
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), geneReactionRuleMap.get(reaction.getBiggId()) + " or " + result.getGeneReactionRule());
}
geneReactionRuleMapList.put(reaction.getBiggId(), reactions);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter geneReactionRuleWriter = new PrintWriter(path+"Bigg_Files/Results/"+"geneReactionRuleFile.txt");
for (Entry<String,String> entry : geneReactionRuleMap.entrySet())
geneReactionRuleWriter.println(entry.getKey() + " - " + entry.getValue());
geneReactionRuleWriter.close();
return geneReactionRuleMap;
}
public static Map<String,String> readGenesReactionsRuleFile(String dockerPath) throws IOException{
Map<String,String> map = new HashMap<String, String>();
BufferedReader br = null;
String fileName = dockerPath+"Bigg_Files/Results/"+"geneReactionRuleFile.txt";
try {
br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
String key = line.split("-")[0].trim();
String value = line.split("-")[1].trim();
map.put(key, value);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
}
return map;
}
private static HashMap<String,List<String>> readGeneReactionRelationFile(String dockerPath) {
HashMap<String,List<String>> geneReactionMap = new HashMap<String,List<String>>();
......@@ -1096,7 +1218,7 @@ public class BiggDataCenter {
}
private static Map<String,String> readSeqIDGenesRelationFile(String file, String dockerPath) throws IOException {
public static Map<String,String> readSeqIDGenesRelationFile(String file, String dockerPath) throws IOException {
Map<String,String> map = new HashMap<String, String>();
BufferedReader br = null;
String fileName = dockerPath+"Bigg_Files/Results/"+file;
......
......@@ -45,7 +45,7 @@ public class UpdateBiggFiles {
// TODO Auto-generated method stub
String resultModel = BiggUtils.fetch("models");
System.out.println("----starting get models details-----");
getModelsDetails(resultModel);
......@@ -57,7 +57,7 @@ public class UpdateBiggFiles {
createFastaFile(resultModel);
System.out.println("----Seq Id reaction relation-----");
writeSeqIDReactionRelationFile();
genesReactionsRuleFile();
zipResultsFolder();
exportBiggDatabase();
......@@ -932,6 +932,93 @@ public class UpdateBiggFiles {
}
public static Map<String,String> genesReactionsRuleFile() throws Exception {
Map<String,String> geneReactionRuleMap = new HashMap<String, String>();
Map<String,List<String>> geneReactionRuleMapList = new HashMap<String,List<String>>();
try {
for (String model : modelBiggIds) {
File f = new File(modelsPath+model+".txt");
if(!f.exists())
continue;
String file = BiggUtils.readFile(modelsPath+model+".txt", StandardCharsets.UTF_8);
System.out.println(model);
JSONObject modelDetails = new JSONObject(file);
modelDetails = modelDetails.getJSONObject(model);
BiggModels biggModel = new BiggModels();
//model genes
biggModel.setReactions(readModelReactions(modelDetails.getJSONObject("reaction"))); //Get all the reaction
for(BiggReactions reaction : biggModel.getReactions()) {
List<String> reactions = new ArrayList<String>();
List<BiggReactionResult> results = reaction.getResults();
for(BiggReactionResult result: results) {
if(result.getGeneReactionRule().equals(null) || result.getGeneReactionRule().equals(""))
continue;
if(!geneReactionRuleMap.containsKey(reaction.getBiggId())) {//If the key (Gene ID) doesn't exist already
if(reactions.isEmpty()) {//If it's the first result, just uses the value
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), result.getGeneReactionRule());
}
else { //Else, checks if the value is different and updates
if(reactions.contains(result.getGeneReactionRule()))
continue;
else {
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), geneReactionRuleMap.get(reaction.getBiggId()) + " OR " + result.getGeneReactionRule());
}
}
geneReactionRuleMapList.put(reaction.getBiggId(), reactions);
}
else {//If already exists
reactions = geneReactionRuleMapList.get(reaction.getBiggId());
if(reactions.contains(result.getGeneReactionRule()))
continue;
else {
reactions.add(result.getGeneReactionRule());
geneReactionRuleMap.put(reaction.getBiggId(), geneReactionRuleMap.get(reaction.getBiggId()) + " OR " + result.getGeneReactionRule());
}
geneReactionRuleMapList.put(reaction.getBiggId(), reactions);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter geneReactionRuleWriter = new PrintWriter(resultsPath+"geneReactionRuleFile.txt");
for (Entry<String,String> entry : geneReactionRuleMap.entrySet())
geneReactionRuleWriter.println(entry.getKey() + " - " + entry.getValue());
geneReactionRuleWriter.close();
return geneReactionRuleMap;
}
private static void exportBiggDatabase() {
try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment