Search in sources :

Example 6 with Sequence

use of com.google.showcase.v1beta1.Sequence in project libSBOLj by SynBioDex.

the class SBOLWorkshop2018 method compile.

public static void compile(SBOLDocument sbolDocument, ComponentDefinition componentDefinition) throws SBOLValidationException {
    for (Component component : componentDefinition.getComponents()) {
        SequenceAnnotation sa = componentDefinition.getSequenceAnnotation(component);
        if (sa == null) {
            sa = componentDefinition.createSequenceAnnotation(component.getDisplayId() + "_annot", "location");
            sa.setComponent(component.getIdentity());
        }
    }
    int start = 1;
    String elements = "";
    for (Component component : componentDefinition.getSortedComponents()) {
        Sequence seq = component.getDefinition().getSequenceByEncoding(Sequence.IUPAC_DNA);
        int end = start + seq.getElements().length() - 1;
        SequenceAnnotation sa = componentDefinition.getSequenceAnnotation(component);
        componentDefinition.removeSequenceAnnotation(sa);
        sa = componentDefinition.createSequenceAnnotation(component.getDisplayId() + "_annot", "range", start, end);
        start = end + 1;
        sa.setComponent(component.getIdentity());
        elements += seq.getElements();
    }
    Sequence seq = sbolDocument.createSequence(componentDefinition.getDisplayId() + "_seq", version, elements, Sequence.IUPAC_DNA);
    componentDefinition.addSequence(seq);
}
Also used : SequenceAnnotation(org.sbolstandard.core2.SequenceAnnotation) Sequence(org.sbolstandard.core2.Sequence) Component(org.sbolstandard.core2.Component)

Example 7 with Sequence

use of com.google.showcase.v1beta1.Sequence in project libSBOLj by SynBioDex.

the class SBOLWorkshop2018 method main.

public static void main(String[] args) throws SBOLValidationException, IOException, SBOLConversionException, SynBioHubException {
    /* Getting a Device from an SBOL Compliant XML */
    // Start a new SBOL Document to hold the device
    SBOLDocument doc = new SBOLDocument();
    doc.setCreateDefaults(true);
    // Set your Homespace. All new SBOL objects will be created in this namespace
    String my_namespace = "http://my_namespace.org/";
    doc.setDefaultURIprefix(my_namespace);
    // Create a new device
    ComponentDefinition my_device = doc.createComponentDefinition("my_device", version, ComponentDefinition.DNA_REGION);
    System.out.println(my_device.getIdentity());
    System.out.println("");
    // Load some genetic parts taken from the Cello paper
    // TODO: need to fill in path
    SBOLDocument cello_parts = SBOLReader.read("/Users/myers/Downloads/parts.xml");
    System.out.println(len(cello_parts));
    printCounts(cello_parts);
    System.out.println("");
    // componentDefinitions and sequences
    for (TopLevel topLevel : cello_parts.getTopLevels()) {
        System.out.println(topLevel.getIdentity());
    }
    System.out.println("");
    // Import these objects into your Document
    doc.createCopy(cello_parts);
    for (TopLevel topLevel : doc.getTopLevels()) {
        System.out.println(topLevel.getIdentity());
    }
    System.out.println("");
    // Retrieve an object from the Document using its uniform resource identifier (URI)
    Collection promoter_collection = doc.getCollection(URI.create("http://examples.org/Collection/promoters/1"));
    // A Collection contains a list of URI references to objects, not the object themselves
    for (TopLevel topLevel : promoter_collection.getMembers()) {
        System.out.println(topLevel.getIdentity());
    }
    System.out.println("");
    // Retrieve a component, using its full URI
    ComponentDefinition promoter = doc.getComponentDefinition(URI.create("http://examples.org/ComponentDefinition/pPhlF/1"));
    // Review the BioPAX and Sequence Ontology terms that describe this component
    System.out.println(promoter.getTypes());
    System.out.println(promoter.getRoles());
    System.out.println("");
    /* Getting a Device from Synbiohub */
    // Start an interface to the part shop
    SynBioHubFrontend part_shop = new SynBioHubFrontend("https://synbiohub.org");
    // Search for records from the interlab study
    ArrayList<IdentifiedMetadata> records = part_shop.getMatchingComponentDefinitionMetadata("interlab", null, null, null, 0, 50);
    for (IdentifiedMetadata record : records) {
        System.out.println(record.getDisplayId() + ": " + record.getUri());
    }
    System.out.println("");
    // Import the medium device into the user's Document
    doc.createCopy(part_shop.getSBOL(URI.create("https://synbiohub.org/public/iGEM_2016_interlab/Medium_2016Interlab/1")));
    // Explore the new parts
    for (TopLevel topLevel : doc.getTopLevels()) {
        System.out.println(topLevel.getClass().getSimpleName() + ": " + topLevel.getIdentity());
    }
    System.out.println("");
    /* Extracting a ComponentDefinition from a Pre-existing Device */
    // Extract the medium strength promoter
    ComponentDefinition medium_strength_promoter = doc.getComponentDefinition(URI.create("https://synbiohub.org/public/igem/BBa_J23106/1"));
    // Get parts for a new circuit
    ComponentDefinition rbs = doc.getComponentDefinition(URI.create("http://examples.org/ComponentDefinition/Q2/1"));
    ComponentDefinition cds = doc.getComponentDefinition(URI.create("http://examples.org/ComponentDefinition/LuxR/1"));
    ComponentDefinition terminator = doc.getComponentDefinition(URI.create("http://examples.org/ComponentDefinition/ECK120010818/1"));
    // Assemble a new gene
    my_device.createSequenceConstraint("constraint1", RestrictionType.PRECEDES, medium_strength_promoter.getIdentity(), rbs.getIdentity());
    my_device.createSequenceConstraint("constraint2", RestrictionType.PRECEDES, rbs.getIdentity(), cds.getIdentity());
    my_device.createSequenceConstraint("constraint3", RestrictionType.PRECEDES, cds.getIdentity(), terminator.getIdentity());
    // Annotate the target construct with a Sequence Ontology term
    my_device.addRole(SequenceOntology.ENGINEERED_REGION);
    // Explore the newly assembled gene
    for (Component component : my_device.getComponents()) {
        System.out.println(component.getDisplayId());
    }
    System.out.println("");
    compile(doc, my_device);
    Sequence seq = my_device.getSequenceByEncoding(Sequence.IUPAC_DNA);
    System.out.println(seq.getElements());
    System.out.println("");
    /* Managing a Design-Build-Test-Learn workflow */
    Activity workflow_step_1 = doc.createActivity("build_1", version);
    Activity workflow_step_2 = doc.createActivity("build_2", version);
    Activity workflow_step_3 = doc.createActivity("test_1", version);
    Activity workflow_step_4 = doc.createActivity("analysis_1", version);
    Plan workflow_step_1_plan = doc.createPlan("gibson_assembly", version);
    Plan workflow_step_2_plan = doc.createPlan("transformation", version);
    Plan workflow_step_3_plan = doc.createPlan("promoter_characterization", version);
    Plan workflow_step_4_plan = doc.createPlan("parameter_optimization", version);
    workflow_step_1.createAssociation("association", URI.create("mailto:jdoe@my_namespace.org")).setPlan(workflow_step_1_plan.getIdentity());
    workflow_step_2.createAssociation("association", URI.create("mailto:jdoe@my_namespace.org")).setPlan(workflow_step_2_plan.getIdentity());
    workflow_step_3.createAssociation("association", URI.create("http://sys-bio.org/plate_reader_1")).setPlan(workflow_step_3_plan.getIdentity());
    workflow_step_4.createAssociation("association", URI.create("http://tellurium.analogmachine.org")).setPlan(workflow_step_4_plan.getIdentity());
    Implementation gibson_mix = doc.createImplementation("gibson_mix", version);
    gibson_mix.setBuilt(my_device);
    gibson_mix.addWasGeneratedBy(workflow_step_1.getIdentity());
    workflow_step_1.createUsage("usage", my_device.getIdentity());
    Collection clones = doc.createCollection("clones", version);
    Implementation clone1 = doc.createImplementation("clone1", version);
    clone1.setBuilt(my_device);
    clones.addMember(clone1.getIdentity());
    Implementation clone2 = doc.createImplementation("clone2", version);
    clone2.setBuilt(my_device);
    clones.addMember(clone2.getIdentity());
    Implementation clone3 = doc.createImplementation("clone3", version);
    clone3.setBuilt(my_device);
    clones.addMember(clone3.getIdentity());
    clones.addWasGeneratedBy(workflow_step_2.getIdentity());
    workflow_step_2.createUsage("usage", gibson_mix.getIdentity());
    Collection experiment1 = doc.createCollection("experiment1", version);
    experiment1.addWasGeneratedBy(workflow_step_3.getIdentity());
    workflow_step_3.createUsage("usage", clones.getIdentity());
    Collection analysis1 = doc.createCollection("analysis1", version);
    analysis1.addWasGeneratedBy(workflow_step_4.getIdentity());
    workflow_step_4.createUsage("usage", experiment1.getIdentity());
    // Validate the Document
    SBOLValidate.validateSBOL(doc, false, false, false);
    for (String error : SBOLValidate.getErrors()) {
        System.out.println(error);
    }
    System.out.println(analysis1.getIdentity());
    System.out.println("");
    /* Uploading the Device back to SynBioHub */
    // TODO: Need to provide your credentials
    String user_name = "myers";
    String password = "MaWen69!";
    part_shop.login(user_name, password);
    // Upon submission, the Document will be converted to a Collection with the following properties
    // The new Collection will have a URI that conforms to the following pattern:
    // https://synbiohub.org/user/<USERNAME>/<DOC.DISPLAYID>/<DOC.DISPLAYID>_collection
    String displayId = "my_device";
    String name = "my device";
    String description = "a description of the cassette";
    part_shop.createCollection(displayId, version, name, description, "", true, doc);
    // TODO: need to fill in your path
    String attachment_path = "/Users/myers/Downloads/results.txt";
    // Attach raw experimental data to the Test object here. Note the pattern
    URI test_uri = URI.create("https://synbiohub.org/user/" + user_name + "/" + displayId + "/experiment1/1");
    part_shop.attachFile(test_uri, attachment_path);
    // Attach processed experimental data here
    // TODO: need to fill in your path
    String other_attachement_path = "/Users/myers/Downloads/results.txt";
    URI analysis_uri = URI.create("https://synbiohub.org/user/" + user_name + "/" + displayId + "/analysis1/1");
    part_shop.attachFile(analysis_uri, other_attachement_path);
    System.out.println("Successfully uploaded");
}
Also used : SBOLDocument(org.sbolstandard.core2.SBOLDocument) Activity(org.sbolstandard.core2.Activity) Sequence(org.sbolstandard.core2.Sequence) Plan(org.sbolstandard.core2.Plan) URI(java.net.URI) Implementation(org.sbolstandard.core2.Implementation) Collection(org.sbolstandard.core2.Collection) TopLevel(org.sbolstandard.core2.TopLevel) SynBioHubFrontend(org.synbiohub.frontend.SynBioHubFrontend) Component(org.sbolstandard.core2.Component) IdentifiedMetadata(org.synbiohub.frontend.IdentifiedMetadata) ComponentDefinition(org.sbolstandard.core2.ComponentDefinition)

Example 8 with Sequence

use of com.google.showcase.v1beta1.Sequence in project libSBOLj by SynBioDex.

the class SequenceTest method test_deep_copy.

@Test
public void test_deep_copy() throws SBOLValidationException {
    Sequence gen_seq = doc.createSequence("CRa_U6_seq", CRa_U6_seq, Sequence.IUPAC_DNA);
    Sequence copy_seq = (Sequence) doc.createCopy(gen_seq, "copy_seq");
    // assertTrue(copy_seq.equals(gen_seq));
    assertTrue(copy_seq.getElements().equals(CRa_U6_seq));
}
Also used : Sequence(org.sbolstandard.core2.Sequence) Test(org.junit.Test)

Example 9 with Sequence

use of com.google.showcase.v1beta1.Sequence in project jss by dogtagpki.

the class pkcs12 method main.

public static void main(String[] args) {
    try {
        // Read arguments
        if (args.length != 3) {
            System.out.println("Usage: PFX <dbdir> <infile> <outfile>");
            System.exit(-1);
        }
        // open input file for reading
        FileInputStream infile = null;
        try {
            infile = new FileInputStream(args[1]);
        } catch (FileNotFoundException f) {
            System.out.println("Cannot open file " + args[1] + " for reading: " + f.getMessage());
            return;
        }
        int certfile = 0;
        // initialize CryptoManager. This is necessary because there is
        // crypto involved with decoding a PKCS #12 file
        CryptoManager.initialize(args[0]);
        CryptoManager manager = CryptoManager.getInstance();
        // Decode the P12 file
        PFX.Template pfxt = new PFX.Template();
        PFX pfx;
        try (BufferedInputStream is = new BufferedInputStream(infile, 2048)) {
            pfx = (PFX) pfxt.decode(is);
        }
        System.out.println("Decoded PFX");
        // print out information about the top-level PFX structure
        System.out.println("Version: " + pfx.getVersion());
        AuthenticatedSafes authSafes = pfx.getAuthSafes();
        SEQUENCE safeContentsSequence = authSafes.getSequence();
        System.out.println("AuthSafes has " + safeContentsSequence.size() + " SafeContents");
        // Get the password for the old file
        System.out.println("Enter password: ");
        Password pass = Password.readPasswordFromConsole();
        // get new password, which will be used for the new file we create
        // later
        System.out.println("Enter new password:");
        Password newPass = Password.readPasswordFromConsole();
        // Verify the MAC on the PFX.  This is important to be sure
        // it hasn't been tampered with.
        StringBuffer sb = new StringBuffer();
        if (pfx.verifyAuthSafes(pass, sb)) {
            System.out.println("AuthSafes verifies correctly.");
        } else {
            System.out.println("AuthSafes failed to verify because: " + sb);
        }
        // Create a new AuthenticatedSafes. As we read the contents of the
        // old authSafes, we will store them into the new one.  After we have
        // cycled through all the contents, they will all have been copied into
        // the new authSafes.
        AuthenticatedSafes newAuthSafes = new AuthenticatedSafes();
        // for(int i=0; i < asSeq.size(); i++) {
        for (int i = 0; i < safeContentsSequence.size(); i++) {
            // The safeContents may or may not be encrypted.  We always send
            // the password in.  It will get used if it is needed.  If the
            // decryption of the safeContents fails for some reason (like
            // a bad password), then this method will throw an exception
            SEQUENCE safeContents = authSafes.getSafeContentsAt(pass, i);
            System.out.println("\n\nSafeContents #" + i + " has " + safeContents.size() + " bags");
            // Go through all the bags in this SafeContents
            for (int j = 0; j < safeContents.size(); j++) {
                SafeBag safeBag = (SafeBag) safeContents.elementAt(j);
                // The type of the bag is an OID
                System.out.println("\nBag " + j + " has type " + safeBag.getBagType());
                // look for bag attributes
                SET attribs = safeBag.getBagAttributes();
                if (attribs == null) {
                    System.out.println("Bag has no attributes");
                } else {
                    for (int b = 0; b < attribs.size(); b++) {
                        Attribute a = (Attribute) attribs.elementAt(b);
                        if (a.getType().equals(SafeBag.FRIENDLY_NAME)) {
                            // the friendly name attribute is a nickname
                            BMPString bs = (BMPString) ((ANY) a.getValues().elementAt(0)).decodeWith(BMPString.getTemplate());
                            System.out.println("Friendly Name: " + bs);
                        } else if (a.getType().equals(SafeBag.LOCAL_KEY_ID)) {
                            // the local key id is used to match a key
                            // to its cert.  The key id is the SHA-1 hash of
                            // the DER-encoded cert.
                            OCTET_STRING os = (OCTET_STRING) ((ANY) a.getValues().elementAt(0)).decodeWith(OCTET_STRING.getTemplate());
                            System.out.println("LocalKeyID:");
                        /*
                            AuthenticatedSafes.
                                print_byte_array(os.toByteArray());
							*/
                        } else {
                            System.out.println("Unknown attribute type: " + a.getType().toString());
                        }
                    }
                }
                // now look at the contents of the bag
                ASN1Value val = safeBag.getInterpretedBagContent();
                if (val instanceof PrivateKeyInfo) {
                    // A PrivateKeyInfo contains an unencrypted private key
                    System.out.println("content is PrivateKeyInfo");
                } else if (val instanceof EncryptedPrivateKeyInfo) {
                    // An EncryptedPrivateKeyInfo is, well, an encrypted
                    // PrivateKeyInfo. Usually, strong crypto is used in
                    // an EncryptedPrivateKeyInfo.
                    EncryptedPrivateKeyInfo epki = ((EncryptedPrivateKeyInfo) val);
                    System.out.println("content is EncryptedPrivateKeyInfo, algoid:" + epki.getEncryptionAlgorithm().getOID());
                    // Because we are in a PKCS #12 file, the passwords are
                    // char-to-byte converted in a special way.  We have to
                    // use the special converter class instead of the default.
                    PrivateKeyInfo pki = epki.decrypt(pass, new org.mozilla.jss.pkcs12.PasswordConverter());
                    // import the key into the key3.db
                    CryptoToken tok = manager.getTokenByName("Internal Key Storage Token");
                    CryptoStore store = tok.getCryptoStore();
                    tok.login(new ConsolePasswordCallback());
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    pki.encode(baos);
                    store.importPrivateKey(baos.toByteArray(), PrivateKey.RSA);
                    // re-encrypt the PrivateKeyInfo with the new password
                    // and random salt
                    byte[] salt = new byte[PBEAlgorithm.PBE_SHA1_DES3_CBC.getSaltLength()];
                    JSSSecureRandom rand = CryptoManager.getInstance().getSecureRNG();
                    rand.nextBytes(salt);
                    epki = EncryptedPrivateKeyInfo.createPBE(PBEAlgorithm.PBE_SHA1_DES3_CBC, newPass, salt, 1, new PasswordConverter(), pki);
                    // Overwrite the previous EncryptedPrivateKeyInfo with
                    // this new one we just created using the new password.
                    // This is what will get put in the new PKCS #12 file
                    // we are creating.
                    safeContents.insertElementAt(new SafeBag(safeBag.getBagType(), epki, safeBag.getBagAttributes()), i);
                    safeContents.removeElementAt(i + 1);
                } else if (val instanceof CertBag) {
                    System.out.println("content is CertBag");
                    CertBag cb = (CertBag) val;
                    if (cb.getCertType().equals(CertBag.X509_CERT_TYPE)) {
                        // this is an X.509 certificate
                        OCTET_STRING os = (OCTET_STRING) cb.getInterpretedCert();
                        Certificate cert = (Certificate) ASN1Util.decode(Certificate.getTemplate(), os.toByteArray());
                        cert.getInfo().print(System.out);
                    } else {
                        System.out.println("Unrecognized cert type");
                    }
                } else {
                    System.out.println("content is ANY");
                }
            }
            // Add the new safe contents to the new authsafes
            if (authSafes.safeContentsIsEncrypted(i)) {
                newAuthSafes.addEncryptedSafeContents(AuthenticatedSafes.DEFAULT_KEY_GEN_ALG, newPass, null, AuthenticatedSafes.DEFAULT_ITERATIONS, safeContents);
            } else {
                newAuthSafes.addSafeContents(safeContents);
            }
        }
        // Create new PFX from the new authsafes
        PFX newPfx = new PFX(newAuthSafes);
        // Add a MAC to the new PFX
        newPfx.computeMacData(newPass, null, PFX.DEFAULT_ITERATIONS);
        // write the new PFX out to a file
        FileOutputStream fos = new FileOutputStream(args[2]);
        newPfx.encode(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SET(org.mozilla.jss.asn1.SET) Attribute(org.mozilla.jss.pkix.primitive.Attribute) JSSSecureRandom(org.mozilla.jss.crypto.JSSSecureRandom) FileNotFoundException(java.io.FileNotFoundException) CryptoManager(org.mozilla.jss.CryptoManager) ANY(org.mozilla.jss.asn1.ANY) ASN1Value(org.mozilla.jss.asn1.ASN1Value) OCTET_STRING(org.mozilla.jss.asn1.OCTET_STRING) BufferedInputStream(java.io.BufferedInputStream) SEQUENCE(org.mozilla.jss.asn1.SEQUENCE) ConsolePasswordCallback(org.mozilla.jss.util.ConsolePasswordCallback) BMPString(org.mozilla.jss.asn1.BMPString) Password(org.mozilla.jss.util.Password) PFX(org.mozilla.jss.pkcs12.PFX) CryptoToken(org.mozilla.jss.crypto.CryptoToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SafeBag(org.mozilla.jss.pkcs12.SafeBag) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) AuthenticatedSafes(org.mozilla.jss.pkcs12.AuthenticatedSafes) CryptoStore(org.mozilla.jss.crypto.CryptoStore) CertBag(org.mozilla.jss.pkcs12.CertBag) FileOutputStream(java.io.FileOutputStream) EncryptedPrivateKeyInfo(org.mozilla.jss.pkix.primitive.EncryptedPrivateKeyInfo) PasswordConverter(org.mozilla.jss.pkcs12.PasswordConverter) EncryptedPrivateKeyInfo(org.mozilla.jss.pkix.primitive.EncryptedPrivateKeyInfo) PrivateKeyInfo(org.mozilla.jss.pkix.primitive.PrivateKeyInfo) Certificate(org.mozilla.jss.pkix.cert.Certificate)

Example 10 with Sequence

use of com.google.showcase.v1beta1.Sequence in project jss by dogtagpki.

the class SSLClientAuth method makeBasicConstraintsExtension.

static Extension makeBasicConstraintsExtension() throws Exception {
    SEQUENCE bc = new SEQUENCE();
    // cA
    bc.addElement(new BOOLEAN(true));
    OBJECT_IDENTIFIER bcOID = new OBJECT_IDENTIFIER(// from RFC 2459
    new long[] { 2, 5, 29, 19 });
    OCTET_STRING enc = new OCTET_STRING(ASN1Util.encode(bc));
    return new Extension(bcOID, true, enc);
}
Also used : Extension(org.mozilla.jss.pkix.cert.Extension) OCTET_STRING(org.mozilla.jss.asn1.OCTET_STRING) SEQUENCE(org.mozilla.jss.asn1.SEQUENCE) OBJECT_IDENTIFIER(org.mozilla.jss.asn1.OBJECT_IDENTIFIER) BOOLEAN(org.mozilla.jss.asn1.BOOLEAN)

Aggregations

SEQUENCE (org.mozilla.jss.asn1.SEQUENCE)50 OCTET_STRING (org.mozilla.jss.asn1.OCTET_STRING)16 Sequence (org.sbolstandard.core2.Sequence)11 SET (org.mozilla.jss.asn1.SET)9 ANY (org.mozilla.jss.asn1.ANY)8 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)8 OBJECT_IDENTIFIER (org.mozilla.jss.asn1.OBJECT_IDENTIFIER)8 URI (java.net.URI)7 BMPString (org.mozilla.jss.asn1.BMPString)7 CryptoToken (org.mozilla.jss.crypto.CryptoToken)7 ASN1Value (org.mozilla.jss.asn1.ASN1Value)6 INTEGER (org.mozilla.jss.asn1.INTEGER)6 AuthenticatedSafes (org.mozilla.jss.pkcs12.AuthenticatedSafes)6 FileOutputStream (java.io.FileOutputStream)5 IOException (java.io.IOException)5 SignatureException (java.security.SignatureException)5 EXPLICIT (org.mozilla.jss.asn1.EXPLICIT)5 SafeBag (org.mozilla.jss.pkcs12.SafeBag)5 Certificate (org.mozilla.jss.pkix.cert.Certificate)5 ComponentDefinition (org.sbolstandard.core2.ComponentDefinition)5