| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2006 J. Craig Venter Institute |
| 3 | // Author: Brian Walenz |
| 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 <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include "bio++.H" |
| 24 | #include "atac.H" |
| 25 | |
| 26 | |
| 27 | atacFeatureList::atacFeatureList() { |
| 28 | _featuresLen = 0; |
| 29 | _featuresMax = 256; |
| 30 | _features = new atacFeature [_featuresMax]; |
| 31 | } |
| 32 | |
| 33 | atacFeatureList::~atacFeatureList() { |
| 34 | delete [] _features; |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | atacFeatureList::add(atacFeature &m) { |
| 39 | |
| 40 | if (_featuresLen >= _featuresMax) { |
| 41 | _featuresMax <<= 2; |
| 42 | atacFeature *A = new atacFeature [_featuresMax]; |
| 43 | memcpy(A, _features, sizeof(atacFeature) * _featuresLen); |
| 44 | delete [] _features; |
| 45 | _features = A; |
| 46 | } |
| 47 | |
| 48 | memcpy(&_features[_featuresLen], &m, sizeof(atacFeature)); |
| 49 | |
| 50 | _features[_featuresLen].featureiid = _featuresLen++; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | static |
| 56 | int |
| 57 | sort_(const void *a, const void *b) { |
| 58 | const atacFeature *A = (const atacFeature *)a; |
| 59 | const atacFeature *B = (const atacFeature *)b; |
| 60 | |
| 61 | if (A->iid < B->iid) return(-1); |
| 62 | if (A->iid > B->iid) return(1); |
| 63 | if (A->pos < B->pos) return(-1); |
| 64 | if (A->pos > B->pos) return(1); |
| 65 | if (A->len > B->len) return(-1); |
| 66 | if (A->len < B->len) return(1); |
| 67 | return(0); |
| 68 | } |
| 69 | |
| 70 | static |
| 71 | int |
| 72 | sortfeatureuid_(const void *a, const void *b) { |
| 73 | const atacFeature *A = (const atacFeature *)a; |
| 74 | const atacFeature *B = (const atacFeature *)b; |
| 75 | |
| 76 | int r = strcmp(A->featureuid, B->featureuid); |
| 77 | if (r < 0) return(-1); |
| 78 | if (r > 0) return(1); |
| 79 | r = strcmp(A->parentuid, B->parentuid); |
| 80 | if (r < 0) return(-1); |
| 81 | if (r > 0) return(1); |
| 82 | |
| 83 | return(0); |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | int |
| 88 | sortparentuid_(const void *a, const void *b) { |
| 89 | const atacFeature *A = (const atacFeature *)a; |
| 90 | const atacFeature *B = (const atacFeature *)b; |
| 91 | |
| 92 | int r = strcmp(A->parentuid, B->parentuid); |
| 93 | if (r < 0) return(-1); |
| 94 | if (r > 0) return(1); |
| 95 | r = strcmp(A->featureuid, B->featureuid); |
| 96 | if (r < 0) return(-1); |
| 97 | if (r > 0) return(1); |
| 98 | |
| 99 | return(0); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void |
| 104 | atacFeatureList::sort(uint32 first, uint32 len) { |
| 105 | if (len == 0) len = _featuresLen; |
| 106 | qsort(_features + first, len, sizeof(atacFeature), sort_); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | atacFeatureList::sortFeatureUID(uint32 first, uint32 len) { |
| 111 | if (len == 0) len = _featuresLen; |
| 112 | qsort(_features + first, len, sizeof(atacFeature), sortfeatureuid_); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | atacFeatureList::sortParentUID(uint32 first, uint32 len) { |
| 117 | if (len == 0) len = _featuresLen; |
| 118 | qsort(_features + first, len, sizeof(atacFeature), sortparentuid_); |
| 119 | } |