diff --git a/pom.xml b/pom.xml index 3259a1d..b377eca 100644 --- a/pom.xml +++ b/pom.xml @@ -1,194 +1,194 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 - org.cidarlab - eugene - 2.0.0-SNAPSHOT - jar + 4.0.0 + org.cidarlab + eugene + 2.0.0-SNAPSHOT + jar - - + + - - - src/main/rules - - **/*.java - - - + + + src/main/rules + + **/*.java + + + - + - - - org.apache.maven.plugins - maven-assembly-plugin - 2.5 - - - jar-with-dependencies - - - - true - lib/ - org.cidarlab.eugene.Eugene - - - - + + + org.apache.maven.plugins + maven-assembly-plugin + 2.5 + + + jar-with-dependencies + + + + true + lib/ + org.cidarlab.eugene.Eugene + + + + - - - org.apache.maven.plugins - maven-jar-plugin - 2.5 - - - - true - lib/ - org.cidarlab.eugene.Eugene - - - - + + + org.apache.maven.plugins + maven-jar-plugin + 2.5 + + + + true + lib/ + org.cidarlab.eugene.Eugene + + + + - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - - + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + - - + + - + on libSBOLj and BioJava + --> + Sonatype OSS Snapshot Repository https://oss.sonatype.org/content/repositories/snapshots/ - biojava-maven-repo - BioJava repository - http://www.biojava.org/download/maven/ - + biojava-maven-repo + BioJava repository + http://www.biojava.org/download/maven/ + - + - - com.google.protobuf - protobuf-java - 2.6.0 - + + com.google.protobuf + protobuf-java + 2.6.0 + - - uk.com.robust-it - cloning - 1.7.4 - + + uk.com.robust-it + cloning + 1.7.4 + - - org.apache.commons - commons-lang3 - 3.3.2 - + + org.apache.commons + commons-lang3 + 3.3.2 + - - - junit - junit - 4.11 - + + + junit + junit + 4.11 + - - org.apache.httpcomponents - httpclient - 4.3.5 - + + org.apache.httpcomponents + httpclient + 4.3.5 + - - - org.drools - drools-core - 5.6.0.Final - - - org.drools - drools-templates - 5.6.0.Final - - - org.drools - knowledge-api - 5.6.0.Final - + + + org.drools + drools-core + 5.6.0.Final + + + org.drools + drools-templates + 5.6.0.Final + + + org.drools + knowledge-api + 5.6.0.Final + - - + + org.sbolstandard libSBOLj 0.7.0-SNAPSHOT - - - org.cidarlab - miniEugene-core - 1.0 - + + + org.cidarlab + miniEugene-core + 1.0 + - - - org.jacop - jacop - 4.4.0 - + + + org.jacop + jacop + 4.4.0 + - - - org.biojava - biojava3-core - 3.0 - + + + org.biojava + biojava3-core + 3.0 + - - org.biojava - core - 1.9.0 - + + org.biojava + core + 1.9.0 + - - - org.slf4j - slf4j-api - 1.7.5 - + + + org.slf4j + slf4j-api + 1.7.5 + - - org.slf4j - slf4j-simple - 1.6.4 - - - + + + org.mongodb + mongo-java-driver + 3.4.2 + + diff --git a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/Eugene2SBOL.java b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/Eugene2SBOL.java index 05249e6..78fdfa6 100644 --- a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/Eugene2SBOL.java +++ b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/Eugene2SBOL.java @@ -37,6 +37,7 @@ import org.cidarlab.eugene.constants.EugeneConstants; import org.cidarlab.eugene.constants.Orientation; +import org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI.SBOLConversionLogicLayer; import org.cidarlab.eugene.dom.*; import org.cidarlab.eugene.dom.imp.container.EugeneContainer; import org.cidarlab.eugene.exception.EugeneException; @@ -65,519 +66,555 @@ */ public class Eugene2SBOL { - public static ArrayList lstURIs; - private static final String DEFAULT_URI = "http://www.eugenecad.org"; - public static Map reusedComponents; + public static ArrayList lstURIs; + private static final String DEFAULT_URI = "http://www.eugenecad.org"; + public static Map reusedComponents; + private static SBOLConversionLogicLayer _logicLayer = new SBOLConversionLogicLayer(); - /** EugeneContainer --> SBOL Collection **/ - public static Collection convert(EugeneContainer objContainer, String sURI) - throws Exception { + /** + * EugeneContainer --> SBOL Collection * + */ + public static Collection convert(EugeneContainer objContainer, String sURI) + throws Exception { - if (null == objContainer) { - throw new EugeneException("I cannot export a NULL value to SBOL!"); - } - - if (null == lstURIs) { - lstURIs = new ArrayList(); - } + if (null == objContainer) { + throw new EugeneException("I cannot export a NULL value to SBOL!"); + } - if (null == reusedComponents) { - reusedComponents = new HashMap(); - } + if (null == lstURIs) { + lstURIs = new ArrayList(); + } - Collection sbolCollection = SBOLFactory.createCollection(); + if (null == reusedComponents) { + reusedComponents = new HashMap(); + } - // NAME - sbolCollection.setName(objContainer.getName()); + Collection sbolCollection = SBOLFactory.createCollection(); - // DESCRIPTION - sbolCollection.setDescription(objContainer.getName()); + // NAME + sbolCollection.setName(objContainer.getName()); - // DISPLAY ID - sbolCollection.setDisplayId(objContainer.getName()); - - //URI - try { - sbolCollection.setURI(URI.create(DEFAULT_URI + "/" + objContainer.getName())); - addURI(URI.create(DEFAULT_URI + "/" + objContainer.getName()).toASCIIString()); - } catch (Exception e) { - throw new EugeneException(e.toString()); - } + // DESCRIPTION + sbolCollection.setDescription(objContainer.getName()); - // ELEMENTS OF COLLECTION - for (NamedElement element : objContainer.getElements()) { + // DISPLAY ID + sbolCollection.setDisplayId(objContainer.getName()); - if (element instanceof Component) { - sbolCollection.addComponent( - Eugene2SBOL.convert( - (Component) element, - null, - 0)); + // URI + try { + sbolCollection.setURI(URI.create(DEFAULT_URI + "/" + objContainer.getName())); + addURI(URI.create(DEFAULT_URI + "/" + objContainer.getName()).toASCIIString()); + } catch (Exception e) { + throw new EugeneException(e.toString()); + } - } else if (element instanceof ComponentType) { + // ELEMENTS OF COLLECTION + for (NamedElement element : objContainer.getElements()) { - sbolCollection.addComponent( - Eugene2SBOL.convert( - (ComponentType) element, - null, - 0)); - } - } + if (element instanceof Component) { + sbolCollection.addComponent( + Eugene2SBOL.convert( + (Component) element, + null, + 0)); - lstURIs = null; - reusedComponents = null; + } else if (element instanceof ComponentType) { - return sbolCollection; - } + sbolCollection.addComponent( + Eugene2SBOL.convert( + (ComponentType) element, + null, + 0)); + } + } - /** Eugene Component --> SBOL DnaComponent **/ - public static DnaComponent convert(Component objComponent, DnaComponent parent, int pos) - throws EugeneException { + lstURIs = null; + reusedComponents = null; - if (null == objComponent) { - throw new EugeneException("I cannot export a NULL value to SBOL!"); + return sbolCollection; } - if (objComponent instanceof Device) { + /** + * Eugene Component --> SBOL DnaComponent * + */ + public static DnaComponent convert(Component objComponent, DnaComponent parent, int pos) + throws EugeneException { - // Eugene Device --> SBOL DnaComponent w/ SequenceAnnotations and subComponents - return toDnaComponent((Device) objComponent, parent); + if (null == objComponent) { + throw new EugeneException("I cannot export a NULL value to SBOL!"); + } - } else if (objComponent instanceof Part) { + if (objComponent instanceof Device) { - // Eugene Part --> SBOL DnaComponent w/o SequenceAnnotations and subComponents - return toDnaComponent((Part) objComponent, parent, pos); + // Eugene Device --> SBOL DnaComponent w/ SequenceAnnotations and subComponents + return toDnaComponent((Device) objComponent, parent); - } else { - throw new EugeneException( - "I cannot export the " + objComponent.getName() + " element to SBOL!"); - } - } + } else if (objComponent instanceof Part) { - /** Eugene ComponentType --> SBOL DnaComponent w/o DnaSequence **/ - public static DnaComponent convert(ComponentType objType, DnaComponent parent, int pos) - throws EugeneException { + // Eugene Part --> SBOL DnaComponent w/o SequenceAnnotations and subComponents + return toDnaComponent((Part) objComponent, parent, pos); - if (null == objType) { - throw new EugeneException("I cannot export a NULL value to SBOL!"); + } else { + throw new EugeneException( + "I cannot export the " + objComponent.getName() + " element to SBOL!"); + } } - if (objType instanceof PartType) { + /** + * Eugene ComponentType --> SBOL DnaComponent w/o DnaSequence * + */ + public static DnaComponent convert(ComponentType objType, DnaComponent parent, int pos) + throws EugeneException { - // Eugene Part Type --> SBOL DnaComponent w/o Sequence - return toDnaComponent((PartType) objType, parent, pos); + if (null == objType) { + throw new EugeneException("I cannot export a NULL value to SBOL!"); + } - } else { - throw new EugeneException("I cannot export the " + objType.getName() + " element to SBOL!"); - } + if (objType instanceof PartType) { - } + // Eugene Part Type --> SBOL DnaComponent w/o Sequence + return toDnaComponent((PartType) objType, parent, pos); + } else { + throw new EugeneException("I cannot export the " + objType.getName() + " element to SBOL!"); + } - /** Device --> composite DNAComponent **/ - public static DnaComponent toDnaComponent(Device objDevice, DnaComponent parent) - throws EugeneException { + } - DnaComponent dc = SBOLFactory.createDnaComponent(); + /** + * Device --> composite DNAComponent * + */ + public static DnaComponent toDnaComponent(Device objDevice, DnaComponent parent) + throws EugeneException { - // DISPLAY ID - String deviceDisplayId = objDevice.getName(); - if (null == parent) { - dc.setDisplayId(deviceDisplayId); - } else { - dc.setDisplayId(parent.getDisplayId() + "_" + deviceDisplayId); - } + DnaComponent dc = SBOLFactory.createDnaComponent(); - // NAME - dc.setName(objDevice.getName()); + // DISPLAY ID + String deviceDisplayId = objDevice.getName(); + if (null == parent) { + dc.setDisplayId(deviceDisplayId); + } else { + dc.setDisplayId(parent.getDisplayId() + "_" + deviceDisplayId); + } - // DESCRIPTION - dc.setDescription(objDevice.getName()); + // NAME + dc.setName(objDevice.getName()); - // URI - String sURI = null; - if (null != parent) { - sURI = parent.getURI() + "/" + objDevice.getName(); - } else { - sURI = DEFAULT_URI + "/" + deviceDisplayId; - } - dc.setURI(URI.create(sURI)); - addURI(sURI); - - //n is a counter over the device's sub components - int n = 1; - - // nCurrentStrandIdx is a counter for the current index - // in the composite DNA strand - // it's used to calculate for SBOL bioStart and bioEnd - // (for SequenceAnnotations) - int nCurrentStrandIdx = 1; - - int pos = 0; - String subComponentDisplayIds = null; - - StringBuilder device_seq = new StringBuilder(); - - //a Eugene Device is a composite component, i.e. a list of sub-components - // sub-component --> SequenceAnnotaion w/ a sub-DNAComponent - for (NamedElement c : objDevice.getComponentList()) { - - // SEQUENCE ANNOTATION - SequenceAnnotation sa = SBOLFactory.createSequenceAnnotation(); - if (null != parent) { - sa.setURI(URI.create(parent.getURI() + "/" + objDevice.getName() + "/annotation_" + n + "/" + c.getName())); - } else { - sa.setURI(URI.create(DEFAULT_URI + "/" + objDevice.getName() + "/annotation_" + n + "/" + c.getName())); - } - addURI(sa.getURI().toASCIIString()); - - //if the sub-component is a device itself, - //then flip it and inverse the orientation of each component. - // **See the DeviceUtils.flipAndInvert(Device) method for further documentation. - if (c instanceof Device && objDevice.getOrientations().get(n - 1).get(0) == Orientation.REVERSE) { - c = DeviceUtils.flipAndInvert((Device) c); - } - - // STRAND/ORIENTATION OF SEQUENCE ANNOTATION - if (objDevice.getOrientations().get(n - 1).get(0) == Orientation.FORWARD - || objDevice.getOrientations().get(n - 1).get(0) == Orientation.UNDEFINED) { - sa.setStrand(StrandType.POSITIVE); - } else { - sa.setStrand(StrandType.NEGATIVE); - } - - // SUB-DNA COMPONENT - //convert the device's sub component into a SBOL DNAComponent - //and assign it to the current SequenceAnnoation - DnaComponent subComponent = null; - - //DISPLAY ID OF SUB-COMPONENT - if (null == subComponentDisplayIds) { - subComponentDisplayIds = c.getName(); - } else { - subComponentDisplayIds += "_" + c.getName(); - } - - if (c instanceof Component) { - subComponent = Eugene2SBOL.convert((Component) c, dc, pos); - } else if (c instanceof ComponentType) { - subComponent = Eugene2SBOL.convert((ComponentType) c, dc, pos); - } else { - throw new EugeneException("Invalid!"); - } - - // assign the sub-DNAComponent to the SequenceAnnotation - sa.setSubComponent(subComponent); - - // SEQUENCE OF SUB-COMPONENT - if (null != subComponent.getDnaSequence() - && subComponent.getDnaSequence().getNucleotides().length() > 0) { - - //START and END (bioStart, bioEnd) - int start = nCurrentStrandIdx; - int end = -1; - if (null != subComponent.getDnaSequence() - && null != subComponent.getDnaSequence().getNucleotides()) { - end = start + (subComponent.getDnaSequence().getNucleotides().length() - 1); + // DESCRIPTION + dc.setDescription(objDevice.getName()); + + // URI + String sURI = null; + if (null != parent) { + sURI = parent.getURI() + "/" + objDevice.getName(); + } else { + sURI = DEFAULT_URI + "/" + deviceDisplayId; } - sa.setBioStart(start); - sa.setBioEnd(end); + dc.setURI(URI.create(sURI)); + addURI(sURI); - // adjust the current strand index appropriately - nCurrentStrandIdx = end + 1; + //n is a counter over the device's sub components + int n = 1; - // if the orientation is reversed, - // build the reverse complemented sequence - if (c instanceof Part) { + // nCurrentStrandIdx is a counter for the current index + // in the composite DNA strand + // it's used to calculate for SBOL bioStart and bioEnd + // (for SequenceAnnotations) + int nCurrentStrandIdx = 1; - // if sub-component is a part, - // do the reverse complement of the sequence - if (sa.getStrand() == StrandType.NEGATIVE) { + int pos = 0; + String subComponentDisplayIds = null; - try { + StringBuilder device_seq = new StringBuilder(); - // reverse complement the sequence - // from the Eugene SequenceUtils class - String rev_comp = SequenceUtils.reverseComplement( - sa.getSubComponent().getDnaSequence().getNucleotides().toString()); + //a Eugene Device is a composite component, i.e. a list of sub-components + // sub-component --> SequenceAnnotaion w/ a sub-DNAComponent + for (NamedElement c : objDevice.getComponentList()) { - // and assign the reverse complemented DNA sequence - // to the sub-component - sa.getSubComponent().getDnaSequence().setNucleotides(rev_comp); + // SEQUENCE ANNOTATION + SequenceAnnotation sa = SBOLFactory.createSequenceAnnotation(); + if (null != parent) { + sa.setURI(URI.create(parent.getURI() + "/" + objDevice.getName() + "/annotation_" + n + "/" + c.getName())); + } else { + sa.setURI(URI.create(DEFAULT_URI + "/" + objDevice.getName() + "/annotation_" + n + "/" + c.getName())); + } + addURI(sa.getURI().toASCIIString()); - // append the reverse complemented - // DNA sequence to the string buffer - // to keep track of the device's sequence - device_seq.append(rev_comp); + //if the sub-component is a device itself, + //then flip it and inverse the orientation of each component. + // **See the DeviceUtils.flipAndInvert(Device) method for further documentation. + if (c instanceof Device && objDevice.getOrientations().get(n - 1).get(0) == Orientation.REVERSE) { + c = DeviceUtils.flipAndInvert((Device) c); + } - } catch (Exception e) { - e.printStackTrace(); - throw new EugeneException("Invalid DNA sequence!"); + // STRAND/ORIENTATION OF SEQUENCE ANNOTATION + if (objDevice.getOrientations().get(n - 1).get(0) == Orientation.FORWARD + || objDevice.getOrientations().get(n - 1).get(0) == Orientation.UNDEFINED) { + sa.setStrand(StrandType.POSITIVE); + } else { + sa.setStrand(StrandType.NEGATIVE); } - } else { - - // append the part's DNA sequence to the string buffer - // tokeep track of the device's sequence - device_seq.append(sa.getSubComponent().getDnaSequence().getNucleotides().toString()); - } - } else if (c instanceof Device) { - // if sub-component is a Device, - // then append the sub-Device's sequence - device_seq.append(sa.getSubComponent().getDnaSequence().getNucleotides().toString()); + + // SUB-DNA COMPONENT + //convert the device's sub component into a SBOL DNAComponent + //and assign it to the current SequenceAnnoation + DnaComponent subComponent = null; + + //DISPLAY ID OF SUB-COMPONENT + if (null == subComponentDisplayIds) { + subComponentDisplayIds = c.getName(); + } else { + subComponentDisplayIds += "_" + c.getName(); + } + + if (c instanceof Component) { + subComponent = Eugene2SBOL.convert((Component) c, dc, pos); + } else if (c instanceof ComponentType) { + subComponent = Eugene2SBOL.convert((ComponentType) c, dc, pos); + } else { + throw new EugeneException("Invalid!"); + } + + // assign the sub-DNAComponent to the SequenceAnnotation + sa.setSubComponent(subComponent); + + // SEQUENCE OF SUB-COMPONENT + if (null != subComponent.getDnaSequence() + && subComponent.getDnaSequence().getNucleotides().length() > 0) { + + //START and END (bioStart, bioEnd) + int start = nCurrentStrandIdx; + int end = -1; + if (null != subComponent.getDnaSequence() + && null != subComponent.getDnaSequence().getNucleotides()) { + end = start + (subComponent.getDnaSequence().getNucleotides().length() - 1); + } + sa.setBioStart(start); + sa.setBioEnd(end); + + // adjust the current strand index appropriately + nCurrentStrandIdx = end + 1; + + // if the orientation is reversed, + // build the reverse complemented sequence + if (c instanceof Part) { + + // if sub-component is a part, + // do the reverse complement of the sequence + if (sa.getStrand() == StrandType.NEGATIVE) { + + try { + + // reverse complement the sequence + // from the Eugene SequenceUtils class + String rev_comp = SequenceUtils.reverseComplement( + sa.getSubComponent().getDnaSequence().getNucleotides().toString()); + + // and assign the reverse complemented DNA sequence + // to the sub-component + sa.getSubComponent().getDnaSequence().setNucleotides(rev_comp); + + // append the reverse complemented + // DNA sequence to the string buffer + // to keep track of the device's sequence + device_seq.append(rev_comp); + + } catch (Exception e) { + e.printStackTrace(); + throw new EugeneException("Invalid DNA sequence!"); + } + } else { + + // append the part's DNA sequence to the string buffer + // tokeep track of the device's sequence + device_seq.append(sa.getSubComponent().getDnaSequence().getNucleotides().toString()); + } + } else if (c instanceof Device) { + // if sub-component is a Device, + // then append the sub-Device's sequence + device_seq.append(sa.getSubComponent().getDnaSequence().getNucleotides().toString()); + } + + } + + // assign SequenceAnnotation to DNAComponent + dc.addAnnotation(sa); + + n++; + pos++; } - } + // map Eugene device onto the SO "engineered component" + dc.getTypes().add(soMapping("Device")); + + if (null != device_seq.toString() && !device_seq.toString().isEmpty()) { + DnaSequence dnaSeq = SBOLFactory.createDnaSequence(); + dnaSeq.setURI(URI.create(dc.getURI() + "_sequence")); + addURI(URI.create(dc.getURI() + "_sequence").toASCIIString()); - // assign SequenceAnnotation to DNAComponent - dc.addAnnotation(sa); + dnaSeq.setNucleotides(device_seq.toString()); - n++; - pos++; + dc.setDnaSequence(dnaSeq); + } + + return dc; } - // map Eugene device onto the SO "engineered component" - dc.getTypes().add(soMapping("Device")); + /** + * The toDnaComponent(PartType, DnaComponent) method compiles a Eugene Part + * Type into an SBOL DNAComponent w/o SequenceAnnotations and w/o + * DNASequence. + * + * @param objPartType ... the Eugene PartType object that should be compiled + * @param parent ... the PartType's parent + * + * @return an SBOL DNAComponent w/o SequenceAnnotations and w/o DNASequence + */ + public static DnaComponent toDnaComponent(PartType objPartType, DnaComponent parent) { + DnaComponent c = SBOLFactory.createDnaComponent(); + + // DISPLAY ID + if (null != parent) { + c.setDisplayId(parent.getDisplayId() + "_" + objPartType.getName()); + } else { + c.setDisplayId(objPartType.getName()); + } - if (null != device_seq.toString() && !device_seq.toString().isEmpty()) { - DnaSequence dnaSeq = SBOLFactory.createDnaSequence(); - dnaSeq.setURI(URI.create(dc.getURI() + "_sequence")); - addURI(URI.create(dc.getURI() + "_sequence").toASCIIString()); + // DESCRIPTION + c.setDescription(objPartType.getName()); - dnaSeq.setNucleotides(device_seq.toString()); + // URI + if (null != parent) { + c.setURI(URI.create(parent.getURI() + "/" + objPartType.getName())); + addURI(URI.create(parent.getURI() + "/" + objPartType.getName()).toASCIIString()); + } else { + c.setURI(URI.create(DEFAULT_URI + "/" + objPartType.getName())); + addURI(URI.create(DEFAULT_URI + "/" + objPartType.getName()).toASCIIString()); + } - dc.setDnaSequence(dnaSeq); - } + // DNAComponent's type + // i.e. the part type's name mapped to an SO term + c.addType(soMapping(objPartType.getName())); - return dc; - } - - /** - * The toDnaComponent(PartType, DnaComponent) method compiles a Eugene Part - * Type into an SBOL DNAComponent w/o SequenceAnnotations and w/o DNASequence. - * - * @param objPartType ... the Eugene PartType object that should be compiled - * @param parent ... the PartType's parent - * - * @return an SBOL DNAComponent w/o SequenceAnnotations and w/o DNASequence - */ - public static DnaComponent toDnaComponent(PartType objPartType, DnaComponent parent) { - DnaComponent c = SBOLFactory.createDnaComponent(); - - // DISPLAY ID - if (null != parent) { - c.setDisplayId(parent.getDisplayId() + "_" + objPartType.getName()); - } else { - c.setDisplayId(objPartType.getName()); + return c; } - // DESCRIPTION - c.setDescription(objPartType.getName()); + /** + * Part --> basic DnaComponent w/ DnaSequence (if set) * + */ + public static DnaComponent toDnaComponent(Part objPart, DnaComponent parent, int pos) { - // URI - if (null != parent) { - c.setURI(URI.create(parent.getURI() + "/" + objPartType.getName())); - addURI(URI.create(parent.getURI() + "/" + objPartType.getName()).toASCIIString()); - } else { - c.setURI(URI.create(DEFAULT_URI + "/" + objPartType.getName())); - addURI(URI.create(DEFAULT_URI + "/" + objPartType.getName()).toASCIIString()); - } + DnaComponent c = SBOLFactory.createDnaComponent(); - // DNAComponent's type - // i.e. the part type's name mapped to an SO term + // PART TYPE + c.addType(soMapping(objPart.getType().getName())); - c.addType(soMapping(objPartType.getName())); + // NAME => SO term + c.setName(objPart.getName()); - return c; - } + // DESCRIPTION + PropertyValue description = objPart.getPropertyValue(EugeneConstants.DESCRIPTION_PROPERTY); + if (null != description && !description.getTxt().isEmpty()) { + c.setDescription(objPart.getPropertyValue(EugeneConstants.DESCRIPTION_PROPERTY).getTxt()); + } else { + c.setDescription(objPart.getType().getName()); + } - /** Part --> basic DnaComponent w/ DnaSequence (if set) **/ - public static DnaComponent toDnaComponent(Part objPart, DnaComponent parent, int pos) { + // DISPLAY ID + String uuid = UUID.randomUUID().toString(); + PropertyValue displayId = objPart.getPropertyValue(EugeneConstants.DISPLAY_ID_PROPERTY); + if (null != displayId && !displayId.getTxt().isEmpty()) { + c.setDisplayId(objPart.getPropertyValue(EugeneConstants.DISPLAY_ID_PROPERTY).getTxt() + "/" + uuid); + } else { + if (null != parent) { + c.setDisplayId(parent.getDisplayId() + "_" + "pos_" + pos + "_" + objPart.getName()); + } else { + c.setDisplayId("pos_" + pos + "_" + objPart.getName()); + } + } - DnaComponent c = SBOLFactory.createDnaComponent(); + // URI + URI partURI = URI.create(DEFAULT_URI + "/parts/" + objPart.getName()); + if (null != parent && pos != -1) { + partURI = URI.create(parent.getURI() + "/pos_" + pos + "/" + objPart.getName()); + } - // PART TYPE - c.addType(soMapping(objPart.getType().getName())); + PropertyValue uri = objPart.getPropertyValue(EugeneConstants.URI_PROPERTY); + if (null != uri && !uri.getTxt().isEmpty()) { + if (null != parent && pos != -1) { + partURI = URI.create(objPart.getPropertyValue(EugeneConstants.URI_PROPERTY).getTxt() + "/" + uuid); + } else { + partURI = URI.create(objPart.getPropertyValue(EugeneConstants.URI_PROPERTY).getTxt()); + } + } + c.setURI(partURI); + addURI(partURI.toASCIIString()); - // NAME - c.setName(objPart.getName()); + // SEQUENCE + // **part must have a non-empty SEQUENCE property + if (null != objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY) + && null != objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt() + && !(objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt().isEmpty())) { - // DESCRIPTION - PropertyValue description = objPart.getPropertyValue(EugeneConstants.DESCRIPTION_PROPERTY); - if (null != description && !description.getTxt().isEmpty()) { - c.setDescription(objPart.getPropertyValue(EugeneConstants.DESCRIPTION_PROPERTY).getTxt()); - } else { - c.setDescription(objPart.getType().getName()); - } + // SBOL DnaSequence object + DnaSequence seq = SBOLFactory.createDnaSequence(); - // DISPLAY ID - String uuid = UUID.randomUUID().toString(); - PropertyValue displayId = objPart.getPropertyValue(EugeneConstants.DISPLAY_ID_PROPERTY); - if (null != displayId && !displayId.getTxt().isEmpty()) { - c.setDisplayId(objPart.getPropertyValue(EugeneConstants.DISPLAY_ID_PROPERTY).getTxt() + "/" + uuid); - } else { - if (null != parent) { - c.setDisplayId(parent.getDisplayId() + "_" + "pos_" + pos + "_" + objPart.getName()); - } else { - c.setDisplayId("pos_" + pos + "_" + objPart.getName()); - } - } + // URI of the DnaSequence + seq.setURI(URI.create(partURI + "_sequence")); - // URI - URI partURI = URI.create(DEFAULT_URI + "/parts/" + objPart.getName()); - if (null != parent && pos != -1) { - partURI = URI.create(parent.getURI() + "/pos_" + pos + "/" + objPart.getName()); - } + // nucleotides of the DNA sequence + seq.setNucleotides(objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt().toLowerCase()); + c.setDnaSequence(seq); - PropertyValue uri = objPart.getPropertyValue(EugeneConstants.URI_PROPERTY); - if (null != uri && !uri.getTxt().isEmpty()) { - if (null != parent && pos != -1) { - partURI = URI.create(objPart.getPropertyValue(EugeneConstants.URI_PROPERTY).getTxt() + "/" + uuid); - } else { - partURI = URI.create(objPart.getPropertyValue(EugeneConstants.URI_PROPERTY).getTxt()); - } - } - c.setURI(partURI); - addURI(partURI.toASCIIString()); - - // SEQUENCE - // **part must have a non-empty SEQUENCE property - if (null != objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY) - && null != objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt() - && !(objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt().isEmpty())) { - - // SBOL DnaSequence object - DnaSequence seq = SBOLFactory.createDnaSequence(); - - // URI of the DnaSequence - seq.setURI(URI.create(partURI + "_sequence")); - - // nucleotides of the DNA sequence - seq.setNucleotides(objPart.getPropertyValue(EugeneConstants.SEQUENCE_PROPERTY).getTxt().toLowerCase()); - c.setDnaSequence(seq); - - // keep track of the URIs - addURI(URI.create(partURI + "_sequence").toASCIIString()); + // keep track of the URIs + addURI(URI.create(partURI + "_sequence").toASCIIString()); + } + return c; } - return c; - } - /** PartType --> basic DnaComponent w/o DnaSequence **/ - public static DnaComponent toDnaComponent(PartType objType, DnaComponent parent, int pos) { + /** + * PartType --> basic DnaComponent w/o DnaSequence * + */ + public static DnaComponent toDnaComponent(PartType objType, DnaComponent parent, int pos) { - DnaComponent c = SBOLFactory.createDnaComponent(); + DnaComponent c = SBOLFactory.createDnaComponent(); - // PART TYPE - c.addType(soMapping(objType.getName())); + // PART TYPE + c.addType(soMapping(objType.getName())); - // NAME - c.setName(objType.getName()); + // NAME + c.setName(objType.getName()); - // DESCRIPTION - c.setDescription(objType.getName()); + // DESCRIPTION + c.setDescription(objType.getName()); - // DISPLAY ID - if (null != parent) { - c.setDisplayId(parent.getDisplayId() + "_" + "pos_" + pos + "_" + objType.getName()); - } else { - c.setDisplayId("pos_" + pos + "_" + objType.getName()); - } + // DISPLAY ID + if (null != parent) { + c.setDisplayId(parent.getDisplayId() + "_" + "pos_" + pos + "_" + objType.getName()); + } else { + c.setDisplayId("pos_" + pos + "_" + objType.getName()); + } - // URI - URI partURI = URI.create(DEFAULT_URI + "/types/" + objType.getName()); - if (null != parent && pos != -1) { - partURI = URI.create(parent.getURI() + "/pos_" + pos + "/" + objType.getName()); - } - c.setURI(partURI); - addURI(partURI.toASCIIString()); + // URI + URI partURI = URI.create(DEFAULT_URI + "/types/" + objType.getName()); + if (null != parent && pos != -1) { + partURI = URI.create(parent.getURI() + "/pos_" + pos + "/" + objType.getName()); + } + c.setURI(partURI); + addURI(partURI.toASCIIString()); - return c; - } + return c; + } - /*---------------------------------- + /*---------------------------------- * HELPER METHODS *----------------------------------*/ - /** - * mapping an SO term (encoded as string) to the SO term's URI see: - * http://www.sequenceontology.org - * - * @param s ... the SO Term - * - * @return ... the URI of the SO term - */ - private static URI soMapping(String s){ - - URI retVal = URI.create(DEFAULT_URI); - switch(s){ - case "Five_Prime_UTR": retVal = SequenceOntology.FIVE_PRIME_UTR; break; - - case "CDS": retVal = SequenceOntology.CDS; break; - - case "RBS": - // Set to SO of Ribosome Entry Site - try { retVal = new URI("http://purl.obolibrary.org/obo/SO_0000139"); - } catch (Exception e) { - e.printStackTrace(); - } break; - - case "Insulator": retVal = SequenceOntology.INSULATOR; break; - - case "Operator": retVal = SequenceOntology.OPERATOR; break; - - case "Origin_of_Replication": retVal = SequenceOntology.ORIGIN_OF_REPLICATION; break; - - case "Primiter_Binding_Site": retVal = SequenceOntology.PRIMER_BINDING_SITE; break; - - case "Promoter": retVal = SequenceOntology.PROMOTER; break; - - case "Restriction_Enzyme_Recognition_Site": - retVal = SequenceOntology.RESTRICTION_ENZYME_RECOGNITION_SITE; break; - - case "Terminator": retVal = SequenceOntology.TERMINATOR; break; - - case "Device": - - // not recognized by SBOL - // set to SO of Engineered Foreign Region - default: - try { retVal = new URI("http://purl.obolibrary.org/obo/SO_0000805"); - } catch (Exception e) { - e.printStackTrace(); - }; break; - } - - return retVal; - } - - /** - * keeping track of utilized URIs since all URIs throughout an SBOL document - * must be unique - * - * @param s - */ - public static void addURI(String s) { - if (null == lstURIs) { - lstURIs = new ArrayList(); - } + /** + * mapping an SO term (encoded as string) to the SO term's URI see: + * http://www.sequenceontology.org + * + * @param s ... the SO Term + * + * @return ... the URI of the SO term + */ + private static URI soMapping(String s) { + + URI retVal = URI.create(DEFAULT_URI); + switch (s) { + case "Five_Prime_UTR": + retVal = SequenceOntology.FIVE_PRIME_UTR; + break; + + case "CDS": + retVal = SequenceOntology.CDS; + break; + + case "RBS": + // Set to SO of Ribosome Entry Site + try { + retVal = new URI("http://purl.obolibrary.org/obo/SO_0000139"); + } catch (Exception e) { + e.printStackTrace(); + } + break; + + case "Insulator": + retVal = SequenceOntology.INSULATOR; + break; + + case "Operator": + retVal = SequenceOntology.OPERATOR; + break; + + case "Origin_of_Replication": + retVal = SequenceOntology.ORIGIN_OF_REPLICATION; + break; + + case "Primiter_Binding_Site": + retVal = SequenceOntology.PRIMER_BINDING_SITE; + break; + + case "Promoter": + retVal = SequenceOntology.PROMOTER; + break; + + case "Restriction_Enzyme_Recognition_Site": + retVal = SequenceOntology.RESTRICTION_ENZYME_RECOGNITION_SITE; + break; + + case "Terminator": + retVal = SequenceOntology.TERMINATOR; + break; + + case "Device": + + // not recognized by SBOL + // set to SO of Engineered Foreign Region + default: + try { + retVal = new URI("http://purl.obolibrary.org/obo/SO_0000805"); + } catch (Exception e) { + e.printStackTrace(); + } + ; + break; + } - if (!lstURIs.contains(s)) { - lstURIs.add(s); + // Update Lookup Table + _logicLayer.AddOrUpdateSO(retVal.toString(), s); + return retVal; } - } - - /** - * keeping track of the utilized displayIDs since SBOL displayIDs must be - * unique throughout an SBOL document - * - * @param s - */ - public static void addDisplayId(String s) { - if (null == lstURIs) { - lstURIs = new ArrayList(); + + /** + * keeping track of utilized URIs since all URIs throughout an SBOL document + * must be unique + * + * @param s + */ + public static void addURI(String s) { + if (null == lstURIs) { + lstURIs = new ArrayList(); + } + + if (!lstURIs.contains(s)) { + lstURIs.add(s); + } } - if (!lstURIs.contains(s)) { - lstURIs.add(s); + /** + * keeping track of the utilized displayIDs since SBOL displayIDs must be + * unique throughout an SBOL document + * + * @param s + */ + public static void addDisplayId(String s) { + if (null == lstURIs) { + lstURIs = new ArrayList(); + } + + if (!lstURIs.contains(s)) { + lstURIs.add(s); + } } - } } diff --git a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/MongoConnection.java b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/MongoConnection.java new file mode 100644 index 0000000..c46603e --- /dev/null +++ b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/MongoConnection.java @@ -0,0 +1,57 @@ +package org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.MongoCredential; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.MongoIterable; +import org.bson.Document; + +/** + * + * @author Shamseen Rahman + */ +public class MongoConnection { + + private MongoClient client; + private MongoDatabase db; + + public MongoConnection() { + } + + // TO DO: ERROR HANDLING ------------------------------------------------------ + public MongoCollection getCollection(String colName) { + MongoCollection collection = db.getCollection(colName); + return collection; + } + + public MongoIterable getCollectionNames() { +// Document retVal = db.runCommand(new Document("listCollections", 1)); + MongoIterable retVal = db.listCollectionNames(); + + return retVal; + } + + public void login(String user, String pw) throws Exception { + if (user == null || pw == null) { + } + // To connect to mongodb server + String uri = String.format("mongodb://%s:%s@ds153412.mlab.com:53412/lookuptables", user, pw); + + MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("admin", "admin", + "admin123".toCharArray()); + MongoClientURI connectionString = new MongoClientURI(uri); + client = new MongoClient(connectionString); + db = client.getDatabase("lookuptables"); + System.out.println("Connected to database successfully!"); + //boolean auth = db.authenticate(myUserName, myPassword); + //System.out.println("Authentication: "+auth); + + } + + public void logout() { + client.close(); + } +} diff --git a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionController.java b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionController.java new file mode 100644 index 0000000..21427c9 --- /dev/null +++ b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionController.java @@ -0,0 +1,14 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI; + +/** + * + * @author Shamseen Rahman + */ +public class SBOLConversionController { + +} diff --git a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionDataLayer.java b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionDataLayer.java new file mode 100644 index 0000000..adf42c5 --- /dev/null +++ b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionDataLayer.java @@ -0,0 +1,112 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI; + +// MongoDB imports +import org.junit.After; +import org.junit.Before; +import com.mongodb.client.MongoCollection; +import static com.mongodb.client.model.Filters.eq; +import org.bson.Document; + +// Java imports +import java.util.ArrayList; +import java.util.Arrays; + +/** + * SBOLConversionDataLayer connects to the LookupTables database (MongoDB) at + * https://mlab.com/databases/lookuptables . + * + * Performs two-way data exchange with the SBOL2Conversion collection. + * + * @author Shamseen Rahman + */ +public class SBOLConversionDataLayer { + + private MongoConnection _instance; + private MongoCollection collection; + private Document doc; + + public SBOLConversionDataLayer() { + setUp(); + } + + public ArrayList getEugenePartNames(String so) { + ArrayList names = new ArrayList<>(); + + if (findDocument(so)) { + names = (ArrayList) doc.get("eugenePartNames"); + } + + return names; + } + + public void UpsertEugenePartName(String so, String name) { + if (findDocument(so)) { + updateSOTerm(so, name, true); + return; + } + + Document d = new Document("sequenceOntologyNumber", so) + .append("eugenePartNames", Arrays.asList(name)); + + collection.insertOne(d); + } + + public boolean removeEugenePartName(String so, String name) { + boolean retVal = true; + + if (findDocument(so)) { + updateSOTerm(so, name, false); + } + + return retVal; + } + + private void updateSOTerm(String so, String name, boolean addName) { + ArrayList names = (ArrayList) doc.get("eugenePartNames"); + + if (addName == names.contains(name)) { + return; + } + + if (addName) { + names.add(name); + } else { + names.remove(name); + } + + collection.updateOne(eq("sequenceOntologyNumber", so), + new Document("$set", new Document("eugenePartNames", names))); + } + + private boolean findDocument(String so) { + + doc = (Document) collection.find(eq("sequenceOntologyNumber", so)).first(); + + boolean retVal = doc != null; + + return retVal; + } + + @Before + private void setUp() { + _instance = new MongoConnection(); + + try { + _instance.login("shamseen", "lcp"); + collection = _instance.getCollection("SBOL2Conversions"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @After + private void tearDown() { + _instance.logout(); + } +} diff --git a/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionLogicLayer.java b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionLogicLayer.java new file mode 100644 index 0000000..cb78f5c --- /dev/null +++ b/src/main/java/org/cidarlab/eugene/data/sbol/mapping/sbolConversionAPI/SBOLConversionLogicLayer.java @@ -0,0 +1,67 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI; + +import java.net.URI; +import java.util.List; +import org.sbolstandard.core.util.SequenceOntology; + +/** + * + * @author Shamseen Rahman + */ +public class SBOLConversionLogicLayer { + + private SBOLConversionDataLayer _dataLayer; + private List names; + private int uriIndex; + private String _so; + + // TO DO: Error handling + public SBOLConversionLogicLayer() { + _dataLayer = new SBOLConversionDataLayer(); + String uri = SequenceOntology.NAMESPACE.toString() + "SO_"; + uriIndex = uri.length(); + } + + public void AddOrUpdateSO(String so, String name) { + takeSONumberFromURI(so); + insertOrUpdate(name); + } + + public boolean DeleteEugenePartName(String so, String name) { + takeSONumberFromURI(so); + return deleteEugenePartName(name); + } + + public List GetEugeneNames(String so) { + takeSONumberFromURI(so); + getEugeneNames(); + return names; + } + + private boolean deleteEugenePartName(String name) { + boolean retVal = _dataLayer.removeEugenePartName(_so, name); + return retVal; + } + + private void getEugeneNames() { + names = _dataLayer.getEugenePartNames(_so); + } + + private void insertOrUpdate(String name) { + _dataLayer.UpsertEugenePartName(_so, name); + } + + private void takeSONumberFromURI(String so) { + if (so.length() > uriIndex) { + so = so.substring(uriIndex); + } + + _so = so; + } + +} diff --git a/src/test/java/org/cidarlab/eugene/units/data/SBOLTest.java b/src/test/java/org/cidarlab/eugene/units/data/SBOLTest.java index 59d86ec..412e664 100644 --- a/src/test/java/org/cidarlab/eugene/units/data/SBOLTest.java +++ b/src/test/java/org/cidarlab/eugene/units/data/SBOLTest.java @@ -26,9 +26,10 @@ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - package org.cidarlab.eugene.units.data; +import com.mongodb.client.MongoCollection; +import static com.mongodb.client.model.Filters.eq; import static org.junit.Assert.*; import org.cidarlab.eugene.Eugene; @@ -47,203 +48,366 @@ import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.Set; +import org.bson.Document; +import org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI.MongoConnection; +import org.cidarlab.eugene.data.sbol.mapping.sbolConversionAPI.*; +import org.sbolstandard.core.util.SequenceOntology; /** - * The SBOLTest unit-tests for Eugene v2.0's - * data exchange facilities regarding the SBOL standard. - * - * Test-Strategy for Exports: - * -------------------------- - * create in-memory objects, serialize them, read them in again, - * and compare their values. If the values match, then test passed. - * Example: - * A Eugene Part object is being serialized into SBOL. Then, we import - * the SBOL file which should only contain the Part object. - * If so, then we compare the values of the two Part objects. - * + * The SBOLTest unit-tests for Eugene v2.0's data exchange facilities regarding + * the SBOL standard. + * + * Test-Strategy for Exports: -------------------------- create in-memory + * objects, serialize them, read them in again, and compare their values. If the + * values match, then test passed. Example: A Eugene Part object is being + * serialized into SBOL. Then, we import the SBOL file which should only contain + * the Part object. If so, then we compare the values of the two Part objects. + * * @author Ernst Oberortner * */ public class SBOLTest { - - @Test - public void testSBOLExport_PartWithSequence() { - PartType pt = new PartType("PT", new ArrayList()); - Part p1 = new Part(pt, "p1"); - p1.setSequence("ATCG"); - - try { - SBOLExporter.serialize(p1, "./exports/tests/testSBOLExport_PartWithSequence.sbol.xml"); - - Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartWithSequence.sbol.xml"); - assert(set != null); - assert(set.size() == 1); - Iterator it = set.iterator(); - - NamedElement e = it.next(); - assert(e instanceof Part); - assert("p1".equals(((Part)e).getName())); - assert("ATCG".equalsIgnoreCase(((Part)e).getSequence())); - } catch(EugeneException ee) { - ee.printStackTrace(); - } - } - - @Test - public void testSBOLExport_PartWithoutSequence() { - PartType pt = new PartType("PT"); - Part p1 = new Part(pt, "p1"); + + @Test + public void testSBOLExport_PartWithSequence() { + PartType pt = new PartType("PT", new ArrayList()); + Part p1 = new Part(pt, "p1"); + p1.setSequence("ATCG"); + + try { + SBOLExporter.serialize(p1, "./exports/tests/testSBOLExport_PartWithSequence.sbol.xml"); + + Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartWithSequence.sbol.xml"); + assert (set != null); + assert (set.size() == 1); + Iterator it = set.iterator(); + + NamedElement e = it.next(); + assert (e instanceof Part); + assert ("p1".equals(((Part) e).getName())); + assert ("ATCG".equalsIgnoreCase(((Part) e).getSequence())); + } catch (EugeneException ee) { + ee.printStackTrace(); + } + } + + @Test + public void testSBOLExport_PartWithoutSequence() { + PartType pt = new PartType("PT"); + Part p1 = new Part(pt, "p1"); // p1.setSequence("ATCG"); - - try { - SBOLExporter.serialize(p1, "./exports/tests/testSBOLExport_PartWithoutSequence.sbol.xml"); - - Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartWithoutSequence.sbol.xml"); - assert(set != null); - assert(set.size() == 1); - Iterator it = set.iterator(); - - NamedElement e = it.next(); - assert(e instanceof Part); - assert("p1".equals(((Part)e).getName())); - assert(null == ((Part)e).getSequence()); - } catch(EugeneException ee) { - ee.printStackTrace(); - } - } - - @Test - public void testSBOLExport_PartType() { - PartType pt = new PartType("PT"); - - try { - SBOLExporter.serialize(pt, "./exports/tests/testSBOLExport_PartType.sbol.xml"); - - Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartType.sbol.xml"); - assert(set != null); - assert(set.size() == 1); - Iterator it = set.iterator(); - - - NamedElement e = it.next(); - - // hm... a PartType is being converted into a Part... - assert(e instanceof Part); - assert("PT".equals(((Part)e).getName())); - } catch(EugeneException ee) { - ee.printStackTrace(); - } - } - - @Test - public void testSBOLExport_enumeratedDevices() { - String script = "PartType PT();" + - "PT p1; PT p2; PT p3; PT p4;" + - "Device D(PT);" + - "result = product(D);"; - try { - EugeneCollection ec = new Eugene().executeScript(script); - - NamedElement result = ec.get("result"); - - assert(null != result); - assert(result instanceof EugeneArray); - assert(((EugeneArray)result).getElements().size() == 8); - - // export to SBOL - SBOLExporter.serialize(result, - "./exports/tests/testSBOLExport_enumeratedDevice.sbol.xml"); - - // import from SBOL - Set impResult = SBOLImporter.importSBOL( - "./exports/tests/testSBOLExport_enumeratedDevice.sbol.xml"); - - // compare both the impResult and result - assert(null != impResult); - Iterator it = impResult.iterator(); - assert(it.hasNext()); - NamedElement impArray = it.next(); - assert(impArray.getName().equals(result.getName())); - - } catch(EugeneException ee) { - // something's wrong --> Test not passed. - assertTrue(false); - } - } - - @Test - public void testSBOLVisual_Device_with_filename() { - String script = "PartType PT();" + - "PT p1; PT p2; PT p3; PT p4;" + - "Device D(PT);" + - "result = product(D);" + - "SBOL.visualize(result, \"./tests/results/data-exchange/testSBOLVisual_Device.png\");"; - try { - new Eugene().executeScript(script); - - // after executing the Eugene script, - // the file must exists - File f = new File("./tests/results/data-exchange/testSBOLVisual_Device.png"); - assert(f.exists()); - - } catch(Exception ee) { - ee.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void testSBOLVisual_Device_without_filename() { - String script = "PartType PT();" + - "PT p1; PT p2; PT p3; PT p4;" + - "Device D(PT);" + - "result = product(D);" + - "SBOL.visualize(result);"; - try { - Eugene e = new Eugene(); - - // the name of the image is randomly generated. - // hence, we first get the number of .png files - // in the default IMAGE_DIRECTORY - int NR_OF_BEFORE_IMAGES = this.getNrOfImages(); - - e.executeScript(script); - - // after executing the Eugene script, - // there must be one more image - // in the IMAGES_DIRECTORY - int nrOfExpectedDevices = 2 * 4; - - if(nrOfExpectedDevices <= Interp.MAX_IMAGES) { - assertTrue(this.getNrOfImages() == - NR_OF_BEFORE_IMAGES + 1); - } else { - assertTrue(this.getNrOfImages() == - NR_OF_BEFORE_IMAGES + (nrOfExpectedDevices / Interp.MAX_IMAGES)); - } - - - } catch(Exception ee) { + + try { + SBOLExporter.serialize(p1, "./exports/tests/testSBOLExport_PartWithoutSequence.sbol.xml"); + + Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartWithoutSequence.sbol.xml"); + assert (set != null); + assert (set.size() == 1); + Iterator it = set.iterator(); + + NamedElement e = it.next(); + assert (e instanceof Part); + assert ("p1".equals(((Part) e).getName())); + assert (null == ((Part) e).getSequence()); + } catch (EugeneException ee) { + ee.printStackTrace(); + } + } + + @Test + public void testSBOLExport_PartType() { + PartType pt = new PartType("PT"); + + try { + SBOLExporter.serialize(pt, "./exports/tests/testSBOLExport_PartType.sbol.xml"); + + Set set = SBOLImporter.importSBOL("./exports/tests/testSBOLExport_PartType.sbol.xml"); + assert (set != null); + assert (set.size() == 1); + Iterator it = set.iterator(); + + NamedElement e = it.next(); + + // hm... a PartType is being converted into a Part... + assert (e instanceof Part); + assert ("PT".equals(((Part) e).getName())); + } catch (EugeneException ee) { + ee.printStackTrace(); + } + } + + @Test + public void testSBOLExport_PseudoAPI_Does_ConnectToMongo() { + + // Setup + String msg = ""; + boolean expected = true; + + // Run + try { + MongoConnection _instance = loginToMongo(); + } catch (Exception ex) { + msg = "Couldn't log into mongo: " + ex.getMessage(); + expected = false; + } + + // Evaluate + assertTrue(msg, expected); + } + + @Test + public void testSBOLExport_PseudoAPI_Does_GetCollection() { + // Setup + List actual = new ArrayList<>(); + String msg = ""; + + // Run + try { + MongoConnection _instance = loginToMongo(); + + String collection = _instance.getCollection("SBOL2Conversions").getNamespace().getCollectionName(); + + for (String name : _instance.getCollectionNames()) { + actual.add(name); + } + + } catch (Exception e) { + msg = e.getMessage(); + } + + // Evaluate + assertTrue(msg, actual.contains("SBOL2Conversions")); + } + + @Test + public void testSBOLExport_PseudoAPI_Does_GetEugeneNames() { + // Setup + SBOLConversionLogicLayer ll = new SBOLConversionLogicLayer(); + int uriIndex = SequenceOntology.NAMESPACE.toString().length() + 3; + String soNum = SequenceOntology.CDS.toString().substring(uriIndex); + + List expected = new ArrayList<>(); + + // Run + List actual = ll.GetEugeneNames(soNum); + + try { + MongoConnection _instance = loginToMongo(); + MongoCollection collection = _instance.getCollection("SBOL2Conversions"); + + Document doc = (Document) collection.find(eq("sequenceOntologyNumber", soNum)).first(); + + expected = (ArrayList) doc.get("eugenePartNames"); + + } catch (Exception ex) { + + } + + // Evaluate + assertFalse(expected.isEmpty()); + assertEquals(actual, expected); + + } + + @Test + public void testSBOLExport_PseudoAPI__Does_AddEugeneNames() { + // Setup + SBOLConversionLogicLayer ll = new SBOLConversionLogicLayer(); + + String soNum = SequenceOntology.RESTRICTION_ENZYME_RECOGNITION_SITE.toString(); + String partName = "newPart"; + List expected = Arrays.asList(partName); + + // Run + ll.AddOrUpdateSO(soNum, partName); + List actual = ll.GetEugeneNames(soNum); + + // Evaluate + assertTrue(actual.containsAll(expected)); + } + + @Test + public void testSBOLExport_PseudoAPI__DoesNot_AddDuplicateSO() { + // Setup + SBOLConversionLogicLayer ll = new SBOLConversionLogicLayer(); + + String soNum = SequenceOntology.RESTRICTION_ENZYME_RECOGNITION_SITE.toString(); + String partName = "newPart"; + + // Run + ll.AddOrUpdateSO(soNum, partName); + ll.AddOrUpdateSO(soNum, partName); + List names = ll.GetEugeneNames(soNum); + + // Evaluate + assertTrue(names.indexOf(partName) == names.lastIndexOf(partName)); + } + + @Test + public void testSBOLExport_PseudoAPI_Does_UpdateExistingSO() { + // Setup + SBOLConversionLogicLayer ll = new SBOLConversionLogicLayer(); + + String soNum = SequenceOntology.RESTRICTION_ENZYME_RECOGNITION_SITE.toString(); + String partName = "newPart"; + String newPartName = "evenBETTER"; + List expected = Arrays.asList(partName, newPartName); + + // Run + ll.AddOrUpdateSO(soNum, partName); + ll.AddOrUpdateSO(soNum, newPartName); + List actual = ll.GetEugeneNames(soNum); + + // Evaluate + assertTrue(actual.containsAll(expected)); + } + + @Test + public void testSBOLExport_PseudoAPI_Does_RemoveEugeneNamesOnly() { + // Setup + SBOLConversionLogicLayer ll = new SBOLConversionLogicLayer(); + int uriIndex = SequenceOntology.NAMESPACE.toString().length() + 3; + String soNum = SequenceOntology.CDS.toString().substring(uriIndex); + String partName = "newPart"; + Document actual = new Document(); + + // Run + try { + ll.AddOrUpdateSO(soNum, partName); + ll.DeleteEugenePartName(soNum, partName); + + MongoConnection _instance = loginToMongo(); + MongoCollection collection = _instance.getCollection("SBOL2Conversions"); + + actual = (Document) collection.find(eq("sequenceOntologyNumber", soNum)).first(); + + } catch (Exception ex) { + + } + + // Evaluate + assertTrue(actual.containsValue(soNum)); + assertFalse(Arrays.asList(actual.get("eugenePartNames")).contains(partName)); + } + + @Test + public void testSBOLExport_enumeratedDevices() { + String script = "PartType PT();" + + "PT p1; PT p2; PT p3; PT p4;" + + "Device D(PT);" + + "result = product(D);"; + try { + EugeneCollection ec = new Eugene().executeScript(script); + + NamedElement result = ec.get("result"); + + assert (null != result); + assert (result instanceof EugeneArray); + assert (((EugeneArray) result).getElements().size() == 8); + + // export to SBOL + SBOLExporter.serialize(result, + "./exports/tests/testSBOLExport_enumeratedDevice.sbol.xml"); + + // import from SBOL + Set impResult = SBOLImporter.importSBOL( + "./exports/tests/testSBOLExport_enumeratedDevice.sbol.xml"); + + // compare both the impResult and result + assert (null != impResult); + Iterator it = impResult.iterator(); + assert (it.hasNext()); + NamedElement impArray = it.next(); + assert (impArray.getName().equals(result.getName())); + + } catch (EugeneException ee) { + // something's wrong --> Test not passed. + assertTrue(false); + } + } + + @Test + public void testSBOLVisual_Device_with_filename() { + String script = "PartType PT();" + + "PT p1; PT p2; PT p3; PT p4;" + + "Device D(PT);" + + "result = product(D);" + + "SBOL.visualize(result, \"./tests/results/data-exchange/testSBOLVisual_Device.png\");"; + try { + new Eugene().executeScript(script); + + // after executing the Eugene script, + // the file must exists + File f = new File("./tests/results/data-exchange/testSBOLVisual_Device.png"); + assert (f.exists()); + + } catch (Exception ee) { + ee.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testSBOLVisual_Device_without_filename() { + String script = "PartType PT();" + + "PT p1; PT p2; PT p3; PT p4;" + + "Device D(PT);" + + "result = product(D);" + + "SBOL.visualize(result);"; + try { + Eugene e = new Eugene(); + + // the name of the image is randomly generated. + // hence, we first get the number of .png files + // in the default IMAGE_DIRECTORY + int NR_OF_BEFORE_IMAGES = this.getNrOfImages(); + + e.executeScript(script); + + // after executing the Eugene script, + // there must be one more image + // in the IMAGES_DIRECTORY + int nrOfExpectedDevices = 2 * 4; + + if (nrOfExpectedDevices <= Interp.MAX_IMAGES) { + assertTrue(this.getNrOfImages() + == NR_OF_BEFORE_IMAGES + 1); + } else { + assertTrue(this.getNrOfImages() + == NR_OF_BEFORE_IMAGES + (nrOfExpectedDevices / Interp.MAX_IMAGES)); + } + + } catch (Exception ee) { // ee.printStackTrace(); - assertTrue(false); - } - } - - private int getNrOfImages() { - File dir = new File(Eugene.ROOT_DIRECTORY+"/"+Eugene.IMAGES_DIRECTORY+"/"); - File[] files = dir.listFiles(new FilenameFilter() { - public boolean accept(File dir, String name) { - return name.toLowerCase().endsWith(".png"); - } - }); - return files.length; - } - - @Test - public void testReadFASTAFromRegistry() - throws Exception { - - } + assertTrue(false); + } + } + + private int getNrOfImages() { + File dir = new File(Eugene.ROOT_DIRECTORY + "/" + Eugene.IMAGES_DIRECTORY + "/"); + File[] files = dir.listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.toLowerCase().endsWith(".png"); + } + }); + return files.length; + } + + @Test + public void testReadFASTAFromRegistry() + throws Exception { + + } + + private MongoConnection loginToMongo() throws Exception { + MongoConnection _instance = new MongoConnection(); + + _instance.login("shamseen", "lcp"); + + return _instance; + } }