annotate mrcanavar-0.34/utils.c @ 0:86522a0b5f59 default tip

Uploaded source code for mrCaNaVaR
author calkan
date Tue, 21 Feb 2012 10:44:13 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
1
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
2 #include "utils.h"
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
3
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
4
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
5
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
6 void print_error(char *msg){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
7 fprintf(stderr, "\nmrCaNaVaR version %s.\nLast update: %s.\n", VERSION, LAST_UPDATE);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
8 fprintf(stderr, "\n%s\n", msg);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
9 fprintf(stderr, "Invoke parameter -h for help.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
10 exit (0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
11 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
12
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
13 void set_runmode(enum MODETYPE NEWMODE){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
14 if (RUNMODE != NONE && RUNMODE != NEWMODE){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
15 print_error("Select one run mode only.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
16 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
17 RUNMODE = NEWMODE;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
18 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
19
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
20 void set_gender(enum GENDERTYPE SET){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
21 if (GENDER != AUTODETECT && RUNMODE != SET){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
22 print_error("Select only one of --xx or --xy.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
23 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
24 GENDER= SET;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
25 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
26
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
27 void set_str(char **target, char *source){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
28
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
29 if (*target != NULL) free((*target));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
30
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
31 (*target) = (char *) malloc(sizeof(char) * (strlen(source)+1));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
32
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
33 strncpy((*target), source, (strlen(source)+1));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
34
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
35 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
36
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
37
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
38 void init_globals(void){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
39 GENOME_FASTA = NULL;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
40 GENOME_CONF = NULL;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
41 GENOME_GAPS = NULL;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
42 RUNMODE = NONE;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
43 GENDER = AUTODETECT;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
44 MULTGC = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
45 MAX_GC_CORR = 20.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
46 MIN_GC_CORR = 0.05;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
47 VERBOSE = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
48
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
49 num_chrom = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
50
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
51 CHECKSAM = 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
52
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
53 /* set window size defaults */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
54
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
55 LW_SIZE = 5000;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
56 SW_SIZE = 1000;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
57 CW_SIZE = 1000;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
58 LW_SLIDE = 1000;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
59 SW_SLIDE = 1000;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
60
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
61 LW_MEAN = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
62 SW_MEAN = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
63 CW_MEAN = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
64
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
65 LW_STD = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
66 SW_STD = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
67 CW_STD = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
68
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
69 CONT_WINDOW = 7;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
70 CUT_WINDOW = 6;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
71
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
72 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
73
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
74
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
75 FILE *my_fopen(char *fname, char *mode, char GZ){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
76 FILE *fp;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
77
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
78
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
79 if (!GZ)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
80 fp = fopen(fname, mode);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
81 else
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
82 fp = gzopen(fname, mode);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
83
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
84 if (fp == NULL){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
85 fprintf(stderr, "Unable to open file %s in %s mode.", fname, mode[0]=='w' ? "write" : "read");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
86 exit (0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
87 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
88
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
89 return fp;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
90 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
91
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
92
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
93 void saveRefConfig(char *configFile){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
94
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
95 int i;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
96 int wcnt;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
97 char chrom_name_len;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
98 int retVal;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
99 FILE *config;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
100
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
101 config = my_fopen(configFile, "w", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
102
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
103 /* sort the chromosomes pointer array */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
104
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
105 qsort(chromosomes, num_chrom, sizeof (struct chrom *), compChr);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
106
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
107
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
108 /* start with the magicNum, I will use this as a format check when loading */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
109 retVal = fwrite(&magicNum, sizeof(magicNum), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
110
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
111 /* window sizes / slides */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
112
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
113 retVal = fwrite(&LW_SIZE, sizeof(LW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
114 retVal = fwrite(&SW_SIZE, sizeof(SW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
115 retVal = fwrite(&CW_SIZE, sizeof(CW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
116 retVal = fwrite(&LW_SLIDE, sizeof(LW_SLIDE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
117 retVal = fwrite(&SW_SLIDE, sizeof(SW_SLIDE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
118
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
119
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
120 /* reference genome numbers */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
121
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
122 /* number of chromosomes */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
123 retVal = fwrite(&num_chrom, sizeof(num_chrom), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
124
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
125 /* iterate through chromosomes, write their names, and window counts */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
126
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
127 for (i=0; i<num_chrom; i++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
128 chrom_name_len = (char) strlen(chromosomes[i]->name);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
129 retVal = fwrite(&chrom_name_len, sizeof(chrom_name_len), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
130 retVal = fwrite(chromosomes[i]->name, chrom_name_len * sizeof(char), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
131
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
132 retVal = fwrite(&(chromosomes[i]->length), sizeof(chromosomes[i]->length), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
133 retVal = fwrite(&(chromosomes[i]->lw_cnt), sizeof(chromosomes[i]->lw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
134 retVal = fwrite(&(chromosomes[i]->sw_cnt), sizeof(chromosomes[i]->sw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
135 retVal = fwrite(&(chromosomes[i]->cw_cnt), sizeof(chromosomes[i]->cw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
136
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
137 /* long windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
138 for (wcnt=0; wcnt<chromosomes[i]->lw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
139 retVal = fwrite(&(chromosomes[i]->lw[wcnt].start), sizeof(chromosomes[i]->lw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
140 retVal = fwrite(&(chromosomes[i]->lw[wcnt].end), sizeof(chromosomes[i]->lw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
141 retVal = fwrite(&(chromosomes[i]->lw[wcnt].gc), sizeof(chromosomes[i]->lw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
142 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
143
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
144 /* short windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
145 for (wcnt=0; wcnt<chromosomes[i]->sw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
146 retVal = fwrite(&(chromosomes[i]->sw[wcnt].start), sizeof(chromosomes[i]->sw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
147 retVal = fwrite(&(chromosomes[i]->sw[wcnt].end), sizeof(chromosomes[i]->sw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
148 retVal = fwrite(&(chromosomes[i]->sw[wcnt].gc), sizeof(chromosomes[i]->sw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
149 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
150
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
151 /* copy windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
152 for (wcnt=0; wcnt<chromosomes[i]->cw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
153 retVal = fwrite(&(chromosomes[i]->cw[wcnt].start), sizeof(chromosomes[i]->cw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
154 retVal = fwrite(&(chromosomes[i]->cw[wcnt].end), sizeof(chromosomes[i]->cw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
155 retVal = fwrite(&(chromosomes[i]->cw[wcnt].gc), sizeof(chromosomes[i]->cw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
156 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
157
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
158 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
159
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
160 fclose(config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
161
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
162 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
163
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
164
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
165
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
166
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
167 void loadRefConfig(char *configFile){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
168
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
169 int i;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
170 int wcnt;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
171 char chrom_name_len;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
172 char readString[MAX_STR];
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
173 int retVal;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
174
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
175 int isMagicNum;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
176
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
177 FILE *config;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
178
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
179 config = my_fopen(configFile, "r", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
180
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
181 /* start with the magicNum, use this as a format check when loading */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
182 retVal = fread(&isMagicNum, sizeof(isMagicNum), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
183
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
184 if (isMagicNum != magicNum)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
185 print_error("Reference configuration file seems to be invalid or corrupt.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
186
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
187
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
188 fprintf(stdout, "Loading reference configuration, hold on ... ");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
189 fflush(stdout);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
190
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
191 /* window sizes / slides */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
192
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
193 retVal = fread(&LW_SIZE, sizeof(LW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
194 retVal = fread(&SW_SIZE, sizeof(SW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
195 retVal = fread(&CW_SIZE, sizeof(CW_SIZE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
196 retVal = fread(&LW_SLIDE, sizeof(LW_SLIDE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
197 retVal = fread(&SW_SLIDE, sizeof(SW_SLIDE), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
198
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
199
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
200 /* reference genome numbers */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
201
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
202 /* number of chromosomes */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
203 retVal = fread(&num_chrom, sizeof(num_chrom), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
204
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
205 /* create chromosomes data structure */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
206
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
207 chromosomes = (struct chrom **) malloc(sizeof(struct chrom *) * num_chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
208 for (i=0; i<num_chrom; i++)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
209 chromosomes[i] = (struct chrom *) malloc(sizeof(struct chrom) * num_chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
210
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
211 /* iterate through chromosomes, write their names, and window counts */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
212
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
213 for (i=0; i<num_chrom; i++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
214
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
215 retVal = fread(&chrom_name_len, sizeof(chrom_name_len), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
216
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
217 retVal = fread(readString, chrom_name_len * sizeof(char), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
218 readString[chrom_name_len] = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
219 trimspace(readString);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
220
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
221 set_str(&(chromosomes[i]->name), readString);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
222
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
223 retVal = fread(&(chromosomes[i]->length), sizeof(chromosomes[i]->length), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
224 retVal = fread(&(chromosomes[i]->lw_cnt), sizeof(chromosomes[i]->lw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
225 retVal = fread(&(chromosomes[i]->sw_cnt), sizeof(chromosomes[i]->sw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
226 retVal = fread(&(chromosomes[i]->cw_cnt), sizeof(chromosomes[i]->cw_cnt), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
227
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
228 /* create windows structures */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
229
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
230
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
231 chromosomes[i]->lw = (struct window *) malloc (sizeof(struct window) * chromosomes[i]->lw_cnt);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
232 chromosomes[i]->sw = (struct window *) malloc (sizeof(struct window) * chromosomes[i]->sw_cnt);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
233 chromosomes[i]->cw = (struct window *) malloc (sizeof(struct window) * chromosomes[i]->cw_cnt);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
234
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
235
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
236
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
237 /* long windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
238 for (wcnt=0; wcnt<chromosomes[i]->lw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
239 retVal = fread(&(chromosomes[i]->lw[wcnt].start), sizeof(chromosomes[i]->lw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
240 retVal = fread(&(chromosomes[i]->lw[wcnt].end), sizeof(chromosomes[i]->lw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
241 retVal = fread(&(chromosomes[i]->lw[wcnt].gc), sizeof(chromosomes[i]->lw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
242 // this is the default, auto-control-picker will fix this
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
243 chromosomes[i]->lw[wcnt].isControl = 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
244 chromosomes[i]->lw[wcnt].depth = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
245 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
246
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
247 /* short windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
248 for (wcnt=0; wcnt<chromosomes[i]->sw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
249 retVal = fread(&(chromosomes[i]->sw[wcnt].start), sizeof(chromosomes[i]->sw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
250 retVal = fread(&(chromosomes[i]->sw[wcnt].end), sizeof(chromosomes[i]->sw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
251 retVal = fread(&(chromosomes[i]->sw[wcnt].gc), sizeof(chromosomes[i]->sw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
252 chromosomes[i]->sw[wcnt].isControl = 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
253 chromosomes[i]->sw[wcnt].depth = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
254 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
255
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
256 /* copy windows */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
257 for (wcnt=0; wcnt<chromosomes[i]->cw_cnt; wcnt++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
258 retVal = fread(&(chromosomes[i]->cw[wcnt].start), sizeof(chromosomes[i]->cw[wcnt].start), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
259 retVal = fread(&(chromosomes[i]->cw[wcnt].end), sizeof(chromosomes[i]->cw[wcnt].end), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
260 retVal = fread(&(chromosomes[i]->cw[wcnt].gc), sizeof(chromosomes[i]->cw[wcnt].gc), 1, config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
261 chromosomes[i]->cw[wcnt].isControl = 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
262 chromosomes[i]->cw[wcnt].depth = 0.0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
263 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
264
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
265 /* fill in the rest of the chromosomes structure */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
266
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
267 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
268
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
269 fprintf(stdout, "[OK]. %d chromosomes loaded.\n", num_chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
270
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
271 fclose(config);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
272 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
273
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
274
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
275
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
276 static int compChr(const void *p1, const void *p2){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
277 /* compare function to sort the chromosome pointer array */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
278 struct chrom *a, *b;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
279
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
280 a = *((struct chrom **)p1);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
281 b = *((struct chrom **)p2);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
282
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
283
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
284 return (strcmp ( a->name, b->name) );
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
285
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
286 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
287
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
288
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
289 int endswith(char *src, char *end){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
290
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
291
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
292 int endlen = strlen(end);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
293 int srclen = strlen(src);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
294 int copyindex;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
295
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
296
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
297 if (endlen > srclen)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
298 return 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
299
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
300 copyindex = srclen - endlen;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
301
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
302 if (memcmp(src+copyindex, end, endlen))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
303 return 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
304 else
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
305 return 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
306
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
307
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
308 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
309
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
310 void trimspace(char *str){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
311 int len;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
312 len = strlen(str) - 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
313 while(1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
314 if (isspace(str[len]))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
315 str[len--]=0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
316 else
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
317 break;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
318 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
319 }