/
linkedListOps.c
132 lines (115 loc) · 3.58 KB
/
linkedListOps.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
///////////////////// DIVERSITY /////////////////////
// DIVERSITY is a tool to explore multiple ways of protein-DNA
// binding in the genome. More information can be found in the README file.
// Copyright (C) 2015 Sneha Mitra, Anushua Biswas and Leelavati Narlikar
// DIVERSITY is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// DIVERSITY is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "linkedListOps.h"
#include "messages.h"
/* Creates a node to store counts at a position in the given motif. Counts stored in the form of a linked list */
motifStruct* createNode(){
motifStruct *m = (motifStruct*)malloc(sizeof(motifStruct));
if(!m) printMessages(0, NULL);
m->modeMotifCount = (int*)malloc(sizeof(int)*4);
if(!(m->modeMotifCount)) printMessages(0, NULL);
m->next = NULL;
return m;
}
/* Free a node of the linked list */
void freeNode(motifStruct *m){
free(m->modeMotifCount);
m->next = NULL;
m->modeMotifCount = NULL;
free(m);
}
/* Free the entire linked list storing count information of a given motif */
void freeMotif(motifStruct *m){
motifStruct *mc;
while(m != NULL){
mc = m;
m = m->next;
freeNode(mc);
}
}
/* Free all motifs that are stored in the form of a linked list */
void freeMotifs(motifContainer *m, int n){
int i;
for(i = 0; i < n; i++){
freeMotif(m[i].motif);
m[i].motif = NULL;
}
free(m);
}
/* Initialize a motif structure of given size */
motifStruct* initializeMotif(int size){
int i;
motifStruct *m, *mc;
m = createNode();
mc = m;
for(i = 1; i < size; i++){
mc->next = createNode();
mc = mc->next;
}
return m;
}
/* Initialize al motifs for a given model */
motifContainer* initializeMotifs(int *widths, int n){
motifContainer *mc;
int i;
mc = (motifContainer*)malloc(sizeof(motifContainer)*n);
if(!mc) printMessages(0, NULL);
for(i = 0; i < n; i++)
mc[i].motif = initializeMotif(widths[i]);
return mc;
}
/* Add a node to the front. Meaning motif width increased by one on the left */
motifStruct* addNodeFront(motifStruct *f, motifStruct *m){
f->next = m;
return f;
}
/* Add a node to the end. Meaning motif width increased by one on the right */
motifStruct* addNodeEnd(motifStruct *e, motifStruct *m){
motifStruct *mc;
mc = m;
while(mc->next != NULL) mc = mc->next;
mc->next = e;
return m;
}
/* Delete a node from the front. Meaning motif width decreased by one on the left */
motifStruct* delNodeFront(motifStruct *m){
motifStruct *mc;
mc = m->next;
freeNode(m);
return mc;
}
/* Delete a node from the end. Meaning motif width decreased by one on the right */
motifStruct* delNodeEnd(motifStruct *m){
motifStruct *mc, *mc1;
mc = m;
while((mc->next)->next != NULL) mc = mc->next;
mc1 = mc->next;
freeNode(mc1);
mc->next = NULL;
return(m);
}
/* Get pointer to the last node of the motif */
motifStruct* getLastNode(motifStruct *m){
motifStruct *mc;
mc = m;
while(mc->next != NULL){
mc = mc->next;
}
return mc;
}