| 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 | if (string[j] == '"') string[j] = '`'; |
|---|
| 9 | else if (string[j] == '{') string[j] = '('; |
|---|
| 10 | else if (string[j] == '}') string[j] = ')'; |
|---|
| 11 | } |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | static void RemoveQuotes(char *string) { |
|---|
| 15 | int i, j, len; |
|---|
| 16 | |
|---|
| 17 | len = strlen(string); |
|---|
| 18 | for (j=0; j<len; j++) { |
|---|
| 19 | if (string[j] == '"') string[j] = ' '; |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | for (j=0; string[j]==' ' && j<(int)strlen(string); j++) ; |
|---|
| 23 | |
|---|
| 24 | len = strlen(string); |
|---|
| 25 | for (i=0; i<len - j; i++) string[i] = string[i+j]; |
|---|
| 26 | |
|---|
| 27 | for (j=strlen(string)-1; j>=0 && (string[j]=='\n'||string[j]==' '); j--) { |
|---|
| 28 | string[j] = '\0'; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | int WriteGDE(NA_Alignment& aln, char *filename, int method) { |
|---|
| 34 | FILE *file = fopen(filename, "w"); |
|---|
| 35 | if (!file) { |
|---|
| 36 | Warning("Cannot open file for output"); |
|---|
| 37 | return 1; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | for (size_t j=0; j<aln.numelements; j++) { |
|---|
| 41 | if (method == ALL) { |
|---|
| 42 | NA_Sequence *this_elem = &(aln.element[j]); |
|---|
| 43 | // @@@ code below should be in method of NA_Sequence (far too many 'this_elem->') |
|---|
| 44 | fprintf(file, "{\n"); |
|---|
| 45 | if (this_elem->short_name[0]) { |
|---|
| 46 | fprintf(file, "name \"%s\"\n", this_elem->short_name); |
|---|
| 47 | } |
|---|
| 48 | switch (this_elem->elementtype) { |
|---|
| 49 | case DNA: fprintf(file, "type \"DNA\"\n"); break; |
|---|
| 50 | case RNA: fprintf(file, "type \"RNA\"\n"); break; |
|---|
| 51 | case PROTEIN: fprintf(file, "type \"PROTEIN\"\n"); break; |
|---|
| 52 | case MASK: fprintf(file, "type \"MASK\"\n"); break; |
|---|
| 53 | case TEXT: fprintf(file, "type \"TEXT\"\n"); break; |
|---|
| 54 | } |
|---|
| 55 | if (this_elem->seq_name[0]) fprintf(file, "longname %s\n", this_elem->seq_name); |
|---|
| 56 | if (this_elem->id[0]) fprintf(file, "sequence-ID \"%s\"\n", this_elem->id); |
|---|
| 57 | RemoveQuotes(this_elem->barcode); |
|---|
| 58 | RemoveQuotes(this_elem->contig); |
|---|
| 59 | |
|---|
| 60 | if (this_elem->barcode[0]) fprintf(file, "barcode \"%s\"\n", this_elem->barcode); |
|---|
| 61 | if (this_elem->membrane[0]) fprintf(file, "membrane \"%s\"\n", this_elem->membrane); |
|---|
| 62 | if (this_elem->contig[0]) fprintf(file, "contig \"%s\"\n", this_elem->contig); |
|---|
| 63 | if (this_elem->description[0]) fprintf(file, "descrip \"%s\"\n", this_elem->description); |
|---|
| 64 | if (this_elem->authority[0]) fprintf(file, "creator \"%s\"\n", this_elem->authority); |
|---|
| 65 | if (this_elem->groupid) fprintf(file, "group-ID %zu\n", this_elem->groupid); |
|---|
| 66 | if (this_elem->offset+aln.rel_offset) fprintf(file, "offset %d\n", this_elem->offset+aln.rel_offset); |
|---|
| 67 | |
|---|
| 68 | if (this_elem->t_stamp.origin.mm != 0) { |
|---|
| 69 | fprintf(file, |
|---|
| 70 | "creation-date %2d/%2d/%2d %2d:%2d:%2d\n", |
|---|
| 71 | this_elem->t_stamp.origin.mm, |
|---|
| 72 | this_elem->t_stamp.origin.dd, |
|---|
| 73 | (this_elem->t_stamp.origin.yy)>1900 ? |
|---|
| 74 | (this_elem->t_stamp.origin.yy-1900) : |
|---|
| 75 | (this_elem->t_stamp.origin.yy), |
|---|
| 76 | this_elem->t_stamp.origin.hr, |
|---|
| 77 | this_elem->t_stamp.origin.mn, |
|---|
| 78 | this_elem->t_stamp.origin.sc); |
|---|
| 79 | } |
|---|
| 80 | if ((this_elem->attr & IS_ORIG_5_TO_3) && ((this_elem->attr & IS_ORIG_3_TO_5) == 0)) fprintf(file, "orig_direction 1\n"); |
|---|
| 81 | if ((this_elem->attr & IS_CIRCULAR)) fprintf(file, "circular 1\n"); |
|---|
| 82 | if ((this_elem->attr & IS_5_TO_3) && ((this_elem->attr & IS_3_TO_5) == 0)) fprintf(file, "direction 1\n"); |
|---|
| 83 | if ((this_elem->attr & IS_ORIG_3_TO_5) && ((this_elem->attr & IS_ORIG_5_TO_3) == 0)) fprintf(file, "orig_direction -1\n"); |
|---|
| 84 | if ((this_elem->attr & IS_3_TO_5) && ((this_elem->attr & IS_5_TO_3) == 0)) fprintf(file, "direction -1\n"); |
|---|
| 85 | if ((this_elem->attr & IS_ORIG_PRIMARY) && ((this_elem->attr & IS_ORIG_SECONDARY) == 0)) fprintf(file, "orig_strand 1\n"); |
|---|
| 86 | if ((this_elem->attr & IS_PRIMARY) && ((this_elem->attr & IS_SECONDARY) == 0)) fprintf(file, "strandedness 1\n"); |
|---|
| 87 | if (((this_elem->attr & IS_ORIG_PRIMARY) == 0) && (this_elem->attr & IS_ORIG_SECONDARY)) fprintf(file, "orig_strand 2\n"); |
|---|
| 88 | if (((this_elem->attr & IS_PRIMARY) == 0) && (this_elem->attr & IS_SECONDARY)) fprintf(file, "strandedness 2\n"); |
|---|
| 89 | |
|---|
| 90 | if (this_elem->comments) { |
|---|
| 91 | StripSpecial(this_elem->comments); |
|---|
| 92 | fprintf(file, "comments \"%s\"\n", this_elem->comments); |
|---|
| 93 | } |
|---|
| 94 | if (this_elem->baggage) { |
|---|
| 95 | if (this_elem->baggage[strlen(this_elem->baggage)-1] == '\n') { |
|---|
| 96 | fprintf(file, "%s", this_elem->baggage); |
|---|
| 97 | } |
|---|
| 98 | else { |
|---|
| 99 | fprintf(file, "%s\n", this_elem->baggage); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | fprintf(file, "sequence \""); |
|---|
| 103 | if (this_elem->tmatrix) { |
|---|
| 104 | for (int k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) { |
|---|
| 105 | if (k%60 == 0) putc('\n', file); |
|---|
| 106 | putc(this_elem->tmatrix[getelem(this_elem, k)], file); |
|---|
| 107 | } |
|---|
| 108 | fprintf(file, "\"\n"); |
|---|
| 109 | } |
|---|
| 110 | else { |
|---|
| 111 | for (int k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) { |
|---|
| 112 | if (k%60 == 0) putc('\n', file); |
|---|
| 113 | putc(getelem(this_elem, k), file); |
|---|
| 114 | } |
|---|
| 115 | fprintf(file, "\"\n"); |
|---|
| 116 | } |
|---|
| 117 | fprintf(file, "}\n"); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | fclose(file); |
|---|
| 121 | return 0; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /* ALWAYS COPY the result from uniqueID() to a char[32], |
|---|
| 125 | * (strlen(hostname)+1+10). Memory is lost when the function |
|---|
| 126 | * is finished. |
|---|
| 127 | */ |
|---|
| 128 | |
|---|
| 129 | char *uniqueID() { |
|---|
| 130 | static char vname[32]; |
|---|
| 131 | time_t *tp; |
|---|
| 132 | static int cnt = 0; |
|---|
| 133 | |
|---|
| 134 | ARB_calloc(tp, 1); |
|---|
| 135 | |
|---|
| 136 | time(tp); |
|---|
| 137 | sprintf(vname, "host:%d:%ld", cnt, *tp); |
|---|
| 138 | cnt++; |
|---|
| 139 | free(tp); |
|---|
| 140 | return vname; |
|---|
| 141 | } |
|---|
| 142 | |
|---|