annotate mrcanavar-0.34/sam.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 #include "sam.h"
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
2
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
3 void read_depth(char *indirSAM, char *depthFile, char gzSAM){
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 DIR *dp;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
7 int fileCnt;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
8 int totalFile;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
9
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
10 struct dirent *ep;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
11
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
12 int i;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
13
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
14 if (GENOME_CONF == NULL)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
15 print_error("Select genom configuration file (input) through the -conf parameter.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
16 if (indirSAM == NULL)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
17 print_error("Select input directory that contains the SAM files through the -samdir parameter.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
18 if (depthFile == NULL)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
19 print_error("Select read depth output file through the -depth parameter.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
20
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
21
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
22 loadRefConfig(GENOME_CONF);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
23
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
24 fprintf(stdout, "Scanning the SAM directory: %s\n", indirSAM);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
25
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
26 dp = opendir(indirSAM);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
27
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
28 if (dp == NULL)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
29 print_error("SAM directory not found.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
30
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
31 fileCnt = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
32 totalFile = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
33
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
34 while((ep=readdir(dp))){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
35 if (ep->d_name[0] == '.')
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
36 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
37 if (ep->d_type == DT_DIR)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
38 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
39
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
40 if (CHECKSAM && !endswith(ep->d_name, ".sam") && !endswith(ep->d_name, ".sam.gz"))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
41 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
42
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
43 /*
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
44 if (!strstr(ep->d_name, "sam"))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
45 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
46 */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
47
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
48 i = strlen(ep->d_name)-1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
49
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
50 if ((ep->d_name[i] == 'z' && ep->d_name[i-1] == 'g' && ep->d_name[i-2] == '.') && gzSAM == 0){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
51 print_error("File name ends with .gz yet --gz option is not selected. Are you sure?\nIf the files are indeed uncompressed, rename the files.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
52 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
53 totalFile++;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
54 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
55
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
56 rewinddir(dp);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
57
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
58 while((ep=readdir(dp))){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
59 if (ep->d_name[0] == '.')
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
60 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
61 if (ep->d_type == DT_DIR)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
62 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
63
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
64 if (CHECKSAM && !endswith(ep->d_name, ".sam") && !endswith(ep->d_name, ".sam.gz"))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
65 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
66
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
67
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
68 /*
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
69 if (!strstr(ep->d_name, "sam"))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
70 continue;
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 fprintf(stdout, "\r \rLoading file %d of %d: %s...", (fileCnt+1), totalFile, ep->d_name);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
74 fflush(stdout);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
75
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
76 readSAM(indirSAM, ep->d_name, gzSAM);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
77
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
78 fileCnt++;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
79 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
80
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
81 closedir(dp);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
82
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
83 if (fileCnt == 0)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
84 print_error("SAM directory does not contain any files with extensions \".sam\" or \".sam.gz\".\nIf you do have the correct files, please rename them to have either \".sam\" or \".sam.gz\" extensions.\n");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
85 else
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
86 fprintf(stdout, "\n\n%d file%s loaded.\n", fileCnt, fileCnt>1 ? "s" : "");
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
87
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
88 saveDepth(depthFile);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
89
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 readSAM(char *indirSAM, char *fname, char gzSAM){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
94 FILE *sam;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
95
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
96 char *samfile;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
97
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
98 char samLine[4 * MAX_STR];
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
99 char prevChrom[MAX_STR];
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
100 char chrom[MAX_STR];
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
101 char *token;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
102
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
103
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
104 int pos;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
105 int chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
106 int prev_chrom_id = -1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
107 int lineCnt;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
108
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
109 samfile = (char *) malloc (sizeof(char) * (strlen(indirSAM) + strlen(fname) + 2));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
110
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
111 sprintf(samfile, "%s/%s", indirSAM, fname);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
112
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
113 sam = my_fopen(samfile, "r", gzSAM);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
114
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
115 prevChrom[0] = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
116
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
117 while (1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
118
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
119 /* read entire line from the SAM file */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
120 if (gzSAM){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
121 gzgets(sam, samLine, (4 * MAX_STR));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
122 if (gzeof(sam))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
123 break;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
124 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
125 else{
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
126 fgets(samLine, (4 * MAX_STR), sam);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
127 if (feof(sam))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
128 break;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
129 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
130
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
131 /* parse chrom */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
132 token = NULL;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
133
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
134 token = strtok(samLine, "\t"); // read name
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
135 token = strtok(NULL, "\t"); // flag
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
136 token = strtok(NULL, "\t"); //chrom
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
137
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
138 strcpy(chrom, token);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
139 trimspace(chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
140
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
141 token = strtok(NULL, "\t"); // pos
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
142
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
143 pos = atoi(token) - 1; // SAM file is 1-based; our format is 0-based
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
144
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
145 if (pos < 0)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
146 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
147
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
148 /*
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
149 debug if needed
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
150 fprintf(stdout, "%s\t%d\n", chrom, pos);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
151 */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
152
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
153
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
154
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
155 chrom_id = insert_read_lw(chrom, pos, prevChrom, prev_chrom_id);
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 if (VERBOSE)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
159 fprintf(stdout, "[READSAM]\t%s\t%d\tchrom_id: %d\n", chrom, pos, chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
160 */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
161
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
162 if (chrom_id == -1) // this chromosome is not in the config file
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
163 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
164
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
165 chrom_id = insert_read_sw(chrom, pos, prevChrom, chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
166
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
167 if (chrom_id == -1) // this chromosome is not in the config file; it shouldn't come to this
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
168 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
169
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
170 chrom_id = insert_read_cw(chrom, pos, prevChrom, chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
171
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
172
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
173
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
174 if (chrom_id == -1) // this chromosome is not in the config file; it shouldn't come to this
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
175 continue;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
176
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
177 strcpy(prevChrom, chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
178 prev_chrom_id = chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
179
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
180
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
181 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
182
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
183 if (gzSAM)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
184 gzclose(sam);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
185 else
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
186 fclose(sam);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
187
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
188 free(samfile);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
189 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
190
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
191
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
192
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
193
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
194
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
195 int insert_read_lw(char *chrom, int pos, char *prevChrom, int prev_chrom_id){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
196 int chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
197 int window_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
198
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
199 int flank;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
200
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
201 chrom_id = findChrom(chrom, prevChrom, prev_chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
202
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
203 if (chrom_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
204
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
205 if (pos >= chromosomes[chrom_id]->length){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
206 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
207 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
208
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
209 window_id = windowSearch(chromosomes[chrom_id]->lw, chromosomes[chrom_id]->lw_cnt, pos);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
210
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
211
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
212 if (window_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
213
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
214 chromosomes[chrom_id]->lw[window_id].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
215
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
216 /* iterate left */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
217 flank = window_id - 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
218
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
219 while (flank >= 0 && (pos >= chromosomes[chrom_id]->lw[flank].start && pos <= chromosomes[chrom_id]->lw[flank].end))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
220 chromosomes[chrom_id]->lw[flank--].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
221
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
222
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
223 /* iterate right */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
224 flank = window_id + 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
225
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
226 while (flank < chromosomes[chrom_id]->lw_cnt && (pos >= chromosomes[chrom_id]->lw[flank].start && pos <= chromosomes[chrom_id]->lw[flank].end))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
227 chromosomes[chrom_id]->lw[flank++].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
228
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
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
232 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
233
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
234 return chrom_id;
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
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
238
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
239 int insert_read_sw(char *chrom, int pos, char *prevChrom, int prev_chrom_id){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
240 int chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
241 int window_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
242
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
243 int flank;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
244
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
245 chrom_id = findChrom(chrom, prevChrom, prev_chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
246
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
247 if (chrom_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
248
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
249 if (pos >= chromosomes[chrom_id]->length)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
250 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
251
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
252 window_id = windowSearch(chromosomes[chrom_id]->sw, chromosomes[chrom_id]->sw_cnt, pos);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
253
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
254 if (window_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
255
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
256 chromosomes[chrom_id]->sw[window_id].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
257
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
258 /* iterate left */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
259 flank = window_id - 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
260
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
261 while (flank >= 0 && (pos >= chromosomes[chrom_id]->sw[flank].start && pos <= chromosomes[chrom_id]->sw[flank].end))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
262 chromosomes[chrom_id]->sw[flank--].depth += 1;
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 /* iterate right */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
266 flank = window_id + 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
267
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
268 while (flank < chromosomes[chrom_id]->sw_cnt && (pos >= chromosomes[chrom_id]->sw[flank].start && pos <= chromosomes[chrom_id]->sw[flank].end))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
269 chromosomes[chrom_id]->sw[flank++].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
270
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
271
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 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
277 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
278
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
279
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
280 int insert_read_cw(char *chrom, int pos, char *prevChrom, int prev_chrom_id){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
281 int chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
282 int window_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
283
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
284 int flank;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
285
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
286 chrom_id = findChrom(chrom, prevChrom, prev_chrom_id);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
287
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
288 if (chrom_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
289
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
290 if (pos >= chromosomes[chrom_id]->length)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
291 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
292
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
293 window_id = windowSearch(chromosomes[chrom_id]->cw, chromosomes[chrom_id]->cw_cnt, pos);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
294
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
295 if (window_id != -1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
296
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
297 chromosomes[chrom_id]->cw[window_id].depth += 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
298
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
299 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
300
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
301 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
302 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
303 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
304
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
305
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
306 int findChrom(char *chrom, char *prevChrom, int prev_chrom_id){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
307 int chrom_id;
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 if (!strcmp(chrom, prevChrom)){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
311 chrom_id = prev_chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
312 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
313
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
314 else if (prev_chrom_id == -1 && !strcmp(chrom, chromosomes[0]->name)){ // the first entry in the file
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
315 chrom_id = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
316 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
317 else if (prev_chrom_id != -1 && prev_chrom_id < num_chrom -1 && !strcmp(chrom, chromosomes[prev_chrom_id+1]->name)){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
318 chrom_id = prev_chrom_id + 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
319 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
320 else{
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
321 chrom_id = binSearch(chrom);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
322 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
323
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
324 return chrom_id;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
325
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
326 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
327
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
328 int binSearch(char *chrom){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
329 int start;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
330 int end;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
331 int med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
332 int strtest;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
333
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
334 start = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
335
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
336 end = num_chrom - 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
337
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
338 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
339
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
340
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
341 while (1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
342
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
343 if (start > end)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
344 return -1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
345
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
346 if (start == med || end == med){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
347 if (!strcmp(chrom, chromosomes[start]->name))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
348 return start;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
349 else if (!strcmp(chrom, chromosomes[end]->name))
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
350 return end;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
351 else{
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
352 return -1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
353 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
354 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
355
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
356 strtest = strcmp(chrom, chromosomes[med]->name);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
357
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
358 if(strtest == 0)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
359 return med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
360
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
361 else if (strtest < 0){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
362 end = med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
363 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
364 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
365
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
366 else {
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
367 start = med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
368 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
369 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
370
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
371 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
372 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
373
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
374
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
375
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
376 int windowSearch(struct window *searchWin, int winCnt, int pos){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
377 int start;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
378 int end;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
379 int med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
380
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
381 start = 0;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
382 end = winCnt - 1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
383
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
384 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
385
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
386 while (1){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
387
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
388 if (start > end)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
389 return -1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
390
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
391 if (start == med || end == med){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
392
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
393 if (pos >= searchWin[start].start && pos < searchWin[start].end)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
394 return start;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
395
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
396 else if (pos >= searchWin[end].start && pos < searchWin[end].end)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
397 return end;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
398
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
399 else return -1;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
400
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
401 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
402
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
403 if (pos >= searchWin[med].start && pos < searchWin[med].end)
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
404 return med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
405
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
406 else if (pos < searchWin[med].start){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
407 end = med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
408 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
409 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
410
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
411 else {
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
412 start = med;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
413 med = (start + end) / 2;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
414 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
415 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
416 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
417
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
418
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
419
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
420 void saveDepth(char *depthFile){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
421 int i;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
422 int j;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
423 int retVal;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
424
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
425 char *fname;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
426 FILE *txtDepth;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
427 FILE *binDepth;
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
428
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
429 fname = (char *) malloc (sizeof(char) * (strlen(depthFile) + 10));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
430
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
431 sprintf(fname, "%s.lw.txt", depthFile);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
432
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
433 txtDepth = my_fopen(fname, "w", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
434 binDepth = my_fopen(depthFile, "w", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
435
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
436 /* start with the magicNum, I will use this as a format check when loading */
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
437 retVal = fwrite(&magicNum, sizeof(magicNum), 1, binDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
438
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
439 fprintf(stdout, "Saving depth file %s\n", fname);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
440
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
441 for (i = 0; i < num_chrom; i++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
442 for (j = 0; j < chromosomes[i]->lw_cnt; j++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
443 fprintf(txtDepth, "%s\t%d\t%d\t%f\t%d\n", chromosomes[i]->name, chromosomes[i]->lw[j].start, chromosomes[i]->lw[j].end, chromosomes[i]->lw[j].gc, (int) (chromosomes[i]->lw[j].depth));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
444 retVal = fwrite(&(chromosomes[i]->lw[j].depth), sizeof(chromosomes[i]->lw[j].depth), 1, binDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
445 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
446 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
447
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
448 fclose(txtDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
449
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
450 sprintf(fname, "%s.sw.txt", depthFile);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
451 txtDepth = my_fopen(fname, "w", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
452 fprintf(stdout, "Saving depth file %s\n", fname);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
453
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
454 for (i = 0; i < num_chrom; i++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
455
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
456 for (j = 0; j < chromosomes[i]->sw_cnt; j++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
457 fprintf(txtDepth, "%s\t%d\t%d\t%f\t%d\n", chromosomes[i]->name, chromosomes[i]->sw[j].start, chromosomes[i]->sw[j].end, chromosomes[i]->sw[j].gc, (int) (chromosomes[i]->sw[j].depth));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
458 retVal = fwrite(&(chromosomes[i]->sw[j].depth), sizeof(chromosomes[i]->cw[j].depth), 1, binDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
459 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
460 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
461
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
462 fclose(txtDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
463
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
464 sprintf(fname, "%s.cw.txt", depthFile);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
465 txtDepth = my_fopen(fname, "w", 0);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
466 fprintf(stdout, "Saving depth file %s\n", fname);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
467
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
468 for (i = 0; i < num_chrom; i++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
469 for (j = 0; j < chromosomes[i]->cw_cnt; j++){
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
470 fprintf(txtDepth, "%s\t%d\t%d\t%f\t%d\n", chromosomes[i]->name, chromosomes[i]->cw[j].start, chromosomes[i]->cw[j].end, chromosomes[i]->cw[j].gc, (int) (chromosomes[i]->cw[j].depth));
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
471 retVal = fwrite(&(chromosomes[i]->cw[j].depth), sizeof(chromosomes[i]->cw[j].depth), 1, binDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
472 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
473 }
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
474
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
475 fclose(txtDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
476 fclose(binDepth);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
477
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
478 free(fname);
86522a0b5f59 Uploaded source code for mrCaNaVaR
calkan
parents:
diff changeset
479 }