1 static char rcsid[] = "$Id: reader.c 218153 2019-01-17 05:38:29Z twu $";
2 #ifdef HAVE_CONFIG_H
3 #include <config.h>
4 #endif
5 
6 #include "reader.h"
7 #include "mem.h"
8 
9 #ifdef DEBUG
10 #define debug(x) x
11 #else
12 #define debug(x)
13 #endif
14 
15 #ifdef DEBUG9
16 #define debug9(x) x
17 #else
18 #define debug9(x)
19 #endif
20 
21 
22 #define T Reader_T
23 struct T {
24   int querystart;
25   int queryend;
26   int querystart_save;
27   int queryend_save;
28 
29   char *startinit;
30   char *startptr;
31   char *endptr;
32 
33   char *startbound;		/* Saved for reset */
34   char *endbound;
35 };
36 
37 int
Reader_querystart(T this)38 Reader_querystart (T this) {
39   return this->querystart;
40 }
41 
42 int
Reader_queryend(T this)43 Reader_queryend (T this) {
44   return this->queryend;
45 }
46 
47 /* Same as current querypos + oligosize */
48 int
Reader_startpos(T this)49 Reader_startpos (T this) {
50   return (this->startptr - this->startinit);
51 }
52 
53 int
Reader_endpos(T this)54 Reader_endpos (T this) {
55   return (this->endptr - this->startinit);
56 }
57 
58 
59 void
Reader_reset_start(T this,int querypos)60 Reader_reset_start (T this, int querypos) {
61   char *sequence;
62 
63   sequence = this->startinit;
64   this->startptr = &(sequence[querypos]);
65   return;
66 }
67 
68 void
Reader_reset_end(T this,int querypos)69 Reader_reset_end (T this, int querypos) {
70   char *sequence;
71 
72   sequence = this->startinit;
73   this->endptr = &(sequence[querypos]);
74   return;
75 }
76 
77 
78 void
Reader_reset_ends(T this)79 Reader_reset_ends (T this) {
80   /*
81   char *sequence;
82   sequence = this->startinit;
83   this->startptr = &(sequence[this->querystart]);
84   this->endptr = &(sequence[this->queryend-1]);
85   */
86 
87   this->querystart = this->querystart_save;
88   this->queryend = this->queryend_save;
89 
90   this->startptr = this->startbound;
91   this->endptr = this->endbound;
92 
93   return;
94 }
95 
96 
97 
98 T
Reader_new(char * sequence,int querystart,int queryend)99 Reader_new (char *sequence, int querystart, int queryend) {
100   T new = (T) MALLOC(sizeof(*new));
101 
102   new->querystart_save = new->querystart = querystart;
103   new->queryend_save = new->queryend = queryend;
104 
105   new->startinit = sequence;
106   new->startbound = new->startptr = &(sequence[querystart]);
107   new->endbound = new->endptr = &(sequence[queryend-1]);
108   return new;
109 }
110 
111 void
Reader_free(T * old)112 Reader_free (T *old) {
113   if (*old) {
114     FREE(*old);
115   }
116   return;
117 }
118 
119 #ifndef GSNAP
120 char
Reader_getc(T this,cDNAEnd_T cdnaend,int blocksize)121 Reader_getc (T this, cDNAEnd_T cdnaend, int blocksize) {
122   debug(fprintf(stderr,"Read_getc has startptr %d and endptr %d\n",
123 		this->startptr-this->startinit,this->endptr-this->startinit));
124   if (this->startptr - this->endptr >= blocksize) {
125     return '\0';
126   } else if (cdnaend == FIVE) {
127     return *(this->startptr++);
128   } else {
129     return *(this->endptr--);
130   }
131 }
132 #endif
133 
134 char
Reader_getc_5(T this)135 Reader_getc_5 (T this) {
136   debug(printf("Read_getc has startptr %d and endptr %d\n",
137 	       this->startptr - this->startinit,this->endptr - this->startinit));
138   if (this->startptr > this->endbound) {
139     return '\0';
140   } else {
141     return *(this->startptr++);
142   }
143 }
144 
145 char
Reader_getc_3(T this)146 Reader_getc_3 (T this) {
147   debug(printf("Read_getc has startptr %d and endptr %d\n",
148 	       this->startptr - this->startinit,this->endptr - this->startinit));
149   if (this->endptr < this->startbound) {
150     return '\0';
151   } else {
152     return *(this->endptr--);
153   }
154 }
155 
156 
157 #if 0
158 void
159 Reader_set_5 (T this, int querypos, int oligosize) {
160   this->startptr = this->startinit + querypos;
161   debug(printf("Reader_set now has startpos %d\n",this->startptr - this->startinit));
162   return;
163 }
164 #endif
165 
166 #if 0
167 void
168 Reader_set_3 (T this, int querypos, int oligosize) {
169   this->endptr = this->startinit + querypos + oligosize - 1;
170   debug(printf("Reader_set now has endpos %d\n",this->endptr - this->startinit));
171   return;
172 }
173 #endif
174 
175 #if 0
176 void
177 Reader_skip_5 (T this, int nskip) {
178   this->startptr += nskip;
179   return;
180 }
181 #endif
182 
183 #if 0
184 void
185 Reader_skip_3 (T this, int nskip) {
186   this->endptr -= nskip;
187   return;
188 }
189 #endif
190 
191 
192 #if 0
193 /* For debugging */
194 static Oligospace_T
195 nt_oligo (char *query, int indexsize) {
196   Oligospace_T oligo = 0U;
197   int i;
198 
199   debug9(printf("Reader_check: "));
200   for (i = 0; i < indexsize; i++) {
201     oligo *= 4;
202 
203     debug9(printf("%c",query[i]));
204     switch (query[i]) {
205     case 'A': break;
206     case 'C': oligo += 1; break;
207     case 'G': oligo += 2; break;
208     case 'T': oligo += 3; break;
209     default:
210       fprintf(stderr,"Saw N in nt_oligo\n");
211       abort();
212     }
213   }
214   debug9(printf(" => oligo %016lX\n",oligo));
215 
216   return oligo;
217 }
218 
219 Oligospace_T
220 Reader_check (T this, int querypos, int indexsize) {
221 
222   debug9(printf("Read: %s\n",this->startinit));
223   return nt_oligo(&(this->startinit[querypos]),indexsize);
224 }
225 #endif
226 
227 
228 /* For testing */
229 /*
230 static void
231 process_input (FILE *input) {
232   bool ssfile;
233   int queryno = 0, seqlength, skiplength;
234   char *query, initc;
235 
236   if ((initc = Reader_input_init(input)) == '>') {
237     ssfile = false;
238     query = Reader_input_header(input);
239   } else {
240     ssfile = true;
241     query = (char *) CALLOC(strlen("NO_HEADER")+1,sizeof(char));
242     strcpy(query,"NO_HEADER");
243   }
244   fprintf(stderr,"Read sequence %s\n",query);
245   Reader_input_sequence(&seqlength,&skiplength,input,initc);
246   print_contents(&(Sequence[0]),SEQUENCELEN);
247   fprintf(stderr,"\nSeqlength = %d\n",seqlength);
248   FREE(query);
249   queryno++;
250 
251   while ((query = Reader_input_header(input)) != NULL) {
252     fprintf(stderr,"Read sequence %s\n",query);
253     Reader_input_sequence(&seqlength,&skiplength,input,initc);
254     print_contents(&(Sequence[0]),SEQUENCELEN);
255     fprintf(stderr,"\nSeqlength = %d\n",seqlength);
256     FREE(query);
257     queryno++;
258   }
259 
260   return;
261 }
262 
263 int
264 main (int argc, char *argv[]) {
265   process_input(stdin);
266   return 0;
267 }
268 */
269