| 1 | #include "GDE_extglob.h" |
|---|
| 2 | |
|---|
| 3 | static void StripSpecial(char *string) { |
|---|
| 4 | int j, len; |
|---|
| 5 | |
|---|
| 6 | len = strlen(string); |
|---|
| 7 | for (j=0; j<len; j++) |
|---|
| 8 | { |
|---|
| 9 | if (string[j] == '"') |
|---|
| 10 | string[j] = '`'; |
|---|
| 11 | else if (string[j] == '{') |
|---|
| 12 | string[j] = '('; |
|---|
| 13 | else if (string[j] == '}') |
|---|
| 14 | string[j] = ')'; |
|---|
| 15 | } |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | static void RemoveQuotes(char *string) |
|---|
| 19 | { |
|---|
| 20 | int i, j, len; |
|---|
| 21 | |
|---|
| 22 | len = strlen(string); |
|---|
| 23 | for (j=0; j<len; j++) { |
|---|
| 24 | if (string[j] == '"') string[j] = ' '; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | for (j=0; string[j]==' ' && j<(int)strlen(string); j++) ; |
|---|
| 28 | |
|---|
| 29 | len = strlen(string); |
|---|
| 30 | for (i=0; i<len - j; i++) string[i] = string[i+j]; |
|---|
| 31 | |
|---|
| 32 | for (j=strlen(string)-1; j>=0 && (string[j]=='\n'||string[j]==' '); j--) { |
|---|
| 33 | string[j] = '\0'; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | int WriteGDE(NA_Alignment& aln, char *filename, int method) { |
|---|
| 39 | FILE *file = fopen(filename, "w"); |
|---|
| 40 | if (file == NULL) |
|---|
| 41 | { |
|---|
| 42 | Warning("Cannot open file for output"); |
|---|
| 43 | return (1); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | for (size_t j=0; j<aln.numelements; j++) |
|---|
| 47 | { |
|---|
| 48 | if (method == ALL) |
|---|
| 49 | { |
|---|
| 50 | NA_Sequence *this_elem = &(aln.element[j]); |
|---|
| 51 | // @@@ code below should be in method of NA_Sequence (far too many 'this_elem->') |
|---|
| 52 | fprintf(file, "{\n"); |
|---|
| 53 | if (this_elem->short_name[0]) |
|---|
| 54 | fprintf(file, "name \"%s\"\n", this_elem->short_name); |
|---|
| 55 | switch (this_elem->elementtype) |
|---|
| 56 | { |
|---|
| 57 | case DNA: |
|---|
| 58 | fprintf(file, "type \"DNA\"\n"); |
|---|
| 59 | break; |
|---|
| 60 | case RNA: |
|---|
| 61 | fprintf(file, "type \"RNA\"\n"); |
|---|
| 62 | break; |
|---|
| 63 | case PROTEIN: |
|---|
| 64 | fprintf(file, "type \"PROTEIN\"\n"); |
|---|
| 65 | break; |
|---|
| 66 | case MASK: |
|---|
| 67 | fprintf(file, "type \"MASK\"\n"); |
|---|
| 68 | break; |
|---|
| 69 | case TEXT: |
|---|
| 70 | fprintf(file, "type \"TEXT\"\n"); |
|---|
| 71 | break; |
|---|
| 72 | } |
|---|
| 73 | if (this_elem->seq_name[0]) |
|---|
| 74 | fprintf(file, "longname %s\n", this_elem->seq_name); |
|---|
| 75 | |
|---|
| 76 | if (this_elem->id[0]) |
|---|
| 77 | fprintf(file, "sequence-ID \"%s\"\n", this_elem->id); |
|---|
| 78 | RemoveQuotes(this_elem->barcode); |
|---|
| 79 | RemoveQuotes(this_elem->contig); |
|---|
| 80 | |
|---|
| 81 | if (this_elem->barcode[0]) |
|---|
| 82 | fprintf(file, "barcode \"%s\"\n", this_elem->barcode); |
|---|
| 83 | if (this_elem->membrane[0]) |
|---|
| 84 | fprintf(file, "membrane \"%s\"\n", this_elem->membrane); |
|---|
| 85 | if (this_elem->contig[0]) |
|---|
| 86 | fprintf(file, "contig \"%s\"\n", this_elem->contig); |
|---|
| 87 | if (this_elem->description[0]) |
|---|
| 88 | fprintf(file, "descrip \"%s\"\n", this_elem->description); |
|---|
| 89 | if (this_elem->authority[0]) |
|---|
| 90 | fprintf(file, "creator \"%s\"\n", this_elem->authority); |
|---|
| 91 | if (this_elem->groupid) |
|---|
| 92 | fprintf(file, "group-ID %zu\n", this_elem->groupid); |
|---|
| 93 | if (this_elem->offset+aln.rel_offset) |
|---|
| 94 | fprintf(file, "offset %d\n", this_elem->offset+aln.rel_offset); |
|---|
| 95 | |
|---|
| 96 | if (this_elem->t_stamp.origin.mm != 0) |
|---|
| 97 | fprintf(file, |
|---|
| 98 | "creation-date %2d/%2d/%2d %2d:%2d:%2d\n", |
|---|
| 99 | this_elem->t_stamp.origin.mm, |
|---|
| 100 | this_elem->t_stamp.origin.dd, |
|---|
| 101 | (this_elem->t_stamp.origin.yy)>1900 ? |
|---|
| 102 | (this_elem->t_stamp.origin.yy-1900) : |
|---|
| 103 | (this_elem->t_stamp.origin.yy), |
|---|
| 104 | this_elem->t_stamp.origin.hr, |
|---|
| 105 | this_elem->t_stamp.origin.mn, |
|---|
| 106 | this_elem->t_stamp.origin.sc); |
|---|
| 107 | if ((this_elem->attr & IS_ORIG_5_TO_3) && |
|---|
| 108 | ((this_elem->attr & IS_ORIG_3_TO_5) == 0)) |
|---|
| 109 | fprintf(file, "orig_direction 1\n"); |
|---|
| 110 | |
|---|
| 111 | if ((this_elem->attr & IS_CIRCULAR)) |
|---|
| 112 | fprintf(file, "circular 1\n"); |
|---|
| 113 | |
|---|
| 114 | if ((this_elem->attr & IS_5_TO_3) && |
|---|
| 115 | ((this_elem->attr & IS_3_TO_5) == 0)) |
|---|
| 116 | fprintf(file, "direction 1\n"); |
|---|
| 117 | |
|---|
| 118 | if ((this_elem->attr & IS_ORIG_3_TO_5) && |
|---|
| 119 | ((this_elem->attr & IS_ORIG_5_TO_3) == 0)) |
|---|
| 120 | fprintf(file, "orig_direction -1\n"); |
|---|
| 121 | |
|---|
| 122 | if ((this_elem->attr & IS_3_TO_5) && |
|---|
| 123 | ((this_elem->attr & IS_5_TO_3) == 0)) |
|---|
| 124 | fprintf(file, "direction -1\n"); |
|---|
| 125 | |
|---|
| 126 | if ((this_elem->attr & IS_ORIG_PRIMARY) && |
|---|
| 127 | ((this_elem->attr & IS_ORIG_SECONDARY) == 0)) |
|---|
| 128 | fprintf(file, "orig_strand 1\n"); |
|---|
| 129 | |
|---|
| 130 | if ((this_elem->attr & IS_PRIMARY) && |
|---|
| 131 | ((this_elem->attr & IS_SECONDARY) == 0)) |
|---|
| 132 | fprintf(file, "strandedness 1\n"); |
|---|
| 133 | |
|---|
| 134 | if (((this_elem->attr & IS_ORIG_PRIMARY) == 0) && |
|---|
| 135 | (this_elem->attr & IS_ORIG_SECONDARY)) |
|---|
| 136 | fprintf(file, "orig_strand 2\n"); |
|---|
| 137 | |
|---|
| 138 | if (((this_elem->attr & IS_PRIMARY) == 0) && |
|---|
| 139 | (this_elem->attr & IS_SECONDARY)) |
|---|
| 140 | fprintf(file, "strandedness 2\n"); |
|---|
| 141 | |
|---|
| 142 | if (this_elem->comments != NULL) |
|---|
| 143 | { |
|---|
| 144 | StripSpecial(this_elem->comments); |
|---|
| 145 | fprintf(file, "comments \"%s\"\n", this_elem->comments); |
|---|
| 146 | } |
|---|
| 147 | if (this_elem->baggage != NULL) |
|---|
| 148 | { |
|---|
| 149 | if (this_elem-> |
|---|
| 150 | baggage[strlen(this_elem->baggage)-1] == '\n') |
|---|
| 151 | fprintf(file, "%s", this_elem->baggage); |
|---|
| 152 | else |
|---|
| 153 | fprintf(file, "%s\n", this_elem->baggage); |
|---|
| 154 | } |
|---|
| 155 | fprintf(file, "sequence \""); |
|---|
| 156 | if (this_elem->tmatrix) |
|---|
| 157 | { |
|---|
| 158 | for (int k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 159 | { |
|---|
| 160 | if (k%60 == 0) putc('\n', file); |
|---|
| 161 | putc(this_elem->tmatrix[getelem(this_elem, k)], file); |
|---|
| 162 | } |
|---|
| 163 | fprintf(file, "\"\n"); |
|---|
| 164 | } |
|---|
| 165 | else |
|---|
| 166 | { |
|---|
| 167 | for (int k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 168 | { |
|---|
| 169 | if (k%60 == 0) |
|---|
| 170 | putc('\n', file); |
|---|
| 171 | putc(getelem(this_elem, k), file); |
|---|
| 172 | } |
|---|
| 173 | fprintf(file, "\"\n"); |
|---|
| 174 | } |
|---|
| 175 | fprintf(file, "}\n"); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | fclose(file); |
|---|
| 179 | return (0); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /* ALWAYS COPY the result from uniqueID() to a char[32], |
|---|
| 183 | * (strlen(hostname)+1+10). Memory is lost when the function |
|---|
| 184 | * is finished. |
|---|
| 185 | */ |
|---|
| 186 | |
|---|
| 187 | char *uniqueID() |
|---|
| 188 | { |
|---|
| 189 | static char vname[32]; |
|---|
| 190 | time_t *tp; |
|---|
| 191 | static int cnt = 0; |
|---|
| 192 | |
|---|
| 193 | tp = (time_t *)Calloc(1, sizeof(time_t)); |
|---|
| 194 | |
|---|
| 195 | time(tp); |
|---|
| 196 | sprintf(vname, "host:%d:%ld", cnt, *tp); |
|---|
| 197 | cnt++; |
|---|
| 198 | Cfree((char*)tp); |
|---|
| 199 | return (vname); |
|---|
| 200 | } |
|---|
| 201 | |
|---|