@@ -17,7 +17,7 @@ void SequenceAlignment::clear()
1717 m_charToIndexMap.clear ();
1818 m_numScores = 0 ;
1919 m_gapScore = 0 ;
20- m_scores.clear ();
20+ m_scores.reset ();
2121}
2222
2323bool SequenceAlignment::read (std::istream& in)
@@ -28,20 +28,20 @@ bool SequenceAlignment::read(std::istream& in)
2828 std::string alphabet;
2929 in >> m_alphabet;
3030
31- uint32_t idx = 0 ;
31+ uint64_t idx = 0 ;
3232 for (auto c : m_alphabet)
3333 {
3434 m_charToIndexMap.insert (std::make_pair (c, idx++));
3535 }
3636
3737 m_numScores = m_alphabet.size ();
38- m_scores.resize (m_numScores * m_numScores, 0 );
38+ m_scores.resize (m_numScores, m_numScores );
3939
40- for (uint32_t i = 0 ; i < m_numScores; ++i)
40+ for (uint64_t i = 0 ; i < m_numScores; ++i)
4141 {
42- for (uint32_t j = 0 ; j < m_numScores; ++j)
42+ for (uint64_t j = 0 ; j < m_numScores; ++j)
4343 {
44- in >> m_scores[i * m_numScores + j];
44+ in >> m_scores[i][ j];
4545 }
4646 }
4747 in >> m_gapScore;
@@ -55,53 +55,53 @@ bool SequenceAlignment::run(
5555 uint32_t &scoreNeedlemanWunsch,
5656 std::string (&seq)[2])
5757{
58- uint32_t size[2 ] = { m_sequnces[0 ].size () + 1 , m_sequnces[1 ].size () + 1 };
59- std::vector< char > nwScore (size[0 ] * size[1 ], 0 );
60- for (uint32_t i = 0 ; i < size[1 ]; ++i)
58+ uint64_t size[2 ] = { m_sequnces[0 ].size () + 1 , m_sequnces[1 ].size () + 1 };
59+ Matrix< uint32_t > nwScore (size[0 ], size[1 ]);
60+ for (uint64_t i = 0 ; i < size[0 ]; ++i)
6161 {
62- nwScore[i] = (i + 1 ) * m_gapScore;
62+ nwScore[i][ 0 ] = (i + 1 ) * m_gapScore;
6363 }
64- for (uint32_t i = 0 ; i < size[0 ]; ++i)
64+ for (uint64_t i = 0 ; i < size[1 ]; ++i)
6565 {
66- nwScore[i * size[ 1 ] ] = (i + 1 ) * m_gapScore;
66+ nwScore[0 ][i ] = (i + 1 ) * m_gapScore;
6767 }
6868
6969 uint32_t tmpScore[3 ];
70- for (uint32_t i = 1 ; i < size[0 ]; ++i)
70+ for (uint64_t i = 1 ; i < size[0 ]; ++i)
7171 {
72- for (uint32_t j = 1 ; j < size[1 ]; ++j)
72+ for (uint64_t j = 1 ; j < size[1 ]; ++j)
7373 {
74- uint32_t idx1 = m_charToIndexMap[m_sequnces[0 ][i - 1 ]];
75- uint32_t idx2 = m_charToIndexMap[m_sequnces[1 ][j - 1 ]];
76- tmpScore[0 ] = nwScore[( i - 1 ) * size[ 1 ] + ( j - 1 ) ] + m_scores[idx1 * m_numScores + idx2];
77- tmpScore[1 ] = nwScore[i * size[ 1 ] + ( j - 1 ) ] + m_gapScore;
78- tmpScore[2 ] = nwScore[( i - 1 ) * size[ 1 ] + j] + m_gapScore;
79- nwScore[i * size[ 1 ] + j] = std::min (tmpScore[0 ], std::min (tmpScore[1 ], tmpScore[2 ]));
74+ uint64_t idx1 = m_charToIndexMap[m_sequnces[0 ][i - 1 ]];
75+ uint64_t idx2 = m_charToIndexMap[m_sequnces[1 ][j - 1 ]];
76+ tmpScore[0 ] = nwScore[i - 1 ][ j - 1 ] + m_scores[idx1][ idx2];
77+ tmpScore[1 ] = nwScore[i][ j - 1 ] + m_gapScore;
78+ tmpScore[2 ] = nwScore[i - 1 ][ j] + m_gapScore;
79+ nwScore[i][ j] = std::min (tmpScore[0 ], std::min (tmpScore[1 ], tmpScore[2 ]));
8080 }
8181 }
8282
83- scoreNeedlemanWunsch = nwScore[size[0 ] * size[1 ] - 1 ];
83+ scoreNeedlemanWunsch = nwScore[size[0 ] - 1 ][ size[1 ] - 1 ];
8484
85- for (uint32_t i = size[0 ] - 1 , j = size[1 ] - 1 ; (i > 0 ) && (j > 0 );)
85+ for (uint64_t i = size[0 ] - 1 , j = size[1 ] - 1 ; (i > 0 ) && (j > 0 );)
8686 {
87- uint32_t idx1 = m_charToIndexMap[m_sequnces[0 ][i - 1 ]];
88- uint32_t idx2 = m_charToIndexMap[m_sequnces[1 ][j - 1 ]];
89- tmpScore[0 ] = nwScore[( i - 1 ) * size[ 1 ] + ( j - 1 ) ] + m_scores[idx1 * m_numScores + idx2];
90- tmpScore[1 ] = nwScore[i * size[ 1 ] + ( j - 1 ) ] + m_gapScore;
91- tmpScore[2 ] = nwScore[( i - 1 ) * size[ 1 ] + j] + m_gapScore;
92- if (nwScore[i * size[ 1 ] + j] == tmpScore[0 ])
87+ uint64_t idx1 = m_charToIndexMap[m_sequnces[0 ][i - 1 ]];
88+ uint64_t idx2 = m_charToIndexMap[m_sequnces[1 ][j - 1 ]];
89+ tmpScore[0 ] = nwScore[i - 1 ][ j - 1 ] + m_scores[idx1][ idx2];
90+ tmpScore[1 ] = nwScore[i][ j - 1 ] + m_gapScore;
91+ tmpScore[2 ] = nwScore[i - 1 ][ j] + m_gapScore;
92+ if (nwScore[i][ j] == tmpScore[0 ])
9393 {
9494 seq[0 ].push_back (m_sequnces[0 ][i - 1 ]);
9595 seq[1 ].push_back (m_sequnces[1 ][j - 1 ]);
9696 --i, --j;
9797 }
98- else if (nwScore[i * size[ 1 ] + j] == tmpScore[1 ])
98+ else if (nwScore[i][ j] == tmpScore[1 ])
9999 {
100100 seq[0 ].push_back (' ' );
101101 seq[1 ].push_back (m_sequnces[1 ][j - 1 ]);
102102 --j;
103103 }
104- else if (nwScore[i * size[ 1 ] + j] == tmpScore[2 ])
104+ else if (nwScore[i][ j] == tmpScore[2 ])
105105 {
106106 seq[0 ].push_back (m_sequnces[0 ][i - 1 ]);
107107 seq[1 ].push_back (' ' );
0 commit comments