annotate IMSAME/src/reverseComplement.c @ 0:762009a91895 draft

Uploaded
author bitlab
date Sat, 15 Dec 2018 18:04:10 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
762009a91895 Uploaded
bitlab
parents:
diff changeset
1 /* reverseComplement.c
762009a91895 Uploaded
bitlab
parents:
diff changeset
2 Program reading a FASTA file as input (one or multiple sequence)
762009a91895 Uploaded
bitlab
parents:
diff changeset
3 and then returning the reverse complementary sequence in the output file.
762009a91895 Uploaded
bitlab
parents:
diff changeset
4 It is important to note that for multiple sequence file the first input
762009a91895 Uploaded
bitlab
parents:
diff changeset
5 sequence is the last one at the output file and the last one in the input
762009a91895 Uploaded
bitlab
parents:
diff changeset
6 is the first one in the output.
762009a91895 Uploaded
bitlab
parents:
diff changeset
7 oscart@uma.es
762009a91895 Uploaded
bitlab
parents:
diff changeset
8 */
762009a91895 Uploaded
bitlab
parents:
diff changeset
9
762009a91895 Uploaded
bitlab
parents:
diff changeset
10 #include <stdio.h>
762009a91895 Uploaded
bitlab
parents:
diff changeset
11 #include <stdlib.h>
762009a91895 Uploaded
bitlab
parents:
diff changeset
12 #include <string.h>
762009a91895 Uploaded
bitlab
parents:
diff changeset
13 #include <ctype.h>
762009a91895 Uploaded
bitlab
parents:
diff changeset
14 #include <inttypes.h>
762009a91895 Uploaded
bitlab
parents:
diff changeset
15 #include "commonFunctions.h"
762009a91895 Uploaded
bitlab
parents:
diff changeset
16
762009a91895 Uploaded
bitlab
parents:
diff changeset
17 #define SEQSIZE 2000000000
762009a91895 Uploaded
bitlab
parents:
diff changeset
18 #define READINPUT 10000
762009a91895 Uploaded
bitlab
parents:
diff changeset
19 #define WSIZE 32
762009a91895 Uploaded
bitlab
parents:
diff changeset
20 #define NREADS 1000000
762009a91895 Uploaded
bitlab
parents:
diff changeset
21
762009a91895 Uploaded
bitlab
parents:
diff changeset
22 int main(int ac, char** av) {
762009a91895 Uploaded
bitlab
parents:
diff changeset
23 FILE *fIn, *fOut;
762009a91895 Uploaded
bitlab
parents:
diff changeset
24 int64_t i, j, nR, seqLen = 0;
762009a91895 Uploaded
bitlab
parents:
diff changeset
25 char *seq, c, toW;
762009a91895 Uploaded
bitlab
parents:
diff changeset
26 long *offset = NULL;
762009a91895 Uploaded
bitlab
parents:
diff changeset
27
762009a91895 Uploaded
bitlab
parents:
diff changeset
28 if (ac != 3)
762009a91895 Uploaded
bitlab
parents:
diff changeset
29 terror("USE: reverseComplement seqFile.IN reverseComplementarySeq.OUT");
762009a91895 Uploaded
bitlab
parents:
diff changeset
30
762009a91895 Uploaded
bitlab
parents:
diff changeset
31 /**
762009a91895 Uploaded
bitlab
parents:
diff changeset
32 * Allocating memory for the sequence
762009a91895 Uploaded
bitlab
parents:
diff changeset
33 * Fixed amount of memory, change it to loadSeqDB?
762009a91895 Uploaded
bitlab
parents:
diff changeset
34 */
762009a91895 Uploaded
bitlab
parents:
diff changeset
35 if ((seq = (char*) malloc(SEQSIZE)) == NULL)
762009a91895 Uploaded
bitlab
parents:
diff changeset
36 terror("memory for Seq");
762009a91895 Uploaded
bitlab
parents:
diff changeset
37
762009a91895 Uploaded
bitlab
parents:
diff changeset
38 if ((fIn = fopen(av[1], "rt")) == NULL)
762009a91895 Uploaded
bitlab
parents:
diff changeset
39 terror("opening IN sequence FASTA file");
762009a91895 Uploaded
bitlab
parents:
diff changeset
40
762009a91895 Uploaded
bitlab
parents:
diff changeset
41 if ((fOut = fopen(av[2], "wt")) == NULL)
762009a91895 Uploaded
bitlab
parents:
diff changeset
42 terror("opening OUT sequence Words file");
762009a91895 Uploaded
bitlab
parents:
diff changeset
43
762009a91895 Uploaded
bitlab
parents:
diff changeset
44 if ((offset = (long *) malloc(sizeof(long)*NREADS)) == NULL)
762009a91895 Uploaded
bitlab
parents:
diff changeset
45 terror("memory for offsets");
762009a91895 Uploaded
bitlab
parents:
diff changeset
46
762009a91895 Uploaded
bitlab
parents:
diff changeset
47 for(i=0;i<NREADS;i++){
762009a91895 Uploaded
bitlab
parents:
diff changeset
48 offset[i]=0;
762009a91895 Uploaded
bitlab
parents:
diff changeset
49 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
50
762009a91895 Uploaded
bitlab
parents:
diff changeset
51 nR = 0;
762009a91895 Uploaded
bitlab
parents:
diff changeset
52 c = fgetc(fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
53 while(!feof(fIn)){
762009a91895 Uploaded
bitlab
parents:
diff changeset
54 if(c == '>'){
762009a91895 Uploaded
bitlab
parents:
diff changeset
55 offset[nR++] = ftell(fIn)-1;
762009a91895 Uploaded
bitlab
parents:
diff changeset
56 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
57 c = fgetc(fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
58 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
59
762009a91895 Uploaded
bitlab
parents:
diff changeset
60 for(i=nR-1; i>=0; i--){
762009a91895 Uploaded
bitlab
parents:
diff changeset
61 fseek(fIn, offset[i], SEEK_SET);
762009a91895 Uploaded
bitlab
parents:
diff changeset
62 //READ and write header
762009a91895 Uploaded
bitlab
parents:
diff changeset
63 fgets(seq, READINPUT, fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
64
762009a91895 Uploaded
bitlab
parents:
diff changeset
65 fprintf(fOut, "%s", seq);
762009a91895 Uploaded
bitlab
parents:
diff changeset
66 //READ and write sequence
762009a91895 Uploaded
bitlab
parents:
diff changeset
67 c = fgetc(fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
68 while(c != '>' && !feof(fIn)){
762009a91895 Uploaded
bitlab
parents:
diff changeset
69 if(isupper(c) || islower(c)){
762009a91895 Uploaded
bitlab
parents:
diff changeset
70 seq[seqLen++]=c;
762009a91895 Uploaded
bitlab
parents:
diff changeset
71 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
72 c = fgetc(fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
73 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
74 for(j=seqLen-1; j >= 0; j--){
762009a91895 Uploaded
bitlab
parents:
diff changeset
75 switch(seq[j]){
762009a91895 Uploaded
bitlab
parents:
diff changeset
76 case 'A':
762009a91895 Uploaded
bitlab
parents:
diff changeset
77 toW = 'T';
762009a91895 Uploaded
bitlab
parents:
diff changeset
78 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
79 case 'C':
762009a91895 Uploaded
bitlab
parents:
diff changeset
80 toW = 'G';
762009a91895 Uploaded
bitlab
parents:
diff changeset
81 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
82 case 'G':
762009a91895 Uploaded
bitlab
parents:
diff changeset
83 toW = 'C';
762009a91895 Uploaded
bitlab
parents:
diff changeset
84 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
85 case 'T':
762009a91895 Uploaded
bitlab
parents:
diff changeset
86 toW = 'A';
762009a91895 Uploaded
bitlab
parents:
diff changeset
87 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
88 case 'U':
762009a91895 Uploaded
bitlab
parents:
diff changeset
89 toW = 'A';
762009a91895 Uploaded
bitlab
parents:
diff changeset
90 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
91 case 'a':
762009a91895 Uploaded
bitlab
parents:
diff changeset
92 toW = 't';
762009a91895 Uploaded
bitlab
parents:
diff changeset
93 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
94 case 'c':
762009a91895 Uploaded
bitlab
parents:
diff changeset
95 toW = 'g';
762009a91895 Uploaded
bitlab
parents:
diff changeset
96 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
97 case 'g':
762009a91895 Uploaded
bitlab
parents:
diff changeset
98 toW = 'c';
762009a91895 Uploaded
bitlab
parents:
diff changeset
99 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
100 case 't':
762009a91895 Uploaded
bitlab
parents:
diff changeset
101 toW = 'a';
762009a91895 Uploaded
bitlab
parents:
diff changeset
102 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
103 case 'u':
762009a91895 Uploaded
bitlab
parents:
diff changeset
104 toW = 'a';
762009a91895 Uploaded
bitlab
parents:
diff changeset
105 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
106 default:
762009a91895 Uploaded
bitlab
parents:
diff changeset
107 toW = seq[j];
762009a91895 Uploaded
bitlab
parents:
diff changeset
108 break;
762009a91895 Uploaded
bitlab
parents:
diff changeset
109 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
110 fwrite(&toW, sizeof(char), 1, fOut);
762009a91895 Uploaded
bitlab
parents:
diff changeset
111 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
112 toW='\n';
762009a91895 Uploaded
bitlab
parents:
diff changeset
113 fwrite(&toW, sizeof(char), 1, fOut);
762009a91895 Uploaded
bitlab
parents:
diff changeset
114 seqLen=0;
762009a91895 Uploaded
bitlab
parents:
diff changeset
115 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
116
762009a91895 Uploaded
bitlab
parents:
diff changeset
117 fclose(fIn);
762009a91895 Uploaded
bitlab
parents:
diff changeset
118 fclose(fOut);
762009a91895 Uploaded
bitlab
parents:
diff changeset
119
762009a91895 Uploaded
bitlab
parents:
diff changeset
120 return 0;
762009a91895 Uploaded
bitlab
parents:
diff changeset
121 }
762009a91895 Uploaded
bitlab
parents:
diff changeset
122
762009a91895 Uploaded
bitlab
parents:
diff changeset
123