-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.c
133 lines (122 loc) · 2.46 KB
/
encode.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @file encode.c
* @Author Jonathan Zernik
* @date 2012
* @brief Read a FASTA file and
* encode or decode it.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bwt.h"
#include "encode.h"
int main(int argc, char *argv[])
{
if(!((argc == 4) && ((strcmp(argv[1], "-bwt") == 0) || (strcmp(argv[1], "-ibwt") == 0))))
{
printf("usage:\n");
printf("%s -bwt [inputfile] [outputfile]\n", argv[0]);
printf("%s -ibwt [inputfile] [outputfile]\n", argv[0]);
}
else
{
FILE *infile = fopen(argv[2], "r");
FILE *outfile = fopen(argv[3], "w");
if(infile == 0 || outfile == 0)
{
printf("Could not open file\n");
}
else
{
char *sequence = create_empty_str();
char line[100];
while((fgets(line, 100, infile) != NULL) && (line[0] != '>'))
{
}
while(fgets(line, 100, infile) != NULL)
{
if(line[strlen(line)-1] == '\n')
{
line[strlen(line)-1] = '\0';
}
sequence = extend_str(sequence, line);
}
char out[strlen(sequence)+1];
if(strcmp(argv[1], "-bwt") == 0)
{
encode_bwt(out, sequence, strlen(sequence));
write_fasta_file(outfile, "BWT", out);
}
else if(strcmp(argv[1], "-ibwt") == 0)
{
decode_bwt(out, sequence, strlen(sequence));
write_fasta_file(outfile, "iBWT", out);
}
free_str(sequence);
fclose(infile);
fclose(outfile);
}
}
return 0;
}
/*
* Allocate an empty string on
* the heap
*/
char *create_empty_str()
{
char *str;
if ((str = malloc(1*(sizeof(char)))) == NULL) {
printf("\nout of memory.\n");
}
str[0] = '\0';
return str;
}
/*
* Extend a string that is allocated
* on the heap
*/
char *extend_str(char *str, char *new_str)
{
int new_len = strlen(str) + strlen(new_str) + 1;
if ((str = realloc(str, new_len*(sizeof(char)))) == NULL) {
printf("\nout of memory.\n");
}
strcat(str, new_str);
return str;
}
/*
* Deallocate a string
* on the heap
*/
void free_str(char *str)
{
free(str);
}
/*
* Write a fasta file given a
* name and sequence data
*/
void write_fasta_file(FILE *fp, char *name, char *content)
{
int i, count;
fputc('>', fp);
for(i = 0; i < strlen(name); i++)
{
fputc(name[i], fp);
}
fputc('\n', fp);
i = 0;
count = 0;
for(i = 0; i < strlen(content); i++)
{
if(count == 80)
{
fputc('\n', fp);
count = 0;
}
fputc(content[i], fp);
count++;
}
fputc('\n', fp);
}