By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,096 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
9 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Text method find() is the answer. This function should do it:
Expand|Select|Wrap|Line Numbers
  1. def alignment(source, target):
  2.     i = source.find(target)
  3.     if i < 0:
  4.         return "No alignment"
  5.     results = ["_"*i,target]
  6.     j = i+len(target)
  7.     while True:
  8.         i = source.find(target, j)
  9.         if i < 0:
  10.             results.append(len(source[j:])*"_")
  11.             return "".join(results)
  12.         results.extend([(i-j)*"_", target])
  13.         j = i+len(target)
Some interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> s1 = "ACCTGTAGGTGTACCGTGT"
  2. >>> print alignment(s1, "TGTACCGT")
  3. _________TGTACCGT__
  4. >>> print alignment(s1, "CTGTA")
  5. __CTGTA____________
  6. >>> print alignment(s1, "TGT")
  7. ___TGT___TGT____TGT
  8. >>> print alignment(s1, "xxx")
  9. No alignment
  10. >>> 
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

bvdet
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
  1. filename = input("Please enter a filename:")
  2. with open(filename, "r") as myfile:
  3.     data = myfile.readlines()
  4. for i in range(len(data)):
  5.     data[i] = data[i].rstrip("\n")
  6.  
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

bvdet
Expert Mod 2.5K+
P: 2,851
You can iterate on data like this:
Expand|Select|Wrap|Line Numbers
  1. for line in data:
  2.     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

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

Post your reply

Sign in to post your reply or Sign up for a free account.