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