-
Notifications
You must be signed in to change notification settings - Fork 25
/
vcfReader.c
99 lines (83 loc) · 2.77 KB
/
vcfReader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright [1999-2017] EMBL-European Bioinformatics Institute
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <string.h>
#include "wiggleIterator.h"
typedef struct bedReaderData_st {
char *filename;
FILE * file;
char * chrom;
int stop;
} VcfReaderData;
void VcfReaderPop(WiggleIterator * wi) {
VcfReaderData * data = (VcfReaderData *) wi->data;
char line[5000];
char chrom[1000];
if (wi->done)
return;
while (fgets(line, 5000, data->file)) {
if (line[0] != '#') {
sscanf(line, "%s\t%i", chrom, &wi->start);
wi->finish = wi->start + 1;
// The reason for creating a new string instead of simply
// overwriting is that other functions may still be pointin
// at the old label
if (wi->chrom[0] == '\0' || strcmp(wi->chrom, chrom)) {
wi->chrom = (char *) calloc(strlen(chrom), sizeof(char));
strcpy(wi->chrom, chrom);
}
if (data->stop > 0) {
if ((wi->start >= data->stop && strcmp(wi->chrom, data->chrom) == 0) || strcmp(wi->chrom, data->chrom) > 0) {
wi->done = true;
return;
} else if (wi->finish > data->stop) {
wi->finish = data->stop;
}
}
return;
}
}
fclose(data->file);
data->file = NULL;
wi->done = true;
}
void VcfReaderSeek(WiggleIterator * wi, const char * chrom, int start, int finish) {
VcfReaderData * data = (VcfReaderData*) wi->data;
data->stop = finish;
data->chrom = chrom;
if (wi->done || strcmp(chrom, wi->chrom) < 0 || (strcmp(chrom, wi->chrom) == 0 && start < wi->start)) {
if (data->file)
fclose(data->file);
if (!(data->file = fopen(data->filename, "r"))) {
fprintf(stderr, "Could not open input file %s\n", data->filename);
exit(1);
}
wi->done = false;
pop(wi);
}
while (!wi->done && (strcmp(wi->chrom, chrom) < 0 || (strcmp(chrom, wi->chrom) == 0 && wi->finish < start)))
pop(wi);
if (!wi->done && strcmp(chrom, wi->chrom) == 0 && wi->start < start)
wi->start = start;
}
WiggleIterator * VcfReader(char * filename) {
VcfReaderData * data = (VcfReaderData *) calloc(1, sizeof(VcfReaderData));
data->filename = filename;
data->stop = -1;
if (!(data->file = fopen(filename, "r"))) {
fprintf(stderr, "Could not open bed file %s\n", filename);
exit(1);
}
return newWiggleIteratorChromName(data, &VcfReaderPop, &VcfReaderSeek, 0, true);
}