File: fastq-lib.h

package info (click to toggle)
ea-utils 1.1.2+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 840 kB
  • sloc: ansic: 11,459; cpp: 3,752; perl: 426; makefile: 74; sh: 42
file content (113 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download | duplicates (3)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (c) 2011 Expression Analysis / Erik Aronesty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// 32-bit o/s support
#if defined(__i386__)
	#define _FILE_OFFSET_BITS 64
#endif

// standard libs
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <assert.h>
#include <math.h>
#include <sys/stat.h>
#include <search.h>
#include <limits.h>
#include <stdint.h>
#include <stddef.h>

#if defined(__APPLE__)
	#define getopt(a,b,c) getopt_long(a,b,c,NULL,NULL)
#endif

// misc useful macros
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define meminit(l) (memset(&l,0,sizeof(l)))
#define fail(s,...) ((fprintf(stderr,s,##__VA_ARGS__), exit(1)))
#define warn(s,...) ((fprintf(stderr,s,##__VA_ARGS__)))
#define stdev(cnt, sum, ssq) sqrt((((double)cnt)*ssq-pow((double)sum,2)) / ((double)cnt*((double)cnt-1)))

// maximum number of files that can be tracked by poorquals lib
#define MAX_FILENO_QUALS 6

// read line, read fq
typedef struct line {
        char *s; int n; size_t a;
} line;

struct fq {
        line id;
        line seq;
        line com;
        line qual;
};


void free_line(struct line *l);
void free_fq(struct fq *fq);

// not GNU?  probably no getline & strtok_r...
#if !defined( __GNUC__) || defined(WIN32) || defined(__APPLE__)
	ssize_t getline(char **lineptr, size_t *n, FILE *stream);
	char* strtok_r(char *str, const char *delim, char **nextp);
#endif
    
// get file extension
const char *fext(const char *f);

// read fq
int read_line(FILE *in, struct line &l);                // 0=done, 1=ok, -1=err+continue
int read_fq(FILE *in, int rno, struct fq *fq, const char *name=NULL);          // 0=done, 1=ok, -1=err+continue
void free_fq(struct fq *fq);

// open a file, possibly gzipped, exit on failure
FILE *gzopen(const char *in, const char *mode, bool *isgz);
int gzclose(FILE *f, bool isgz);

// keep track of poor quals (n == "file number", maybe should have persistent stat struct instead?)
bool poorqual(int n, int l, const char *s, const char *q);

// returns number of differences between 2 strings, where n is the "max-length to check"
inline int hd(char *a, char *b, int n) {
        int d=0;
        //if (debug) fprintf(stderr, "hd: %s,%s ", a, b);
        while (*a && *b && n > 0) {
                if (*a != *b) ++d;
                --n;
                ++a;
                ++b;
        }
        //if (debug) fprintf(stderr, ", %d/%d\n", d, n);
        return d+n;
}

// reverse complement an fq entry into a blank (memset 0) one
void revcomp(struct fq *dest, struct fq* src);