Skip to content

Commit

Permalink
Made it possible o read not only compressed biopax data, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorRodchenkov committed Jun 2, 2024
1 parent c912daa commit c1dbbd6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ dependencies {
implementation "org.springframework.boot:spring-boot-configuration-processor"
implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'

implementation 'org.biopax.paxtools:sbgn-converter:6.0.0-SNAPSHOT'
implementation 'org.biopax.paxtools:sbgn-converter:6.0.0'
implementation 'org.biopax.paxtools:paxtools-core:6.0.0'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.apache.commons:commons-collections4:4.4'

Expand Down
16 changes: 5 additions & 11 deletions src/main/java/factoid/converter/BiopaxToFactoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.biopax.paxtools.model.Model;
import org.biopax.paxtools.model.level3.BioSource;
Expand All @@ -28,21 +26,18 @@
import org.biopax.paxtools.model.level3.EvidenceCodeVocabulary;
import org.biopax.paxtools.model.level3.Interaction;
import org.biopax.paxtools.model.level3.ModificationFeature;
import org.biopax.paxtools.model.level3.MolecularInteraction;
import org.biopax.paxtools.model.level3.PhysicalEntity;
import org.biopax.paxtools.model.level3.Process;
import org.biopax.paxtools.model.level3.Protein;
import org.biopax.paxtools.model.level3.ProteinReference;
import org.biopax.paxtools.model.level3.Rna;
import org.biopax.paxtools.model.level3.SequenceEntity;
import org.biopax.paxtools.model.level3.SimplePhysicalEntity;
import org.biopax.paxtools.model.level3.SmallMolecule;
import org.biopax.paxtools.model.level3.TemplateReaction;
import org.biopax.paxtools.model.level3.TemplateReactionRegulation;
import org.biopax.paxtools.model.level3.Xref;

import com.google.gson.JsonArray;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;


Expand All @@ -64,7 +59,7 @@ public JsonObject convert(Model model) {
.collect(Collectors.toSet());

for (Interaction intn : intns) {
Set<String> markedPmids = new HashSet<String>();
Set<String> markedPmids = new HashSet<>();
Process controlled = getControlled(intn);
for ( Evidence evidence : controlled.getEvidence() ) {
for (Xref xref : evidence.getXref()) {
Expand Down Expand Up @@ -153,7 +148,6 @@ private boolean keepIntn(Interaction intn) {
}
}
}

return false;
}

Expand Down Expand Up @@ -225,11 +219,11 @@ private JsonObject makeEntityJson(Entity entity) {

if ( originalDb.contains("uniprot") ) {
db = "uniprot";
}
else if ( originalDb.equals("hgnc symbol") || originalDb.equals("refseq") ) {
} else if ( originalDb.equalsIgnoreCase("hgnc.symbol")
|| originalDb.equalsIgnoreCase("hgnc symbol")
|| originalDb.equalsIgnoreCase("refseq") ) {
db = originalDb;
}
else {
} else {
logger.warning("Unhandled xref database: " + originalDb);
// if the entity db is not handled return null
// which will signal that the related interaction must be skipped as well
Expand Down
29 changes: 11 additions & 18 deletions src/main/java/factoid/web/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -120,12 +113,12 @@ public String biopaxToFactoid(
produces = "application/json"
)
public String biopaxUrlToFactoid(
@Parameter(description = "URL of a TAR-GZ compressed BioPAX RDF/XML file") @RequestBody String url) {
@Parameter(description = "URL of a BioPAX RDF/XML file") @RequestBody String url) {
BiopaxToFactoid converter = new BiopaxToFactoid();
try {
String body = getContentFromUrl(url);
InputStream is = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
Model model = new SimpleIOHandler().convertFromOWL(is);
Model model = new SimpleIOHandler().convertFromOWL(is);
return converter.convert(model).toString();
} catch (IllegalStateException | JsonSyntaxException | JsonIOException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
Expand All @@ -136,26 +129,26 @@ public String biopaxUrlToFactoid(

private String getContentFromUrl(String url) {
InputStream is = null;
GZIPInputStream gzipInputStream = null;
try {
is = new URL(url).openStream();
gzipInputStream = new GZIPInputStream(is);
try {
is = new GZIPInputStream(new URL(url).openStream());
} catch (IOException e) {
//e.printStackTrace();
is = new URL(url).openStream();
}
} catch (IOException e) {
e.printStackTrace();
}
InputStreamReader reader = new InputStreamReader(gzipInputStream);
BufferedReader in = new BufferedReader(reader);
Reader reader = new InputStreamReader(is);
Writer writer = new StringWriter();

char[] buffer = new char[10240];
try {
for (int length = 0; (length = reader.read(buffer)) > 0;) {
for (int length; (length = reader.read(buffer)) > 0;) {
writer.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}

String body = writer.toString();
return body;
}
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/factoid/converter/BiopaxToFactoidTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void testToJson() throws IOException {
Set<Map.Entry<String, JsonElement>> entries = js.entrySet();

for ( Map.Entry<String, JsonElement> entry : entries ) {
Set<String> entityIds1 = new HashSet<String>();
Set<String> entityIds2 = new HashSet<String>();
Set<String> entityIds1 = new HashSet<>();
Set<String> entityIds2 = new HashSet<>();
JsonArray arr = entry.getValue().getAsJsonArray();
for ( JsonElement el : arr ) {
JsonObject obj = el.getAsJsonObject();
Expand All @@ -49,7 +49,6 @@ public void testToJson() throws IOException {

assertEquals(entityIds1, entityIds2);
}



}
}
13 changes: 12 additions & 1 deletion src/test/java/factoid/web/ControllerT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package factoid.web;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void testBiopaxToFactoid() throws IOException {
}

@Test
public void testBiopaxUrlToFactoid() {
public void testBiopaxArchiveUrlToFactoid() {
String url = "https://www.pathwaycommons.org/archives/PC2/v12/PathwayCommons12.psp.BIOPAX.owl.gz";
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "text/plain");
Expand All @@ -79,4 +80,14 @@ public void testBiopaxUrlToFactoid() {
assertNotNull(res);
assertTrue(res.contains("interaction"));
}

@Test
@Disabled
public void testPc2UrlToFactoid() {
String url = "https://beta.pathwaycommons.org/pc2/get?uri=biofactoid:74ea0716-473c-4a6c-96e0-113a33112827";
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> request = new HttpEntity<>(url, headers);
String res = template.postForObject("/v2/biopax-url-to-json", request, String.class);
assertNotNull(res);
}
}

0 comments on commit c1dbbd6

Please sign in to comment.