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