0

Okay I've been working on this script for a while now but it doesn't work how it should be. It should work like a restriction enzyme, a user gives in an enzyme and the program checks if restriction sites are present in the created dictionary. If so it should calculate the weight of the fragments. Now the case if there's no restriction site present it returns an empty list which is correct, BUT if there are restriction sites presents the program doesn't work. There are no errors but when I run the script it does littary nothing. Does anyone has something for me to fix this ??

note: some functions are left away at purpose, like the weight function.

import sys

enzyme = input ("Give me some enzymes to work with, please ") 

enzymesDict = {"EcorI":("GAATTC",1),"BamHI":("GGATCC",1),"EcoRII":("CCWGG",0),"HindIII":("AAGCTT",1),
            "TaqI":("TCGA",1),"NotI":("GCGGCCGC",2),"HinfI":("GANTCA",1),"Sau3A":("GATC",0),
            "HaeIII":("GGCC",2),"EcoRV":("GATATC",3),"PstI":("CTGCAG",4),"XbaI":("TCTAGA",1),
            "MboI":("GATC",0),"mst2":("CCTNAGG",2)}

def getSequences ():
    '''Gets sequences of command-line with as output a dictionary'''

    sequenceDict = {}

    #opens given file as string and reads it's content
    inputFile = open("sequence.txt")
    outputSequence = inputFile.read()
    outputSequence = outputSequence.split('\n')

    for lines in outputSequence:
        if len(lines) % 2  == 0 :
            header = lines
        else:
            if len(lines) % 2 == 1 :
                outputSequence = lines

        #adds header and outputSequence to the empty dict sequenceDict      
            sequenceDict[header] = [outputSequence]
    inputFile.close()   

    return sequenceDict

def digestFragmentWithOneEnzym (sequenceDict):
    '''Function which will cut fragment of DNA with help of enzymes
     Input: Dictionary with sequence and header. Output:'''

    #using the global dictionary of enzymes to cut a fragment of DNA
    #the output of this function should be a list with cutted fragments of DNA

    fragmentList = []
    outputSequence = getSequences()
    #checks if given enzym is in global enzymesDict if true then it continues

    #~ for sequence in sequenceDict:
        #~ header = sequenceDict[sequence]


    if enzyme in enzymesDict:
        offset = enzymesDict[enzyme][1]
        enzymeSeq = enzymesDict[enzyme][0]      
        item = 0
        outputSequence = sequenceDict['>Sequence number 1'][0]

        while item != -1:
            item = outputSequence.find(enzymeSeq)
            if item == -1:
                continue

            end = item + offset
            fragment = outputSequence[0:end]
            sequence = outputSequence[end: ]


            fragmentList.append(fragment)


    return fragmentList #returns a list of cutted fragments


def main ():
    '''Main function which will process the given data'''

    #def performRestriction(path to sequencefile, enzymelist)

    sequenceDict = getSequences()
    outputSequence = getSequences()
    fragmentList = digestFragmentWithOneEnzym(sequenceDict) 
    fragWeight = getMoleculairWeight(fragmentList)

    #prints input sequence plus cutted fragments and weights in a list
    print (outputSequence)
    print("Cutted fragments are:",fragmentList , "\n \t \t Their weights:" , fragWeight)

if __name__ =="__main__":
    main()
Blckknght
  • 100,903
  • 11
  • 120
  • 169
user3309936
  • 25
  • 1
  • 8
  • You might need to help us here. We don't know the format of your file. Have you tested your getSequences function? Your code seems a bit odd. Does it return what you expect? Can you post an example dictionary? Its often useful to break the code down into pieces and track down a bug systematically. – Graeme Stuart Mar 23 '14 at 15:26
  • here's some debugging tips: [What are good ways to make my Python code run first time?](http://stackoverflow.com/q/299704/4279) – jfs Mar 23 '14 at 17:06

1 Answers1

0

If the code you reported here

if name == "main": main()

is inserted into script you're trying to run, you should modify it as follow:

if __name__ == '__main__':
    main()
DonCallisto
  • 29,419
  • 9
  • 72
  • 100