| 1 |
|
| /* |
| 2 |
|
| * segmentize.c |
| 3 |
|
| * |
| 4 |
|
| * Created on: 10 aug 2012 |
| 5 |
|
| * Author: Wolftower |
| 6 |
|
| * |
| 7 |
|
| * Description |
| 8 |
|
| * This library implements a function that segments |
| 9 |
|
| * chromosome/substring files into smaller chunks |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #include "segmentize.h" |
| 13 |
|
| |
| 14 |
0 |
| void seq2chunks(char* sequence, char* seqHeader, char* outputDir, const char* nopathFile, int offset, int* chunkIndex){ |
| 15 |
0 |
| int windowSize = 200; |
| 16 |
0 |
| int stepSize = 20; |
| 17 |
0 |
| int currentStep = 0; |
| 18 |
0 |
| int windowsPerChunk = 50000; |
| 19 |
|
| int windowIndex; |
| 20 |
|
| char window[203]; |
| 21 |
0 |
| int breakSwitch = 0; |
| 22 |
0 |
| int seqLen = strlen(sequence); |
| 23 |
|
| int nucIndex; |
| 24 |
0 |
| FILE *chunk = NULL; |
| 25 |
0 |
▶ | for (windowIndex = 0; windowIndex * stepSize < seqLen; windowIndex++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (windowIndex = 0; windowIndex * stepSize < seqLen; windowIndex++) { |
0 |
0 |
0 |
|
| 26 |
0 |
| int windowStart = windowIndex * stepSize; |
| 27 |
|
| |
| 28 |
|
| //Create new chunk |
| 29 |
0 |
▶ | if (windowIndex % windowsPerChunk == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (windowIndex % windowsPerChunk == 0) { |
0 |
0 |
0 |
|
| 30 |
0 |
| *chunkIndex = *chunkIndex + 1; |
| 31 |
|
| char indexBuffer[10]; |
| 32 |
|
| char chunkName[100]; |
| 33 |
0 |
| sprintf(indexBuffer, "%d", *chunkIndex); |
| 34 |
0 |
| strcpy(chunkName, outputDir); |
| 35 |
0 |
| strcat(chunkName, nopathFile); |
| 36 |
0 |
| strcat(chunkName, "_chunk"); |
| 37 |
0 |
| strcat(chunkName, indexBuffer); |
| 38 |
0 |
| strcat(chunkName, ".txt"); |
| 39 |
0 |
▶ | if (chunk != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chunk != NULL) { |
0 |
0 |
0 |
|
| 40 |
0 |
| fclose(chunk); |
| 41 |
|
| } |
| 42 |
0 |
▶ | if (!(chunk = fopen(chunkName, "wt"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(chunk = fopen(chunkName, "wt"))) { |
0 |
0 |
0 |
|
| 43 |
0 |
| printf("GenoScan error: Unable to write '%s' chunk\n", chunkName); |
| 44 |
0 |
| exit(EXIT_FAILURE); |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
|
| //Reset window |
| 49 |
0 |
| strcpy(window, ""); |
| 50 |
|
| |
| 51 |
|
| //Copy nucleotides from sequence |
| 52 |
|
| int windowStop; |
| 53 |
0 |
▶ | if (seqLen - windowStart < windowSize){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (seqLen - windowStart < windowSize){ |
0 |
0 |
0 |
|
| 54 |
0 |
| windowStop = seqLen - windowStart; |
| 55 |
0 |
| breakSwitch = 1; |
| 56 |
|
| } |
| 57 |
|
| else { |
| 58 |
0 |
| windowStop = windowSize; |
| 59 |
|
| } |
| 60 |
0 |
▶ | for (nucIndex = 0; nucIndex < windowStop; nucIndex++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (nucIndex = 0; nucIndex < windowStop; nucIndex++) { |
0 |
0 |
0 |
|
| 61 |
0 |
| window[nucIndex] = sequence[windowStart + nucIndex]; |
| 62 |
|
| } |
| 63 |
0 |
| window[nucIndex] = '\n'; |
| 64 |
0 |
| window[nucIndex+1] = '\n'; |
| 65 |
0 |
| window[nucIndex+2] = '\0'; |
| 66 |
|
| |
| 67 |
|
| //Create header |
| 68 |
|
| char header[200]; |
| 69 |
|
| char nucStart[100]; |
| 70 |
|
| char nucEnd[100]; |
| 71 |
0 |
| int headerStart = windowStart + offset; |
| 72 |
0 |
| int headerEnd = windowStart + offset + nucIndex; |
| 73 |
0 |
| sprintf(nucStart, "%d", headerStart); |
| 74 |
0 |
| sprintf(nucEnd, "%d", headerEnd); |
| 75 |
0 |
| strcpy(header, seqHeader); |
| 76 |
0 |
| strcat(header, " | pos "); |
| 77 |
0 |
| strcat(header, nucStart); |
| 78 |
0 |
| strcat(header, "-"); |
| 79 |
0 |
| strcat(header, nucEnd); |
| 80 |
0 |
| strcat(header, "\n"); |
| 81 |
|
| |
| 82 |
|
| //Write to file |
| 83 |
0 |
| fwrite(header, sizeof(char), strlen(header), chunk); |
| 84 |
0 |
| fwrite(window, sizeof(char), strlen(window), chunk); |
| 85 |
|
| |
| 86 |
0 |
▶ | if (breakSwitch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (breakSwitch) { |
0 |
0 |
0 |
|
| 87 |
0 |
| break; |
| 88 |
|
| } |
| 89 |
|
| } |
| 90 |
0 |
| fclose(chunk); |
| 91 |
0 |
| } |
| 92 |
|
| |
| 93 |
0 |
| int segmentize(int* filterFlags, char* manifest, char* outputDir, int VERBOSE) { |
| 94 |
|
| FILE *inputFile; |
| 95 |
0 |
| int numFiles = 0; |
| 96 |
|
| char line[200]; |
| 97 |
|
| char fileArray[100][100]; |
| 98 |
|
| |
| 99 |
0 |
▶ | if (!(inputFile = fopen(manifest, "rt"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(inputFile = fopen(manifest, "rt"))) { |
0 |
0 |
0 |
|
| 100 |
0 |
| printf("GenoScan error: Input manifest '%s' could not be read\n", manifest); |
| 101 |
0 |
| exit(EXIT_FAILURE); |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| //Read input file |
| 105 |
0 |
▶ | while (fgets(line, sizeof(line), inputFile) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (fgets(line, sizeof(line), inputFile) != NULL) { |
0 |
0 |
0 |
|
| 106 |
0 |
| int len = strlen(line); |
| 107 |
0 |
▶ | if(line[len-1] == '\n'){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(line[len-1] == '\n'){ |
0 |
0 |
0 |
|
| 108 |
0 |
| strncpy(fileArray[numFiles], line, len-1); |
| 109 |
0 |
| fileArray[numFiles][len-1] = '\0'; |
| 110 |
|
| } |
| 111 |
|
| else{ |
| 112 |
0 |
| strcpy(fileArray[numFiles], line); |
| 113 |
|
| } |
| 114 |
0 |
| numFiles++; |
| 115 |
|
| } |
| 116 |
0 |
| numFiles = numFiles / 2; |
| 117 |
0 |
| fclose(inputFile); |
| 118 |
|
| |
| 119 |
|
| //Read sequence files |
| 120 |
|
| int file; |
| 121 |
0 |
| int fileCounter = -1; |
| 122 |
0 |
| int seqMemory = 280000000; |
| 123 |
0 |
| char* sequence = malloc(seqMemory * sizeof(char)); |
| 124 |
0 |
▶ | for (file = 0; file < numFiles * 2; file+=2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (file = 0; file < numFiles * 2; file+=2) { |
0 |
0 |
0 |
|
| 125 |
0 |
| fileCounter++; |
| 126 |
|
| char seqfile[100]; |
| 127 |
|
| char nopathFile[100]; |
| 128 |
0 |
| strcpy(seqfile, fileArray[file]); |
| 129 |
0 |
| strcpy(nopathFile, fileArray[file+1]); |
| 130 |
0 |
| int fileIndex = fileCounter + 1; |
| 131 |
0 |
▶ | if (VERBOSE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (VERBOSE) { |
0 |
0 |
0 |
|
| 132 |
0 |
| printf(" Segmentizing file %d/%d\r", fileIndex, numFiles); |
| 133 |
|
| } |
| 134 |
0 |
| fflush(stdout); |
| 135 |
|
| char buffer[200]; |
| 136 |
|
| int nucIndex; |
| 137 |
|
| FILE *fp; |
| 138 |
|
| char fileHeader[200]; |
| 139 |
|
| |
| 140 |
|
| //Open sequence file |
| 141 |
0 |
▶ | if (!(fp = fopen(seqfile, "rt"))){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(fp = fopen(seqfile, "rt"))){ |
0 |
0 |
0 |
|
| 142 |
0 |
| printf("GenoScan error: Unable to read input sequence file '%s'\n", seqfile); |
| 143 |
0 |
| exit(EXIT_FAILURE); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| //Segmentize chromosome sequence |
| 147 |
0 |
| int chunkIndex = 0; |
| 148 |
0 |
▶ | if(!filterFlags[fileCounter]){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!filterFlags[fileCounter]){ |
0 |
0 |
0 |
|
| 149 |
0 |
| fgets(line, sizeof(line), fp); |
| 150 |
0 |
| strncpy(fileHeader, line, strlen(line) - 1); |
| 151 |
0 |
| fileHeader[strlen(line) - 1] = '\0'; |
| 152 |
0 |
| int nucRead = 0; |
| 153 |
0 |
▶ | while(fgets(line, sizeof(line), fp) != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(fgets(line, sizeof(line), fp) != NULL){ |
0 |
0 |
0 |
|
| 154 |
0 |
| int length = strlen(line); |
| 155 |
0 |
| strcpy(buffer, line); |
| 156 |
0 |
| int nucBuffer = 0; |
| 157 |
0 |
▶ | for (nucIndex = 0; nucIndex < length; nucIndex++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (nucIndex = 0; nucIndex < length; nucIndex++) { |
0 |
0 |
0 |
|
| 158 |
0 |
▶ | if (buffer[nucIndex] != '\n') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer[nucIndex] != '\n') { |
0 |
0 |
0 |
|
| 159 |
0 |
| sequence[nucRead + nucIndex] = buffer[nucIndex]; |
| 160 |
0 |
| nucBuffer++; |
| 161 |
|
| } |
| 162 |
|
| } |
| 163 |
0 |
| nucRead += nucBuffer; |
| 164 |
|
| } |
| 165 |
0 |
| sequence[nucRead] = '\0'; |
| 166 |
0 |
| seq2chunks(sequence, fileHeader, outputDir, nopathFile, 1, &chunkIndex); |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| //Segmentize substring sequences |
| 170 |
|
| else{ |
| 171 |
0 |
▶ | while(fgets(line, sizeof(line), fp) != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(fgets(line, sizeof(line), fp) != NULL){ |
0 |
0 |
0 |
|
| 172 |
0 |
| strncpy(fileHeader, line, strlen(line) - 1); |
| 173 |
0 |
| fileHeader[strlen(line) - 1] = '\0'; |
| 174 |
|
| int subStart; |
| 175 |
0 |
| fgets(line, sizeof(line), fp); |
| 176 |
0 |
| subStart = atoi(line); |
| 177 |
0 |
| fgets(line, sizeof(line), fp); |
| 178 |
0 |
| fgets(sequence, seqMemory, fp); |
| 179 |
0 |
| seq2chunks(sequence, fileHeader, outputDir, nopathFile, subStart, &chunkIndex); |
| 180 |
0 |
| strcpy(sequence, ""); |
| 181 |
|
| } |
| 182 |
|
| } |
| 183 |
0 |
| fclose(fp); |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
|
| //Free sequnce memory |
| 187 |
0 |
| free(sequence); |
| 188 |
|
| |
| 189 |
0 |
▶ | if(VERBOSE){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(VERBOSE){ |
0 |
0 |
0 |
|
| 190 |
0 |
| printf("\n"); |
| 191 |
|
| } |
| 192 |
0 |
| return 1; |
| 193 |
|
| } |
| 194 |
|
| |