1 /*
2 Copyright 2007, 2008 Daniel Zerbino (zerbino@ebi.ac.uk)
3 
4     This file is part of Velvet.
5 
6     Velvet is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     Velvet is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with Velvet; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 */
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 
25 #include "globals.h"
26 #include "graph.h"
27 #include "recycleBin.h"
28 #include "tightString.h"
29 #include "roadMap.h"
30 #include "utility.h"
31 #include "kmer.h"
32 
33 #ifndef NULL
34 #define NULL 0
35 #endif
36 
37 union positionPtr {
38 	Coordinate coord;
39 	IDnum nodeID;
40 };
41 
42 struct annotation_st {
43 	Coordinate position;	// 32
44 	union positionPtr start;	// 32
45 	union positionPtr finish;	// 32
46 	Coordinate length;	// 32
47 	IDnum sequenceID;	// 32
48 };
49 
50 struct roadmap_st {
51 	IDnum annotationCount;
52 };
53 
54 // Creates empty RoadMap
newRoadMap()55 RoadMap *newRoadMap()
56 {
57 	return callocOrExit(1, RoadMap);
58 }
59 
getAnnotationCount(RoadMap * rdmap)60 IDnum getAnnotationCount(RoadMap * rdmap)
61 {
62 	return rdmap->annotationCount;
63 }
64 
getFinish(Annotation * annot)65 Coordinate getFinish(Annotation * annot)
66 {
67 	return annot->finish.coord;
68 }
69 
getAnnotSequenceID(Annotation * annot)70 IDnum getAnnotSequenceID(Annotation * annot)
71 {
72 	return annot->sequenceID;
73 }
74 
getStart(Annotation * annot)75 Coordinate getStart(Annotation * annot)
76 {
77 	return annot->start.coord;
78 }
79 
getPosition(Annotation * annot)80 Coordinate getPosition(Annotation * annot)
81 {
82 	return annot->position;
83 }
84 
getAnnotationLength(Annotation * annot)85 Coordinate getAnnotationLength(Annotation * annot)
86 {
87 	if (annot == NULL)
88 		return 0;
89 
90 	return annot->length;
91 }
92 
93 // Imports roadmap from the appropriate file format
94 // Memory allocated within the function
importRoadMapArray(char * filename)95 RoadMapArray *importRoadMapArray(char *filename)
96 {
97 	FILE *file;
98 	const int maxline = 100;
99 	char *line = mallocOrExit(maxline, char);
100 	RoadMap *array;
101 	RoadMap *rdmap = NULL;
102 	IDnum rdmapIndex = 0;
103 	IDnum seqID;
104 	Coordinate position, start, finish;
105 	Annotation *nextAnnotation;
106 	RoadMapArray *result = mallocOrExit(1, RoadMapArray);
107 	IDnum sequenceCount;
108 	IDnum annotationCount = 0;
109 	short short_var;
110 	long long_var;
111 	long long longlong_var, longlong_var2, longlong_var3;
112 
113 	printf("Reading roadmap file %s\n", filename);
114 
115 	file = fopen(filename, "r");
116 	if (!fgets(line, maxline, file))
117 		exitErrorf(EXIT_FAILURE, true, "%s incomplete.", filename);
118 	sscanf(line, "%ld\t%i\t%hi\n", &long_var, &(result->WORDLENGTH), &short_var);
119 	sequenceCount = (IDnum) long_var;
120 	resetWordFilter(result->WORDLENGTH);
121 	result->length = sequenceCount;
122 	array = mallocOrExit(sequenceCount, RoadMap);
123 	result->array = array;
124 	result->double_strand = (boolean) short_var;
125 
126 	while (fgets(line, maxline, file) != NULL)
127 		if (line[0] != 'R')
128 			annotationCount++;
129 
130 	result->annotations = callocOrExit(annotationCount, Annotation);
131 	nextAnnotation = result->annotations;
132 	fclose(file);
133 
134 	file = fopen(filename, "r");
135 
136 	if (!fgets(line, maxline, file))
137 		exitErrorf(EXIT_FAILURE, true, "%s incomplete.", filename);
138 	while (fgets(line, maxline, file) != NULL) {
139 		if (line[0] == 'R') {
140 			rdmap = getRoadMapInArray(result, rdmapIndex++);
141 			rdmap->annotationCount = 0;
142 		} else {
143 			sscanf(line, "%ld\t%lld\t%lld\t%lld\n", &long_var,
144 			       &longlong_var, &longlong_var2, &longlong_var3);
145 			seqID = (IDnum) long_var;
146 			position = (Coordinate) longlong_var;
147 			start = (Coordinate) longlong_var2;
148 			finish = (Coordinate) longlong_var3;
149 			nextAnnotation->sequenceID = seqID;
150 			nextAnnotation->position = position;
151 			nextAnnotation->start.coord = start;
152 			nextAnnotation->finish.coord = finish;
153 
154 			if (seqID > 0)
155 				nextAnnotation->length = finish - start;
156 			else
157 				nextAnnotation->length = start - finish;
158 
159 
160 			rdmap->annotationCount++;
161 			nextAnnotation++;
162 		}
163 	}
164 
165 	printf("%d roadmaps reads\n", rdmapIndex);
166 
167 	fclose(file);
168 	free(line);
169 	return result;
170 }
171 
getRoadMapInArray(RoadMapArray * array,IDnum index)172 RoadMap *getRoadMapInArray(RoadMapArray * array, IDnum index)
173 {
174 	return &(array->array[index]);
175 }
176 
setStartID(Annotation * annot,IDnum nodeID)177 void setStartID(Annotation * annot, IDnum nodeID)
178 {
179 	annot->start.nodeID = nodeID;
180 }
181 
setFinishID(Annotation * annot,IDnum nodeID)182 void setFinishID(Annotation * annot, IDnum nodeID)
183 {
184 	annot->finish.nodeID = nodeID;
185 }
186 
getStartID(Annotation * annot)187 IDnum getStartID(Annotation * annot)
188 {
189 	return annot->start.nodeID;
190 }
191 
getFinishID(Annotation * annot)192 IDnum getFinishID(Annotation * annot)
193 {
194 	return annot->finish.nodeID;
195 }
196 
incrementAnnotationCoordinates(Annotation * annot)197 void incrementAnnotationCoordinates(Annotation * annot)
198 {
199 	annot->start.coord++;
200 	annot->finish.coord++;
201 }
202 
getNextAnnotation(Annotation * annot)203 Annotation *getNextAnnotation(Annotation * annot)
204 {
205 	return annot + 1;
206 }
207