File: writefasta.cpp

package info (click to toggle)
piler 0~20140707-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 360 kB
  • sloc: cpp: 5,369; makefile: 39
file content (62 lines) | stat: -rwxr-xr-x 1,188 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "piler2.h"


unsigned char CompChar[256];

static void InitCompChar()
	{
	for (unsigned i = 0; i < 256; ++i)
		CompChar[i] = (unsigned char) i;

	CompChar['a'] = 't';
	CompChar['c'] = 'g';
	CompChar['g'] = 'c';
	CompChar['t'] = 'a';
	CompChar['n'] = 'n';
	CompChar['A'] = 'T';
	CompChar['C'] = 'G';
	CompChar['G'] = 'C';
	CompChar['T'] = 'A';
	}

void WriteFasta(FILE *f, const char *Seq, int Length, const char *Label, bool Rev)
	{
	static bool CompCharInit = false;
	if (!CompCharInit)
		{
		InitCompChar();
		CompCharInit = true;
		}

	fprintf(f, ">%s\n", Label);

	if (Rev)
		{
		int Index = Length;
		for (int i = 0; i < Length; i += FASTA_BLOCK)
			{
			int n = FASTA_BLOCK;
			if (i + n > Length)
				n = Length - i;
			for (int j = 0; j < n; ++j)
				{
				const unsigned char c = Seq[--Index];
				const unsigned char cComp = CompChar[c];
				fputc(cComp, f);
				}
			fputc('\n', f);
			}
		}
	else
		{
		for (int i = 0; i < Length; i += FASTA_BLOCK)
			{
			int n = FASTA_BLOCK;
			if (i + n > Length)
				n = Length - i;
			for (int j = 0; j < n; ++j)
				fputc(*Seq++, f);
			fputc('\n', f);
			}
		}
	}