예제 #1
0
    def createNS(self, NStype, NSname, NSseq):
        """Create a NamedSequence using raw data and then adding it to the database
        as a NamedSequenceDB.

        PARAMETERS:
                NStype: string type of the named sequence
                NSname: string name of the named sequence
                NSseq: string sequence of the named sequence

        RETURNS:
                NSentry: created NamedSequenceDB that is in the database
        """
        # type checking
        checkType(NStype, "NStype")

        # get the nameID
        nameID = self.getEntry().getNextNSid()[NStype]

        # create it as a NamedSequence
        newNS = NamedSequence.makeNew(NStype, NSname, NSseq, nameID)

        # add it as a NamedSequenceDB to the database and the user
        NSentry = self.addNS(newNS)

        return NSentry
예제 #2
0
    def findNamedSequenceNoSeq(self, NStype, NSname):
        """Searches UserDataDB for a NamedSequenceDB; don't need sequence.

        PARAMETERS:
                NStype: string type of the named sequence
                NSname: string name of the named sequence

        RETURNS:
                found NamedSequenceDB

        RAISES:
                SequenceNotFoundError: if the named sequence was not found
                Exception: if multiple named sequences that fits the requirements were found
        """
        # type checking
        checkType(NStype, "NStype")
        if type(NSname) != str:
            raise TypeError("name not a string")

        # query database
        namedSeqList = (
            self.getAllNSQuery().filter_by(elemType=NStype, name=NSname).all()
        )

        # handle the results from the query
        if len(namedSeqList) == 0:
            raise e.SequenceNotFoundError("Could not find sequence.")
        if len(namedSeqList) > 1:
            raise Exception("Multiple sequences found.")
        else:
            return namedSeqList[0]
예제 #3
0
    def makeNew(cls, NSType, NSName, NSSeq, nameID):
        """Create a new NamedSequence.

        PARAMETERS:
                NSType: string type of the named sequence
                NSName: string name of the named sequence
                NSSeq: string sequence of the named sequence
                NSnameID: string name ID of the named sequence

        RETURNS:
                newNS: the new NamedSequence
        """
        # type checking
        checkType(NSType, "NSType")

        if type(NSName) != str:
            raise TypeError("NSName not a string")
        if type(NSSeq) != str:
            raise TypeError("NSSeq not a string")
        if type(nameID) != int:
            raise TypeError("newID not an int")

        # initialize the instance
        newNS = cls()

        # set instance's variables
        newNS.__type = NSType
        newNS.__name = NSName
        newNS.__seq = NSSeq
        newNS.__nameID = nameID

        return newNS
예제 #4
0
    def findComponent(self, compType, compName, compPos, compTerminalLetter):
        """Searches UserDataDB for ComponentDB with specifications.

        PARAMETERS:
                compType: string type of the component's sequence to find
                compName: string name of the component's sequence to find
                compPos: integer position of the component
                compTerminalLetter: string component's terminal letter

        RETURNS:
                comp: found component that fits the paramters

        RAISES:
                ComponentNotFoundError: if no component was found that fits the parameters.
        """
        # type checking
        checkType(compType, "compType")
        if type(compName) != str:
            raise TypeError("compName not a string")
        if type(compPos) != int:
            raise TypeError("comPos not an int")
        if type(compTerminalLetter) != str:
            raise TypeError("compTerminalLetter not a string")

        # first search for a NamedSequenceDB that fits the type and name
        foundNS = self.findNamedSequenceNoSeq(compType, compName)

        # search through the components of foundNS
        for comp in foundNS.getAllComponents():
            # for each component, check the position and terminal letter
            if (comp.getPosition() == compPos) and (
                comp.getTerminalLetter() == compTerminalLetter
            ):
                # return if found.
                return comp

        # did not find it
        raise e.ComponentNotFoundError("Could not find component.")
예제 #5
0
def validateNewNS(newNSType, newNSName, newNSSeq):
    """Validates the input from creating a new NamedSequence on the design page.

    PARAMETERS:
            newNSType: string type for the named sequence ("Pr", "RBS", "GOI", or "Term")
            newNSName: string name for the named sequence
            newNSSeq: string sequence for the named sequence

    RETURNS:
            validInput: boolean. True if all input is valid, False otherwise.
            outputStr: HTML string to output to the website so errors are displayed.
    """
    validInput = True
    outputStr = ""

    #####   TYPE CHECKING   #####
    if type(newNSType) != type(newNSName) != type(newNSSeq) != str:
        validInput = False
        outputStr += "ERROR: input received not all strings.<br>"

    try:
        checkType(newNSType, "newNSType")
    except ValueError:
        validInput = False
        outputStr += "ERROR: '" + newNSType + "' is not a valid type.<br>"

        #####   VALIDATE NAME   #####
    # length
    if len(newNSName) < 1 or len(newNSName) > 20:
        validInput = False
        outputStr += "ERROR: Sequence name must be 1-20 characters.<br>"

    # whether it already exists in default:
    longNames = {
        "Pr": "promoter",
        "RBS": "ribosome binding site",
        "GOI": "gene",
        "Term": "terminator",
    }
    for elemType in ["Pr", "RBS", "GOI", "Term"]:
        try:
            defaultUser.findNamedSequenceNoSeq(elemType, newNSName)

            validInput = False
            outputStr += (
                "ERROR: {newNSName} already exists in the default "
                "library as a {longName}.<br>"
            ).format(newNSName=newNSName, longName=longNames[elemType])
            break
        except SequenceMismatchError:
            validInput = False
            outputStr += (
                "ERROR: {newNSName} already exists in the default "
                "library as a {longName}, and with a different sequence.<br>"
            ).format(newNSName=newNSName, longName=longNames[elemType])
            break
        except SequenceNotFoundError:
            pass

    # characters
    for special in specialChars:
        newNSName = newNSName.replace(special, specialChars[special])

    invalidCharactersName = []

    for character in newNSName:
        if (character not in validCharacters) and (
            character not in invalidCharactersName
        ):
            validInput = False
            outputStr += (
                "ERROR: '" + character + "' is not allowed in a sequence's name.<br>"
            )
            invalidCharactersName.append(character)

            #####   VALIDATE SEQUENCE       #####
    # length
    if (
        len(newNSSeq) < 1 or len(newNSSeq) > 99999
    ):  # I don't know what limits should be used
        validInput = False
        outputStr += "ERROR: Sequence must be 1-99999 nucleotides.<br>"

    # characters
    validNucleotides = "AGTCBDHKMNRSVWY"

    invalidCharactersSeq = []

    # check special Chars first:
    for special in specialChars:
        if special in newNSSeq:
            validInput = False
            outputStr += (
                "ERROR: {char} is not allowed in a sequence's name.<br>".format(
                    char=specialChars[special]
                )
            )
            invalidCharactersSeq.append(specialChars[special])

    for character in newNSSeq:
        if (character not in validNucleotides) and (
            character not in invalidCharactersSeq
        ):
            validInput = False
            outputStr += "ERROR: '" + character + "' is not an allowed nucleotide.<br>"
            invalidCharactersSeq.append(character)

    return (validInput, outputStr)