Beispiel #1
0
    def addNSDB(self, namedSeq):
        """Create a copy of a NamedSequenceDB in the user's library.

        PARAMETER:
                namedSeq: NamedSequenceDB to copy

        RAISES:
                AlreadyExistsError: if a named sequence of the same name is already
                        in the user's library
        """
        # type checking
        if type(namedSeq) != NamedSequenceDB:
            raise TypeError("namedSeq not a NamedSequenceDB.")

        # check if it already exists
        try:
            self.findNamedSequence(
                namedSeq.getType(), namedSeq.getName(), namedSeq.getSeq()
            )

            # raise error if it exists
            raise e.AlreadyExistsError("Sequence already exists.")
        except e.SequenceNotFoundError:
            pass

        # get info. about namedSeq
        NStype = namedSeq.getType()
        NSname = namedSeq.getName()
        NSseq = namedSeq.getSeq()

        nameID = (
            namedSeq.getNameID()
        )  #####<----- What if, somehow, that NameID already exists in the personal library?

        # create a new NamedSequenceDB using the information from namedSeq
        # the only difference is the user_id
        ns = NamedSequenceDB(
            elemType=NStype, name=NSname, seq=NSseq, nameID=nameID, user_id=self.getID()
        )

        # add to database
        db.session.add(ns)
        db.session.commit()

        # increment NS id
        self.getEntry().incrementID(NStype)

        return ns
Beispiel #2
0
    def new(cls, email):
        """Create a new user: creates a UserData and a UserDataDB.

        PARAMTERS:
                email: string email of the user

        RETURN:
                newUserData: created UserData
        """
        printIf("Calling UserData.new({email}).\n".format(email=email))

        # type validation
        if type(email) != str:
            raise TypeError("email not a str")

        # see if a user with the email already exists
        if UserDataDB.query.filter_by(email=email).all() != []:
            raise e.AlreadyExistsError(
                "User with email {email} already exists.".format(email=email)
            )

        # initialize
        newUserData = cls()

        # start nextNSid (the first number for the IDs of named sequences of the user)
        if email == "default":
            nextNSid = 1
        else:
            nextNSid = 101

        # add user to database
        u = UserDataDB(
            nextNSidPR=nextNSid,
            nextNSidRBS=nextNSid,
            nextNSidGOI=nextNSid,
            nextNSidTERM=nextNSid,
            email=email,
        )

        db.session.add(u)
        db.session.commit()

        # add database info. to the user
        newUserData.__DBid = u.id
        newUserData.__entryDB = u

        return newUserData
Beispiel #3
0
    def addNS(self, namedSeq):
        """Add a NamedSequenceDB to the database using a NamedSequence.

        PARAMETERS:
                namedSeq: NamedSequence to make a NamedSequenceDB from

        RAISES:
                AlreadyExistsError: if a NamedSequenceDB with the same name and type
                        already exists in the database for the user
        """
        # type checking
        if type(namedSeq) != NamedSequence:
            raise TypeError("namedSeq not a NamedSequence.")

        # check if it already exists
        try:
            self.findNamedSequenceNoSeq(namedSeq.getType(), namedSeq.getName())

            # an error will be raised if it exists
            raise e.AlreadyExistsError("Sequence already exists.")

        except e.SequenceNotFoundError:
            pass

        # make entry
        NStype = namedSeq.getType()
        NSname = namedSeq.getName()
        NSseq = namedSeq.getSeq()

        nameID = self.getEntry().getNextNSid()[NStype]

        ns = NamedSequenceDB(
            elemType=NStype, name=NSname, seq=NSseq, nameID=nameID, user_id=self.getID()
        )

        # add to database
        db.session.add(ns)
        db.session.commit()

        # increment NS id
        self.getEntry().incrementID(NStype)

        return ns
Beispiel #4
0
    def createBackbone(self, name, BBtype, desc, seqBefore, seqAfter, features):
        """Create a BackboneDB and add it to the database.

        PARAMETERS:
                name: string name of the backbone
                BBtype: string type of the backbone; is either "i" for integrative or
                        "r" for replicative
                desc: string description of the backbone
                seqBefore: string sequence of the backbone before the insertion region
                seqAfter: string sequence of the backbone after the insertion region
                features: string features, formatted as it should appear in a .gb file

        RETURNS:
                bb: created BackboneDB

        RAISES:
                AlreadyExistsError: if a backbone with the same name is already in
                        the user's library
        """
        # type checking
        if type(name) != str:
            raise TypeError("backbone name not a string")
        if type(BBtype) != str:
            raise TypeError("backbone type not a string")
        elif BBtype not in ["i", "r"]:
            raise ValueError("backbone type not valid")
        if type(desc) != str:
            raise TypeError("backbone desc not a string")
        if type(seqBefore) != str:
            raise TypeError("backbone seqBefore not a string")
        if type(seqAfter) != str:
            raise TypeError("backbone seqAfter not a string")
        if type(features) != str:
            raise TypeError("backbone features not a string")

        print("done with the type checking")

        # see if it exists already
        try:
            self.findBackbone(name)
            raise e.AlreadyExistsError(
                "Backbone {name} already exists.".format(name=name)
            )
        except e.BackboneNotFoundError:
            pass

        # create in database
        bb = BackboneDB(
            name=name,
            type=BBtype,
            desc=desc,
            seqBefore=seqBefore,
            seqAfter=seqAfter,
            features=features,
            user_id=self.getID(),
        )

        db.session.add(bb)
        db.session.commit()

        return bb
Beispiel #5
0
    def createComp(self, NSentry, spacerData, primerData):
        """Make a ComponentDB and add to the database using a NamedSequenceDB in the
        database, a SpacerData, and a PrimerData.

        PARAMETERS:
                NSentry: NamedSequenceDB in the database that the component will use
                spacerData: SpacerData for the component
                primerData: PrimerData for the component

        RETURNS:
                c: created ComponentDB that is now in the database.
        """
        # type checking
        if type(NSentry) != NamedSequenceDB:
            raise TypeError("NSentry not a NamedSequenceDB")
        if type(spacerData) != SpacerData:
            raise TypeError("spacerData not a SpacerData")
        if type(primerData) != PrimerData:
            raise TypeError("primerData not a PrimerData")

        # check if the component already exists
        try:
            self.findComponent(
                NSentry.getType(),
                NSentry.getName(),
                spacerData.getPosition(),
                spacerData.getTerminalLetter(),
            )

            raise e.AlreadyExistsError("Component already exists.")
        except e.ComponentNotFoundError:
            pass

        # create database entries
        s = SpacerDataDB(
            position=spacerData.getPosition(),
            spacerLeft=spacerData.getSpacerLeft(),
            spacerRight=spacerData.getSpacerRight(),
            isTerminal=spacerData.getIsTerminal(),
            terminalLetter=spacerData.getTerminalLetter(),
            leftNN=spacerData.getLeftNN(),
            rightNN=spacerData.getRightNN(),
        )

        p = PrimerDataDB(
            primersFound=primerData.getPrimersFound(),
            seqLeft=primerData.getLeftSeq(),
            seqRight=primerData.getRightSeq(),
            GCleft=primerData.getGCleft(),
            GCright=primerData.getGCright(),
            TMleft=primerData.getTMleft(),
            TMright=primerData.getTMright(),
        )

        c = ComponentDB(namedSequence_id=NSentry.getID(), user_id=self.getID())

        # add to the database
        db.session.add(c)
        db.session.add(s)
        db.session.add(p)

        db.session.commit()

        # edit database entries so they have proper relations
        c.setSpacerDataID(s.getID())
        c.setPrimerDataID(p.getID())
        s.setCompID(c.getID())
        p.setCompID(c.getID())

        db.session.commit()

        return c