| 1 | #include "GDE_extglob.h" |
|---|
| 2 | #include <ctime> |
|---|
| 3 | |
|---|
| 4 | /* |
|---|
| 5 | Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | static void AdjustGroups(NA_Alignment *aln) |
|---|
| 10 | { |
|---|
| 11 | size_t i; |
|---|
| 12 | size_t j; |
|---|
| 13 | int c, done=FALSE; |
|---|
| 14 | |
|---|
| 15 | #ifdef HGL |
|---|
| 16 | return; |
|---|
| 17 | #else |
|---|
| 18 | for (c=0; c<200 && !done; c++) |
|---|
| 19 | { |
|---|
| 20 | for (j=1; j<=aln->numgroups; j++) |
|---|
| 21 | { |
|---|
| 22 | done = FALSE; |
|---|
| 23 | for (i=0; i<aln->numelements; i++) |
|---|
| 24 | { |
|---|
| 25 | if (aln->element[i].groupid == j) |
|---|
| 26 | { |
|---|
| 27 | if (aln->element[i].groupf!=NULL || |
|---|
| 28 | aln->element[i].groupb!=NULL) |
|---|
| 29 | done = TRUE; |
|---|
| 30 | else |
|---|
| 31 | aln->element[i].groupid = 0; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | if (done == FALSE) |
|---|
| 35 | { |
|---|
| 36 | for (i=0; i<aln->numelements; i++) |
|---|
| 37 | if (aln->element[i].groupid == aln->numgroups) |
|---|
| 38 | aln->element[i].groupid = j; |
|---|
| 39 | aln->numgroups--; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | if (aln->numgroups == 0) |
|---|
| 43 | done = TRUE; |
|---|
| 44 | } |
|---|
| 45 | return; |
|---|
| 46 | #endif |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | static void StripSpecial(char *string) { |
|---|
| 50 | int j, len; |
|---|
| 51 | |
|---|
| 52 | len = strlen(string); |
|---|
| 53 | for (j=0; j<len; j++) |
|---|
| 54 | { |
|---|
| 55 | if (string[j] == '"') |
|---|
| 56 | string[j] = '`'; |
|---|
| 57 | else if (string[j] == '{') |
|---|
| 58 | string[j] = '('; |
|---|
| 59 | else if (string[j] == '}') |
|---|
| 60 | string[j] = ')'; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | static void RemoveQuotes(char *string) |
|---|
| 66 | { |
|---|
| 67 | int i, j, len; |
|---|
| 68 | |
|---|
| 69 | len = strlen(string); |
|---|
| 70 | for (j=0; j<len; j++) { |
|---|
| 71 | if (string[j] == '"') string[j] = ' '; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | for (j=0; string[j]==' ' && j<(int)strlen(string); j++) ; |
|---|
| 75 | |
|---|
| 76 | len = strlen(string); |
|---|
| 77 | for (i=0; i<len - j; i++) string[i] = string[i+j]; |
|---|
| 78 | |
|---|
| 79 | for (j=strlen(string)-1; j>=0 && (string[j]=='\n'||string[j]==' '); j--) { |
|---|
| 80 | string[j] = '\0'; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | static int OverWrite(NA_Sequence *thiss, NA_Alignment *aln) { |
|---|
| 86 | /* OverWrite(), overwrite all non-default data from a sequence entry |
|---|
| 87 | * onto any entry with the same ID or short name. |
|---|
| 88 | */ |
|---|
| 89 | size_t j; |
|---|
| 90 | int indx = -1; |
|---|
| 91 | NA_Sequence *that; |
|---|
| 92 | for (j=0; j<aln->numelements; j++) |
|---|
| 93 | { |
|---|
| 94 | if (Find2(thiss->id, aln->element[j].id) != -1) |
|---|
| 95 | if (Find2(aln->element[j].id, thiss->id) != -1) |
|---|
| 96 | indx = j; |
|---|
| 97 | } |
|---|
| 98 | if (indx == -1) |
|---|
| 99 | for (j=0; j<aln->numelements; j++) |
|---|
| 100 | { |
|---|
| 101 | if (Find2(thiss->short_name, aln->element[j].short_name) != -1) |
|---|
| 102 | if (Find2(aln->element[j].short_name, thiss->short_name) != -1) |
|---|
| 103 | indx = j; |
|---|
| 104 | } |
|---|
| 105 | if (indx != -1) |
|---|
| 106 | { |
|---|
| 107 | that = &(aln->element[indx]); |
|---|
| 108 | if (thiss->seq_name[0]) |
|---|
| 109 | strcpy(that->seq_name, thiss->seq_name); |
|---|
| 110 | if (thiss->barcode[0]) |
|---|
| 111 | strcpy(that->barcode, thiss->barcode); |
|---|
| 112 | if (thiss->contig[0]) |
|---|
| 113 | strcpy(that->contig, thiss->contig); |
|---|
| 114 | if (thiss->membrane[0]) |
|---|
| 115 | strcpy(that->membrane, thiss->membrane); |
|---|
| 116 | if (thiss->authority[0]) |
|---|
| 117 | strcpy(that->authority, thiss->authority); |
|---|
| 118 | if (thiss->short_name[0]) |
|---|
| 119 | strcpy(that->short_name, thiss->short_name); |
|---|
| 120 | if (thiss->description[0]) |
|---|
| 121 | strcpy(that->description, thiss->description); |
|---|
| 122 | if (thiss->sequence) |
|---|
| 123 | { |
|---|
| 124 | free(that->sequence); |
|---|
| 125 | that->sequence = thiss->sequence; |
|---|
| 126 | that->seqlen = thiss->seqlen; |
|---|
| 127 | that->seqmaxlen = thiss->seqmaxlen; |
|---|
| 128 | } |
|---|
| 129 | if (thiss->baggage) |
|---|
| 130 | { |
|---|
| 131 | that->baggage_len += thiss->baggage_len; |
|---|
| 132 | that->baggage_maxlen += thiss->baggage_maxlen; |
|---|
| 133 | if (that->baggage) |
|---|
| 134 | that->baggage = |
|---|
| 135 | Realloc(that->baggage, that->baggage_maxlen*sizeof(char)); |
|---|
| 136 | else |
|---|
| 137 | that->baggage = Calloc(that->baggage_maxlen, sizeof(char)); |
|---|
| 138 | strncat(that->baggage, thiss->baggage, that->baggage_maxlen); |
|---|
| 139 | } |
|---|
| 140 | if (thiss->comments) |
|---|
| 141 | { |
|---|
| 142 | that->comments_len += thiss->comments_len; |
|---|
| 143 | that->comments_maxlen += thiss->comments_maxlen; |
|---|
| 144 | if (that->comments) |
|---|
| 145 | that->comments = |
|---|
| 146 | Realloc(that->comments, that->comments_maxlen*sizeof(char)); |
|---|
| 147 | else |
|---|
| 148 | that->comments = Calloc(that->comments_maxlen, sizeof(char)); |
|---|
| 149 | strncat(that->comments, thiss->comments, that->comments_maxlen); |
|---|
| 150 | } |
|---|
| 151 | if (thiss->cmask) |
|---|
| 152 | { |
|---|
| 153 | free(that->cmask); |
|---|
| 154 | that->cmask = thiss->cmask; |
|---|
| 155 | } |
|---|
| 156 | if (thiss->offset != that->offset) |
|---|
| 157 | that->offset = thiss->offset; |
|---|
| 158 | if (thiss->attr != 0) |
|---|
| 159 | that->attr = thiss->attr; |
|---|
| 160 | if (thiss->groupid != 0) |
|---|
| 161 | { |
|---|
| 162 | that->groupid = thiss->groupid; |
|---|
| 163 | } |
|---|
| 164 | that->groupb = NULL; |
|---|
| 165 | that->groupf = NULL; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | return (indx); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void ReadGDE(char *filename, NA_Alignment *dataset) { |
|---|
| 172 | int done = FALSE; |
|---|
| 173 | size_t len = 0, j=0; |
|---|
| 174 | int success, temp = 0; |
|---|
| 175 | char in_line[GBUFSIZ]; |
|---|
| 176 | char *buffer, *line; |
|---|
| 177 | size_t buflen = GBUFSIZ; |
|---|
| 178 | int curelem = 0; |
|---|
| 179 | NA_Sequence *this_elem = NULL, temp_elem; |
|---|
| 180 | FILE *file; |
|---|
| 181 | |
|---|
| 182 | ErrorOut5(0!=(file = fopen(filename, "r")), "No such file"); |
|---|
| 183 | |
|---|
| 184 | for (; fgets(in_line, GBUFSIZ, file) != 0;) |
|---|
| 185 | { |
|---|
| 186 | for (line = in_line; line[0]==' ' || line[0] == '\t'; line++) ; |
|---|
| 187 | |
|---|
| 188 | if (Find2(line, "{")==0) |
|---|
| 189 | { |
|---|
| 190 | this_elem = &temp_elem; |
|---|
| 191 | InitNASeq(this_elem, DNA); |
|---|
| 192 | this_elem->offset = -(dataset->rel_offset); |
|---|
| 193 | } |
|---|
| 194 | else if (Find2(line, "type")==0) |
|---|
| 195 | { |
|---|
| 196 | if (Find(line, "DNA")) |
|---|
| 197 | { |
|---|
| 198 | this_elem->elementtype = DNA; |
|---|
| 199 | this_elem->tmatrix = Default_DNA_Trans; |
|---|
| 200 | this_elem->rmatrix = Default_NA_RTrans; |
|---|
| 201 | } |
|---|
| 202 | else if (Find(line, "RNA")) |
|---|
| 203 | { |
|---|
| 204 | this_elem->elementtype = RNA; |
|---|
| 205 | this_elem->tmatrix = Default_RNA_Trans; |
|---|
| 206 | this_elem->rmatrix = Default_NA_RTrans; |
|---|
| 207 | } |
|---|
| 208 | else if (Find(line, "MASK")) |
|---|
| 209 | { |
|---|
| 210 | this_elem->elementtype = MASK; |
|---|
| 211 | this_elem->rmatrix = NULL; |
|---|
| 212 | this_elem->tmatrix = NULL; |
|---|
| 213 | this_elem->col_lut = NULL; |
|---|
| 214 | } |
|---|
| 215 | else if (Find(line, "TEXT")) |
|---|
| 216 | { |
|---|
| 217 | this_elem->elementtype = TEXT; |
|---|
| 218 | this_elem->rmatrix = NULL; |
|---|
| 219 | this_elem->tmatrix = NULL; |
|---|
| 220 | this_elem->col_lut = NULL; |
|---|
| 221 | } |
|---|
| 222 | else if (Find(line, "PROT")) |
|---|
| 223 | { |
|---|
| 224 | this_elem->elementtype = PROTEIN; |
|---|
| 225 | this_elem->rmatrix = NULL; |
|---|
| 226 | this_elem->tmatrix = NULL; |
|---|
| 227 | this_elem->col_lut = Default_PROColor_LKUP; |
|---|
| 228 | } |
|---|
| 229 | /* |
|---|
| 230 | this_elem->attr = DEFAULT_X_ATTR; |
|---|
| 231 | */ |
|---|
| 232 | } |
|---|
| 233 | else if (Find2(line, "circular")==0) |
|---|
| 234 | { |
|---|
| 235 | sscanf(line, "%*s %d", &temp); |
|---|
| 236 | if (temp == 1) |
|---|
| 237 | { |
|---|
| 238 | this_elem->attr |= IS_CIRCULAR; |
|---|
| 239 | } |
|---|
| 240 | else |
|---|
| 241 | { |
|---|
| 242 | this_elem->attr &= ~IS_CIRCULAR; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | else if (Find2(line, "orig_direction")==0) |
|---|
| 246 | { |
|---|
| 247 | sscanf(line, "%*s %d", &temp); |
|---|
| 248 | if (temp == 1) |
|---|
| 249 | { |
|---|
| 250 | this_elem->attr |= IS_ORIG_5_TO_3; |
|---|
| 251 | this_elem->attr &= ~IS_ORIG_3_TO_5; |
|---|
| 252 | } |
|---|
| 253 | else |
|---|
| 254 | { |
|---|
| 255 | this_elem->attr |= IS_ORIG_3_TO_5; |
|---|
| 256 | this_elem->attr &= ~IS_ORIG_5_TO_3; |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | else if (Find2(line, "direction")==0) |
|---|
| 260 | { |
|---|
| 261 | sscanf(line, "%*s %d", &temp); |
|---|
| 262 | if (temp == 1) |
|---|
| 263 | { |
|---|
| 264 | this_elem->attr |= IS_5_TO_3; |
|---|
| 265 | this_elem->attr &= ~IS_3_TO_5; |
|---|
| 266 | } |
|---|
| 267 | else |
|---|
| 268 | { |
|---|
| 269 | this_elem->attr |= IS_3_TO_5; |
|---|
| 270 | this_elem->attr &= ~IS_5_TO_3; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | else if (Find2(line, "orig_strand")==0) |
|---|
| 274 | { |
|---|
| 275 | sscanf(line, "%*s %d", &temp); |
|---|
| 276 | if (temp == 1) |
|---|
| 277 | { |
|---|
| 278 | this_elem->attr |= IS_ORIG_PRIMARY; |
|---|
| 279 | this_elem->attr &= ~IS_ORIG_SECONDARY; |
|---|
| 280 | } |
|---|
| 281 | else |
|---|
| 282 | { |
|---|
| 283 | this_elem->attr |= IS_ORIG_SECONDARY; |
|---|
| 284 | this_elem->attr &= ~IS_ORIG_PRIMARY; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | else if (Find2(line, "strandedness")==0) |
|---|
| 288 | { |
|---|
| 289 | sscanf(line, "%*s %d", &temp); |
|---|
| 290 | if (temp == 1) |
|---|
| 291 | { |
|---|
| 292 | this_elem->attr |= IS_PRIMARY; |
|---|
| 293 | this_elem->attr &= ~IS_SECONDARY; |
|---|
| 294 | } |
|---|
| 295 | else |
|---|
| 296 | { |
|---|
| 297 | this_elem->attr |= IS_SECONDARY; |
|---|
| 298 | this_elem->attr &= ~IS_PRIMARY; |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | else if (Find2(line, "creator")==0) |
|---|
| 302 | { |
|---|
| 303 | sscanf(line, "%*s %[^\n]", this_elem->authority); |
|---|
| 304 | RemoveQuotes(this_elem->authority); |
|---|
| 305 | } |
|---|
| 306 | else if (Find2(line, "longname")==0) |
|---|
| 307 | { |
|---|
| 308 | sscanf(line, "%*s %[^\n]", this_elem->seq_name); |
|---|
| 309 | RemoveQuotes(this_elem->seq_name); |
|---|
| 310 | } |
|---|
| 311 | else if (Find2(line, "descrip")==0) |
|---|
| 312 | { |
|---|
| 313 | sscanf(line, "%*s %[^\n]", this_elem->description); |
|---|
| 314 | RemoveQuotes(this_elem->description); |
|---|
| 315 | } |
|---|
| 316 | else if (Find2(line, "name")==0) |
|---|
| 317 | { |
|---|
| 318 | sscanf(line, "%*s %[^\n]", this_elem->short_name); |
|---|
| 319 | RemoveQuotes(this_elem->short_name); |
|---|
| 320 | } |
|---|
| 321 | else if (Find2(line, "group-ID")==0) |
|---|
| 322 | { |
|---|
| 323 | sscanf(line, "%*s %zu", &(this_elem->groupid)); |
|---|
| 324 | dataset->numgroups = |
|---|
| 325 | MAX(this_elem->groupid, dataset->numgroups); |
|---|
| 326 | } |
|---|
| 327 | else if (Find2(line, "sequence-ID")==0) |
|---|
| 328 | { |
|---|
| 329 | sscanf(line, "%*s %[^\n]", this_elem->id); |
|---|
| 330 | RemoveQuotes(this_elem->id); |
|---|
| 331 | } |
|---|
| 332 | else if (Find2(line, "barcode")==0) |
|---|
| 333 | { |
|---|
| 334 | sscanf(line, "%*s %[^\n]", this_elem->barcode); |
|---|
| 335 | RemoveQuotes(this_elem->barcode); |
|---|
| 336 | } |
|---|
| 337 | else if (Find2(line, "membrane")==0) |
|---|
| 338 | { |
|---|
| 339 | sscanf(line, "%*s %[^\n]", this_elem->membrane); |
|---|
| 340 | RemoveQuotes(this_elem->membrane); |
|---|
| 341 | } |
|---|
| 342 | else if (Find2(line, "contig")==0) |
|---|
| 343 | { |
|---|
| 344 | sscanf(line, "%*s %[^\n]", this_elem->contig); |
|---|
| 345 | RemoveQuotes(this_elem->contig); |
|---|
| 346 | } |
|---|
| 347 | else if (Find2(line, "creation-date")==0) |
|---|
| 348 | { |
|---|
| 349 | sscanf(line, "%*s %2d%*c%2d%*c%2d%*c%2d%*c%2d%*c%2d\n", |
|---|
| 350 | &(this_elem->t_stamp.origin.mm), |
|---|
| 351 | &(this_elem->t_stamp.origin.dd), |
|---|
| 352 | &(this_elem->t_stamp.origin.yy), |
|---|
| 353 | &(this_elem->t_stamp.origin.hr), |
|---|
| 354 | &(this_elem->t_stamp.origin.mn), |
|---|
| 355 | &(this_elem->t_stamp.origin.sc)); |
|---|
| 356 | } |
|---|
| 357 | else if (Find2(line, "offset")==0) |
|---|
| 358 | { |
|---|
| 359 | sscanf(line, "%*s %d", &(this_elem->offset)); |
|---|
| 360 | this_elem->offset -= dataset->rel_offset; |
|---|
| 361 | } |
|---|
| 362 | else if (Find2(line, "comments")==0) |
|---|
| 363 | { |
|---|
| 364 | if (this_elem->comments_maxlen == 0) |
|---|
| 365 | buflen = 2048; |
|---|
| 366 | else |
|---|
| 367 | buflen = this_elem->comments_maxlen; |
|---|
| 368 | |
|---|
| 369 | done = FALSE; |
|---|
| 370 | len = this_elem->comments_len; |
|---|
| 371 | |
|---|
| 372 | for (; line[0] != '"'; line++) |
|---|
| 373 | if (line[0] == '\0') |
|---|
| 374 | ErrorOut5(0, "Error in input file"); |
|---|
| 375 | line++; |
|---|
| 376 | buffer = Calloc(buflen, sizeof(char)); |
|---|
| 377 | for (; !done;) |
|---|
| 378 | { |
|---|
| 379 | for (j=0; j<strlen(line); j++) |
|---|
| 380 | { |
|---|
| 381 | if (len+strlen(line) >= buflen) |
|---|
| 382 | { |
|---|
| 383 | buflen *= 2; |
|---|
| 384 | buffer = Realloc(buffer, |
|---|
| 385 | buflen*sizeof(char)); |
|---|
| 386 | } |
|---|
| 387 | if (line[j] == '"') done = TRUE; |
|---|
| 388 | |
|---|
| 389 | else |
|---|
| 390 | buffer[len++] = line[j]; |
|---|
| 391 | } |
|---|
| 392 | // Check pad with null |
|---|
| 393 | buffer[len] = '\0'; |
|---|
| 394 | if (!done) |
|---|
| 395 | { |
|---|
| 396 | if (fgets(in_line, GBUFSIZ, file) == 0) |
|---|
| 397 | done = TRUE; |
|---|
| 398 | line = in_line; |
|---|
| 399 | } |
|---|
| 400 | } |
|---|
| 401 | this_elem->comments = buffer; |
|---|
| 402 | this_elem->comments_len = strlen(buffer); |
|---|
| 403 | this_elem->comments_maxlen = buflen; |
|---|
| 404 | RemoveQuotes(this_elem->comments); |
|---|
| 405 | } |
|---|
| 406 | else if (Find2(line, "sequence")==0) |
|---|
| 407 | { |
|---|
| 408 | buflen = GBUFSIZ; |
|---|
| 409 | done = FALSE; |
|---|
| 410 | len = 0; |
|---|
| 411 | |
|---|
| 412 | buffer = Calloc(buflen, sizeof(char)); |
|---|
| 413 | for (; line[0] != '"'; line++) |
|---|
| 414 | if (line[0] == '\0') |
|---|
| 415 | ErrorOut5(0, "Error in input file"); |
|---|
| 416 | |
|---|
| 417 | line++; |
|---|
| 418 | for (; !done;) |
|---|
| 419 | { |
|---|
| 420 | for (j=0; j<strlen(line); j++) |
|---|
| 421 | { |
|---|
| 422 | if (len+strlen(line) >= buflen) |
|---|
| 423 | { |
|---|
| 424 | buflen *= 2; |
|---|
| 425 | buffer = Realloc(buffer, |
|---|
| 426 | buflen*sizeof(char)); |
|---|
| 427 | } |
|---|
| 428 | if (line[j] == '"') done = TRUE; |
|---|
| 429 | |
|---|
| 430 | else |
|---|
| 431 | { |
|---|
| 432 | // If not text, ignore spaces... |
|---|
| 433 | if (this_elem->elementtype != TEXT) |
|---|
| 434 | { |
|---|
| 435 | if (line[j]!=' ' && line[j] != |
|---|
| 436 | '\t' && line[j] != '\n') |
|---|
| 437 | buffer[len++] = line[j]; |
|---|
| 438 | } |
|---|
| 439 | else |
|---|
| 440 | if (line[j] != '\t' && line[j] != '\n') |
|---|
| 441 | buffer[len++] = line[j]; |
|---|
| 442 | } |
|---|
| 443 | } |
|---|
| 444 | if (!done) |
|---|
| 445 | { |
|---|
| 446 | if (fgets(in_line, GBUFSIZ, file) == 0) |
|---|
| 447 | done = TRUE; |
|---|
| 448 | line = in_line; |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | if (this_elem->rmatrix) { |
|---|
| 452 | for (j=0; j<len; j++) { |
|---|
| 453 | buffer[j]=this_elem->rmatrix[(unsigned char)buffer[j]]; |
|---|
| 454 | } |
|---|
| 455 | } |
|---|
| 456 | this_elem->sequence = (NA_Base*)buffer; |
|---|
| 457 | this_elem->seqlen = len; |
|---|
| 458 | this_elem->seqmaxlen = buflen; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | else if (Find2(line, "}")==0) |
|---|
| 462 | { |
|---|
| 463 | if (this_elem->id[0] == '\0') |
|---|
| 464 | strncpy_terminate(this_elem->id, uniqueID(), SIZE_ID); |
|---|
| 465 | if (this_elem->short_name[0] == '\0') |
|---|
| 466 | strncpy_terminate(this_elem->short_name, this_elem->id, SIZE_SHORT_NAME); |
|---|
| 467 | if (this_elem->seqlen == 0) |
|---|
| 468 | this_elem->protect = |
|---|
| 469 | PROT_BASE_CHANGES+ |
|---|
| 470 | PROT_GREY_SPACE+ |
|---|
| 471 | PROT_WHITE_SPACE+ |
|---|
| 472 | PROT_TRANSLATION; |
|---|
| 473 | |
|---|
| 474 | // Make a new sequence entry... |
|---|
| 475 | success = -1; |
|---|
| 476 | if (OVERWRITE) |
|---|
| 477 | success = OverWrite(this_elem, dataset); |
|---|
| 478 | |
|---|
| 479 | if (success == -1) |
|---|
| 480 | { |
|---|
| 481 | curelem = dataset->numelements++; |
|---|
| 482 | if (curelem == 0) |
|---|
| 483 | { |
|---|
| 484 | dataset->element=(NA_Sequence*) |
|---|
| 485 | Calloc(5, sizeof(NA_Sequence)); |
|---|
| 486 | dataset->maxnumelements = 5; |
|---|
| 487 | } |
|---|
| 488 | else if (curelem==dataset->maxnumelements) |
|---|
| 489 | { |
|---|
| 490 | (dataset->maxnumelements) *= 2; |
|---|
| 491 | dataset->element = (NA_Sequence*) |
|---|
| 492 | Realloc((char*)dataset->element, |
|---|
| 493 | dataset->maxnumelements * sizeof(NA_Sequence)); |
|---|
| 494 | } |
|---|
| 495 | dataset->element[curelem] = *this_elem; |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | else if (this_elem != NULL) |
|---|
| 499 | { |
|---|
| 500 | if (this_elem->baggage == NULL) |
|---|
| 501 | { |
|---|
| 502 | this_elem->baggage = strdup(line); |
|---|
| 503 | this_elem->baggage_maxlen = |
|---|
| 504 | this_elem->baggage_len = |
|---|
| 505 | strlen(this_elem->baggage)+1; |
|---|
| 506 | } |
|---|
| 507 | else |
|---|
| 508 | { |
|---|
| 509 | this_elem->baggage_len += strlen(line)+1; |
|---|
| 510 | this_elem->baggage = Realloc( |
|---|
| 511 | this_elem->baggage, this_elem->baggage_len * |
|---|
| 512 | sizeof(char)); |
|---|
| 513 | this_elem->baggage_maxlen = |
|---|
| 514 | this_elem->baggage_len; |
|---|
| 515 | |
|---|
| 516 | strncat(this_elem->baggage, line, GBUFSIZ); |
|---|
| 517 | } |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | fclose(file); |
|---|
| 522 | NormalizeOffset(dataset); |
|---|
| 523 | Regroup(dataset); |
|---|
| 524 | AdjustGroups(dataset); |
|---|
| 525 | return; |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | int WriteGDE(NA_Alignment *aln, char *filename, int method, int maskable) |
|---|
| 530 | { |
|---|
| 531 | int i; |
|---|
| 532 | size_t j; |
|---|
| 533 | int k, mask = -1; |
|---|
| 534 | FILE *file; |
|---|
| 535 | NA_Sequence *this_elem; |
|---|
| 536 | |
|---|
| 537 | if (aln == NULL) return (1); |
|---|
| 538 | |
|---|
| 539 | file = fopen(filename, "w"); |
|---|
| 540 | if (file == NULL) |
|---|
| 541 | { |
|---|
| 542 | Warning("Cannot open file for output"); |
|---|
| 543 | return (1); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | if (maskable && method != SELECT_REGION) |
|---|
| 547 | for (j=0; j<aln->numelements; j++) |
|---|
| 548 | if (aln->element[j].elementtype == MASK && |
|---|
| 549 | aln->element[j].selected) |
|---|
| 550 | mask = j; |
|---|
| 551 | |
|---|
| 552 | for (j=0; j<aln->numelements; j++) |
|---|
| 553 | { |
|---|
| 554 | if ((aln->element[j].selected && (int)j!=mask && method!=SELECT_REGION) |
|---|
| 555 | || (method == ALL) |
|---|
| 556 | || (aln->element[j].subselected && method == SELECT_REGION)) |
|---|
| 557 | { |
|---|
| 558 | this_elem = &(aln->element[j]); |
|---|
| 559 | fprintf(file, "{\n"); |
|---|
| 560 | if (this_elem->short_name[0]) |
|---|
| 561 | fprintf(file, "name \"%s\"\n", this_elem->short_name); |
|---|
| 562 | switch (this_elem->elementtype) |
|---|
| 563 | { |
|---|
| 564 | case DNA: |
|---|
| 565 | fprintf(file, "type \"DNA\"\n"); |
|---|
| 566 | break; |
|---|
| 567 | case RNA: |
|---|
| 568 | fprintf(file, "type \"RNA\"\n"); |
|---|
| 569 | break; |
|---|
| 570 | case PROTEIN: |
|---|
| 571 | fprintf(file, "type \"PROTEIN\"\n"); |
|---|
| 572 | break; |
|---|
| 573 | case MASK: |
|---|
| 574 | fprintf(file, "type \"MASK\"\n"); |
|---|
| 575 | break; |
|---|
| 576 | case TEXT: |
|---|
| 577 | fprintf(file, "type \"TEXT\"\n"); |
|---|
| 578 | break; |
|---|
| 579 | } |
|---|
| 580 | if (this_elem->seq_name[0]) |
|---|
| 581 | fprintf(file, "longname %s\n", this_elem->seq_name); |
|---|
| 582 | |
|---|
| 583 | if (this_elem->id[0]) |
|---|
| 584 | fprintf(file, "sequence-ID \"%s\"\n", this_elem->id); |
|---|
| 585 | RemoveQuotes(this_elem->barcode); |
|---|
| 586 | RemoveQuotes(this_elem->contig); |
|---|
| 587 | |
|---|
| 588 | if (this_elem->barcode[0]) |
|---|
| 589 | fprintf(file, "barcode \"%s\"\n", this_elem->barcode); |
|---|
| 590 | if (this_elem->membrane[0]) |
|---|
| 591 | fprintf(file, "membrane \"%s\"\n", this_elem->membrane); |
|---|
| 592 | if (this_elem->contig[0]) |
|---|
| 593 | fprintf(file, "contig \"%s\"\n", this_elem->contig); |
|---|
| 594 | if (this_elem->description[0]) |
|---|
| 595 | fprintf(file, "descrip \"%s\"\n", this_elem->description); |
|---|
| 596 | if (this_elem->authority[0]) |
|---|
| 597 | fprintf(file, "creator \"%s\"\n", this_elem->authority); |
|---|
| 598 | if (this_elem->groupid) |
|---|
| 599 | fprintf(file, "group-ID %zu\n", this_elem->groupid); |
|---|
| 600 | if (this_elem->offset+aln->rel_offset && method!=SELECT_REGION) |
|---|
| 601 | fprintf(file, "offset %d\n", this_elem->offset+aln->rel_offset); |
|---|
| 602 | if (method == SELECT_REGION) |
|---|
| 603 | { |
|---|
| 604 | /* If selecting a region, the offset should be moved to the first |
|---|
| 605 | * non-'0' space in the mask. |
|---|
| 606 | */ |
|---|
| 607 | for (k=this_elem->offset; k<aln->selection_mask_len && aln->selection_mask[k] == '0'; k++) ; |
|---|
| 608 | fprintf(file, "offset %d\n", aln->rel_offset+k); |
|---|
| 609 | } |
|---|
| 610 | if (this_elem->t_stamp.origin.mm != 0) |
|---|
| 611 | fprintf(file, |
|---|
| 612 | "creation-date %2d/%2d/%2d %2d:%2d:%2d\n", |
|---|
| 613 | this_elem->t_stamp.origin.mm, |
|---|
| 614 | this_elem->t_stamp.origin.dd, |
|---|
| 615 | (this_elem->t_stamp.origin.yy)>1900 ? |
|---|
| 616 | (this_elem->t_stamp.origin.yy-1900) : |
|---|
| 617 | (this_elem->t_stamp.origin.yy), |
|---|
| 618 | this_elem->t_stamp.origin.hr, |
|---|
| 619 | this_elem->t_stamp.origin.mn, |
|---|
| 620 | this_elem->t_stamp.origin.sc); |
|---|
| 621 | if ((this_elem->attr & IS_ORIG_5_TO_3) && |
|---|
| 622 | ((this_elem->attr & IS_ORIG_3_TO_5) == 0)) |
|---|
| 623 | fprintf(file, "orig_direction 1\n"); |
|---|
| 624 | |
|---|
| 625 | if ((this_elem->attr & IS_CIRCULAR)) |
|---|
| 626 | fprintf(file, "circular 1\n"); |
|---|
| 627 | |
|---|
| 628 | if ((this_elem->attr & IS_5_TO_3) && |
|---|
| 629 | ((this_elem->attr & IS_3_TO_5) == 0)) |
|---|
| 630 | fprintf(file, "direction 1\n"); |
|---|
| 631 | |
|---|
| 632 | if ((this_elem->attr & IS_ORIG_3_TO_5) && |
|---|
| 633 | ((this_elem->attr & IS_ORIG_5_TO_3) == 0)) |
|---|
| 634 | fprintf(file, "orig_direction -1\n"); |
|---|
| 635 | |
|---|
| 636 | if ((this_elem->attr & IS_3_TO_5) && |
|---|
| 637 | ((this_elem->attr & IS_5_TO_3) == 0)) |
|---|
| 638 | fprintf(file, "direction -1\n"); |
|---|
| 639 | |
|---|
| 640 | if ((this_elem->attr & IS_ORIG_PRIMARY) && |
|---|
| 641 | ((this_elem->attr & IS_ORIG_SECONDARY) == 0)) |
|---|
| 642 | fprintf(file, "orig_strand 1\n"); |
|---|
| 643 | |
|---|
| 644 | if ((this_elem->attr & IS_PRIMARY) && |
|---|
| 645 | ((this_elem->attr & IS_SECONDARY) == 0)) |
|---|
| 646 | fprintf(file, "strandedness 1\n"); |
|---|
| 647 | |
|---|
| 648 | if (((this_elem->attr & IS_ORIG_PRIMARY) == 0) && |
|---|
| 649 | (this_elem->attr & IS_ORIG_SECONDARY)) |
|---|
| 650 | fprintf(file, "orig_strand 2\n"); |
|---|
| 651 | |
|---|
| 652 | if (((this_elem->attr & IS_PRIMARY) == 0) && |
|---|
| 653 | (this_elem->attr & IS_SECONDARY)) |
|---|
| 654 | fprintf(file, "strandedness 2\n"); |
|---|
| 655 | |
|---|
| 656 | if (this_elem->comments != NULL) |
|---|
| 657 | { |
|---|
| 658 | StripSpecial(this_elem->comments); |
|---|
| 659 | fprintf(file, "comments \"%s\"\n", this_elem->comments); |
|---|
| 660 | } |
|---|
| 661 | if (this_elem->baggage != NULL) |
|---|
| 662 | { |
|---|
| 663 | if (this_elem-> |
|---|
| 664 | baggage[strlen(this_elem->baggage)-1] == '\n') |
|---|
| 665 | fprintf(file, "%s", this_elem->baggage); |
|---|
| 666 | else |
|---|
| 667 | fprintf(file, "%s\n", this_elem->baggage); |
|---|
| 668 | } |
|---|
| 669 | fprintf(file, "sequence \""); |
|---|
| 670 | if (this_elem->tmatrix) |
|---|
| 671 | { |
|---|
| 672 | if (mask == -1) |
|---|
| 673 | { |
|---|
| 674 | for (k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 675 | { |
|---|
| 676 | if (k%60 == 0) |
|---|
| 677 | putc('\n', file); |
|---|
| 678 | if (method == SELECT_REGION) |
|---|
| 679 | { |
|---|
| 680 | if (aln->selection_mask[k] == '1') |
|---|
| 681 | putc(this_elem->tmatrix[getelem(this_elem, k)], |
|---|
| 682 | file); |
|---|
| 683 | } |
|---|
| 684 | else |
|---|
| 685 | putc(this_elem->tmatrix[getelem(this_elem, k)], |
|---|
| 686 | file); |
|---|
| 687 | } |
|---|
| 688 | } |
|---|
| 689 | else |
|---|
| 690 | { |
|---|
| 691 | for (i=0, k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 692 | if (aln->element[mask].seqlen+this_elem->offset>k) |
|---|
| 693 | if ((char)getelem(&(aln->element[mask]), k) != '0' |
|---|
| 694 | && ((char)getelem(&(aln->element[mask]), k) != '-')) |
|---|
| 695 | { |
|---|
| 696 | if (i%60 == 0) |
|---|
| 697 | putc('\n', file); |
|---|
| 698 | putc(this_elem->tmatrix[getelem(this_elem, k)], |
|---|
| 699 | file); |
|---|
| 700 | i++; |
|---|
| 701 | } |
|---|
| 702 | } |
|---|
| 703 | fprintf(file, "\"\n"); |
|---|
| 704 | } |
|---|
| 705 | else |
|---|
| 706 | { |
|---|
| 707 | if (mask == -1) |
|---|
| 708 | { |
|---|
| 709 | for (k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 710 | { |
|---|
| 711 | if (k%60 == 0) |
|---|
| 712 | putc('\n', file); |
|---|
| 713 | if (method == SELECT_REGION) |
|---|
| 714 | { |
|---|
| 715 | if (aln->selection_mask[k] == '1') |
|---|
| 716 | putc(getelem(this_elem, k), file); |
|---|
| 717 | } |
|---|
| 718 | else |
|---|
| 719 | putc(getelem(this_elem, k), file); |
|---|
| 720 | } |
|---|
| 721 | } |
|---|
| 722 | else |
|---|
| 723 | { |
|---|
| 724 | for (i=0, k=this_elem->offset; k<this_elem->seqlen+this_elem->offset; k++) |
|---|
| 725 | if (((aln->element[mask].seqlen)+(aln->element[mask]. |
|---|
| 726 | offset)) > k) |
|---|
| 727 | if ((char)getelem(&(aln->element[mask]), k) == '1') |
|---|
| 728 | { |
|---|
| 729 | if (i%60 == 0) |
|---|
| 730 | putc('\n', file); |
|---|
| 731 | putc(getelem(this_elem, k), file); |
|---|
| 732 | i++; |
|---|
| 733 | } |
|---|
| 734 | } |
|---|
| 735 | fprintf(file, "\"\n"); |
|---|
| 736 | } |
|---|
| 737 | fprintf(file, "}\n"); |
|---|
| 738 | } |
|---|
| 739 | } |
|---|
| 740 | fclose(file); |
|---|
| 741 | return (0); |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | void SeqNorm(NA_Sequence *seq) |
|---|
| 747 | { |
|---|
| 748 | // Normalize seq (remove leading indels in the sequence; |
|---|
| 749 | int len, j, shift_width, trailer; |
|---|
| 750 | char *sequence; |
|---|
| 751 | len = seq->seqlen; |
|---|
| 752 | |
|---|
| 753 | sequence = (char*)seq->sequence; |
|---|
| 754 | |
|---|
| 755 | if (len == 0) return; |
|---|
| 756 | |
|---|
| 757 | if (seq->tmatrix) { |
|---|
| 758 | for (shift_width=0; (shift_width<len) && ((sequence[shift_width]&15) == '\0'); shift_width++) ; |
|---|
| 759 | } |
|---|
| 760 | else { |
|---|
| 761 | for (shift_width=0; (shift_width<len) && (sequence[shift_width] == '-'); shift_width++) ; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | for (j=0; j<len-shift_width; j++) { |
|---|
| 765 | sequence[j] = sequence[j+shift_width]; |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | seq->seqlen -= shift_width; |
|---|
| 769 | seq->offset += shift_width; |
|---|
| 770 | for (trailer=seq->seqlen-1; |
|---|
| 771 | (sequence[trailer] == '-' || sequence[trailer] == '\0') && trailer>=0; |
|---|
| 772 | trailer--) |
|---|
| 773 | { |
|---|
| 774 | sequence[trailer] = '\0'; |
|---|
| 775 | } |
|---|
| 776 | seq->seqlen = trailer+1; |
|---|
| 777 | return; |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | /* ALWAYS COPY the result from uniqueID() to a char[32], |
|---|
| 781 | * (strlen(hostname)+1+10). Memory is lost when the function |
|---|
| 782 | * is finished. |
|---|
| 783 | */ |
|---|
| 784 | |
|---|
| 785 | char *uniqueID() |
|---|
| 786 | { |
|---|
| 787 | static char vname[32]; |
|---|
| 788 | time_t *tp; |
|---|
| 789 | static int cnt = 0; |
|---|
| 790 | |
|---|
| 791 | tp = (time_t *)Calloc(1, sizeof(time_t)); |
|---|
| 792 | |
|---|
| 793 | time(tp); |
|---|
| 794 | sprintf(vname, "host:%d:%ld", cnt, *tp); |
|---|
| 795 | cnt++; |
|---|
| 796 | Cfree((char*)tp); |
|---|
| 797 | return (vname); |
|---|
| 798 | } |
|---|