Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
April 17th, 2013, 08:20 PM
 Zavtacular
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 54 m 52 sec
Reputation Power: 0

I am new to python (and coding in general) and was hoping to get some help with setting up and initializing a matrix. I am in a bioinformatics class and am attempting to code the Needleman-Wunsch algorithm. It is a method of comparing two DNA sequences together to determine similarity.

Wikipedia and a basic google search will give a brief overview (sorry I am a new user and can not post URLs to summaries).

The algorithm involves a matix which is sequence length 1+1 by sequence length 2+1. You then fill out the matrix based on whether based on the sequence's positions.

I was hoping someone could show me some code on how to set up the matrix to begin with.
Thanks!

#2
April 17th, 2013, 09:57 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,116
Time spent in forums: 1 Month 3 Weeks 2 Days 3 h 2 m 43 sec
Reputation Power: 455
Using the wikipedia example,
Quote:
 Originally Posted by wikipedia The pseudo-code for the algorithm to compute the F matrix therefore looks like this:
and assuming their index origin is 1 the code below computes F as (the matrix displays better in j, the leading underscore is the j negative sign)
Code:
```   _12[\F
0  _5 _10 _15 _20 _25 _30 _35 _40 _45 _50 _55
_5  _3  _6   0  _5 _10 _15 _20 _25 _30 _35 _40
_10  _8   4  _1  _5 _10 _15  _8 _13 _18 _23 _28
_15 _13  _1  14   9   4  _1  _6   2  _3  _8 _13
_20  _6  _6   9   9   4  _1  _6  _3  11   6   1
_25 _11  _9   4   4   4  _1  _4  _8   6   8  14
_30 _16 _12   1  _1  _1  _1  _2   6   1   5   9
_35 _21  _9  _4  _4  _6  _6   6   1   1   8   4
_40 _26 _14  _9  _9  _9 _11   1   2   1   3  16
_45 _31 _19 _14 _14 _14 _14  _4  _3   2  _2  11
_50 _36 _24  _9 _14 _19 _19  _9   6   1   1   6
_55 _41 _29 _14 _14 _19 _24 _14   1  15  10   5
```

Code:
```import pprint
import collections

gap_penalty = -5

#values of s are determined by chemistry?
s = collections.defaultdict(lambda*args:gap_penalty)

s.update(dict(
AA=10,AG=-1,AC=-3,AT=-4,
GA=-1,GG= 7,GC=-5,GT=-3,
CA=-3,CG=-5,CC= 9,CT= 0,
TA=-4,TG=-3,TC= 0,TT= 8,
))

def compute_F(A, B, s):
F = []
#for i=0 to length(A)
#    F(i,0) ← d*i
for i in range(len(A)+1):
F.append([i * s['']])
#for j=0 to length(B)
#    F(0,j) ← d*j
for j in range(1,len(B)+1):
F[0].append(j * s[''])
#for i=1 to length(A)
#    for j=1 to length(B)
#    {
#        Match ← F(i-1,j-1) + S(Ai, Bj)
#        Delete ← F(i-1, j) + d
#        Insert ← F(i, j-1) + d
#        F(i,j) ← max(Match, Insert, Delete)
#    }
for i in range(1,len(A)+1):
a = A[i-1]
for j in range(1,len(B)+1):
b = B[j-1]
match = F[i-1][j-1] + s[a+b]
delet = F[i-1][j] + s['']
inser = F[i][j-1] + s['']
F[i].append(max((match, inser, delet)))
return F

pprint.pprint(compute_F('AGACTAGTTAC', 'CGA---GACGT', s))```

Please use your experience and deep knowledge to figure out if F is correct. My indexing could easily be incorrect. Generating all the lists of correct length at the start of the algorithm might be a clearer programming style. Actually, if I had to use python for this project I'd go with numpy and preallocate

F = numpy.zeros((len(A)+1,len(B)+1))
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : April 17th, 2013 at 10:08 PM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Matrix help please

Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |