| 1 |
// This file is part of A2Amapper. |
| 2 |
// Copyright (c) 2004 Applera Corporation |
| 3 |
// Author: Clark Mobarry |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 2 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received (LICENSE.txt) a copy of the GNU General Public |
| 16 |
// License along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
#include <assert.h> |
| 23 |
|
| 24 |
#include <Python.h> |
| 25 |
|
| 26 |
#include "GF_ALN_local.H" |
| 27 |
|
| 28 |
static Local_Overlap *desc = NULL; |
| 29 |
|
| 30 |
|
| 31 |
// This should be in the "library" not in the client. Sigh. |
| 32 |
|
| 33 |
void |
| 34 |
syntenicSegments(char const * const Aseq, int const Astart, int const Astop, |
| 35 |
char const * const Bseq, int const Bstart, int const Bstop, |
| 36 |
double const erate) { |
| 37 |
|
| 38 |
desc = NULL; // In case an early exit happens! |
| 39 |
|
| 40 |
// Key data types ("Local_Segment" and "Local_Overlap") are defined |
| 41 |
// in "CA_ALN_local.H" |
| 42 |
|
| 43 |
assert(Astop >= Astart); |
| 44 |
assert(Bstop >= Bstart); |
| 45 |
|
| 46 |
// Step 1: get local segments: |
| 47 |
char const * const Ausable = Aseq + Astart; |
| 48 |
char const * const Busable = Bseq + Bstart; |
| 49 |
|
| 50 |
int const alen = Astop - Astart; |
| 51 |
int const blen = Bstop - Bstart; |
| 52 |
|
| 53 |
int NumSegs = 0; /* number of local matches returned */ |
| 54 |
|
| 55 |
Local_Segment *local_results = Find_Local_Segments(Ausable, /* sequence A */ |
| 56 |
alen, |
| 57 |
Busable, |
| 58 |
blen, |
| 59 |
LOCAL_FORW, /* whether to compute a forward search , reverse, or both */ |
| 60 |
16, /* minimum length of a reportable match */ |
| 61 |
erate, /* maximum error for a match to be returned */ |
| 62 |
&NumSegs); /* number of local matches returned */ |
| 63 |
|
| 64 |
if(NumSegs==0) |
| 65 |
return; |
| 66 |
|
| 67 |
// Step 2: get a chain of local segments: |
| 68 |
|
| 69 |
Local_Overlap *Ov = Find_Local_Overlap(alen, /* length of sequence A */ |
| 70 |
blen, /* length of sequence B */ |
| 71 |
0, /* comp==0 -> fwd orientation */ |
| 72 |
0, /* nextbest==0 -> find best overlap*/ |
| 73 |
local_results, /* the input set of local segments */ |
| 74 |
NumSegs, /* number of input local segments */ |
| 75 |
20 - 6, /* shortest "overlap" to report" */ |
| 76 |
1.0); /* fraction of overlap not in a match -- needs to be large to allow substantial mismatches */ |
| 77 |
|
| 78 |
if(Ov == NULL) |
| 79 |
return; |
| 80 |
|
| 81 |
// Step 3 (optional): |
| 82 |
// |
| 83 |
// NOT optional! AS_Local_Trace seems to have been extended to |
| 84 |
// clean up segments. |
| 85 |
// |
| 86 |
// a) fix the chain of segments so that the segments don't overlap. |
| 87 |
// It must be a 1-1 mapping. (can either trim or delete segments--or |
| 88 |
// leave them completely alone) |
| 89 |
// |
| 90 |
// b) construct an alignment "trace" |
| 91 |
// |
| 92 |
// The "trace" is the standard "AS" encoding of an alignment. |
| 93 |
|
| 94 |
// coordinate munge between Gene's local aligner and |
| 95 |
// DP_Compare()-related routines coordinates from Find_Local |
| 96 |
// routines will be one off from those expected by the trace |
| 97 |
// routines, so adjust them! |
| 98 |
|
| 99 |
for(int i=0;i<=Ov->num_pieces;i++){ |
| 100 |
if(i<Ov->num_pieces){ |
| 101 |
Ov->chain[i].piece.abpos++; |
| 102 |
Ov->chain[i].piece.bbpos++; |
| 103 |
Ov->chain[i].piece.aepos++; |
| 104 |
Ov->chain[i].piece.bepos++; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// AS_Local_Trace assumes string pointer one before start of string! |
| 109 |
// |
| 110 |
// The original used to complain if no trace was returned, but we |
| 111 |
// don't care...and, in fact, we disabled trace generation anyway! |
| 112 |
// |
| 113 |
AS_Local_Trace(Ov, Ausable - 1, Busable - 1); |
| 114 |
|
| 115 |
for(int i=0;i<=Ov->num_pieces;i++){ |
| 116 |
if(i<Ov->num_pieces){ |
| 117 |
Ov->chain[i].piece.abpos--; |
| 118 |
Ov->chain[i].piece.bbpos--; |
| 119 |
Ov->chain[i].piece.aepos--; |
| 120 |
Ov->chain[i].piece.bepos--; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
Ov->next = 0; |
| 125 |
|
| 126 |
desc = Ov; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
int iterate_Local_Overlap(int &seg_abpos, int &seg_bbpos, |
| 133 |
int &seg_alen, int &seg_blen, |
| 134 |
double &seg_error) { |
| 135 |
|
| 136 |
if (desc == NULL) |
| 137 |
return(0); |
| 138 |
|
| 139 |
Local_Chain *chain = desc->chain; |
| 140 |
|
| 141 |
assert(NULL != desc->chain); |
| 142 |
|
| 143 |
for(; 0 <= desc->next && desc->next < desc->num_pieces; ) { |
| 144 |
int the_piece = (desc->next)++; |
| 145 |
|
| 146 |
Local_Segment *seg = &(chain[the_piece].piece); |
| 147 |
|
| 148 |
assert(NULL != seg); |
| 149 |
assert(!chain[the_piece].reversed); |
| 150 |
|
| 151 |
// Set the return data |
| 152 |
|
| 153 |
seg_abpos = seg->abpos; |
| 154 |
seg_alen = seg->aepos - seg->abpos; |
| 155 |
seg_bbpos = seg->bbpos; |
| 156 |
seg_blen = seg->bepos - seg->bbpos; |
| 157 |
seg_error = seg->error; |
| 158 |
|
| 159 |
// Skip over the "deleted in-place" segments. |
| 160 |
if((seg->aepos <= seg->abpos)&&(seg->bepos <= seg->bbpos)) |
| 161 |
continue; |
| 162 |
|
| 163 |
// the data is valid |
| 164 |
return(1); |
| 165 |
} |
| 166 |
|
| 167 |
// Nothing left. |
| 168 |
return(0); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static PyObject * |
| 175 |
spam_syntenicSegments(PyObject *self, PyObject *args) { |
| 176 |
char *Aseq = "undefined"; |
| 177 |
int Astart = -1; |
| 178 |
int Astop = -1; // substring of Aseq |
| 179 |
char *Bseq = "undefined"; |
| 180 |
int Bstart = -1; |
| 181 |
int Bstop = -1; // substring of Bseq |
| 182 |
double erate = 1.0 / 3.0; |
| 183 |
|
| 184 |
PyObject *py_outfile = NULL; |
| 185 |
|
| 186 |
if (!PyArg_ParseTuple(args, "Osiisiid", &py_outfile, &Aseq, &Astart, &Astop, &Bseq, &Bstart, &Bstop, &erate)) |
| 187 |
return NULL; |
| 188 |
|
| 189 |
try { |
| 190 |
syntenicSegments(Aseq, Astart, Astop, // substring of Aseq |
| 191 |
Bseq, Bstart, Bstop, // substring of Bseq |
| 192 |
erate); |
| 193 |
} catch (...) { |
| 194 |
PyErr_SetString(PyExc_RuntimeError,"sytenicSegments failed"); |
| 195 |
return(Py_None); |
| 196 |
} |
| 197 |
|
| 198 |
Py_INCREF(Py_None); // This is a module function returning void. |
| 199 |
return(Py_None); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
static PyObject * |
| 205 |
spam_iterateSegments(PyObject *self, PyObject *args) { |
| 206 |
int seg_bgn1 = 0; |
| 207 |
int seg_bgn2 = 0; |
| 208 |
int seg_len1 = 0; |
| 209 |
int seg_len2 = 0; |
| 210 |
double seg_error = 0.0; |
| 211 |
|
| 212 |
if (iterate_Local_Overlap(seg_bgn1, seg_bgn2, seg_len1, seg_len2, seg_error)) |
| 213 |
return(Py_BuildValue("(iiiid)", seg_bgn1, seg_bgn2, seg_len1, seg_len2, seg_error)); |
| 214 |
|
| 215 |
Py_INCREF(Py_None); // This is a module function returning void. |
| 216 |
return(Py_None); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static |
| 222 |
PyMethodDef |
| 223 |
registration_table[] = { |
| 224 |
{"syntenicSegments", spam_syntenicSegments, METH_VARARGS, "Compute syntenic segments"}, |
| 225 |
{"iterateSegments", spam_iterateSegments, METH_VARARGS, "Iterator returning syntenic segments"}, |
| 226 |
{NULL, NULL, 0, NULL} |
| 227 |
}; |
| 228 |
|
| 229 |
|
| •230 |
static struct PyModuleDef cModMethods = |
| 231 |
{ |
| 232 |
PyModuleDef_HEAD_INIT, |
| 233 |
"localAlignerInterface", /* name of module */ |
| 234 |
"", /* module documentation, may be NULL */ |
| 235 |
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ |
| 236 |
registration_table |
| 237 |
}; |
| 238 |
|
| 239 |
extern "C" |
| •240 |
void PyInit_localAlignerInterface() { |
| 241 |
PyModule_Create(&cModMethods); |
| 242 |
} |
| 243 |
|