440,096 Members | 1,568 Online
Need help? Post your question and get tips & solutions from a community of 440,096 IT Pros & Developers. It's quick & easy.

# need help with substring alignment

 P: 13 Situation..Given two sequences, where one is a substring of the other, we define a substring alignment by matching the substring with the longer sequence and placing gaps everywhere else. For example if the input is ACCTGTAGG and TGT then the substring alignment is ACCTGTAGG ---TGT--- i need help designing program that prints the substring alignment of two unaligned sequences. If no substring alignment exists then the program should print "No alignment found"...im puzzled over this situation. please help Dec 10 '10 #1
9 Replies

 Expert Mod 2.5K+ P: 2,851 Text method find() is the answer. This function should do it: Expand|Select|Wrap|Line Numbers def alignment(source, target):     i = source.find(target)     if i < 0:         return "No alignment"     results = ["_"*i,target]     j = i+len(target)     while True:         i = source.find(target, j)         if i < 0:             results.append(len(source[j:])*"_")             return "".join(results)         results.extend([(i-j)*"_", target])         j = i+len(target) Some interaction: Expand|Select|Wrap|Line Numbers >>> s1 = "ACCTGTAGGTGTACCGTGT" >>> print alignment(s1, "TGTACCGT") _________TGTACCGT__ >>> print alignment(s1, "CTGTA") __CTGTA____________ >>> print alignment(s1, "TGT") ___TGT___TGT____TGT >>> print alignment(s1, "xxx") No alignment >>>  Dec 10 '10 #2

 P: 13 ok, by any chance is it possible to make this read from a txt file and for it to work for any substring alignment Dec 11 '10 #3

 Expert Mod 2.5K+ P: 2,851 What is posted is a function that receives arguments. You can read a file line by line, pass the each line to the function as source and pass the target substring as target. I will leave that exercise to you. Dec 11 '10 #4

 P: 13 ok, i inserted this into the program but nothing printed and there wasnt an error message Expand|Select|Wrap|Line Numbers filename = input("Please enter a filename:") with open(filename, "r") as myfile:     data = myfile.readlines() for i in range(len(data)):     data[i] = data[i].rstrip("\n")   Dec 11 '10 #5

 P: 7 if youre given ----------TGT---- as the substring, is there a way to tell the command to only read the letters TGT and ignore the "-" ? Dec 11 '10 #6

 P: 13 in this situation the ---TGT--- needs to be read as is...we cannot ignore the dashes or the program will not work Dec 12 '10 #7

 Expert Mod 2.5K+ P: 2,851 You can iterate on data like this: Expand|Select|Wrap|Line Numbers for line in data:     print alignment(line.strip(), "TGT") Dec 12 '10 #8

 P: 7 CCGG26 did you figure out this problem yet? I'm still having trouble coming up with answers for this one. Dec 13 '10 #9

 Expert Mod 2.5K+ P: 2,851 Would str method strip() do what you need? Expand|Select|Wrap|Line Numbers >>> "----------TGT----".strip("-") 'TGT' >>>  Dec 13 '10 #10