简体   繁体   中英

Python Regex Findall Lookahead

I've created a function which searches a protein string for an open reading frame. Here it is:

def orf_finder(seq,format):
    record = SeqIO.read(seq,format) #Reads in the sequence and tells biopython what format it is.
    string = [] #creates an empty list

    for i in range(3):
        string.append(record.seq[i:]) #creates a list of three lists, each holding a different reading frame.

        protein_string = [] #creates an empty list
        protein_string.append([str(i.translate()) for i in string]) #translates each list in 'string' and combines them into one long list
        regex = re.compile('M''[A-Z]'+r'*') #compiles a regular expression pattern: methionine, followed by any amino acid and ending with a stop codon.
        res = max(regex.findall(str(protein_string)), key=len) #res is a string of the longest translated orf in the sequence.
        print "The longest ORF (translated) is:\n\n",res,"\n"
        print "The first blast result for this protein is:\n"

        blast_records = NCBIXML.parse(NCBIWWW.qblast("blastp", "nr", res)) #blasts the sequence and puts the results into a 'record object'.
        blast_record = blast_records.next()

        counter = 0 #the counter is a method for outputting the first blast record. After it is printed, the counter equals '1' and therefore the loop stops.
        for alignment in blast_record.alignments:
            for hsp in alignment.hsps:
                if counter < 1: #mechanism for stopping loop
                   print 'Sequence:', alignment.title
                   print 'Sength:', alignment.length
                   print 'E value:', hsp.expect
                   print 'Query:',hsp.query[0:]
                   print 'Match:',hsp.match[0:]
                   counter = 1     

The only issue is, I don't think that my regex, re.compile('M''[AZ]'+r'*') , does not find overlapping matches. I know that a lookahead clause, ?= , might solve my problem, but I can't seem to implement it without returning an error.

Does anyone know how I can get it to work?

The code above uses biopython to read-in the DNA sequence, translate it and then searches for a protein readin frame; a sequence starting with 'M' and ending with '*'.

re.compile(r"M[A-Z]+\*")

假设您搜索的字符串以“ M”开头,然后是一个或多个大写字母“ AZ”,并以“ *”结尾。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM