ARB
GDE_HGLfile.cxx
Go to the documentation of this file.
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 
#define MASK
Definition: GDE_def.h:44
int sc
Definition: GDE_def.h:69
char short_name[SIZE_SHORT_NAME]
Definition: GDE_def.h:86
int elementtype
Definition: GDE_def.h:107
#define IS_5_TO_3
Definition: GDE_def.h:48
char seq_name[SIZE_SEQ_NAME]
Definition: GDE_def.h:85
#define DNA
Definition: GDE_def.h:41
static void RemoveQuotes(char *string)
Definition: GDE_HGLfile.cxx:14
#define PROTEIN
Definition: GDE_def.h:45
TimeStamp t_stamp
Definition: GDE_def.h:96
#define IS_ORIG_SECONDARY
Definition: GDE_def.h:54
#define RNA
Definition: GDE_def.h:42
NA_Sequence * element
Definition: GDE_def.h:124
int attr
Definition: GDE_def.h:100
int yy
Definition: GDE_def.h:64
#define IS_3_TO_5
Definition: GDE_def.h:49
char contig[80]
Definition: GDE_def.h:88
#define IS_ORIG_PRIMARY
Definition: GDE_def.h:53
#define TEXT
Definition: GDE_def.h:43
int dd
Definition: GDE_def.h:66
#define IS_ORIG_3_TO_5
Definition: GDE_def.h:56
char * uniqueID()
#define IS_CIRCULAR
Definition: GDE_def.h:50
struct TimeStamp::@1 origin
size_t groupid
Definition: GDE_def.h:101
#define IS_ORIG_5_TO_3
Definition: GDE_def.h:55
#define IS_SECONDARY
Definition: GDE_def.h:52
static void StripSpecial(char *string)
Definition: GDE_HGLfile.cxx:3
char * baggage
Definition: GDE_def.h:108
void Warning(const char *s)
Definition: GDE_FileIO.cxx:334
char membrane[80]
Definition: GDE_def.h:89
int mm
Definition: GDE_def.h:65
int mn
Definition: GDE_def.h:68
int offset
Definition: GDE_def.h:97
TYPE * ARB_calloc(size_t nelem)
Definition: arb_mem.h:81
int * tmatrix
Definition: GDE_def.h:110
char barcode[80]
Definition: GDE_def.h:87
size_t numelements
Definition: GDE_def.h:120
int getelem(NA_Sequence *a, int b)
int WriteGDE(NA_Alignment &aln, char *filename, int method)
Definition: GDE_HGLfile.cxx:33
int rel_offset
Definition: GDE_def.h:123
char description[SIZE_DESCRIPTION]
Definition: GDE_def.h:90
int hr
Definition: GDE_def.h:67
char authority[SIZE_AUTHORITY]
Definition: GDE_def.h:91
char id[SIZE_ID]
Definition: GDE_def.h:84
#define IS_PRIMARY
Definition: GDE_def.h:51
char * comments
Definition: GDE_def.h:92