modified: nfig1.py
[GalaxyCodeBases.git] / BGI / BASE / src / 2bwt / FASTAToText.c
blob75928f52562c1222b8a225b56d84f99807671ff9
1 /* FASTAtoText.c FASTA to Text
3 Copyright 2004, Wong Chi Kwong, all rights reserved.
5 This module convert FASTA DNA format to plain text format.
7 This software may be used freely for any purpose. However, when distributed,
8 the original source must be clearly stated, and, when the source code is
9 distributed, the copyright notice must be retained and any alterations in
10 the code must be clearly marked. No warranty is given regarding the quality
11 of this software.
15 #include <stdio.h>
16 #include "TypeNLimit.h"
17 #include <stdlib.h>
18 #include <time.h>
20 int main(int argc, char** argv) {
22 int c;
23 FILE* inputFile;
24 FILE* outputFile;
25 char uppercase[256];
26 char lowercase[256];
27 char dna[4] = {'A', 'C', 'G', 'T'};
29 if (argc != 3 && argc != 4) {
30 fprintf(stderr, "%s <input> <output>\n", argv[0]);
31 fprintf(stderr, "%s <input> <output> a\n", argv[0]);
32 fprintf(stderr, "%s <input> <output> n\n", argv[0]);
33 fprintf(stderr, "%s <input> <output> r\n", argv[0]);
34 return 1;
36 inputFile = (FILE*)fopen64(argv[1], "r");
37 if (inputFile == NULL) {
38 fprintf(stderr, "Input file not exists!\n");
39 return 1;
41 outputFile = (FILE*)fopen64(argv[2], "r");
42 if (outputFile != NULL) {
43 fprintf(stderr, "Output file exists!\n");
44 return 1;
46 outputFile = (FILE*)fopen64(argv[2], "wb");
48 srand(clock());
50 for (c=0; c<256; c++) {
51 uppercase[c] = (char)c;
53 for (c='a'; c<='z'; c++) {
54 uppercase[c] = (char)c + 'A' - 'a';
56 for (c=0; c<256; c++) {
57 lowercase[c] = (char)c;
59 for (c='A'; c<='Z'; c++) {
60 lowercase[c] = (char)c + 'a' - 'A';
63 c = fgetc(inputFile);
64 while (c == '\n' || c == '\r') {
65 c = fgetc(inputFile);
67 while (c == '>') {
68 while (c != '\n' && c != '\r' && c != EOF) {
69 c = fgetc(inputFile);
71 while (c == '\n' || c == '\r') {
72 c = fgetc(inputFile);
76 while (c != EOF) {
77 if (argc == 4 || (c != 'n' && c != 'N')) {
78 if ((c == 'n' || c == 'N') && (argc == 4 && argv[3][0] == 'a')) {
79 if (c == 'n') {
80 fputc('a', outputFile);
81 } else {
82 fputc('A', outputFile);
84 } else {
85 if ((c == 'n' || c == 'N') && (argc == 4 && argv[3][0] == 'r')) {
86 if (c == 'n') {
87 fputc(lowercase[dna[rand() % 4]], outputFile);
88 } else {
89 fputc(uppercase[dna[rand() % 4]], outputFile);
91 } else {
92 fputc(c, outputFile);
96 c = fgetc(inputFile);
97 while (c == '\n' || c == '\r') {
98 c = fgetc(inputFile);
100 while (c == '>') {
101 while (c != '\n' && c != '\r' && c != EOF) {
102 c = fgetc(inputFile);
104 while (c == '\n' || c == '\r') {
105 c = fgetc(inputFile);
110 fclose(inputFile);
111 fclose(outputFile);