├── shm.h ├── align.h ├── index.h ├── Makefile ├── .gitignore ├── main.c ├── utils.h ├── README.md ├── utils.c ├── index.c ├── shm.c ├── align.c ├── kseq.h ├── ksort.h └── LICENSE /shm.h: -------------------------------------------------------------------------------- 1 | #ifndef SHM_M 2 | #define SHM_M 3 | 4 | int mapper_shm(int argc, char *argv[]); 5 | #endif -------------------------------------------------------------------------------- /align.h: -------------------------------------------------------------------------------- 1 | #ifndef ALIGN_H 2 | #define ALIGN_H 3 | 4 | int mapper_align(int argc, char *argv[]); 5 | #endif -------------------------------------------------------------------------------- /index.h: -------------------------------------------------------------------------------- 1 | #ifndef INDEX_H 2 | #define INDEX_H 3 | 4 | int mapper_index(int argc, char *argv[]); 5 | 6 | #endif -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-W -Wall -O2 -std=c99 3 | LDFLAGS=-lz -lrt 4 | 5 | mapper: main.o utils.o utils.h index.o index.h align.o align.h shm.o shm.h 6 | $(CC) -o mapper main.o utils.o index.o align.o shm.o $(LDFLAGS) 7 | 8 | test: utils.o 9 | $(CC) -o test utils.o $(LDFLAGS) 10 | 11 | dump: dump_idx.o utils.o utils.h 12 | $(CC) -o dump utils.o dump_idx.o $(LDFLAGS) 13 | 14 | clean: 15 | rm -f mapper 16 | rm -f *.o 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | #include "index.h" 3 | #include "align.h" 4 | #include "shm.h" 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | int ret = 0; 9 | if (argc < 2) { 10 | printf("Usage: mapper [options]\n\n"); 11 | printf("command: index index sequences in the FASTA format\n"); 12 | printf(" align reads alignment\n"); 13 | printf(" shm manage indices in shared memory\n\n"); 14 | } 15 | else if (strcmp(argv[1], "index") == 0) ret = mapper_index(argc - 1, argv + 1); 16 | else if (strcmp(argv[1], "align") == 0) ret = mapper_align(argc - 1, argv + 1); 17 | else if (strcmp(argv[1], "shm") == 0) ret = mapper_shm(argc - 1, argv + 1); 18 | else { 19 | printf("[main] unrecognized command '%s'\n", argv[1]); 20 | ret = -1; 21 | } 22 | 23 | return ret; 24 | } -------------------------------------------------------------------------------- /utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define GENOME_LEN 3100000000 // length of human genome 12 | #define MER_LEN 36 // must be multiples of 4 13 | 14 | typedef struct 15 | { 16 | uint8_t bnt[MER_LEN / 4]; // 2-bit format of mer. bnt(bit nucleotide) 17 | uint8_t chr; // chromosome number. i.e. 1, 2, 3, ..., 22, 23(X), 24(Y), 25(M) 18 | uint8_t gc; // GC count of this 36-mer 19 | uint32_t pos; // chromosome position start at 0 20 | } meridx_t; // meridx(mer index) 21 | 22 | 23 | uint8_t *mer2bytes(const char *mer); 24 | char *revcmpl(const char *mer); 25 | bool check_nt(char nt); 26 | bool check_mer(const char *mer); 27 | char *get_fn(char *path); 28 | 29 | 30 | #define UTILS_H 31 | #endif 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://img.shields.io/badge/License-GPL-brightgreen.svg) 2 | ![](https://img.shields.io/badge/build-pass-brightgreen.svg) 3 | ![](https://img.shields.io/badge/download-1k-brightgreen.svg) 4 | 5 | # mapper: 用于 NIPT 和 CNV 的短序列比对工具 6 | 7 | ## 0. 简介 8 | 9 | mapper 是一个短序列比对工具,它可以高效地将固定长度的短序列比对到人基因组上。非常适合 NIPT/CNV 等低覆盖度二代测序产生的短序列的比对。它具有以下特点: 10 | 11 | - 固定长度的短序列 12 | - 精准匹配(不支持错配和 gap) 13 | - 速度快 10M条/分钟,且并行效率不降低 14 | 15 | 从原理上讲,mapper 是基于索引的序列比对。在比对之前需要对人基因组进行索引,这个过程大约耗时 20~30 分钟,生成的索引文件大小约 38G。这个索引文件将被加载并常驻于共享内存中。之后的比对会直接检索存放于共享内存中的索引,速度非常快,并允许多个进程访问,彼此不影响,并行效率高。在实际应用中,对于一张测序芯片(96样本,每个样本数据量大约为 10M 对 PE reads),32核并行处理,比对时间在 5 分钟左右,因此,非常适合临床应用。 16 | 17 | ## 1. 系统要求 18 | 19 | mapper 适用于服务器,不适合个人电脑。 20 | 21 | - 硬盘大于 50G 22 | - 内存大于 50G 23 | - Linux 系统 24 | 25 | ## 2. 安装 26 | 27 | ``` 28 | git clone https://github.com/jia-zhuang/mapper.git 29 | cd mapper 30 | make 31 | ``` 32 | 33 | 产生可执行文件 mapper,可以拷贝到任意位置使用。 34 | 35 | ## 3. 使用 36 | 37 | ##### 建立索引 38 | 39 | 下载人参考基因组文件(hg19 或 hg38),预处理,只保留 1-22,X,Y,M 共 25 条染色体,且染色体命名规则为 chrN(N = 1-22,X,Y,M)。 40 | 41 | ```bash 42 | mapper index hg19.fa 43 | ``` 44 | 45 | ##### 将索引加载到共享内存 46 | 47 | ```bash 48 | mapper shm -l hg19.fa 49 | ``` 50 | 51 | 可通过如下命令检出索引是否已存在于共享内存中: 52 | 53 | ```bash 54 | mapper shm -c hg19.fa 55 | ``` 56 | 57 | 或者打印共享内存里的文件名 58 | 59 | ```bash 60 | mapper shm -p 61 | ``` 62 | 63 | 销毁共享内存中的索引 64 | ```bash 65 | mapper shm -d hg19.fa 66 | ``` 67 | 68 | ##### 比对 69 | 70 | 测序文件格式为 Fastq,支持压缩格式 .gz 71 | 72 | ```bash 73 | mapper align hg19.fa read.fq.gz # SE 74 | mapper align hg19.fa read1.fq.gz read2.fq.gz # PE 75 | ``` 76 | 77 | ##### 获取帮助 78 | 79 | 不带参数地运行命令可获取帮助 80 | 81 | ```bash 82 | mapper 83 | mapper index 84 | mapper shm 85 | mapper align 86 | ``` 87 | 88 | ## 4. 注意事项和局限性 89 | 90 | - 不适用于常规的序列比对,对于长序列、序列长度可变、允许错配和 gap的情况,请使用 [bwa](https://github.com/lh3/bwa)。 91 | - 如果某个 reads 在基因组上存在多位点,该 reads 会被丢弃。 92 | - 程序默认 reads 读长为 36bp,如果你的测序读长为其他值,编译前请修改 `utils.h` 文件中 `MER_LEN` 参数,但要确保是 4 的倍数。 93 | 94 | -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | 3 | static int8_t nt2code(char nt) { 4 | /* 5 | * Conver nucleotide to number 6 | * A/a -> 0 7 | * C/c -> 1 8 | * G/g -> 2 9 | * T/t -> 3 10 | */ 11 | switch (nt) { 12 | case 'A': 13 | return 0; 14 | case 'a': 15 | return 0; 16 | case 'C': 17 | return 1; 18 | case 'c': 19 | return 1; 20 | case 'G': 21 | return 2; 22 | case 'g': 23 | return 2; 24 | case 'T': 25 | return 3; 26 | case 't': 27 | return 3; 28 | default: 29 | return -1; // this should not happen! 30 | } 31 | } 32 | 33 | static uint8_t four_nt2one_byte(const char *nt) { 34 | /* 35 | * every nucleotide has 4 state which can be represent by 2 bit 36 | * Convert 4 nucleotide sequence to 1 byte 37 | * length(nt) must be 4 38 | */ 39 | return ( 40 | nt2code(nt[0]) << 6 | \ 41 | nt2code(nt[1]) << 4 | \ 42 | nt2code(nt[2]) << 2 | \ 43 | nt2code(nt[3]) 44 | ); 45 | } 46 | 47 | uint8_t *mer2bytes(const char *mer) { 48 | /* 49 | * Convert mer to bytes. e.g. 36-mer to 9 bytes 50 | * return a static array which will be changed by next calling, be carefull! 51 | */ 52 | static uint8_t bytes[MER_LEN / 4]; 53 | for (int i = 0; i < MER_LEN / 4; ++i) { 54 | bytes[i] = four_nt2one_byte(mer + 4*i); 55 | } 56 | return bytes; 57 | } 58 | 59 | static char cmpl(char nt) { 60 | /* 61 | * Nucleotide complement 62 | * A <=> T 63 | * C <=> G 64 | */ 65 | switch (nt) { 66 | case 'A': 67 | return 'T'; 68 | case 'a': 69 | return 't'; 70 | case 'C': 71 | return 'G'; 72 | case 'c': 73 | return 'g'; 74 | case 'g': 75 | return 'c'; 76 | case 'G': 77 | return 'C'; 78 | case 'T': 79 | return 'A'; 80 | case 't': 81 | return 'a'; 82 | default: 83 | return -1; // this should not happen! 84 | } 85 | } 86 | 87 | char *revcmpl(const char *mer) { 88 | /* 89 | * The reverse complement of 36-mer 90 | * return a static array which will be changed by next calling, be carefull! 91 | */ 92 | static char rcmpl[MER_LEN]; 93 | for (int i = 0; i < MER_LEN; ++i) { 94 | rcmpl[i] = cmpl(mer[MER_LEN - 1 - i]); 95 | } 96 | return rcmpl; 97 | } 98 | 99 | bool check_nt(char nt) { 100 | /* 101 | * Check whether nucleotide is valid. i.e. equals 'A/a' or 'C/c' or 'G/g' or 'T/t' 102 | */ 103 | if ( 104 | nt != 'A' && \ 105 | nt != 'a' && \ 106 | nt != 'C' && \ 107 | nt != 'c' && \ 108 | nt != 'G' && \ 109 | nt != 'g' && \ 110 | nt != 'T' && \ 111 | nt != 't' 112 | ) 113 | return false; 114 | else 115 | return true; 116 | } 117 | 118 | bool check_mer(const char *mer) { 119 | /* 120 | * Check whether the mer is valid. i.e. contains only 'A/a', 'C/c', 'G/g' and 'T/t' 121 | */ 122 | for (int i = 0; i < MER_LEN; ++i) { 123 | if (!check_nt(mer[i])) 124 | return false; 125 | } 126 | return true; 127 | } 128 | 129 | char *get_fn(char *path) { 130 | /* 131 | * Get file name 132 | * /path/to/ref.fa => ref.fa 133 | */ 134 | char *fn = strrchr(path, '/'); 135 | return (fn == NULL? path: fn + 1); 136 | } 137 | 138 | /* 139 | * Self Test 140 | */ 141 | 142 | /* 143 | int main(int argc, char const *argv[]) 144 | { 145 | // test nt2code 146 | for (int i = 0; i < strlen(argv[1]); ++i) { 147 | printf("%d\n", nt2code(argv[1][i])); 148 | } 149 | 150 | 151 | // test four_nt2one_byte 152 | //printf("%02x\n", (uint8_t)four_nt2one_byte(argv[1])); 153 | 154 | 155 | // test mer2bytes 156 | int8_t *bytes = mer2bytes(argv[1]); 157 | for (int i = 0; i < MER_LEN / 4; ++i) { 158 | printf("%02x\n", (uint8_t)bytes[i]); 159 | } 160 | 161 | 162 | // test revcmpl 163 | printf("%s\n", revcmpl(argv[1])); 164 | 165 | // test check_mer 166 | printf("%d\n", check_mer(argv[1])); 167 | 168 | return 0; 169 | 170 | } 171 | 172 | */ -------------------------------------------------------------------------------- /index.c: -------------------------------------------------------------------------------- 1 | #include "index.h" 2 | #include "utils.h" 3 | #include "ksort.h" 4 | #include "kseq.h" 5 | KSEQ_INIT(gzFile, gzread) 6 | 7 | #define meridx_lt(a, b) (memcmp((a).bnt, (b).bnt, MER_LEN / 4) < 0) 8 | KSORT_INIT(MERIDX_T, meridx_t, meridx_lt) 9 | 10 | static uint8_t get_chr_num(const char *chr) { 11 | /* 12 | * Conver chromosome name to number 13 | * chr1 -> 1 14 | * chr2 -> 2 15 | * ... 16 | * chrX -> 23 17 | * chrY -> 24 18 | * chrM -> 25 19 | */ 20 | if (strcmp(chr, "chrX") == 0) 21 | return 23; 22 | else if (strcmp(chr, "chrY") == 0) 23 | return 24; 24 | else if (strcmp(chr, "chrM") == 0) 25 | return 25; 26 | else { 27 | return atoi(chr + 3); 28 | } 29 | } 30 | 31 | int mapper_index(int argc, char *argv[]) 32 | { 33 | if (argc != 2) { 34 | printf("Usage: mapper index \n\n"); 35 | return 1; 36 | } 37 | 38 | // get file name: /path/to/ref.fa -> ref.fa 39 | char *fn = get_fn(argv[1]); 40 | 41 | // create/open output index file. i.e. ref.fa.idx and ref.fa.sz 42 | char *fn_idx = (char *)malloc(strlen(fn) + 5); 43 | strcpy(fn_idx, fn); strcat(fn_idx, ".idx"); 44 | FILE *fidx = fopen(fn_idx, "wb"); 45 | 46 | char *fn_sz = (char *)malloc(strlen(fn) + 4); 47 | strcpy(fn_sz, fn); strcat(fn_sz, ".sz"); 48 | FILE *fsz = fopen(fn_sz, "wb"); 49 | 50 | // open reference genome file 51 | gzFile fa = gzopen(argv[1], "r"); 52 | kseq_t *record = kseq_init(fa); 53 | 54 | // start indexing 55 | clock_t start = clock(); // record time 56 | 57 | int rcd_num = 0; 58 | char mer[MER_LEN]; 59 | uint8_t *bnt; 60 | meridx_t *meridx = (meridx_t *)malloc(GENOME_LEN * sizeof(meridx_t)); 61 | //meridx_t *meridx = (meridx_t *)calloc(GENOME_LEN, sizeof(meridx_t)); 62 | meridx_t *tmp; 63 | uint32_t idx_len = 0; 64 | while (kseq_read(record) >= 0) { 65 | printf("Record number %d, chromosome name %s, chromosome number %u, chromosome length %zu\n", rcd_num++, record->name.s, get_chr_num(record->name.s), record->seq.l); 66 | 67 | for (uint32_t i = 0; i < record->seq.l - MER_LEN + 1; ++i) { 68 | /* 69 | * Can be extended for Masked Fasta File in here 70 | */ 71 | 72 | uint8_t gc = 0; 73 | uint8_t j; 74 | // printf("***debug*** MER_LEN: %d\n", MER_LEN); 75 | for (j = 0; j < MER_LEN; ++j) { 76 | mer[j] = record->seq.s[i + j]; 77 | 78 | // printf("**debug*** check_nt(mer[%d]): %d\n", j, check_nt(mer[j])); 79 | if (!check_nt(mer[j])) break; 80 | 81 | if (mer[j] == 'G' || mer[j] == 'g' || mer[j] == 'C' || mer[j] == 'c') gc++; 82 | } 83 | 84 | // if mer pass check_nt, then j == MER_LEN 85 | // printf("***debug*** j = %u\n", j); 86 | if (j == MER_LEN) { 87 | bnt = mer2bytes(mer); 88 | tmp = meridx + idx_len; 89 | memcpy(tmp->bnt, bnt, MER_LEN / 4); 90 | tmp->chr = get_chr_num(record->name.s); 91 | tmp->gc = gc; 92 | tmp->pos = i + 1; 93 | idx_len++; 94 | } 95 | } 96 | } 97 | 98 | ks_introsort(MERIDX_T, idx_len, meridx); 99 | 100 | // remove duplicated and save index 101 | meridx_t *p = meridx; 102 | meridx_t *q = meridx + 1; 103 | uint32_t idx_len_rmdp = 0; 104 | while (p < meridx + idx_len) { 105 | if (memcmp(p->bnt, q->bnt, MER_LEN / 4) == 0) { 106 | while (q < meridx + idx_len) { 107 | q++; 108 | if (memcmp(p->bnt, q->bnt, MER_LEN / 4) != 0) { 109 | p = q++; 110 | break; 111 | } 112 | } 113 | } 114 | else { 115 | idx_len_rmdp++; 116 | fwrite(p, sizeof(meridx_t), 1, fidx); 117 | p = q++; 118 | } 119 | } 120 | 121 | printf("Length of index: %u\n", idx_len); 122 | printf("Length of unique index: %u\n", idx_len_rmdp); 123 | fwrite(&idx_len_rmdp, sizeof(uint32_t), 1, fsz); 124 | 125 | printf("Finish indexing in %fs\n", (double)(clock() - start) / CLOCKS_PER_SEC); 126 | 127 | // close file and free memory 128 | kseq_destroy(record); 129 | gzclose(fa); 130 | fclose(fidx); 131 | fclose(fsz); 132 | free(meridx); meridx = NULL; 133 | free(fn_idx); fn_idx = NULL; 134 | free(fn_sz); fn_sz = NULL; 135 | 136 | return 0; 137 | } -------------------------------------------------------------------------------- /shm.c: -------------------------------------------------------------------------------- 1 | #define _XOPEN_SOURCE 500 2 | #include "shm.h" 3 | #include "utils.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int mapper_shm(int argc, char *argv[]) 11 | { 12 | if (argc == 1 || (argc == 2 && strcmp(argv[1], "-p") != 0) || argc > 3) { 13 | printf("Usage: mapper shm [-l|-c|-p|-d] \n"); 14 | printf("Options: -l load index of ref.fa into shared memory\n"); 15 | printf(" -c check whether index of ref.fa is in shared memory\n"); 16 | printf(" -p print files in /dev/shm/\n"); 17 | printf(" -d destroy index of ref.fa in shared memeory\n\n"); 18 | return 1; 19 | } 20 | 21 | if (strcmp(argv[1], "-l") == 0) { 22 | // get file name with relative path 23 | char *path_fn_sz = (char *)malloc(strlen(argv[2]) + 4); 24 | strcpy(path_fn_sz, argv[2]); strcat(path_fn_sz, ".sz"); 25 | char *path_fn_idx = (char *)malloc(strlen(argv[2]) + 5); 26 | strcpy(path_fn_idx, argv[2]); strcat(path_fn_idx, ".idx"); 27 | 28 | // get file name 29 | char *fn_sz = get_fn(path_fn_sz); 30 | char *fn_idx = get_fn(path_fn_idx); 31 | 32 | // open sz file 33 | FILE *fsz = fopen(path_fn_sz, "rb"); 34 | uint32_t idx_len; 35 | fread(&idx_len, sizeof(uint32_t), 1, fsz); 36 | 37 | // open shared memory 38 | int fd_idx = shm_open(fn_idx, O_RDWR | O_CREAT | O_EXCL, 00666); 39 | int fd_sz = shm_open(fn_sz, O_RDWR | O_CREAT | O_EXCL, 00666); 40 | if (fd_idx == -1 || fd_sz == -1) { 41 | printf("Error loading index to shm!\n"); 42 | printf("Use command 'mapper shm -c %s' to check if the index is already in shared memory\n", argv[2]); 43 | return 2; 44 | } 45 | ftruncate(fd_idx, idx_len * sizeof(meridx_t)); 46 | ftruncate(fd_sz, sizeof(uint32_t)); 47 | void *pidx = mmap(0, idx_len*sizeof(meridx_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd_idx, 0); 48 | void *psz = mmap(0, sizeof(uint32_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd_sz, 0); 49 | 50 | // open idx file 51 | FILE *fidx = fopen(path_fn_idx, "rb"); 52 | printf("Write %zu items to shared memory\n", fread(pidx, sizeof(meridx_t), idx_len, fidx)); 53 | memcpy(psz, &idx_len, sizeof(uint32_t)); 54 | 55 | // close file and free memory 56 | munmap(fn_idx, idx_len * sizeof(meridx_t)); 57 | munmap(fn_sz, sizeof(uint32_t)); 58 | close(fd_idx); 59 | close(fd_sz); 60 | fclose(fidx); 61 | fclose(fsz); 62 | free(path_fn_sz); path_fn_sz = NULL; 63 | free(path_fn_idx); path_fn_idx = NULL; 64 | 65 | } else if (strcmp(argv[1], "-c") == 0) { 66 | char *fn = get_fn(argv[2]); 67 | char *fn_idx = (char *)malloc(strlen(fn) + 5); 68 | strcpy(fn_idx, fn); strcat(fn_idx, ".idx"); 69 | 70 | int fidx = shm_open(fn_idx, O_RDWR | O_CREAT | O_EXCL, 00666); 71 | if (fidx == -1) { 72 | printf("Index of %s is already in shared memory\n", fn); 73 | } else { 74 | printf("Index of %s is *not* in shared memory\n", fn); 75 | shm_unlink(fn_idx); 76 | close(fidx); 77 | } 78 | 79 | free(fn_idx); fn_idx = NULL; 80 | } else if (strcmp(argv[1], "-p") == 0) { 81 | system("ls /dev/shm/"); 82 | 83 | } else if (strcmp(argv[1], "-d") == 0) { 84 | char *fn = get_fn(argv[2]); 85 | // file name 86 | char *fn_sz = (char *)malloc(strlen(fn) + 4); 87 | strcpy(fn_sz, fn); strcat(fn_sz, ".sz"); 88 | char *fn_idx = (char *)malloc(strlen(fn) + 5); 89 | strcpy(fn_idx, fn); strcat(fn_idx, ".idx"); 90 | 91 | // check whether the index is in shared memory 92 | int fidx = shm_open(fn_idx, O_RDWR | O_CREAT | O_EXCL, 00666); 93 | if (fidx != -1) { 94 | printf("Index of %s is not in shared memory. Nothing to be done!\n", fn); 95 | shm_unlink(fn_idx); 96 | close(fidx); 97 | free(fn_sz); fn_sz = NULL; 98 | free(fn_idx); fn_idx = NULL; 99 | return 2; 100 | } 101 | 102 | // remove index 103 | shm_unlink(fn_idx); 104 | shm_unlink(fn_sz); 105 | free(fn_sz); fn_sz = NULL; 106 | free(fn_idx); fn_idx = NULL; 107 | printf("Successfully destroyed index of %s in shared memory\n", fn); 108 | 109 | } else { 110 | printf("[shm] wrong options: %s\n", argv[1]); 111 | return 1; 112 | } 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /align.c: -------------------------------------------------------------------------------- 1 | #include "align.h" 2 | #include "utils.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "kseq.h" 8 | KSEQ_INIT(gzFile, gzread) 9 | 10 | int meridx_cmp(const void *p, const void *q) { 11 | return memcmp(((meridx_t *)p)->bnt, ((meridx_t *)q)->bnt, MER_LEN / 4); 12 | } 13 | 14 | int mapper_align(int argc, char *argv[]) 15 | { 16 | if (argc < 3 || argc > 4) { 17 | printf("Usage: \n"); 18 | printf("For SE: mapper align \n"); 19 | printf("For PE: mapper align \n\n"); 20 | return 1; 21 | } 22 | 23 | char *fn_ref = get_fn(argv[1]); 24 | char *fn_read = get_fn(argv[2]); 25 | 26 | // check if the index if in shared memory 27 | char *fn_sz = (char *)malloc(strlen(fn_ref) + 4); 28 | strcpy(fn_sz, fn_ref); strcat(fn_sz, ".sz"); 29 | char *fn_idx = (char *)malloc(strlen(fn_ref) + 5); 30 | strcpy(fn_idx, fn_ref); strcat(fn_idx, ".idx"); 31 | 32 | int fd_idx = shm_open(fn_idx, O_RDONLY, 00444); 33 | if (fd_idx == -1) { 34 | printf("[align] Error: index of %s is not in shared memory!\n", fn_ref); 35 | printf("Use command 'mapper shm -l %s' to load first\n", fn_ref); 36 | return 2; 37 | } 38 | 39 | // load index from shared memory 40 | int fd_sz = shm_open(fn_sz, O_RDONLY, 00444); 41 | uint32_t *psz = (uint32_t *)mmap(NULL, sizeof(uint32_t), PROT_READ, MAP_SHARED, fd_sz, 0); 42 | meridx_t *pidx = (meridx_t *)mmap(NULL, *psz * sizeof(meridx_t), PROT_READ, MAP_SHARED, fd_idx, 0); 43 | 44 | // create/open output file 45 | char *fn_out = (char *)malloc(strlen(fn_read) + 5); 46 | strcpy(fn_out, fn_read); strcat(fn_out, ".out"); 47 | FILE *fout = fopen(fn_out, "w"); 48 | 49 | // SE 50 | if (argc == 3) { 51 | // read in reads file 52 | clock_t start = clock(); // record time 53 | gzFile fp_read = gzopen(argv[2], "r"); 54 | kseq_t *record = kseq_init(fp_read); 55 | meridx_t target, *pt; 56 | uint8_t *bnt; 57 | fprintf(fout, "chr\tpos\tGC\n"); 58 | while (kseq_read(record) >= 0) { 59 | if (record->seq.l == MER_LEN && check_mer(record->seq.s)) { 60 | // + 61 | bnt = mer2bytes(record->seq.s); 62 | memcpy(target.bnt, bnt, MER_LEN / 4); 63 | pt = (meridx_t *)bsearch(&target, pidx, *psz, sizeof(meridx_t), meridx_cmp); 64 | if (pt != NULL) { 65 | fprintf(fout, "%u\t%u\t%u\n", pt->chr, pt->pos, pt->gc); 66 | } 67 | 68 | // - reverse complement 69 | bnt = mer2bytes(revcmpl(record->seq.s)); 70 | memcpy(target.bnt, bnt, MER_LEN / 4); 71 | pt = (meridx_t *)bsearch(&target, pidx, *psz, sizeof(meridx_t), meridx_cmp); 72 | if (pt != NULL) { 73 | fprintf(fout, "%u\t%u\t%u\n", pt->chr, pt->pos, pt->gc); 74 | } 75 | } 76 | } 77 | 78 | printf("Finish alignment in %fs\n", (double)(clock() - start) / CLOCKS_PER_SEC); 79 | kseq_destroy(record); 80 | gzclose(fp_read); 81 | } 82 | 83 | // PE 84 | else if (argc == 4) { 85 | // read in reads file 86 | clock_t start = clock(); // record time 87 | gzFile fp_r1 = gzopen(argv[2], "r"); 88 | gzFile fp_r2 = gzopen(argv[3], "r"); 89 | kseq_t *record1 = kseq_init(fp_r1); 90 | kseq_t *record2 = kseq_init(fp_r2); 91 | meridx_t t1, t2; 92 | meridx_t *p1, *p2; 93 | uint8_t *bnt; 94 | fprintf(fout, "chr1\tpos1\tGC1\tchr2\tpos2\tGC2\n"); 95 | while (kseq_read(record1) >= 0 && kseq_read(record2) >= 0) { 96 | if (record1->seq.l == MER_LEN && record2->seq.l == MER_LEN && check_mer(record1->seq.s) && check_mer(record2->seq.s)) { 97 | // + 98 | bnt = mer2bytes(record1->seq.s); 99 | memcpy(t1.bnt, bnt, MER_LEN / 4); 100 | bnt = mer2bytes(revcmpl(record2->seq.s)); 101 | memcpy(t2.bnt, bnt, MER_LEN / 4); 102 | p1 = (meridx_t *)bsearch(&t1, pidx, *psz, sizeof(meridx_t), meridx_cmp); 103 | p2 = (meridx_t *)bsearch(&t2, pidx, *psz, sizeof(meridx_t), meridx_cmp); 104 | if (p1 != NULL && p2 != NULL) { 105 | fprintf(fout, "%u\t%u\t%u\t%u\t%u\t%u\n", p1->chr, p1->pos, p1->gc, p2->chr, p2->pos, p2->gc); 106 | } 107 | 108 | // - 109 | bnt = mer2bytes(revcmpl(record1->seq.s)); 110 | memcpy(t1.bnt, bnt, MER_LEN / 4); 111 | bnt = mer2bytes(record2->seq.s); 112 | memcpy(t2.bnt, bnt, MER_LEN / 4); 113 | p1 = (meridx_t *)bsearch(&t1, pidx, *psz, sizeof(meridx_t), meridx_cmp); 114 | p2 = (meridx_t *)bsearch(&t2, pidx, *psz, sizeof(meridx_t), meridx_cmp); 115 | if (p1 != NULL && p2 != NULL) { 116 | fprintf(fout, "%u\t%u\t%u\t%u\t%u\t%u\n", p1->chr, p1->pos, p1->gc, p2->chr, p2->pos, p2->gc); 117 | } 118 | } 119 | } 120 | 121 | printf("Finish alignment in %fs\n", (double)(clock() - start) / CLOCKS_PER_SEC); 122 | kseq_destroy(record1); 123 | kseq_destroy(record2); 124 | gzclose(fp_r1); 125 | gzclose(fp_r2); 126 | } 127 | 128 | 129 | // close file and free memory 130 | free(fn_out); fn_out = NULL; 131 | fclose(fout); 132 | munmap(fn_idx, *psz * sizeof(meridx_t)); 133 | munmap(fn_sz, sizeof(uint32_t)); 134 | close(fd_idx); 135 | close(fd_sz); 136 | free(fn_sz); fn_sz = NULL; 137 | free(fn_idx); fn_idx = NULL; 138 | 139 | return 0; 140 | } -------------------------------------------------------------------------------- /kseq.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (c) 2008, 2009, 2011 Attractive Chaos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | /* Last Modified: 05MAR2012 */ 27 | 28 | #ifndef AC_KSEQ_H 29 | #define AC_KSEQ_H 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #ifdef USE_MALLOC_WRAPPERS 36 | # include "malloc_wrap.h" 37 | #endif 38 | 39 | #define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r 40 | #define KS_SEP_TAB 1 // isspace() && !' ' 41 | #define KS_SEP_LINE 2 // line separator: "\n" (Unix) or "\r\n" (Windows) 42 | #define KS_SEP_MAX 2 43 | 44 | #define __KS_TYPE(type_t) \ 45 | typedef struct __kstream_t { \ 46 | unsigned char *buf; \ 47 | int begin, end, is_eof; \ 48 | type_t f; \ 49 | } kstream_t; 50 | 51 | #define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end) 52 | #define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0) 53 | 54 | #define __KS_BASIC(type_t, __bufsize) \ 55 | static inline kstream_t *ks_init(type_t f) \ 56 | { \ 57 | kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \ 58 | ks->f = f; \ 59 | ks->buf = (unsigned char*)malloc(__bufsize); \ 60 | return ks; \ 61 | } \ 62 | static inline void ks_destroy(kstream_t *ks) \ 63 | { \ 64 | if (ks) { \ 65 | free(ks->buf); \ 66 | free(ks); \ 67 | } \ 68 | } 69 | 70 | #define __KS_GETC(__read, __bufsize) \ 71 | static inline int ks_getc(kstream_t *ks) \ 72 | { \ 73 | if (ks->is_eof && ks->begin >= ks->end) return -1; \ 74 | if (ks->begin >= ks->end) { \ 75 | ks->begin = 0; \ 76 | ks->end = __read(ks->f, ks->buf, __bufsize); \ 77 | if (ks->end == 0) { ks->is_eof = 1; return -1;} \ 78 | } \ 79 | return (int)ks->buf[ks->begin++]; \ 80 | } 81 | 82 | #ifndef KSTRING_T 83 | #define KSTRING_T kstring_t 84 | typedef struct __kstring_t { 85 | size_t l, m; 86 | char *s; 87 | } kstring_t; 88 | #endif 89 | 90 | #ifndef kroundup32 91 | #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) 92 | #endif 93 | 94 | #define __KS_GETUNTIL(__read, __bufsize) \ 95 | static int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \ 96 | { \ 97 | int gotany = 0; \ 98 | if (dret) *dret = 0; \ 99 | str->l = append? str->l : 0; \ 100 | for (;;) { \ 101 | int i; \ 102 | if (ks->begin >= ks->end) { \ 103 | if (!ks->is_eof) { \ 104 | ks->begin = 0; \ 105 | ks->end = __read(ks->f, ks->buf, __bufsize); \ 106 | if (ks->end == 0) { ks->is_eof = 1; break; } \ 107 | } else break; \ 108 | } \ 109 | if (delimiter == KS_SEP_LINE) { \ 110 | for (i = ks->begin; i < ks->end; ++i) \ 111 | if (ks->buf[i] == '\n') break; \ 112 | } else if (delimiter > KS_SEP_MAX) { \ 113 | for (i = ks->begin; i < ks->end; ++i) \ 114 | if (ks->buf[i] == delimiter) break; \ 115 | } else if (delimiter == KS_SEP_SPACE) { \ 116 | for (i = ks->begin; i < ks->end; ++i) \ 117 | if (isspace(ks->buf[i])) break; \ 118 | } else if (delimiter == KS_SEP_TAB) { \ 119 | for (i = ks->begin; i < ks->end; ++i) \ 120 | if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \ 121 | } else i = 0; /* never come to here! */ \ 122 | if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \ 123 | str->m = str->l + (i - ks->begin) + 1; \ 124 | kroundup32(str->m); \ 125 | str->s = (char*)realloc(str->s, str->m); \ 126 | } \ 127 | gotany = 1; \ 128 | memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \ 129 | str->l = str->l + (i - ks->begin); \ 130 | ks->begin = i + 1; \ 131 | if (i < ks->end) { \ 132 | if (dret) *dret = ks->buf[i]; \ 133 | break; \ 134 | } \ 135 | } \ 136 | if (!gotany && ks_eof(ks)) return -1; \ 137 | if (str->s == 0) { \ 138 | str->m = 1; \ 139 | str->s = (char*)calloc(1, 1); \ 140 | } else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \ 141 | str->s[str->l] = '\0'; \ 142 | return str->l; \ 143 | } \ 144 | static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \ 145 | { return ks_getuntil2(ks, delimiter, str, dret, 0); } 146 | 147 | #define KSTREAM_INIT(type_t, __read, __bufsize) \ 148 | __KS_TYPE(type_t) \ 149 | __KS_BASIC(type_t, __bufsize) \ 150 | __KS_GETC(__read, __bufsize) \ 151 | __KS_GETUNTIL(__read, __bufsize) 152 | 153 | #define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0) 154 | 155 | #define __KSEQ_BASIC(SCOPE, type_t) \ 156 | SCOPE kseq_t *kseq_init(type_t fd) \ 157 | { \ 158 | kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \ 159 | s->f = ks_init(fd); \ 160 | return s; \ 161 | } \ 162 | SCOPE void kseq_destroy(kseq_t *ks) \ 163 | { \ 164 | if (!ks) return; \ 165 | free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \ 166 | ks_destroy(ks->f); \ 167 | free(ks); \ 168 | } 169 | 170 | /* Return value: 171 | >=0 length of the sequence (normal) 172 | -1 end-of-file 173 | -2 truncated quality string 174 | */ 175 | #define __KSEQ_READ(SCOPE) \ 176 | SCOPE int kseq_read(kseq_t *seq) \ 177 | { \ 178 | int c; \ 179 | kstream_t *ks = seq->f; \ 180 | if (seq->last_char == 0) { /* then jump to the next header line */ \ 181 | while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \ 182 | if (c == -1) return -1; /* end of file */ \ 183 | seq->last_char = c; \ 184 | } /* else: the first header char has been read in the previous call */ \ 185 | seq->comment.l = seq->seq.l = seq->qual.l = 0; /* reset all members */ \ 186 | if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; /* normal exit: EOF */ \ 187 | if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); /* read FASTA/Q comment */ \ 188 | if (seq->seq.s == 0) { /* we can do this in the loop below, but that is slower */ \ 189 | seq->seq.m = 256; \ 190 | seq->seq.s = (char*)malloc(seq->seq.m); \ 191 | } \ 192 | while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \ 193 | if (c == '\n') continue; /* skip empty lines */ \ 194 | seq->seq.s[seq->seq.l++] = c; /* this is safe: we always have enough space for 1 char */ \ 195 | ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); /* read the rest of the line */ \ 196 | } \ 197 | if (c == '>' || c == '@') seq->last_char = c; /* the first header char has been read */ \ 198 | if (seq->seq.l + 1 >= seq->seq.m) { /* seq->seq.s[seq->seq.l] below may be out of boundary */ \ 199 | seq->seq.m = seq->seq.l + 2; \ 200 | kroundup32(seq->seq.m); /* rounded to the next closest 2^k */ \ 201 | seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \ 202 | } \ 203 | seq->seq.s[seq->seq.l] = 0; /* null terminated string */ \ 204 | if (c != '+') return seq->seq.l; /* FASTA */ \ 205 | if (seq->qual.m < seq->seq.m) { /* allocate memory for qual in case insufficient */ \ 206 | seq->qual.m = seq->seq.m; \ 207 | seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \ 208 | } \ 209 | while ((c = ks_getc(ks)) != -1 && c != '\n'); /* skip the rest of '+' line */ \ 210 | if (c == -1) return -2; /* error: no quality string */ \ 211 | while (ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l); \ 212 | seq->last_char = 0; /* we have not come to the next header line */ \ 213 | if (seq->seq.l != seq->qual.l) return -2; /* error: qual string is of a different length */ \ 214 | return seq->seq.l; \ 215 | } 216 | 217 | #define __KSEQ_TYPE(type_t) \ 218 | typedef struct { \ 219 | kstring_t name, comment, seq, qual; \ 220 | int last_char; \ 221 | kstream_t *f; \ 222 | } kseq_t; 223 | 224 | #define KSEQ_INIT2(SCOPE, type_t, __read) \ 225 | KSTREAM_INIT(type_t, __read, 16384) \ 226 | __KSEQ_TYPE(type_t) \ 227 | __KSEQ_BASIC(SCOPE, type_t) \ 228 | __KSEQ_READ(SCOPE) 229 | 230 | #define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read) 231 | 232 | #define KSEQ_DECLARE(type_t) \ 233 | __KS_TYPE(type_t) \ 234 | __KSEQ_TYPE(type_t) \ 235 | extern kseq_t *kseq_init(type_t fd); \ 236 | void kseq_destroy(kseq_t *ks); \ 237 | int kseq_read(kseq_t *seq); 238 | 239 | #endif 240 | -------------------------------------------------------------------------------- /ksort.h: -------------------------------------------------------------------------------- 1 | /* The MIT License 2 | 3 | Copyright (c) 2008, by Attractive Chaos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | */ 25 | 26 | /* 27 | 2008-11-16 (0.1.4): 28 | 29 | * Fixed a bug in introsort() that happens in rare cases. 30 | 31 | 2008-11-05 (0.1.3): 32 | 33 | * Fixed a bug in introsort() for complex comparisons. 34 | 35 | * Fixed a bug in mergesort(). The previous version is not stable. 36 | 37 | 2008-09-15 (0.1.2): 38 | 39 | * Accelerated introsort. On my Mac (not on another Linux machine), 40 | my implementation is as fast as std::sort on random input. 41 | 42 | * Added combsort and in introsort, switch to combsort if the 43 | recursion is too deep. 44 | 45 | 2008-09-13 (0.1.1): 46 | 47 | * Added k-small algorithm 48 | 49 | 2008-09-05 (0.1.0): 50 | 51 | * Initial version 52 | 53 | */ 54 | 55 | #ifndef AC_KSORT_H 56 | #define AC_KSORT_H 57 | 58 | #include 59 | #include 60 | 61 | #ifdef USE_MALLOC_WRAPPERS 62 | # include "malloc_wrap.h" 63 | #endif 64 | 65 | typedef struct { 66 | void *left, *right; 67 | int depth; 68 | } ks_isort_stack_t; 69 | 70 | #define KSORT_SWAP(type_t, a, b) { register type_t t=(a); (a)=(b); (b)=t; } 71 | 72 | #define KSORT_INIT(name, type_t, __sort_lt) \ 73 | void ks_mergesort_##name(size_t n, type_t array[], type_t temp[]) \ 74 | { \ 75 | type_t *a2[2], *a, *b; \ 76 | int curr, shift; \ 77 | \ 78 | a2[0] = array; \ 79 | a2[1] = temp? temp : (type_t*)malloc(sizeof(type_t) * n); \ 80 | for (curr = 0, shift = 0; (1ul<> 1) - 1; i != (size_t)(-1); --i) \ 136 | ks_heapadjust_##name(i, lsize, l); \ 137 | } \ 138 | void ks_heapsort_##name(size_t lsize, type_t l[]) \ 139 | { \ 140 | size_t i; \ 141 | for (i = lsize - 1; i > 0; --i) { \ 142 | type_t tmp; \ 143 | tmp = *l; *l = l[i]; l[i] = tmp; ks_heapadjust_##name(0, i, l); \ 144 | } \ 145 | } \ 146 | static inline void __ks_insertsort_##name(type_t *s, type_t *t) \ 147 | { \ 148 | type_t *i, *j, swap_tmp; \ 149 | for (i = s + 1; i < t; ++i) \ 150 | for (j = i; j > s && __sort_lt(*j, *(j-1)); --j) { \ 151 | swap_tmp = *j; *j = *(j-1); *(j-1) = swap_tmp; \ 152 | } \ 153 | } \ 154 | void ks_combsort_##name(size_t n, type_t a[]) \ 155 | { \ 156 | const double shrink_factor = 1.2473309501039786540366528676643; \ 157 | int do_swap; \ 158 | size_t gap = n; \ 159 | type_t tmp, *i, *j; \ 160 | do { \ 161 | if (gap > 2) { \ 162 | gap = (size_t)(gap / shrink_factor); \ 163 | if (gap == 9 || gap == 10) gap = 11; \ 164 | } \ 165 | do_swap = 0; \ 166 | for (i = a; i < a + n - gap; ++i) { \ 167 | j = i + gap; \ 168 | if (__sort_lt(*j, *i)) { \ 169 | tmp = *i; *i = *j; *j = tmp; \ 170 | do_swap = 1; \ 171 | } \ 172 | } \ 173 | } while (do_swap || gap > 2); \ 174 | if (gap != 1) __ks_insertsort_##name(a, a + n); \ 175 | } \ 176 | void ks_introsort_##name(size_t n, type_t a[]) \ 177 | { \ 178 | int d; \ 179 | ks_isort_stack_t *top, *stack; \ 180 | type_t rp, swap_tmp; \ 181 | type_t *s, *t, *i, *j, *k; \ 182 | \ 183 | if (n < 1) return; \ 184 | else if (n == 2) { \ 185 | if (__sort_lt(a[1], a[0])) { swap_tmp = a[0]; a[0] = a[1]; a[1] = swap_tmp; } \ 186 | return; \ 187 | } \ 188 | for (d = 2; 1ul<>1) + 1; \ 199 | if (__sort_lt(*k, *i)) { \ 200 | if (__sort_lt(*k, *j)) k = j; \ 201 | } else k = __sort_lt(*j, *i)? i : j; \ 202 | rp = *k; \ 203 | if (k != t) { swap_tmp = *k; *k = *t; *t = swap_tmp; } \ 204 | for (;;) { \ 205 | do ++i; while (__sort_lt(*i, rp)); \ 206 | do --j; while (i <= j && __sort_lt(rp, *j)); \ 207 | if (j <= i) break; \ 208 | swap_tmp = *i; *i = *j; *j = swap_tmp; \ 209 | } \ 210 | swap_tmp = *i; *i = *t; *t = swap_tmp; \ 211 | if (i-s > t-i) { \ 212 | if (i-s > 16) { top->left = s; top->right = i-1; top->depth = d; ++top; } \ 213 | s = t-i > 16? i+1 : t; \ 214 | } else { \ 215 | if (t-i > 16) { top->left = i+1; top->right = t; top->depth = d; ++top; } \ 216 | t = i-s > 16? i-1 : s; \ 217 | } \ 218 | } else { \ 219 | if (top == stack) { \ 220 | free(stack); \ 221 | __ks_insertsort_##name(a, a+n); \ 222 | return; \ 223 | } else { --top; s = (type_t*)top->left; t = (type_t*)top->right; d = top->depth; } \ 224 | } \ 225 | } \ 226 | } \ 227 | /* This function is adapted from: http://ndevilla.free.fr/median/ */ \ 228 | /* 0 <= kk < n */ \ 229 | type_t ks_ksmall_##name(size_t n, type_t arr[], size_t kk) \ 230 | { \ 231 | type_t *low, *high, *k, *ll, *hh, *mid; \ 232 | low = arr; high = arr + n - 1; k = arr + kk; \ 233 | for (;;) { \ 234 | if (high <= low) return *k; \ 235 | if (high == low + 1) { \ 236 | if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \ 237 | return *k; \ 238 | } \ 239 | mid = low + (high - low) / 2; \ 240 | if (__sort_lt(*high, *mid)) KSORT_SWAP(type_t, *mid, *high); \ 241 | if (__sort_lt(*high, *low)) KSORT_SWAP(type_t, *low, *high); \ 242 | if (__sort_lt(*low, *mid)) KSORT_SWAP(type_t, *mid, *low); \ 243 | KSORT_SWAP(type_t, *mid, *(low+1)); \ 244 | ll = low + 1; hh = high; \ 245 | for (;;) { \ 246 | do ++ll; while (__sort_lt(*ll, *low)); \ 247 | do --hh; while (__sort_lt(*low, *hh)); \ 248 | if (hh < ll) break; \ 249 | KSORT_SWAP(type_t, *ll, *hh); \ 250 | } \ 251 | KSORT_SWAP(type_t, *low, *hh); \ 252 | if (hh <= k) low = ll; \ 253 | if (hh >= k) high = hh - 1; \ 254 | } \ 255 | } 256 | 257 | #define ks_mergesort(name, n, a, t) ks_mergesort_##name(n, a, t) 258 | #define ks_introsort(name, n, a) ks_introsort_##name(n, a) 259 | #define ks_combsort(name, n, a) ks_combsort_##name(n, a) 260 | #define ks_heapsort(name, n, a) ks_heapsort_##name(n, a) 261 | #define ks_heapmake(name, n, a) ks_heapmake_##name(n, a) 262 | #define ks_heapadjust(name, i, n, a) ks_heapadjust_##name(i, n, a) 263 | #define ks_ksmall(name, n, a, k) ks_ksmall_##name(n, a, k) 264 | 265 | #define ks_lt_generic(a, b) ((a) < (b)) 266 | #define ks_lt_str(a, b) (strcmp((a), (b)) < 0) 267 | 268 | typedef const char *ksstr_t; 269 | 270 | #define KSORT_INIT_GENERIC(type_t) KSORT_INIT(type_t, type_t, ks_lt_generic) 271 | #define KSORT_INIT_STR KSORT_INIT(str, ksstr_t, ks_lt_str) 272 | 273 | #endif 274 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------