modified: makefile
[GalaxyCodeBases.git] / BGI / SOAPdenovo2 / standardPregraph / arc.c
blob32cf554ee9adfa4da943b8c66b11238c24aef2b0
1 /*
2 * arc.c
4 * Copyright (c) 2008-2012 BGI-Shenzhen <soap at genomics dot org dot cn>.
6 * This file is part of SOAPdenovo.
8 * SOAPdenovo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * SOAPdenovo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with SOAPdenovo. If not, see <http://www.gnu.org/licenses/>.
23 #include "stdinc.h"
24 #include "newhash.h"
25 #include "kmerhash.h"
26 #include "extfunc.h"
27 #include "extvab.h"
29 #define preARCBLOCKSIZE 100000
31 void createPreArcMemManager ()
33 prearc_mem_manager = createMem_manager ( preARCBLOCKSIZE, sizeof ( preARC ) );
36 void prlDestroyPreArcMem ()
38 if ( !preArc_mem_managers )
40 return;
43 int i;
45 for ( i = 0; i < thrd_num; i++ )
47 freeMem_manager ( preArc_mem_managers[i] );
50 free ( ( void * ) preArc_mem_managers );
51 preArc_mem_managers = NULL;
54 void destroyPreArcMem ()
56 freeMem_manager ( prearc_mem_manager );
57 prearc_mem_manager = NULL;
60 preARC * prlAllocatePreArc ( unsigned int edgeid, MEM_MANAGER * manager )
62 preARC * newArc;
63 newArc = ( preARC * ) getItem ( manager );
64 newArc->to_ed = edgeid;
65 newArc->multiplicity = 1;
66 newArc->next = NULL;
67 return newArc;
70 preARC * allocatePreArc ( unsigned int edgeid )
72 arcCounter++;
73 preARC * newArc;
74 newArc = ( preARC * ) getItem ( prearc_mem_manager );
75 newArc->to_ed = edgeid;
76 newArc->multiplicity = 1;
77 newArc->next = NULL;
78 return newArc;
81 void output_arcGVZ ( char * outfile, boolean IsContig )
83 ARC * pArc;
84 preARC * pPreArc;
85 char name[256];
86 FILE * fp;
87 unsigned int i;
88 sprintf ( name, "%s.arc.gvz", outfile );
89 fp = ckopen ( name, "w" );
90 fprintf ( fp, "digraph G{\n" );
91 fprintf ( fp, "\tsize=\"512,512\";\n" );
93 for ( i = 1; i <= num_ed; i++ )
95 if ( IsContig )
97 pPreArc = contig_array[i].arcs;
99 while ( pPreArc )
101 fprintf ( fp, "\tC%d -> C%d[label =\"%d\"];\n", i, pPreArc->to_ed, pPreArc->multiplicity );
102 pPreArc = pPreArc->next;
105 else
107 pArc = edge_array[i].arcs;
109 while ( pArc )
111 fprintf ( fp, "\tC%d -> C%d[label =\"%d\"];\n", i, pArc->to_ed, pArc->multiplicity );
112 pArc = pArc->next;
117 fprintf ( fp, "}\n" );
118 fclose ( fp );
121 /**************** below this line all codes are about ARC ****************/
122 #define ARCBLOCKSIZE 100000
123 void createArcMemo ()
125 if ( !arc_mem_manager )
127 arc_mem_manager = createMem_manager ( ARCBLOCKSIZE, sizeof ( ARC ) );
129 else
131 fprintf ( stderr, "Warning from createArcMemo: arc_mem_manager is a active pointer.\n" );
135 void destroyArcMem ()
137 freeMem_manager ( arc_mem_manager );
138 arc_mem_manager = NULL;
141 ARC * allocateArc ( unsigned int edgeid )
143 arcCounter++;
144 ARC * newArc;
145 newArc = ( ARC * ) getItem ( arc_mem_manager );
146 newArc->to_ed = edgeid;
147 newArc->multiplicity = 1;
148 newArc->prev = NULL;
149 newArc->next = NULL;
150 return newArc;
153 void dismissArc ( ARC * arc )
155 returnItem ( arc_mem_manager, arc );
158 /***************** below this line all codes are about lookup table *****************/
160 void createArcLookupTable ()
162 if ( !arcLookupTable )
164 arcLookupTable = ( ARC ** ) ckalloc ( ( 3 * num_ed + 1 ) * sizeof ( ARC * ) );
168 void deleteArcLookupTable ()
170 if ( arcLookupTable )
172 free ( ( void * ) arcLookupTable );
173 arcLookupTable = NULL;
177 void putArc2LookupTable ( unsigned int from_ed, ARC * arc )
179 if ( !arc || !arcLookupTable )
181 return;
184 unsigned int index = 2 * from_ed + arc->to_ed;
185 arc->nextInLookupTable = arcLookupTable[index];
186 arcLookupTable[index] = arc;
189 static ARC * getArcInLookupTable ( unsigned int from_ed, unsigned int to_ed )
191 unsigned int index = 2 * from_ed + to_ed;
192 ARC * ite_arc = arcLookupTable[index];
194 while ( ite_arc )
196 if ( ite_arc->to_ed == to_ed )
198 return ite_arc;
201 ite_arc = ite_arc->nextInLookupTable;
204 return NULL;
207 void removeArcInLookupTable ( unsigned int from_ed, unsigned int to_ed )
209 unsigned int index = 2 * from_ed + to_ed;
210 ARC * ite_arc = arcLookupTable[index];
211 ARC * arc;
213 if ( !ite_arc )
215 fprintf ( stderr, "RemoveArcInLookupTable: not found A.\n" );
216 return;
219 if ( ite_arc->to_ed == to_ed )
221 arcLookupTable[index] = ite_arc->nextInLookupTable;
222 return;
225 while ( ite_arc->nextInLookupTable && ite_arc->nextInLookupTable->to_ed != to_ed )
227 ite_arc = ite_arc->nextInLookupTable;
230 if ( ite_arc->nextInLookupTable )
232 arc = ite_arc->nextInLookupTable;
233 ite_arc->nextInLookupTable = arc->nextInLookupTable;
234 return;
237 fprintf ( stderr, "RemoveArcInLookupTable: not found B.\n" );
238 return;
241 void recordArcsInLookupTable ()
243 unsigned int i;
244 ARC * ite_arc;
246 for ( i = 1; i <= num_ed; i++ )
248 ite_arc = edge_array[i].arcs;
250 while ( ite_arc )
252 putArc2LookupTable ( i, ite_arc );
253 ite_arc = ite_arc->next;
258 /*************************************************
259 Function:
260 getArcBetween
261 Description:
262 Gets the arc between the edge "from_ed" and the edge "to_ed", then return it.
263 Input:
264 1. from_ed: the origin edge
265 2. to_ed: the destination edge
266 Output:
267 None.
268 Return:
269 The arc between the edge "from_ed" and the edge "to_ed".
270 *************************************************/
271 ARC * getArcBetween ( unsigned int from_ed, unsigned int to_ed )
273 ARC * parc;
275 if ( arcLookupTable )
277 parc = getArcInLookupTable ( from_ed, to_ed );
278 return parc;
281 parc = edge_array[from_ed].arcs;
283 while ( parc )
285 if ( parc->to_ed == to_ed )
287 return parc;
290 parc = parc->next;
293 return parc;
296 /*************************************************
297 Function:
298 validArcCount
299 Description:
300 Compute the count of the valid arc, whose weight is larger than "cutoff", then return it.
301 Input:
302 1. arc: the head of the "preARC" linked list
303 2. cutoff: the minimum requirement for the weight of the valid arc
304 Output:
305 None.
306 Return:
307 The number of the valid arc.
308 *************************************************/
309 int validArcCount ( preARC * arc, int cutoff )
311 int arc_num = 0;
313 while ( arc )
315 if ( arc->multiplicity >= cutoff )
316 { ++arc_num; }
318 arc = arc->next;
321 return arc_num;
324 /*************************************************
325 Function:
326 maxArcWeight
327 Description:
328 Gets the maximum weight in all arcs, then return it.
329 Input:
330 1. arc: the head of the "preARC" linked list
331 Output:
332 None.
333 Return:
334 The maximum weight.
335 *************************************************/
336 unsigned int maxArcWeight ( preARC * arc )
338 unsigned int max = 0;
340 while ( arc )
342 if ( arc->multiplicity > max )
343 { max = arc->multiplicity; }
345 arc = arc->next;
348 return max;