Skip to content
This repository
Browse code

In pairwise2, allow penalize_end_gaps to be different for the two seq…

…uences
  • Loading branch information...
commit c65e3cf17fe58c859d071fe061deca1ee2a16a5d 1 parent c1ff29a
authored June 28, 2013
11  Bio/cpairwise2module.c
@@ -232,7 +232,7 @@ static PyObject *cpairwise2__make_score_matrix_fast(
232 232
     char *sequenceA=NULL, *sequenceB=NULL;
233 233
     int use_sequence_cstring;
234 234
     double open_A, extend_A, open_B, extend_B;
235  
-    int penalize_extend_when_opening, penalize_end_gaps;
  235
+    int penalize_extend_when_opening, penalize_end_gaps_A, penalize_end_gaps_B;
236 236
     int align_globally, score_only;
237 237
 
238 238
     PyObject *py_match=NULL, *py_mismatch=NULL;
@@ -251,9 +251,10 @@ static PyObject *cpairwise2__make_score_matrix_fast(
251 251
 
252 252
     PyObject *py_retval = NULL;
253 253
 
254  
-    if(!PyArg_ParseTuple(args, "OOOddddiiii", &py_sequenceA, &py_sequenceB,
  254
+    if(!PyArg_ParseTuple(args, "OOOddddi(ii)ii", &py_sequenceA, &py_sequenceB,
255 255
 			 &py_match_fn, &open_A, &extend_A, &open_B, &extend_B,
256  
-			 &penalize_extend_when_opening, &penalize_end_gaps,
  256
+			 &penalize_extend_when_opening,
  257
+                         &penalize_end_gaps_A, &penalize_end_gaps_B,
257 258
 			 &align_globally, &score_only))
258 259
 	return NULL;
259 260
     if(!PySequence_Check(py_sequenceA) || !PySequence_Check(py_sequenceB)) {
@@ -347,7 +348,7 @@ static PyObject *cpairwise2__make_score_matrix_fast(
347 348
 					use_match_mismatch_scores);
348 349
 	if(PyErr_Occurred())
349 350
 	    goto _cleanup_make_score_matrix_fast;
350  
-	if(penalize_end_gaps)
  351
+	if(penalize_end_gaps_B)
351 352
 	    score += calc_affine_penalty(i, open_B, extend_B,
352 353
 					 penalize_extend_when_opening);
353 354
 	score_matrix[i*lenB] = score;
@@ -361,7 +362,7 @@ static PyObject *cpairwise2__make_score_matrix_fast(
361 362
 					use_match_mismatch_scores);
362 363
 	if(PyErr_Occurred())
363 364
 	    goto _cleanup_make_score_matrix_fast;
364  
-	if(penalize_end_gaps)
  365
+	if(penalize_end_gaps_A)
365 366
 	    score += calc_affine_penalty(i, open_A, extend_A,
366 367
 					 penalize_extend_when_opening);
367 368
 	score_matrix[i] = score;
27  Bio/pairwise2.py
@@ -295,6 +295,13 @@ def decode(self, *args, **keywds):
295 295
                 ]
296 296
             for name, default in default_params:
297 297
                 keywds[name] = keywds.get(name, default)
  298
+            value = keywds['penalize_end_gaps']
  299
+            try:
  300
+                n = len(value)
  301
+            except TypeError:
  302
+                keywds['penalize_end_gaps'] = tuple([value]*2)
  303
+            else:
  304
+                assert n==2
298 305
             return keywds
299 306
 
300 307
         def __call__(self, *args, **keywds):
@@ -385,12 +392,12 @@ def _make_score_matrix_generic(
385 392
         # sequence A.  This is like opening up i gaps at the beginning
386 393
         # of sequence B.
387 394
         score = match_fn(sequenceA[i], sequenceB[0])
388  
-        if penalize_end_gaps:
  395
+        if penalize_end_gaps[1]:
389 396
             score += gap_B_fn(0, i)
390 397
         score_matrix[i][0] = score
391 398
     for i in range(1, lenB):
392 399
         score = match_fn(sequenceA[0], sequenceB[i])
393  
-        if penalize_end_gaps:
  400
+        if penalize_end_gaps[0]:
394 401
             score += gap_A_fn(0, i)
395 402
         score_matrix[0][i] = score
396 403
 
@@ -469,13 +476,13 @@ def _make_score_matrix_fast(
469 476
         # sequence A.  This is like opening up i gaps at the beginning
470 477
         # of sequence B.
471 478
         score = match_fn(sequenceA[i], sequenceB[0])
472  
-        if penalize_end_gaps:
  479
+        if penalize_end_gaps[1]:
473 480
             score += calc_affine_penalty(
474 481
                 i, open_B, extend_B, penalize_extend_when_opening)
475 482
         score_matrix[i][0] = score
476 483
     for i in range(1, lenB):
477 484
         score = match_fn(sequenceA[0], sequenceB[i])
478  
-        if penalize_end_gaps:
  485
+        if penalize_end_gaps[0]:
479 486
             score += calc_affine_penalty(
480 487
                 i, open_A, extend_A, penalize_extend_when_opening)
481 488
         score_matrix[0][i] = score
@@ -663,12 +670,8 @@ def _find_start(score_matrix, sequenceA, sequenceB, gap_A_fn, gap_B_fn,
663 670
     # Return a list of (score, (row, col)) indicating every possible
664 671
     # place to start the tracebacks.
665 672
     if align_globally:
666  
-        if penalize_end_gaps:
667  
-            starts = _find_global_start(
668  
-                sequenceA, sequenceB, score_matrix, gap_A_fn, gap_B_fn, 1)
669  
-        else:
670  
-            starts = _find_global_start(
671  
-                sequenceA, sequenceB, score_matrix, None, None, 0)
  673
+        starts = _find_global_start(
  674
+            sequenceA, sequenceB, score_matrix, gap_A_fn, gap_B_fn, penalize_end_gaps)
672 675
     else:
673 676
         starts = _find_local_start(score_matrix)
674 677
     return starts
@@ -684,13 +687,13 @@ def _find_global_start(sequenceA, sequenceB,
684 687
     for row in range(nrows):
685 688
         # Find the score, penalizing end gaps if necessary.
686 689
         score = score_matrix[row][ncols-1]
687  
-        if penalize_end_gaps:
  690
+        if penalize_end_gaps[1]:
688 691
             score += gap_B_fn(ncols, nrows-row-1)
689 692
         positions.append((score, (row, ncols-1)))
690 693
     # Search all columns in the last row.
691 694
     for col in range(ncols-1):
692 695
         score = score_matrix[nrows-1][col]
693  
-        if penalize_end_gaps:
  696
+        if penalize_end_gaps[0]:
694 697
             score += gap_A_fn(nrows, ncols-col-1)
695 698
         positions.append((score, (nrows-1, col)))
696 699
     return positions

0 notes on commit c65e3cf

Please sign in to comment.
Something went wrong with that request. Please try again.