| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ali_sequence.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include "ali_sequence.hxx" |
|---|
| 12 | |
|---|
| 13 | // --------------------- |
|---|
| 14 | // ALI_SEQUENCE |
|---|
| 15 | |
|---|
| 16 | int ALI_SEQUENCE::check() |
|---|
| 17 | { |
|---|
| 18 | unsigned char *seq_buf; |
|---|
| 19 | unsigned long i; |
|---|
| 20 | |
|---|
| 21 | seq_buf = seq; |
|---|
| 22 | for (i = 0; i < seq_len; i++, seq_buf++) |
|---|
| 23 | if (*seq_buf > 6) |
|---|
| 24 | ali_fatal_error("STOP"); |
|---|
| 25 | |
|---|
| 26 | return 1; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | char *ALI_SEQUENCE::string() |
|---|
| 30 | { |
|---|
| 31 | char *str, *str_buf; |
|---|
| 32 | unsigned char *seq_buf; |
|---|
| 33 | unsigned long i; |
|---|
| 34 | |
|---|
| 35 | str = (char *) CALLOC((unsigned int) seq_len + 1, sizeof(char)); |
|---|
| 36 | |
|---|
| 37 | str_buf = str; |
|---|
| 38 | seq_buf = seq; |
|---|
| 39 | for (i = 0; i < seq_len; i++) { |
|---|
| 40 | *(str_buf++) = *(seq_buf++); |
|---|
| 41 | } |
|---|
| 42 | *str_buf = '\0'; |
|---|
| 43 | ali_sequence_to_string((unsigned char*) str, seq_len); |
|---|
| 44 | |
|---|
| 45 | return str; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | // -------------------------- |
|---|
| 49 | // ALI_NORM_SEQUENCE |
|---|
| 50 | |
|---|
| 51 | ALI_NORM_SEQUENCE::ALI_NORM_SEQUENCE(char *Name, char *String) |
|---|
| 52 | { |
|---|
| 53 | unsigned long counter; |
|---|
| 54 | unsigned char *s; |
|---|
| 55 | int dot_flag; |
|---|
| 56 | char *str; |
|---|
| 57 | |
|---|
| 58 | // Count only _BASES_ |
|---|
| 59 | for (counter = 0, str = String; *str != '\0'; str++) |
|---|
| 60 | if (ali_is_base(*str)) |
|---|
| 61 | counter++; |
|---|
| 62 | seq_len = counter; |
|---|
| 63 | |
|---|
| 64 | seq = (unsigned char*) CALLOC((unsigned int) seq_len, sizeof(unsigned char)); |
|---|
| 65 | dots = (unsigned char **) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
|---|
| 66 | seq_name = strdup(Name); |
|---|
| 67 | |
|---|
| 68 | if (seq == 0 || dots == 0 || seq_name == 0) { |
|---|
| 69 | ali_fatal_error("Out of memory"); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | dot_flag = 0; |
|---|
| 73 | (*dots)[0] |= (unsigned char) (1<<7); |
|---|
| 74 | for (counter = 0, str = String, s = seq; *str != '\0'; str++) { |
|---|
| 75 | if (ali_is_base(*str)) { |
|---|
| 76 | *s++ = ali_base_to_number(*str); |
|---|
| 77 | dot_flag = 0; |
|---|
| 78 | counter++; |
|---|
| 79 | } |
|---|
| 80 | else { |
|---|
| 81 | if (dot_flag == 0 && ali_is_dot(*str)) { |
|---|
| 82 | (*dots)[(counter/8)] |= (unsigned char) (1<<(7-(counter%8))); |
|---|
| 83 | dot_flag = 1; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ALI_NORM_SEQUENCE::ALI_NORM_SEQUENCE(ALI_SEQUENCE *SEQ) |
|---|
| 90 | { |
|---|
| 91 | unsigned long counter, pos; |
|---|
| 92 | unsigned char *s; |
|---|
| 93 | int dot_flag; |
|---|
| 94 | unsigned char *str; |
|---|
| 95 | |
|---|
| 96 | for (counter = pos = 0, str = SEQ->sequence(); |
|---|
| 97 | pos < SEQ->length(); pos++, str++) |
|---|
| 98 | if (ali_is_base(*str)) |
|---|
| 99 | counter++; |
|---|
| 100 | seq_len = counter; |
|---|
| 101 | |
|---|
| 102 | seq = (unsigned char*) CALLOC((unsigned int) seq_len, sizeof(unsigned char)); |
|---|
| 103 | dots = (unsigned char **) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
|---|
| 104 | seq_name = strdup(SEQ->name()); |
|---|
| 105 | |
|---|
| 106 | if (seq == 0 || dots == 0 || seq_name == 0) { |
|---|
| 107 | ali_fatal_error("Out of memory"); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | dot_flag = 0; |
|---|
| 111 | (*dots)[0] |= (unsigned char) (1<<7); |
|---|
| 112 | for (counter = pos = 0, str = SEQ->sequence(), s = seq; |
|---|
| 113 | pos < SEQ->length(); str++, pos++) { |
|---|
| 114 | if (ali_is_base(*str)) { |
|---|
| 115 | *s++ = *str; |
|---|
| 116 | dot_flag = 0; |
|---|
| 117 | counter++; |
|---|
| 118 | } |
|---|
| 119 | else { |
|---|
| 120 | if (dot_flag == 0 && ali_is_dot(*str)) { |
|---|
| 121 | (*dots)[(counter/8)] |= (unsigned char) (1<<(7-(counter%8))); |
|---|
| 122 | dot_flag = 1; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | char *ALI_NORM_SEQUENCE::string() |
|---|
| 129 | { |
|---|
| 130 | char *str, *str_buf; |
|---|
| 131 | unsigned char *seq_buf; |
|---|
| 132 | unsigned long i; |
|---|
| 133 | |
|---|
| 134 | str = (char *) CALLOC((unsigned int) seq_len + 1, sizeof(char)); |
|---|
| 135 | if (str == 0) |
|---|
| 136 | ali_fatal_error("Out of memory"); |
|---|
| 137 | |
|---|
| 138 | for (i = seq_len, str_buf = str, seq_buf = seq; i-- > 0;) |
|---|
| 139 | *str_buf++ = *seq_buf++; |
|---|
| 140 | *str_buf = '\0'; |
|---|
| 141 | |
|---|
| 142 | ali_sequence_to_string((unsigned char*) str, seq_len); |
|---|
| 143 | |
|---|
| 144 | return str; |
|---|
| 145 | } |
|---|
| 146 | |
|---|