source: tags/arb-6.0-rc1/ARB_GDE/GDE_HGLfile.cxx

Last change on this file was 11886, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1#include "GDE_extglob.h"
2#include <ctime>
3#include <algorithm>
4
5static 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
20static 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
40int WriteGDE(NA_Alignment *aln, char *filename, int method)
41{
42    int i;
43    size_t j;
44    int k;
45    FILE *file;
46    NA_Sequence *this_elem;
47
48    if (aln == NULL) return (1);
49
50    file = fopen(filename, "w");
51    if (file == NULL)
52    {
53        Warning("Cannot open file for output");
54        return (1);
55    }
56
57    for (j=0; j<aln->numelements; j++)
58    {
59        if (method == ALL)
60        {
61            this_elem = &(aln->element[j]);
62            fprintf(file, "{\n");
63            if (this_elem->short_name[0])
64                fprintf(file, "name      \"%s\"\n", this_elem->short_name);
65            switch (this_elem->elementtype)
66            {
67                case DNA:
68                    fprintf(file, "type              \"DNA\"\n");
69                    break;
70                case RNA:
71                    fprintf(file, "type              \"RNA\"\n");
72                    break;
73                case PROTEIN:
74                    fprintf(file, "type              \"PROTEIN\"\n");
75                    break;
76                case MASK:
77                    fprintf(file, "type              \"MASK\"\n");
78                    break;
79                case TEXT:
80                    fprintf(file, "type              \"TEXT\"\n");
81                    break;
82            }
83            if (this_elem->seq_name[0])
84                fprintf(file, "longname  %s\n", this_elem->seq_name);
85
86            if (this_elem->id[0])
87                fprintf(file, "sequence-ID       \"%s\"\n", this_elem->id);
88            RemoveQuotes(this_elem->barcode);
89            RemoveQuotes(this_elem->contig);
90
91            if (this_elem->barcode[0])
92                fprintf(file, "barcode           \"%s\"\n", this_elem->barcode);
93            if (this_elem->membrane[0])
94                fprintf(file, "membrane          \"%s\"\n", this_elem->membrane);
95            if (this_elem->contig[0])
96                fprintf(file, "contig            \"%s\"\n", this_elem->contig);
97            if (this_elem->description[0])
98                fprintf(file, "descrip           \"%s\"\n", this_elem->description);
99            if (this_elem->authority[0])
100                fprintf(file, "creator           \"%s\"\n", this_elem->authority);
101            if (this_elem->groupid)
102                fprintf(file, "group-ID          %zu\n", this_elem->groupid);
103            if (this_elem->offset+aln->rel_offset)
104                fprintf(file, "offset            %d\n", this_elem->offset+aln->rel_offset);
105
106            if (this_elem->t_stamp.origin.mm != 0)
107                fprintf(file,
108                        "creation-date      %2d/%2d/%2d %2d:%2d:%2d\n",
109                        this_elem->t_stamp.origin.mm,
110                        this_elem->t_stamp.origin.dd,
111                        (this_elem->t_stamp.origin.yy)>1900 ?
112                        (this_elem->t_stamp.origin.yy-1900) :
113                        (this_elem->t_stamp.origin.yy),
114                        this_elem->t_stamp.origin.hr,
115                        this_elem->t_stamp.origin.mn,
116                        this_elem->t_stamp.origin.sc);
117            if ((this_elem->attr & IS_ORIG_5_TO_3) &&
118               ((this_elem->attr & IS_ORIG_3_TO_5) == 0))
119                fprintf(file, "orig_direction    1\n");
120
121            if ((this_elem->attr & IS_CIRCULAR))
122                fprintf(file, "circular  1\n");
123
124            if ((this_elem->attr & IS_5_TO_3) &&
125               ((this_elem->attr & IS_3_TO_5) == 0))
126                fprintf(file, "direction 1\n");
127
128            if ((this_elem->attr & IS_ORIG_3_TO_5) &&
129               ((this_elem->attr & IS_ORIG_5_TO_3) == 0))
130                fprintf(file, "orig_direction    -1\n");
131
132            if ((this_elem->attr & IS_3_TO_5) &&
133               ((this_elem->attr & IS_5_TO_3) == 0))
134                fprintf(file, "direction -1\n");
135
136            if ((this_elem->attr & IS_ORIG_PRIMARY) &&
137               ((this_elem->attr & IS_ORIG_SECONDARY) == 0))
138                fprintf(file, "orig_strand       1\n");
139
140            if ((this_elem->attr & IS_PRIMARY) &&
141               ((this_elem->attr & IS_SECONDARY) == 0))
142                fprintf(file, "strandedness      1\n");
143
144            if (((this_elem->attr & IS_ORIG_PRIMARY) == 0) &&
145               (this_elem->attr & IS_ORIG_SECONDARY))
146                fprintf(file, "orig_strand       2\n");
147
148            if (((this_elem->attr & IS_PRIMARY) == 0) &&
149               (this_elem->attr & IS_SECONDARY))
150                fprintf(file, "strandedness      2\n");
151
152            if (this_elem->comments != NULL)
153            {
154                StripSpecial(this_elem->comments);
155                fprintf(file, "comments  \"%s\"\n", this_elem->comments);
156            }
157            if (this_elem->baggage != NULL)
158            {
159                if (this_elem->
160                   baggage[strlen(this_elem->baggage)-1] == '\n')
161                    fprintf(file, "%s", this_elem->baggage);
162                else
163                    fprintf(file, "%s\n", this_elem->baggage);
164            }
165            fprintf(file, "sequence  \"");
166            if (this_elem->tmatrix)
167            {
168                for (k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++)
169                {
170                    if (k%60 == 0) putc('\n', file);
171                    putc(this_elem->tmatrix[getelem(this_elem, k)], file);
172                }
173                fprintf(file, "\"\n");
174            }
175            else
176            {
177                for (k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++)
178                {
179                    if (k%60 == 0)
180                        putc('\n', file);
181                    putc(getelem(this_elem, k), file);
182                }
183                fprintf(file, "\"\n");
184            }
185            fprintf(file, "}\n");
186        }
187    }
188    fclose(file);
189    return (0);
190}
191
192/* ALWAYS COPY the result from uniqueID() to a char[32],
193 * (strlen(hostname)+1+10).  Memory is lost when the function
194 * is finished.
195 */
196
197char *uniqueID()
198{
199    static char vname[32];
200    time_t *tp;
201    static int cnt = 0;
202
203    tp = (time_t *)Calloc(1, sizeof(time_t));
204
205    time(tp);
206    sprintf(vname, "host:%d:%ld", cnt, *tp);
207    cnt++;
208    Cfree((char*)tp);
209    return (vname);
210}
211
Note: See TracBrowser for help on using the repository browser.