comparison fastx_toolkit-0.0.6/src/fastx_reverse_complement/fastx_reverse_complement.c @ 3:997f5136985f draft default tip

Uploaded
author xilinxu
date Thu, 14 Aug 2014 04:52:17 -0400
parents
children
comparison
equal deleted inserted replaced
2:dfe9332138cf 3:997f5136985f
1 /*
2 FASTX-toolkit - FASTA/FASTQ preprocessing tools.
3 Copyright (C) 2009 A. Gordon (gordon@cshl.edu)
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <err.h>
25
26 #include <config.h>
27
28 #include "fastx.h"
29 #include "fastx_args.h"
30
31 const char* usage=
32 "usage: fastx_reverse_complement [-h] [-r] [-z] [-v] [-i INFILE] [-o OUTFILE]\n" \
33 "\n" \
34 "version " VERSION "\n" \
35 " [-h] = This helpful help screen.\n" \
36 " [-z] = Compress output with GZIP.\n" \
37 " [-i INFILE] = FASTA/Q input file. default is STDIN.\n" \
38 " [-o OUTFILE] = FASTA/Q output file. default is STDOUT.\n" \
39 "\n";
40
41 FASTX fastx;
42
43 char reverse_complement_base ( const char input )
44 {
45 switch(input)
46 {
47 case 'N':
48 return 'N';
49 case 'n':
50 return 'n';
51 case 'A':
52 return 'T';
53 case 'T':
54 return 'A';
55 case 'G':
56 return 'C';
57 case 'C':
58 return 'G';
59 case 'a':
60 return 't';
61 case 't':
62 return 'a';
63 case 'g':
64 return 'c';
65 case 'c':
66 return 'g';
67 default:
68 errx(1,"Invalid nucleotide value (%c) in reverse_complement_base()", input );
69 }
70
71 }
72
73 void reverse_complement_fastx(FASTX* pFASTX)
74 {
75 int i,j ;
76 int length = strlen(pFASTX->nucleotides);
77
78 char temp_nuc;
79 int temp_qual;
80
81 for (i=0;i<length;i++)
82 pFASTX->nucleotides[i] = reverse_complement_base ( pFASTX->nucleotides[i] ) ;
83
84 i = 0 ;
85 j = length - 1 ;
86 while ( i < j ) {
87 //Swap the nucleotides
88 temp_nuc = pFASTX->nucleotides[i] ;
89 pFASTX->nucleotides[i] = pFASTX->nucleotides[j] ;
90 pFASTX->nucleotides[j] = temp_nuc;
91
92 //Swap the quality scores
93 if (pFASTX->read_fastq) {
94 temp_qual = pFASTX->quality[i];
95 pFASTX->quality[i] = pFASTX->quality[j];
96 pFASTX->quality[j] = temp_qual ;
97 }
98
99 //Advance to next position
100 i++;
101 j--;
102 }
103 }
104
105
106 int main(int argc, char* argv[])
107 {
108 fastx_parse_cmdline(argc, argv, "", NULL);
109
110 fastx_init_reader(&fastx, get_input_filename(),
111 FASTA_OR_FASTQ, ALLOW_N, REQUIRE_UPPERCASE);
112
113 fastx_init_writer(&fastx, get_output_filename(), OUTPUT_SAME_AS_INPUT, compress_output_flag());
114
115 while ( fastx_read_next_record(&fastx) ) {
116 reverse_complement_fastx(&fastx);
117 fastx_write_record(&fastx);
118 }
119
120 if ( verbose_flag() ) {
121 fprintf(get_report_file(), "Printing Reverse-Complement Sequences.\n" );
122 fprintf(get_report_file(), "Input: %zu reads.\n", num_input_reads(&fastx) ) ;
123 fprintf(get_report_file(), "Output: %zu reads.\n", num_output_reads(&fastx) ) ;
124 }
125 return 0;
126 }