473,379 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to find a overlap between 2 sequences, and make the function return the overlap

I need to make a function that determines the overlap between 2 sequences, and then return the overlap.
The overlap is a coherent sequence that is in the left end of the first sequence, and in the right end of the second sequence.

my sequences are: s1= "CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATC TC"
s2= "GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCC CTAGC"

my function should be named def getOverlap(left, right)

and should return ‘GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC’
if s1 is the left sequence and s2 the right one.
Jan 2 '13 #1
6 6735
bvdet
2,851 Expert Mod 2GB
Have you developed an algorithm? Have you tried anything yet?
Jan 2 '13 #2
I have tried this:
Expand|Select|Wrap|Line Numbers
  1. left = "CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC"
  2. right = "GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC"
  3.  
  4.  
  5. def getOverlap(left,right):
  6.     if left == right[::-1]:
  7.         return ""
  8.     else:
  9.         for i in left:
  10.             for i in right:
  11.                 if left[len(left)-i]==(right[::-1])[len(right)-i]:
  12.                     if False:
  13.                         continue
  14.                     return right[:len(left)-i]



But something is very wrong :)
Jan 2 '13 #3
When i turn the s1, and s2 around, the answer is supposed to be 'c', but i get the same answer.
Jan 2 '13 #4
bvdet
2,851 Expert Mod 2GB
It appears to me you have the sequences switched. The overlap you describe is at the left end of s2 and right end of s1. You can use string method find to find the overlap.

A possible algorithm:
  1. Initialize an empty string and assign to an identifier, let's say "X"
  2. Iterate on the left string, character by character
  3. Assign "X"+next character to an identifier, let's sat "temp"
  4. If the current value of "temp" is found in the the second string, assign the value of "X" to "temp".
  5. If the current value of "temp" is not found in the the second string, return "X"
This assumes the "left" string overlap will always start at the beginning of the string.
Jan 2 '13 #5
bvdet
2,851 Expert Mod 2GB
To add to the qualification above - The algorithm I described does not require the overlap to be at the right end of the "right" string.
Expand|Select|Wrap|Line Numbers
  1. def getOverlap(left, right):
  2.     overlap = ""
  3.     for c in left:
  4.         temp  = overlap+c
  5.         if right.find(temp) >= 0:
  6.             overlap = temp
  7.         else:
  8.             return overlap
  9.     return    
  10.  
  11. s1 = "GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC"
  12. s2 = "CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC"
  13.  
  14. print getOverlap(s1,s2)
  15. print getOverlap(s2,s1)
Produces:
Expand|Select|Wrap|Line Numbers
  1. >>> GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC
  2. CG
  3. >>> 
Jan 2 '13 #6
bvdet
2,851 Expert Mod 2GB
You should use the slice operator to determine the overlap, something like this:
Expand|Select|Wrap|Line Numbers
  1. s1 = "GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC"
  2. s2 = "CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC"
  3.  
  4. def getOverlap(left, right):
  5.     if left == right[-len(left):]:
  6.         return left
  7.     i = -1
  8.     while True:
  9.         temp = left[0:i]
  10.         if temp == right[-len(temp):]:
  11.             return temp
  12.         elif temp == "":
  13.             return ""
  14.         i -= 1
  15.  
  16. print getOverlap(s1,s2)
  17. print getOverlap(s2,s1)
  18.  
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC
  2. C
  3. >>> 
Jan 2 '13 #7

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

Similar topics

5
by: david | last post by:
Hi All, I just want to write a function return char array,but I cant. I am not good at C++ 's concept since I used to use Java before. Would you please help me to fix that ? Thanks.. Best...
2
by: erictham | last post by:
#include <iostream> #include <cmath> using namespace std; // implements a pseudo RNG generator by the linear congruential generator const int n1=100; double *LRNG(int, int, int); int main()...
4
by: wongjoekmeu | last post by:
Hello All, I know that when you pass an argument to a function (if you want let the function be able to change the value) then you can choose to pass the argument by reference or a pointer to...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
8
by: bdobby | last post by:
Hi. I am relatively new to js, but I did think I was starting to get the hang of it. Then this happened... I have a form with an onsubmit event handler: <form id="uploadForm" method="post"...
3
by: linq936 | last post by:
Hi, I have some code like this, vector<MyNode*getNodes() { return theNodes; } int func() { vector<MyNode*nodes; nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());
9
by: mtczx232 | last post by:
it's posible that Function return byref? what the syntax?
1
by: holdingbe | last post by:
Hi all, Normally, the stored function return one values per time.In oracle9i pipelined functions return the amount of data at a time, the first step create a type object. CREATE OR...
6
by: John | last post by:
Hi I have a function that needs to return value from some column in a table. The column type is not known in advance. Is it possible for function to return a value whose type would be determined...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.