Pārlūkot izejas kodu

cmProc5.h/c : Added cmSeqAlign.

master
kevin 8 gadus atpakaļ
vecāks
revīzija
5208cfae60
2 mainītis faili ar 241 papildinājumiem un 0 dzēšanām
  1. 204
    0
      cmProc5.c
  2. 37
    0
      cmProc5.h

+ 204
- 0
cmProc5.c Parādīt failu

@@ -1098,3 +1098,207 @@ void cmNlmsEcSetIrN(    cmNlmsEc_t* p, unsigned hN )
1098 1098
   p->hN = hN;
1099 1099
 }
1100 1100
 
1101
+//=======================================================================================================================
1102
+// 
1103
+//
1104
+
1105
+cmSeqAlign_t* cmSeqAlignAlloc(  cmCtx* ctx, cmSeqAlign_t* ap )
1106
+{
1107
+  cmSeqAlign_t* p = cmObjAlloc(cmSeqAlign_t,ctx,ap);
1108
+
1109
+  if( cmSeqAlignInit(p) != cmOkRC )
1110
+    cmSeqAlignFree(&p);
1111
+
1112
+  return p;  
1113
+}
1114
+
1115
+cmRC_t        cmSeqAlignFree(   cmSeqAlign_t** pp )
1116
+{
1117
+  cmRC_t rc = cmOkRC;
1118
+
1119
+  if( pp == NULL || *pp == NULL )
1120
+    return rc;
1121
+
1122
+  cmSeqAlign_t* p = *pp;
1123
+  if((rc = cmSeqAlignFinal(p)) != cmOkRC )
1124
+    return rc;
1125
+
1126
+  while( p->seqL != NULL )
1127
+  {
1128
+    while( p->seqL->locL != NULL )
1129
+    {
1130
+      cmSeqAlignLoc_t* lp = p->seqL->locL->link;
1131
+      cmMemFree(p->seqL->locL->vV);
1132
+      cmMemFree(p->seqL->locL);
1133
+      p->seqL->locL = lp;
1134
+    }
1135
+
1136
+    cmSeqAlignSeq_t* sp = p->seqL->link;
1137
+    cmMemFree(p->seqL);
1138
+    p->seqL = sp;
1139
+  }
1140
+  
1141
+  cmObjFree(pp);
1142
+
1143
+  return rc;
1144
+}
1145
+
1146
+cmRC_t        cmSeqAlignInit(   cmSeqAlign_t* p )
1147
+{ return cmOkRC; }
1148
+
1149
+cmRC_t        cmSeqAlignFinal(  cmSeqAlign_t* p )
1150
+{ return cmOkRC; }
1151
+
1152
+cmSeqAlignSeq_t* _cmSeqAlignIdToSeq( cmSeqAlign_t* p, unsigned seqId )
1153
+{
1154
+  cmSeqAlignSeq_t* sp = p->seqL;
1155
+  for(; sp!=NULL; sp=sp->link)
1156
+    if( sp->id == seqId )
1157
+      return sp;
1158
+  return NULL;
1159
+}
1160
+
1161
+cmSeqAlignLoc_t* _cmSeqAlignIdToLoc( cmSeqAlignSeq_t* sp, unsigned locId )
1162
+{
1163
+  cmSeqAlignLoc_t* lp = sp->locL;
1164
+  for(; lp!=NULL; lp=lp->link)
1165
+  {
1166
+    // if the locId's match 
1167
+    if( lp->id == locId )
1168
+      return lp;
1169
+
1170
+    if( (lp->link != NULL && lp->link->id > locId) || lp->link==NULL )
1171
+      return lp;  // return record previous to locId 
1172
+  }
1173
+
1174
+  // return NULL: locId is less than all other locations id's in the list
1175
+  return lp;
1176
+}
1177
+
1178
+cmRC_t        cmSeqAlignInsert( cmSeqAlign_t* p, unsigned seqId, unsigned locId, unsigned value )
1179
+{
1180
+  cmSeqAlignSeq_t* sp;
1181
+
1182
+  // if the requested sequence does not already exist ...
1183
+  if((sp = _cmSeqAlignIdToSeq(p,seqId)) == NULL )
1184
+  {
1185
+    // ... then create it
1186
+    sp       = cmMemAllocZ(cmSeqAlignSeq_t,1);
1187
+    sp->id   = seqId;
1188
+
1189
+    if( p->seqL == NULL )
1190
+      p->seqL = sp;
1191
+    else
1192
+    {
1193
+      cmSeqAlignSeq_t* s0 = p->seqL;
1194
+      while( s0->link != NULL )
1195
+        s0 = s0->link;
1196
+
1197
+      s0->link = sp;
1198
+      
1199
+    }    
1200
+  }
1201
+  assert(sp != NULL);
1202
+
1203
+  cmSeqAlignLoc_t* lp;
1204
+
1205
+  // if the requested location does not exist in the requested sequence ... 
1206
+  if((lp = _cmSeqAlignIdToLoc(sp,locId)) == NULL || lp->id != locId)
1207
+  {
1208
+    // ... then create it
1209
+    cmSeqAlignLoc_t* nlp = cmMemAllocZ(cmSeqAlignLoc_t,1);
1210
+    nlp->id = locId;
1211
+
1212
+    // if lp is NULL then link nlp as first record in sequence
1213
+    if( lp == NULL )
1214
+    {      
1215
+      // make new loc recd first on the list
1216
+      nlp->link = sp->locL;
1217
+      sp->locL  = nlp;
1218
+    }
1219
+    else // otherwise link nlp after lp
1220
+    {
1221
+      nlp->link = lp->link;
1222
+      lp->link  = nlp;
1223
+    }
1224
+
1225
+    lp = nlp;
1226
+  }
1227
+
1228
+  assert( lp!=NULL );
1229
+
1230
+  // insert the new value
1231
+  lp->vV = cmMemResizeP(unsigned,lp->vV,lp->vN+1);
1232
+  lp->vV[ lp->vN ] = value;
1233
+  lp->vN += 1;
1234
+
1235
+  return cmOkRC;
1236
+}
1237
+
1238
+double _cmSeqAlignCompare( const cmSeqAlignLoc_t* l0, const cmSeqAlignLoc_t* l1)
1239
+{
1240
+  double dist = 0;
1241
+  unsigned i=0;
1242
+  for(i=0; i<l0->vN; ++i)
1243
+  {
1244
+    unsigned j=0;
1245
+    for(j=0; j<l1->vN; ++j)
1246
+      if( l0->vV[i] == l1->vV[j] )
1247
+        break;
1248
+
1249
+    if( l0->vV[i] != l1->vV[j] )
1250
+      dist += 1.0;
1251
+  }
1252
+  
1253
+  return dist;
1254
+}
1255
+
1256
+cmRC_t cmSeqAlignExec(   cmSeqAlign_t* p )
1257
+{
1258
+  
1259
+  return cmOkRC;
1260
+}
1261
+
1262
+void _cmSeqAlignReportLoc( cmRpt_t* rpt, const cmSeqAlignLoc_t* lp )
1263
+{
1264
+  //cmRptPrintf(rpt,"%5i : ",lp->id);
1265
+  
1266
+  unsigned i;
1267
+  for(i=0; i<lp->vN; ++i)
1268
+  {
1269
+    //cmRptPrintf(rpt,"%3i ",lp->vV[i]);
1270
+    cmRptPrintf(rpt,"%4s ",cmMidiToSciPitch(lp->vV[i],NULL,0));
1271
+  }
1272
+
1273
+  cmRptPrintf(rpt," | ");
1274
+}
1275
+
1276
+void cmSeqAlignReport( cmSeqAlign_t* p, cmRpt_t* rpt )
1277
+{
1278
+  cmSeqAlignLoc_t* slp = p->seqL->locL;
1279
+
1280
+  for(; slp!=NULL; slp=slp->link)
1281
+  {
1282
+    cmRptPrintf(rpt,"%5i : ",slp->id);
1283
+    
1284
+    // report the next location on the first sequence as the reference location
1285
+    _cmSeqAlignReportLoc( rpt, slp );
1286
+
1287
+    // for each remaining sequence 
1288
+    cmSeqAlignSeq_t* sp = p->seqL->link;
1289
+    for(; sp!=NULL; sp=sp->link)
1290
+    {
1291
+      // locate the location with the same id as the reference location ...
1292
+      cmSeqAlignLoc_t* lp;
1293
+      
1294
+      if((lp = _cmSeqAlignIdToLoc(sp,slp->id)) != NULL && lp->id == slp->id)
1295
+      {
1296
+        _cmSeqAlignReportLoc(rpt,lp); // ... and report it
1297
+      }
1298
+      
1299
+    }
1300
+
1301
+    cmRptPrintf(rpt,"\n");
1302
+  }
1303
+}
1304
+

+ 37
- 0
cmProc5.h Parādīt failu

@@ -265,8 +265,45 @@ extern "C" {
265 265
   void        cmNlmsEcSetMu(     cmNlmsEc_t* p, float mu );
266 266
   void        cmNlmsEcSetDelayN( cmNlmsEc_t* p, unsigned delayN );
267 267
   void        cmNlmsEcSetIrN(    cmNlmsEc_t* p, unsigned irN );
268
+  
269
+  //------------------------------------------------------------------------------------------------------------
268 270
   //)
269 271
 
272
+  //( { label:cmSeqAlign file_desc:"Align two sequences where each sequence is composed of records with varying numbers of features." kw:[proc]}
273
+
274
+  typedef struct cmSeqAlignLoc_str
275
+  {
276
+    unsigned                  id;  // location id
277
+    unsigned                  vN;  // 
278
+    unsigned*                 vV;  // vV[vN]
279
+    struct cmSeqAlignLoc_str* link;
280
+  } cmSeqAlignLoc_t;
281
+
282
+  typedef struct cmSeqAlignSeq_str
283
+  {
284
+    unsigned                  id;
285
+    cmSeqAlignLoc_t*          locL;
286
+    struct cmSeqAlignSeq_str* link;
287
+  } cmSeqAlignSeq_t;
288
+  
289
+  typedef struct
290
+  {
291
+    cmObj            obj;
292
+    cmSeqAlignSeq_t* seqL;
293
+    
294
+  } cmSeqAlign_t;
295
+
296
+  cmSeqAlign_t* cmSeqAlignAlloc(  cmCtx* ctx, cmSeqAlign_t* p );
297
+  cmRC_t        cmSeqAlignFree(   cmSeqAlign_t** p );
298
+  cmRC_t        cmSeqAlignInit(   cmSeqAlign_t* p );
299
+  cmRC_t        cmSeqAlignFinal(  cmSeqAlign_t* p );
300
+  cmRC_t        cmSeqAlignInsert( cmSeqAlign_t* p, unsigned seqId, unsigned locId, unsigned value );
301
+  cmRC_t        cmSeqAlignExec(   cmSeqAlign_t* p );
302
+  void          cmSeqAlignReport( cmSeqAlign_t* p, cmRpt_t* rpt );
303
+  
304
+  
305
+  //------------------------------------------------------------------------------------------------------------
306
+  //)
270 307
   
271 308
 #ifdef __cplusplus
272 309
 }

Notiek ielāde…
Atcelt
Saglabāt