forked from fhormoz/sirFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefGenome.c
181 lines (161 loc) · 4.19 KB
/
RefGenome.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (c) <2008 - 2020>, University of Washington, Simon Fraser University, Bilkent University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
* - Neither the names of the University of Washington, Simon Fraser University,
* nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Authors:
Farhad Hormozdiari
Faraz Hach
Can Alkan
Emails:
farhadh AT uw DOT edu
fhach AT cs DOT sfu DOT ca
calkan AT cs DOT bilkent DOT edu DOT tr
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Common.h"
#include "RefGenome.h"
FILE *_rg_fp;
char *_rg_gen;
char *_rg_name;
int _rg_offset;
int _rg_contGen;
void getPosRefGenome(fpos_t * position_ig) {
fgetpos(_rg_fp, position_ig);
}
void setPosRefGenome(fpos_t * position_ig) {
fsetpos(_rg_fp, position_ig);
}
/**********************************************/
int initLoadingRefGenome(char *fileName)
{
char ch;
_rg_fp = fileOpen (fileName, "r");
if (fscanf(_rg_fp, "%c", &ch))
{
if (ch == '>')
{
_rg_contGen = 0;
_rg_offset = 0;
_rg_gen = getMem(CONTIG_MAX_SIZE);
_rg_name = getMem(CONTIG_NAME_SIZE);
return 1;
}
}
return 0;
}
/**********************************************/
void finalizeLoadingRefGenome()
{
freeMem(_rg_gen, CONTIG_MAX_SIZE);
freeMem(_rg_name, CONTIG_NAME_SIZE);
fclose(_rg_fp);
}
/**********************************************/
int loadRefGenome(char **refGen, char **refGenName, int *refGenOff)
{
char ch;
int i;
int returnVal = 0;
int actualSize=0;
int size;
char *tmp;
// New Contig
if (!_rg_contGen)
{
size = 0;
tmp = fgets(_rg_name, SEQ_MAX_LENGTH, _rg_fp);
if (tmp==NULL)
fprintf(stderr, "Error reading the contig.\n");
int k;
for (k=0; k<strlen(_rg_name);k++)
{
if (_rg_name[k] == ' ')
{
_rg_name[k]='\0';
break;
}
}
}
else
{
size=strlen(_rg_gen);
for( i = 0 ; i < CONTIG_OVERLAP ; i++ )
{
_rg_gen[i] = _rg_gen[size-CONTIG_OVERLAP+i];
if (_rg_gen[i] != 'N')
actualSize++;
}
size = CONTIG_OVERLAP;
}
while( fscanf(_rg_fp, "%c", &ch) > 0 )
{
if (ch == '>')
{
_rg_contGen = 0;
returnVal = 1;
break;
}
else if (!isspace(ch))
{
ch = toupper(ch);
_rg_gen[size++] = ch;
if (ch != 'N')
{
actualSize++;
}
if (actualSize == CONTIG_SIZE || size == CONTIG_MAX_SIZE)
{
_rg_contGen = 1;
returnVal=1;
break;
}
}
}
_rg_gen[size] = '\0';
for (i=strlen(_rg_name)-1; i >= 0; i--)
if (!isspace(_rg_name[i]))
break;
_rg_name[i+1] = '\0';
*refGenOff = _rg_offset;
*refGenName = _rg_name;
*refGen = _rg_gen;
if (_rg_contGen == 1)
{
_rg_offset += size-CONTIG_OVERLAP;
}
else
{
_rg_offset = 0;
}
return returnVal;
}
/**********************************************/