├── test_files ├── PURR_SALTY.subst ├── LACI_ECOLI.subst ├── query.fasta ├── README.md └── sample_protein_database.fa ├── .gitmodules ├── .gitignore ├── Makefile ├── sift4g ├── src │ ├── database_search.hpp │ ├── sift_prediction.hpp │ ├── utils.hpp │ ├── database_alignment.hpp │ ├── select_alignments.hpp │ ├── hash.hpp │ ├── utils.cpp │ ├── hash.cpp │ ├── sift_scores.hpp │ ├── database_alignment.cpp │ ├── constants.hpp │ ├── sift_prediction.cpp │ ├── select_alignments.cpp │ ├── database_search.cpp │ ├── main.cpp │ └── sift_scores.cpp ├── Makefile └── LICENSE ├── README.md └── LICENSE /test_files/PURR_SALTY.subst: -------------------------------------------------------------------------------- 1 | H20E 2 | V121L 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "vendor/swsharp"] 2 | path = vendor/swsharp 3 | url = https://github.com/mkorpar/swsharp.git 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | obj/ 3 | 4 | # Executables 5 | bin/ 6 | 7 | # Include Header files 8 | include/ 9 | 10 | # Library files 11 | lib/ 12 | -------------------------------------------------------------------------------- /test_files/LACI_ECOLI.subst: -------------------------------------------------------------------------------- 1 | # this is a sample file of substitutions. This file has 2 substitutions for 2 | # the lacI protein. 3 | # K2S => substitution of amino acid K to S at position 2. SIFT will predict 4 | # whether the S is tolerated or not. 5 | # format should be one substitution / line, any line that starts with # will 6 | # be discarded 7 | K2S 8 | P3M 9 | V15K # comments can be made 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CORE = sift4g 2 | DEPS = vendor/swsharp 3 | 4 | all: TARGETS=cpu 5 | gpu: TARGETS=all 6 | clean: TARGETS=clean 7 | 8 | all: $(CORE) $(DEPS) 9 | gpu: $(CORE) $(DEPS) 10 | 11 | clean: $(CORE) $(DEPS) 12 | @echo [RM] cleaning 13 | @rm $(OBJ_DIR) $(EXC_DIR) -rf 14 | 15 | $(DEPS): 16 | @echo \>\>\> $@ \<\<\< 17 | @$(MAKE) -s -C $@ $(TARGETS) 18 | 19 | $(CORE): $(DEPS) 20 | @echo \>\>\> $@ \<\<\< 21 | @$(MAKE) -s -C $@ $(TARGETS) 22 | 23 | .PHONY: $(CORE) $(DEPS) 24 | -------------------------------------------------------------------------------- /sift4g/src/database_search.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file database_search.hpp 3 | * 4 | * @brief Database search header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "swsharp/swsharp.h" 16 | 17 | uint64_t searchDatabase(std::vector>& dst, 18 | const std::string& database_path, Chain** queries, int32_t queries_length, 19 | uint32_t kmer_length, uint32_t max_candidates, uint32_t num_threads); 20 | -------------------------------------------------------------------------------- /sift4g/src/sift_prediction.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file sift_prediction.hpp 3 | * 4 | * @brief SIFT predictions header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "swsharp/swsharp.h" 16 | 17 | void checkData(Chain** queries, int32_t& queries_length, const std::string& subst_path); 18 | 19 | void siftPredictions(std::vector>& alignment_strings, 20 | Chain** queries, int32_t queries_length, const std::string& subst_path, 21 | int32_t sequence_identity, const std::string& out_path); 22 | -------------------------------------------------------------------------------- /sift4g/src/utils.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file utils.hpp 3 | * 4 | * @brief Utils header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | #define ASSERT(expr, fmt, ...)\ 14 | do {\ 15 | if (!(expr)) {\ 16 | fprintf(stderr, "[ERROR]: " fmt "\n", ##__VA_ARGS__);\ 17 | exit(-1);\ 18 | }\ 19 | } while(0) 20 | 21 | int isExtantPath(const char* path); 22 | 23 | /* call delete[] after usage */ 24 | char* createFileName(const char* name, const std::string& path, const std::string& extension); 25 | 26 | void queryLog(uint32_t part, uint32_t total); 27 | 28 | void databaseLog(uint32_t part, float part_size, float percentage); 29 | -------------------------------------------------------------------------------- /sift4g/src/database_alignment.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file database_alignment.hpp 3 | * 4 | * @brief Database alignment header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "swsharp/evalue.h" 16 | #include "swsharp/swsharp.h" 17 | 18 | void alignDatabase(DbAlignment**** alignments, int** alignments_lengths, Chain*** database, 19 | int32_t* database_length, const std::string& database_path, Chain** queries, 20 | int32_t queries_length, std::vector>& indices, 21 | int32_t algorithm, EValueParams* evalue_params, double max_evalue, 22 | uint32_t max_alignments, Scorer* scorer, int32_t* cards, 23 | int32_t cards_length); 24 | -------------------------------------------------------------------------------- /sift4g/src/select_alignments.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file select_alignments.hpp 3 | * 4 | * @brief Alignment selection header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "swsharp/swsharp.h" 16 | 17 | void selectAlignments(std::vector>& dst, DbAlignment*** alignments, 18 | int32_t* alignments_lengths, Chain** queries, int32_t queries_length, 19 | float threshold); 20 | 21 | void outputSelectedAlignments(std::vector>& alignment_strings, 22 | Chain** queries, int32_t queries_length, const std::string& out_path); 23 | 24 | void deleteSelectedAlignments(std::vector>& alignment_strings); 25 | -------------------------------------------------------------------------------- /test_files/query.fasta: -------------------------------------------------------------------------------- 1 | >LACI_ECOLI 2 | MKPVTLYDVAEYAGVSYQTVSRVVNQASHVSAKTREKVEAAMAELNYIPNRVAQQLAGKQ 3 | SLLIGVATSSLALHAPSQIVAAIKSRADQLGASVVVSMVERSGVEACKAAVHNLLAQRVS 4 | GLIINYPLDDQDAIAVEAACTNVPALFLDVSDQTPINSIIFSHEDGTRLGVEHLVALGHQ 5 | QIALLAGPLSSVSARLRLAGWHKYLTRNQIQPIAEREGDWSAMSGFQQTMQMLNEGIVPT 6 | AMLVANDQMALGAMRAITESGLRVGADISVVGYDDTEDSSCYIPPLTTIKQDFRLLGQTS 7 | VDRLLQLSQGQAVKGNQLLPVSLVKRKTTLAPNTQTASPRALADSLMQLARQVSRLESGQ 8 | 9 | >PURR_SALTY 10 | MATIKDVAKRANVSTTTVSHVINKTRFVAEETRNAVWAAIKELHYSPSAVARSLKVNHTK 11 | SIGLLATSSEAAYFAEIIEAVEKNCFQKGYTLILGNAWNNLEKQRAYLSMMAQKRVDGLL 12 | VMCSEYPEPLLSMLEEYRHIPMVVMDWGEAKADFTDTVIDNAFAGGYMAGRYLVERGHRD 13 | IGVIPGPLERNTGAGRLAGFMKAMEEALINVPDNWIVQGDFEPESGYHAMQQILSQSHRP 14 | TAVFCGGDIMAMGALCAADEMGLRVPQDVSVIGYDNVRNARYFTPALTTIHQPKDSLGET 15 | AFNMLLDRIVNKREESQSIEVHPRLVERRSVADGPFRDYRR 16 | 17 | -------------------------------------------------------------------------------- /sift4g/Makefile: -------------------------------------------------------------------------------- 1 | CP = g++ 2 | LD = nvcc 3 | 4 | NAME = sift4g 5 | 6 | SRC_DIR = src 7 | OBJ_DIR = obj 8 | EXC_DIR = ../bin 9 | VND_DIR = ../vendor 10 | 11 | I_CMD = $(addprefix -I, $(SRC_DIR) $(VND_DIR)/swsharp/include) 12 | L_CMD = $(addprefix -L, $(VND_DIR)/swsharp/lib $(VND_DIR)/sift/lib) 13 | 14 | DEP_LIBS = $(VND_DIR)/swsharp/lib/libswsharp.a 15 | 16 | CP_FLAGS = $(I_CMD) -std=c++11 -O3 -Wall -Wno-write-strings 17 | LD_FLAGS = $(I_CMD) $(L_CMD) -lswsharp -lpthread -lm 18 | 19 | SRC = $(shell find $(SRC_DIR) -type f -regex ".*\.cpp") 20 | OBJ = $(subst $(SRC_DIR), $(OBJ_DIR), $(addsuffix .o, $(basename $(SRC)))) 21 | DEP = $(OBJ:.o=.d) 22 | BIN = $(EXC_DIR)/$(NAME) 23 | 24 | cpu: LD = $(CP) 25 | 26 | all: $(BIN) 27 | cpu: all 28 | 29 | clean: 30 | @echo [RM] cleaning 31 | @rm $(EXC_DIR) $(OBJ_DIR) -rf 32 | 33 | $(BIN): $(OBJ) $(DEP_LIBS) 34 | @echo [LD] $@ 35 | @mkdir -p $(dir $@) 36 | @$(LD) $(OBJ) -o $@ $(LD_FLAGS) 37 | 38 | $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 39 | @echo [CP] $< 40 | @mkdir -p $(dir $@) 41 | @$(CP) $< -c -o $@ -MMD $(CP_FLAGS) 42 | 43 | -include $(DEP) 44 | -------------------------------------------------------------------------------- /test_files/README.md: -------------------------------------------------------------------------------- 1 | ##Input options 2 | 3 | The query file can have multiple protein sequences (in fasta format). 4 | 5 | The substitution file(s) must have the same name as the ID in the protein file. Using the option --subst, pass in the **directory** containing the substitution file(s). Based on the protein ID in the query file, SIFT 4G will look for the corresponding substitution file of the same name. 6 | 7 | ##Output 8 | 9 | The predictions will be in <Protein ID>.SIFTprediction 10 | 11 | To test SIFT 4G, go to the parent directory and type: 12 | 13 | > ./bin/sift4g -q ./test_files/query.fasta --subst ./test_files/ -d ./test_files/sample_protein_database.fa 14 | 15 | To get all 20 amino acid predictions for every position, don't pass in a substitution file: 16 | 17 | > ./bin/sift4g -q ./test_files/query.fasta -d ./test_files/sample_protein_database.fa 18 | 19 | Output (LACI_ECOLI.SIFTprediction and PURR_SALTY.SIFTprediction) will be in the directory where sift4g was run (unless specified otherwise by --out) 20 | -------------------------------------------------------------------------------- /sift4g/src/hash.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file hash.hpp 3 | * 4 | * @brief Hash class header file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | struct Chain; 16 | 17 | void createKmerVector(std::vector& dst, Chain* chain, uint32_t kmer_length); 18 | 19 | class Hit { 20 | public: 21 | 22 | Hit() {}; 23 | Hit(uint32_t _id, uint32_t _position) : 24 | id(_id), position(_position) { 25 | } 26 | 27 | uint32_t id; 28 | uint32_t position; 29 | }; 30 | 31 | class Hash; 32 | 33 | std::unique_ptr createHash(Chain** chains, uint32_t chains_length, 34 | uint32_t start, uint32_t length, uint32_t kmer_length); 35 | 36 | class Hash { 37 | public: 38 | 39 | ~Hash() {}; 40 | 41 | using Iterator = std::vector::iterator; 42 | void hits(Iterator& start, Iterator& end, uint32_t key); 43 | 44 | friend std::unique_ptr createHash(Chain** chains, uint32_t chains_length, 45 | uint32_t start, uint32_t length, uint32_t kmer_length); 46 | 47 | private: 48 | 49 | Hash(Chain** chains, uint32_t chains_length, uint32_t start, uint32_t length, 50 | uint32_t kmer_length); 51 | 52 | Hash(const Hash&) = delete; 53 | const Hash& operator=(const Hash&) = delete; 54 | 55 | std::vector starts_; 56 | std::vector hits_; 57 | }; 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SIFT4G 2 | 3 | SIFT (Sorting Intolerant From Tolerant) For Genomes (http://www.nature.com/nprot/journal/v11/n1/full/nprot.2015.123.html) 4 | 5 | ## Requirements 6 | - g++ (4.9+) 7 | - GNU Make 8 | - nvcc (2.\*+) (optional) 9 | 10 | \*note: It was only tested on Linux (Ubuntu 14.04). 11 | 12 | ## Installation 13 | 14 | To build SIFT4G run the following commands from your terminal: 15 | 16 | git clone --recursive https://github.com/rvaser/sift4g.git sift4g 17 | cd sift4g/ 18 | make 19 | 20 | Running the 'make' command will create the bin folder which contains the sift4g executable. 21 | 22 | **Troubleshooting** 23 | 24 | If you accidentally left out '--recursive' from git clone, run the following commands before running 'make': 25 | 26 | git submodule init 27 | git submodule update 28 | 29 | **Optional** 30 | 31 | If you have a CUDA enabled graphics card (and the nvcc compiler) run 'make gpu' instead of 'make'. 32 | 33 | ## Usage 34 | 35 | To run the default version of SIFT4G run the following command: 36 | 37 | ./bin/sift4g -q -d 38 | 39 | where both the query and database files are protein sequences in fasta format. 40 | 41 | To see all available parameters run the command bellow: 42 | 43 | ./bin/sift4g -h 44 | 45 | ## Check installation 46 | 47 | To test sift4g, go to the [test_files directory](test_files/) and follow directions in [README](test_files/README.md) 48 | -------------------------------------------------------------------------------- /sift4g/src/utils.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file utils.cpp 3 | * 4 | * @brief Utils source file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "utils.hpp" 15 | 16 | constexpr uint32_t kBufferSize = 4096; 17 | 18 | int isExtantPath(const char* path) { 19 | struct stat buffer; 20 | if (stat(path, &buffer) == 0) { 21 | if (buffer.st_mode & S_IFDIR) { 22 | // directory 23 | return 0; 24 | } else if (buffer.st_mode & S_IFREG) { 25 | // file 26 | return 1; 27 | } else { 28 | // something 29 | return 2; 30 | } 31 | } 32 | return -1; 33 | } 34 | 35 | char* createFileName(const char* name, const std::string& path, const std::string& extension) { 36 | 37 | char* file_name = new char[kBufferSize]; 38 | 39 | if (!path.empty()) { 40 | strcpy(file_name, path.c_str()); 41 | strcat(file_name, "/"); 42 | strcat(file_name, name); 43 | } else { 44 | strcpy(file_name, name); 45 | } 46 | 47 | strcat(file_name, extension.c_str()); 48 | 49 | return file_name; 50 | } 51 | 52 | void queryLog(uint32_t part, uint32_t total) { 53 | fprintf(stderr, "* processing queries: %.2f/100.00%% *\r", 100 * part / (float) total); 54 | fflush(stderr); 55 | } 56 | 57 | void databaseLog(uint32_t part, float part_size, float percentage) { 58 | fprintf(stderr, "* processing database part %u (size ~%.2f GB): %.2f/100.00%% *\r", 59 | part, part_size, percentage); 60 | fflush(stderr); 61 | } 62 | -------------------------------------------------------------------------------- /sift4g/src/hash.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file hash.cpp 3 | * 4 | * @brief Hash class source file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #include 10 | 11 | #include "hash.hpp" 12 | #include "utils.hpp" 13 | 14 | #include "swsharp/swsharp.h" 15 | 16 | constexpr uint32_t kProtMaxValue = 25; 17 | constexpr uint32_t kProtBitLength = 5; 18 | std::vector kDelMasks = { 0, 0, 0, 0x7FFF, 0xFFFFF, 0x1FFFFFF }; 19 | std::vector kNumDiffKmers = { 0, 0, 0, 26427, 845627, 27060027 }; 20 | 21 | void createKmerVector(std::vector& dst, Chain* chain, uint32_t kmer_length) { 22 | 23 | dst.clear(); 24 | 25 | uint32_t chain_length = chainGetLength(chain); 26 | if (chain_length < kmer_length) { 27 | return; 28 | } 29 | 30 | auto codes = chainGetCodes(chain); 31 | 32 | uint32_t kmer = 0; 33 | uint32_t del_mask = kDelMasks[kmer_length]; 34 | 35 | for (uint32_t i = 0; i < kmer_length; ++i) { 36 | kmer = (kmer << kProtBitLength) | codes[i]; 37 | } 38 | dst.emplace_back(kmer); 39 | 40 | for (uint32_t i = kmer_length; i < chain_length; ++i) { 41 | kmer = ((kmer << kProtBitLength) | codes[i]) & del_mask; 42 | dst.emplace_back(kmer); 43 | } 44 | } 45 | 46 | std::unique_ptr createHash(Chain** chains, uint32_t chains_length, 47 | uint32_t start, uint32_t length, uint32_t kmer_length) { 48 | 49 | ASSERT(chains_length, "zero chains passed to hash"); 50 | ASSERT(start < chains_length && start + length <= chains_length, "invalid chain interval"); 51 | ASSERT(kmer_length && kmer_length < 6, "invalid kmer_length"); 52 | 53 | return std::unique_ptr(new Hash(chains, chains_length, start, length, kmer_length)); 54 | } 55 | 56 | Hash::Hash(Chain** chains, uint32_t chains_length, uint32_t start, uint32_t length, 57 | uint32_t kmer_length) 58 | : starts_(kNumDiffKmers[kmer_length], 0) { 59 | 60 | std::vector chain_kmers; 61 | for (uint32_t i = start; i < start + length; ++i) { 62 | 63 | createKmerVector(chain_kmers, chains[i], kmer_length); 64 | 65 | for (uint32_t j = 0; j < chain_kmers.size(); ++j) { 66 | ++starts_[chain_kmers[j] + 1]; 67 | } 68 | } 69 | 70 | for (uint32_t i = 1; i < starts_.size() - 1; ++i) { 71 | starts_[i + 1] += starts_[i]; 72 | } 73 | 74 | hits_.resize(starts_[starts_.size() - 1]); 75 | std::vector tmp(starts_.begin(), starts_.end()); 76 | 77 | for (uint32_t i = start; i < start + length; ++i) { 78 | 79 | createKmerVector(chain_kmers, chains[i], kmer_length); 80 | 81 | for (uint32_t j = 0; j < chain_kmers.size(); ++j) { 82 | hits_[tmp[chain_kmers[j]]++] = Hit(i - start, j); 83 | } 84 | } 85 | } 86 | 87 | void Hash::hits(Iterator& start, Iterator& end, uint32_t key) { 88 | start = hits_.begin() + starts_[key]; 89 | end = hits_.begin() + starts_[key + 1]; 90 | } 91 | -------------------------------------------------------------------------------- /sift4g/src/sift_scores.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file sift_prediction.hpp 3 | * 4 | * @brief SIFT predictions header file 5 | * 6 | * @author: pauline-ng, angsm 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "swsharp/swsharp.h" 18 | 19 | void readSubstFile(char* substFilename, std::list& substList); 20 | 21 | void printSubstFile(const std::list& substList, std::unordered_map& medianSeqInfoForPos, 22 | const std::vector>& SIFTscores, const std::vector& aas_stored, 23 | const int total_seq, Chain* query, const std::string outfile); 24 | 25 | void createMatrix(const std::vector& alignment_string, Chain* query, 26 | const std::vector& seq_weights, std::vector>& matrix, 27 | std::vector& tot_pos_weight) ; 28 | 29 | void remove_seqs_percent_identical_to_query(Chain *queries, std::vector& alignment_string, double seq_identity); 30 | 31 | /* this has to be after createMatrix because it uses amino_acids_present */ 32 | void calcSeqWeights(const std::vector& alignment_string, std::vector>& matrix, 33 | std::vector& amino_acids_present, std::vector& seq_weights, std::vector& number_of_diff_aas); 34 | 35 | void seqs_without_X(const std::vector& alignment_string, int pos, std::vector& out_alignment); 36 | 37 | void basic_matrix_construction(const std::vector& alignment_string, 38 | const std::vector& seq_weights, std::vector>& matrix); 39 | 40 | int aa_to_idx(char character); 41 | 42 | bool valid_amino_acid(char aa); 43 | 44 | double calculateMedianSeqInfo(std::vector& alignment_strings, std::unordered_map& invalidSeq, 45 | std::vector& seq_weights, std::vector>& matrix); 46 | 47 | void hashPredictedPos(std::list& substList, std::unordered_map& medianSeqInfoForPos); 48 | 49 | void addPosWithDelRef(Chain* query, std::vector>& SIFTscores, 50 | std::unordered_map& medianSeqInfoForPos); 51 | 52 | void addMedianSeqInfo(std::vector& alignment_string, Chain* query, 53 | std::vector>& matrix, std::unordered_map& medianSeqInfoForPos); 54 | 55 | void printSeqNames(std::vector& alignment_string); 56 | 57 | void check_refaa_against_query(char ref_aa, int aa_pos, Chain* query, std::ofstream& outfp); 58 | 59 | void printMatrixOriginalFormat(std::vector>& matrix, std::string filename); 60 | void printMatrix(std::vector>& matrix, std::string filename); 61 | 62 | void calcDiri(std::vector>& weighted_matrix, std::vector>& diri_matrix); 63 | 64 | void calcSIFTScores(std::vector& alignment_string, Chain* query, 65 | std::vector>& matrix, std::vector>& SIFTscores); 66 | 67 | void add_diric_values(std::vector& count_col, std::vector& diric_col); 68 | 69 | double add_logs(double logx, double logy); 70 | -------------------------------------------------------------------------------- /test_files/sample_protein_database.fa: -------------------------------------------------------------------------------- 1 | >QUERY 2 | MKPVTLYDVAEYAGVSYQTVSRVVNQASHVSAKTREKVEAAMAELNYIPNRVAQQLAGKQ 3 | SLLIGVATSSLALHAPSQIVAAIKSRADQLGASVVVSMVERSGVEACKAAVHNLLAQRVS 4 | GLIINYPLDDQDAIAVEAACTNVPALFLDVSDQTPINSIIFSHEDGTRLGVEHLVALGHQ 5 | QIALLAGPLSSVSARLRLAGWHKYLTRNQIQPIAEREGDWSAMSGFQQTMQMLNEGIVPT 6 | AMLVANDQMALGAMRAITESGLRVGADISVVGYDDTEDSSCYIPPLTTIKQDFRLLGQTS 7 | VDRLLQLSQGQAVKGNQLLPVSLVKRKTTLAPNTQTASPRALADSLMQLARQVSRLESGQ 8 | 9 | >LACI_ECOLI LACTOSE OPERON REPRESSOR 10 | MKPVTLYDVAEYAGVSYQTVSRVVNQASHVSAKTREKVEAAMAELNYIPNRVAQQLAGKQ 11 | SLLIGVATSSLALHAPSQIVAAIKSRADQLGASVVVSMVERSGVEACKAAVHNLLAQRVS 12 | GLIINYPLDDQDAIAVEAACTNVPALFLDVSDQTPINSIIFSHEDGTRLGVEHLVALGHQ 13 | QIALLAGPLSSVSARLRLAGWHKYLTRNQIQPIAEREGDWSAMSGFQQTMQMLNEGIVPT 14 | AMLVANDQMALGAMRAITESGLRVGADISVVGYDDTEDSSCYIPPSTTIKQDFRLLGQTS 15 | VDRLLQLSQGQAVKGNQLLPVSLVKRKTTLAPNTQTASPRALADSLMQLARQVSRLESGQ 16 | 17 | >PURR_HAEIN PURINE NUCLEOTIDE SYNTHESIS REPRESSOR 18 | MATIKDVAKMAGVSTTTVSHVINKTRFVAKDTEEAVLSAIKQLNYSPSAVARSLKVNTTK 19 | SIGMIVTTSEAPYFAEIIHSVEEHCYRQGYSLFCVTHKMDPEKVKNHLEMLAKKRVDGLL 20 | VMCSEYTQDSLDLLSSFSTIPMVVMDWGPNANTDVIDDHSFDGGYLATKHLIECGHKKIG 21 | IICGELNKTTARTRYEGFEKAMEEAKLTINPSWVLEGAFEPEDGYECMNRLLTQEKLPTA 22 | LFCCNDVMALGAISALTEKGLRVPEDMSIIGYDDIHASRFYAPPLTTIHQSKLRLGRQAI 23 | NILLERITHKDEGVQQYSRIDITPELIIRKSVKSIL 24 | 25 | >PURR_ECOLI PURINE NUCLEOTIDE SYNTHESIS REPRESSOR 26 | MATIKDVAKRANVSTTTVSHVINKTRFVAEETRNAVWAAIKELHYSPSAVARSLKVNHTK 27 | SIGLLATSSEAAYFAEIIEAVEKNCFQKGYTLILGNAWNNLEKQRAYLSMMAQKRVDGLL 28 | VMCSEYPEPLLAMLEEYRHIPMVVMDWGEAKADFTDAVIDNAFEGGYMAGRYLIERGHRE 29 | IGVIPGPLERNTGAGRLAGFMKAMEEAMIKVPESWIVQGDFEPESGYRAMQQILSQPHRP 30 | TAVFCGGDIMAMGALCAADEMGLRVPQDVSLIGYDNVRNARYFTPALTTIHQPKDSLGET 31 | AFNMLLDRIVNKREEPQSIEVHPRLIERRSVADGPFRDYRR 32 | 33 | >PURR_SALTY PURINE NUCLEOTIDE SYNTHESIS REPRESSOR 34 | MATIKDVAKRANVSTTTVSHVINKTRFVAEETRNAVWAAIKELHYSPSAVARSLKVNHTK 35 | SIGLLATSSEAAYFAEIIEAVEKNCFQKGYTLILGNAWNNLEKQRAYLSMMAQKRVDGLL 36 | VMCSEYPEPLLSMLEEYRHIPMVVMDWGEAKADFTDTVIDNAFAGGYMAGRYLVERGHRD 37 | IGVIPGPLERNTGAGRLAGFMKAMEEALINVPDNWIVQGDFEPESGYHAMQQILSQSHRP 38 | TAVFCGGDIMAMGALCAADEMGLRVPQDVSVIGYDNVRNARYFTPALTTIHQPKDSLGET 39 | AFNMLLDRIVNKREESQSIEVHPRLVERRSVADGPFRDYRR 40 | 41 | >RBSR_ECOLI RIBOSE OPERON REPRESSOR 42 | MATMKDVARLAGVSTSTVSHVINKDRFVSEAITAKVEAAIKELNYAPSALARSLKLNQTH 43 | TIGMLITASTNPFYSELVRGVERSCFERGYSLVLCNTEGDEQRMNRNLETLMQKRVDGLL 44 | LLCTETHQPSREIMQRYPTVPTVMMDWAPFDGDSDLIQDNSLLGGDLATQYLIDKGHTRI 45 | ACITGPLDKTPARLRLEGYRAAMKRAGLNIPDGYEVTGDFEFNGGFDAMRQLLSHPLRPQ 46 | AVFTGNDAMAVGVYQALYQAELQVPQDIAVIGYDDIELASFMTPPLTTIHQPKDELGELA 47 | IDVLIHRITQPTLQQQRLQLTPILMERGSA 48 | 49 | >DEGA_BACSU DEGRADATION ACTIVATOR 50 | MKTTIYDVAKAAGVSITTVSRVINNTGRISDKTRQKVMNVMNEMAYTPNVHAAALTGKRT 51 | NMIALVAPDISNPFYGELAKSIEERADELGFQMLICSTDYDPKKETKYFSVLKQKKVDGI 52 | IFATGIESHDSMSALEEIASEQIPIAMISQDKPLLPMDIVVIDDVRGGYEAAKHLLSLGH 53 | TNIACIIGDGSTTGEKNRIKGFRQAMEEAGVPIDESLIIQTRFSLESGKEEAGKLLDRNA 54 | PTAIFAFNDVLACAAIQAARIRGIKVPDDLSIIGFDNTILAEMAAPPLTTVAQPIKEMGA 55 | ERHRTAGRSNRGKRKAKQKIVLPPELVVRHSTSPLNT 56 | 57 | >LACI_KLEPN LACTOSE OPERON REPRESSOR (FRAGMENT) 58 | MPRRTATLEDVARRGRVPADGLRRVLNRPEVVSARTREQVIRAMQALHYVPNRSAQLLAG 59 | KAAPSIGLITASVTLHAPSQIAAAIKSHASLHQLEVAIAMPAQADFVALQARLDELRAQH 60 | IRGVIVSLPLESATAERLVQDNPDMACLFLDVSPEADVCCVRFDHRDGCGACVRHLWEMG 61 | HREFGLLAGPESSVSARLRLASWREALHSLNIARSTTVFGDWSAASGWQKTFELLHLQPR 62 | ISAIVVANDQMALGVLSALAQLNRSGSQAVSVTGYDDTADSLYFQPPLTTVAQDFDLLGK 63 | RAVERLIALMAAPQLRIRELLPTRLIVRQSAWPVAAAEDRQQTLAQLKALVEKL 64 | 65 | >RBSR_HAEIN RIBOSE OPERON REPRESSOR 66 | MATMKDIARLAQVSTSTVSHVINGSRFVSDEIREKVMRIVAELNYTPSAVARSLKVRETK 67 | TIGLLVTATNNPFFAEVMAGVEQYCQKNQYNLIIATTGGDAKRLQQNLQTLMHKQVDGLL 68 | LMCGDSRFQADIELAISLPLVVMDWWFTELNADKILENSALGGYLATKALIDAGHRKIGI 69 | ITGNLKKSVAQNRLQGYKNALSEAKIALNPHWIVESHFDFEGGVLGIQSLLTQSSRPTAV 70 | FCCSDTIAVGAYQAIQQQGLRIPQDLSIMGYDDIELARYLSPPLSTICQPKAELGKLAVE 71 | TLLQRIKNPNENYRTLVLEPTCVLRESIYSLK 72 | -------------------------------------------------------------------------------- /sift4g/src/database_alignment.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file database_alignment.cpp 3 | * 4 | * @brief Database alignment source file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #include "utils.hpp" 10 | #include "database_alignment.hpp" 11 | 12 | constexpr uint32_t database_chunk = 1000000000; /* ~1GB */ 13 | constexpr float log_step_percentage = 2.5; 14 | 15 | void valueFunction(double* values, int* scores, Chain* query, Chain** database, 16 | int databaseLen, int* cards, int cardsLen, void* param_ ); 17 | 18 | void createFilteredDatabase(std::vector& used_indices, Chain*** filtered_database, 19 | std::vector& indices, Chain** database, uint32_t database_length); 20 | 21 | void alignDatabase(DbAlignment**** alignments, int** alignments_lengths, Chain*** _database, 22 | int32_t* _database_length, const std::string& database_path, Chain** queries, 23 | int32_t queries_length, std::vector>& indices, 24 | int32_t algorithm, EValueParams* evalue_params, double max_evalue, 25 | uint32_t max_alignments, Scorer* scorer, int32_t* cards, 26 | int32_t cards_length) { 27 | 28 | fprintf(stderr, "** Aligning queries with candidate sequences **\n"); 29 | 30 | Chain** database = nullptr; 31 | int database_length = 0; 32 | int database_start = 0; 33 | 34 | FILE* handle = nullptr; 35 | int serialized = 0; 36 | readFastaChainsPartInit(&database, &database_length, &handle, &serialized, 37 | database_path.c_str()); 38 | 39 | uint32_t part = 1; 40 | float part_size = database_chunk / (float) 1000000000; 41 | uint32_t log_size = queries_length / (100. / log_step_percentage); 42 | 43 | while (true) { 44 | 45 | int status = 1; 46 | 47 | status &= readFastaChainsPart(&database, &database_length, handle, 48 | serialized, database_chunk); 49 | 50 | databaseLog(part, part_size, 0); 51 | 52 | uint32_t log_counter = 0; 53 | float log_percentage = log_step_percentage; 54 | 55 | /* have to malloc... */ 56 | DbAlignment*** alignments_part = (DbAlignment***) malloc(queries_length * sizeof(DbAlignment**)); 57 | int* alignments_part_lengths = (int*) malloc(queries_length * sizeof(int)); 58 | 59 | std::vector used_sequences(database_length, false); 60 | 61 | for (int32_t i = 0; i < queries_length; ++i) { 62 | 63 | ++log_counter; 64 | if (log_size != 0 && log_counter % log_size == 0 && log_percentage < 100.) { 65 | databaseLog(part, part_size, log_percentage); 66 | log_percentage += log_step_percentage; 67 | } 68 | 69 | std::vector used_indices; 70 | Chain** filtered_database = nullptr; 71 | createFilteredDatabase(used_indices, &filtered_database, indices[i], 72 | database, database_length); 73 | 74 | if (used_indices.empty()) { 75 | alignments_part[i] = nullptr; 76 | alignments_part_lengths[i] = 0; 77 | continue; 78 | } 79 | 80 | ChainDatabase* chain_database = chainDatabaseCreate(filtered_database, 0, 81 | used_indices.size(), cards, cards_length); 82 | 83 | alignDatabase(&alignments_part[i], &alignments_part_lengths[i], algorithm, 84 | queries[i], chain_database, scorer, max_alignments, valueFunction, 85 | (void*) evalue_params, max_evalue, nullptr, 0, cards, 86 | cards_length, nullptr); 87 | 88 | for (int32_t j = 0; j < alignments_part_lengths[i]; ++j) { 89 | used_sequences[used_indices[dbAlignmentGetTargetIdx(alignments_part[i][j])]] = true; 90 | } 91 | 92 | chainDatabaseDelete(chain_database); 93 | 94 | delete[] filtered_database; 95 | } 96 | 97 | if (*alignments == nullptr) { 98 | *alignments = alignments_part; 99 | *alignments_lengths = alignments_part_lengths; 100 | } else { 101 | dbAlignmentsMerge(*alignments, *alignments_lengths, alignments_part, 102 | alignments_part_lengths, queries_length, max_alignments); 103 | deleteShotgunDatabase(alignments_part, alignments_part_lengths, queries_length); 104 | } 105 | 106 | for (int32_t i = database_start; i < database_length; ++i) { 107 | if (!used_sequences[i] && database[i] != nullptr) { 108 | chainDelete(database[i]); 109 | database[i] = nullptr; 110 | } 111 | } 112 | 113 | databaseLog(part, part_size, 100); 114 | ++part; 115 | 116 | if (status == 0) { 117 | break; 118 | } 119 | 120 | database_start = database_length; 121 | } 122 | fprintf(stderr, "\n\n"); 123 | 124 | fclose(handle); 125 | *_database = database; 126 | *_database_length = database_length; 127 | } 128 | 129 | void valueFunction(double* values, int* scores, Chain* query, Chain** database, 130 | int databaseLen, int* cards, int cardsLen, void* param_ ) { 131 | 132 | EValueParams* eValueParams = (EValueParams*) param_; 133 | eValues(values, scores, query, database, databaseLen, cards, cardsLen, eValueParams); 134 | } 135 | 136 | void createFilteredDatabase(std::vector& used_indices, Chain*** filtered_database, 137 | std::vector& indices, Chain** database, uint32_t database_length) { 138 | 139 | uint32_t database_end = database_length == 0 ? 0 : database_length - 1; 140 | 141 | uint32_t i = 0; 142 | for (; i < indices.size(); ++i) { 143 | if (indices[i] > database_end) { 144 | break; 145 | } 146 | } 147 | 148 | uint32_t used_indices_length = i; 149 | 150 | if (used_indices_length != 0) { 151 | used_indices.reserve(used_indices_length); 152 | *filtered_database = new Chain*[used_indices_length](); 153 | 154 | for (uint32_t j = 0; j < used_indices_length; ++j) { 155 | used_indices.emplace_back(indices[j]); 156 | (*filtered_database)[j] = database[indices[j]]; 157 | } 158 | 159 | std::vector tmp(indices.begin() + used_indices_length, indices.end()); 160 | indices.swap(tmp); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /sift4g/src/constants.hpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file constants.h 3 | * @brief contains constant arrays with various parameters 4 | * 5 | * @author: pauline-ng, angsm 6 | */ 7 | 8 | #pragma once 9 | 10 | constexpr double kLog_2_20 = 4.321928095; 11 | 12 | /* generate from default.rank, but re-orderd to alphabet index using 13 | convert_rank_matrix.py . Matrix is assymetric. rank_matrix[row][col]*/ 14 | constexpr int rank_matrix[26][26] = { 15 | { 1, -1, 6, 17, 10, 19, 3, 16, 12, -1, 7, 14, 11, 15, -1, 9, 8, 13, 2, 4, -1, 5, 20, -1, 18, -1}, 16 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 17 | { 2, -1, 1, 19, 20, 10, 12, 16, 6, -1, 17, 7, 8, 13, -1, 14, 15, 18, 5, 4, -1, 3, 9, -1, 11, -1}, 18 | { 12, -1, 17, 1, 2, 18, 9, 8, 15, -1, 6, 19, 13, 3, -1, 10, 5, 11, 4, 7, -1, 16, 20, -1, 14, -1}, 19 | { 10, -1, 20, 3, 1, 18, 14, 6, 19, -1, 4, 17, 12, 8, -1, 11, 2, 5, 7, 9, -1, 15, 16, -1, 13, -1}, 20 | { 10, -1, 12, 19, 18, 1, 16, 8, 6, -1, 15, 4, 5, 14, -1, 20, 17, 13, 11, 9, -1, 7, 3, -1, 2, -1}, 21 | { 2, -1, 14, 5, 10, 17, 1, 9, 20, -1, 6, 19, 15, 4, -1, 11, 8, 12, 3, 7, -1, 18, 13, -1, 16, -1}, 22 | { 12, -1, 18, 9, 5, 10, 14, 1, 20, -1, 7, 17, 11, 3, -1, 15, 4, 6, 8, 13, -1, 19, 16, -1, 2, -1}, 23 | { 8, -1, 7, 16, 17, 5, 20, 19, 1, -1, 12, 3, 4, 18, -1, 13, 14, 15, 10, 6, -1, 2, 11, -1, 9, -1}, 24 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 25 | { 10, -1, 19, 8, 4, 20, 13, 9, 17, -1, 1, 16, 12, 5, -1, 11, 3, 2, 6, 7, -1, 15, 18, -1, 14, -1}, 26 | { 9, -1, 8, 19, 16, 5, 20, 15, 3, -1, 14, 1, 2, 18, -1, 17, 11, 12, 13, 7, -1, 4, 10, -1, 6, -1}, 27 | { 8, -1, 12, 20, 16, 5, 19, 15, 3, -1, 10, 2, 1, 17, -1, 18, 6, 11, 14, 7, -1, 4, 13, -1, 9, -1}, 28 | { 11, -1, 15, 2, 8, 17, 9, 4, 18, -1, 7, 19, 14, 1, -1, 12, 5, 10, 3, 6, -1, 16, 20, -1, 13, -1}, 29 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 30 | { 3, -1, 16, 8, 6, 19, 11, 12, 15, -1, 4, 17, 14, 9, -1, 1, 7, 10, 2, 5, -1, 13, 20, -1, 18, -1}, 31 | { 11, -1, 19, 8, 2, 20, 14, 5, 18, -1, 3, 16, 9, 6, -1, 12, 1, 4, 7, 10, -1, 17, 15, -1, 13, -1}, 32 | { 10, -1, 20, 11, 4, 18, 15, 5, 19, -1, 2, 14, 9, 6, -1, 13, 3, 1, 7, 8, -1, 16, 17, -1, 12, -1}, 33 | { 3, -1, 12, 8, 6, 18, 9, 13, 17, -1, 7, 19, 14, 4, -1, 11, 5, 10, 1, 2, -1, 15, 20, -1, 16, -1}, 34 | { 3, -1, 11, 12, 10, 19, 16, 18, 9, -1, 7, 15, 6, 4, -1, 13, 8, 14, 2, 1, -1, 5, 20, -1, 17, -1}, 35 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 36 | { 6, -1, 7, 20, 14, 8, 19, 18, 2, -1, 12, 3, 4, 17, -1, 13, 11, 15, 10, 5, -1, 1, 16, -1, 9, -1}, 37 | { 11, -1, 7, 20, 16, 3, 10, 8, 12, -1, 17, 5, 4, 19, -1, 18, 6, 13, 14, 9, -1, 15, 1, -1, 2, -1}, 38 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 39 | { 13, -1, 17, 20, 15, 2, 19, 4, 8, -1, 14, 6, 5, 16, -1, 18, 9, 12, 11, 10, -1, 7, 3, -1, 1, -1}, 40 | { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} 41 | }; 42 | 43 | /* 44 | 13-component Dirichlet 45 | Name = merge-opt.13comp 46 | Order of amino acids is A,B,C,D,... 47 | */ 48 | const double diri_q[13] = { 49 | 0.10935200, 0.08624370, 0.09149870, 0.08177380, 0.07078570, 0.04727980, 0.06654990, 0.10472800, 0.09536710, 0.05250920, 0.03843090, 0.09601160, 0.05946920 50 | }; 51 | 52 | const double aa_frequency[26] = { 53 | 0.07587, 0, 0.01666, 0.05287, 0.06377, 0.04096, 0.06847, 0.02246, 0.05817, 0, 0.05957, 0.09427, 0.02376, 0.04456, 0, 0.04906, 0.03976, 0.05166, 0.07127, 0.05677, 0, 0.06587, 0.01236, 0, 0.03186, 0 54 | }; 55 | 56 | const double diri_altot[13] = { 57 | 1.30226000, 1.81388000, 1.70395000, 2.11182000, 4.96847000, 3.33809000, 2.19222000, 0.05904830, 0.08441720, 2.10287000, 1.10699000, 7.12351000, 2.18943000 58 | }; 59 | 60 | const double diri_alpha[13][26] = { 61 | { 0.388746, 0, 0.0367385, 0.026518, 0.0322076, 0.0137612, 0.206244, 0.0059514, 0.0158113, 0, 0.0351076, 0.0350778, 0.0164858, 0.0224268, 0, 0.0917171, 0.0241792, 0.0253054, 0.187681, 0.0626166, 0, 0.0602319, 0.00498456, 0, 0.0104666, 0}, 62 | { 0.0517631, 0, 0.0133196, 0.00631385, 0.0132966, 0.141159, 0.0103533, 0.0104886, 0.264004, 0, 0.0177537, 0.807661, 0.175596, 0.0127646, 0, 0.0220077, 0.0271782, 0.0177749, 0.0144998, 0.0299289, 0, 0.143995, 0.016497, 0, 0.0175224, 0}, 63 | { 0.0610492, 0, 0.0148718, 0.315694, 0.0908927, 0.012609, 0.222958, 0.0672017, 0.006044, 0, 0.0877828, 0.0135798, 0.00485097, 0.368744, 0, 0.0498296, 0.0588252, 0.044453, 0.168811, 0.0652581, 0, 0.010088, 0.00652446, 0, 0.0338823, 0}, 64 | { 0.0796662, 0, 0.00761021, 0.0136208, 0.110994, 0.0165387, 0.0563167, 0.0713015, 0.0341013, 0, 0.564383, 0.0808963, 0.0267238, 0.077247, 0, 0.0627218, 0.18985, 0.475607, 0.0651016, 0.0855045, 0, 0.0452026, 0.0125425, 0, 0.0358879, 0}, 65 | { 0.317696, 0, 0.110939, 0.128582, 0.170879, 0.441344, 0.206556, 0.339981, 0.246824, 0, 0.181928, 0.407255, 0.138334, 0.192446, 0, 0.127958, 0.183911, 0.217794, 0.251084, 0.245119, 0, 0.314739, 0.115968, 0, 0.629135, 0}, 66 | { 0.0446044, 0, 0.0170118, 0.0134169, 0.014928, 0.0818952, 0.0279526, 2.74064e-06, 1.24306, 0, 0.0235435, 0.433818, 0.0752952, 0.0156684, 0, 0.00769209, 0.00598883, 0.0194969, 0.0237729, 0.0605068, 0, 1.17986, 0.00984592, 0, 0.039723, 0}, 67 | { 0.142671, 0, 0.00172471, 0.391867, 0.796428, 0.0127552, 0.0507474, 0.0337799, 0.0169946, 0, 0.143701, 0.0315845, 0.0130077, 0.0560163, 0, 0.086296, 0.189149, 0.0377603, 0.0701466, 0.0617127, 0, 0.0327986, 0.00600271, 0, 0.0170779, 0}, 68 | { 0.00281745, 0, 0.00552169, 0.00417499, 0.00218621, 0.00287303, 0.00978086, 0.00226513, 0.00229518, 0, 0.000288688, 0.00312013, 0.00106406, 0.00244641, 0, 0.00279557, 0.00165027, 0.00285057, 0.00125284, 0.00271746, 0, 0.00320849, 0.00225774, 0, 0.00348149, 0}, 69 | { 0.00322581, 0, 0.00013552, 0.00597818, 0.00438741, 0.00193385, 0.0158901, 0.00243118, 0.00206323, 0, 0.00656549, 0.00518845, 0.000562418, 0.00273016, 0, 0.0121928, 0.00322314, 0.00579248, 0.00329082, 0.00288987, 0, 0.00206985, 0.00227726, 0, 0.00158929, 0}, 70 | { 0.14246, 0, 0.0306225, 0.0639103, 0.0396449, 0.0154444, 0.0394262, 0.018336, 0.0452637, 0, 0.0606921, 0.0412351, 0.0225931, 0.117688, 0, 0.0752369, 0.0475692, 0.038186, 0.50948, 0.696177, 0, 0.0708571, 0.0102097, 0, 0.0178433, 0}, 71 | { 0.0177105, 0, 0.00908289, 0.00274088, 0.00444563, 0.411122, 0.00282536, 1.4813e-06, 0.0384249, 0, 1.47392e-06, 0.0931481, 0.0186396, 1.47581e-06, 0, 0.00676848, 1.47629e-06, 1.47755e-06, 0.012354, 0.0111622, 0, 0.035578, 0.0874503, 0, 0.35553, 0}, 72 | { 0.668982, 0, 0.0472318, 0.554692, 0.890999, 0.0440375, 0.336612, 0.107352, 0.16552, 0, 0.822984, 0.267021, 0.0936297, 0.437926, 0, 0.242805, 0.521498, 0.464298, 0.649512, 0.507446, 0, 0.252405, 0.0151661, 0, 0.0333911, 0}, 73 | { 0.304645, 0, 0.0610182, 0.0139475, 0.0452959, 0.0312398, 1.47952e-06, 0.0113568, 0.3088088, 0, 0.0448228, 0.209537, 0.0639208, 0.020799, 0, 0.0641272, 0.0336194, 0.0248684, 0.0434541, 0.21585, 0, 0.677503, 0.0052348, 0, 0.00937749, 0} 74 | }; 75 | 76 | 77 | static float getMedian(float* a, int len) { 78 | 79 | std::sort(&a[0], &a[len - 1]); 80 | 81 | if (len % 2 == 0) { 82 | return (a[len / 2 - 1] + a[len / 2]) / 2.0; 83 | } else { 84 | return a[len / 2]; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /sift4g/src/sift_prediction.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file sift_prediction.cpp 3 | * 4 | * @brief SIFT predictions source file 5 | * 6 | * @author: rvaser, pauline-ng, angsm 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "utils.hpp" 16 | #include "sift_scores.hpp" 17 | #include "sift_prediction.hpp" 18 | 19 | constexpr uint32_t kMaxSequences = 400; 20 | 21 | class ThreadPredictionData { 22 | public: 23 | ThreadPredictionData(std::vector& _alignment_strings, Chain* _query, const std::string& _subst_path, 24 | int32_t _sequence_identity, const std::string& _out_path) 25 | : alignment_strings(_alignment_strings), query(_query), sequence_identity(_sequence_identity), 26 | subst_path(_subst_path), out_path(_out_path) { 27 | } 28 | 29 | std::vector& alignment_strings; 30 | Chain* query; 31 | int32_t sequence_identity; 32 | std::string subst_path; 33 | std::string out_path; 34 | }; 35 | 36 | void* threadSiftPredictions(void* params); 37 | 38 | /***************************************************************************** 39 | *****************************************************************************/ 40 | 41 | void checkData(Chain** queries, int32_t& queries_length, const std::string& subst_path) { 42 | 43 | auto read_subst_file = [](std::vector& substitutions, const std::string& path) -> void { 44 | std::ifstream infile(path); 45 | std::string line; 46 | if (infile.good()) { 47 | while (std::getline(infile, line)) { 48 | substitutions.push_back(line); 49 | } 50 | } 51 | infile.close(); 52 | return; 53 | }; 54 | 55 | auto check_substitutions = [](const std::vector& substitutions, Chain* chain) -> bool { 56 | 57 | bool is_valid = true; 58 | std::regex regex_subst("^([A-Z])([0-9]+)([A-Z])"); /*, std::regex_constants::basic); */ 59 | std::smatch m; 60 | 61 | uint32_t num_valid_lines = 0; 62 | for (const auto& it: substitutions) { 63 | if (regex_search(it, m, regex_subst)) { 64 | ++num_valid_lines; 65 | char ref_aa = std::string(m[1])[0]; 66 | int pos = stoi(std::string(m[2])) - 1; 67 | if (pos >= chainGetLength(chain)) { 68 | fprintf(stderr, "* skipping protein [ %s ]: substitution list has a position out of bounds (line: %s, query length = %d) *\n", 69 | chainGetName(chain), it.c_str(), chainGetLength(chain)); 70 | is_valid = false; 71 | break; 72 | } 73 | if (chainGetChar(chain, pos) != ref_aa) { 74 | fprintf(stderr, "* skipping protein [ %s ]: substitution list assumes wrong amino acid at position %d (line: %s, query amino acid = %c) *\n", 75 | chainGetName(chain), pos+1, it.c_str(), chainGetChar(chain, pos)); 76 | is_valid = false; 77 | break; 78 | } 79 | } 80 | } 81 | 82 | 83 | if (num_valid_lines == 0) { 84 | fprintf(stderr, "* skipping protein [ %s ]: substitution list contains zero valid lines *\n", chainGetName(chain)); 85 | is_valid = false; 86 | } 87 | 88 | return is_valid; 89 | }; 90 | 91 | std::string subst_extension = ".subst"; 92 | std::vector is_valid_chain(queries_length, true); 93 | bool shrink = false; 94 | 95 | fprintf(stderr, "** Checking query data and substitutions files **\n"); 96 | 97 | for (int32_t i = 0; i < queries_length; ++i) { 98 | 99 | char* subst_file_name = createFileName(chainGetName(queries[i]), subst_path, subst_extension); 100 | 101 | if (isExtantPath(subst_file_name) == 1) { 102 | std::vector substitutions; 103 | read_subst_file(substitutions, subst_file_name); 104 | if (check_substitutions(substitutions, queries[i]) == false) { 105 | chainDelete(queries[i]); 106 | queries[i] = nullptr; 107 | is_valid_chain[i] = false; 108 | shrink = true; 109 | } 110 | } 111 | 112 | delete[] subst_file_name; 113 | 114 | queryLog(i + 1, queries_length); 115 | } 116 | 117 | if (shrink == true) { 118 | int32_t i = 0, j = 0; 119 | for (; i < queries_length; ++i) { 120 | if (is_valid_chain[i] == true) { 121 | continue; 122 | } 123 | j = std::max(j, i); 124 | while (j < queries_length && !is_valid_chain[j]) { 125 | ++j; 126 | } 127 | 128 | if (j >= queries_length) { 129 | break; 130 | } else if (i != j) { 131 | queries[i] = queries[j]; 132 | is_valid_chain[i] = true; 133 | is_valid_chain[j] = false; 134 | } 135 | } 136 | if (i < queries_length) { 137 | queries_length = i; 138 | } 139 | } 140 | 141 | fprintf(stderr, "\n\n"); 142 | } 143 | 144 | void siftPredictions(std::vector>& alignment_strings, 145 | Chain** queries, int32_t queries_length, const std::string& subst_path, 146 | int32_t sequence_identity, const std::string& out_path) { 147 | 148 | fprintf(stderr, "** Generating SIFT predictions with sequence identity: %.2f%% **\n", (float) sequence_identity); 149 | 150 | std::vector thread_tasks(queries_length, nullptr); 151 | 152 | for (int32_t i = 0; i < queries_length; ++i) { 153 | 154 | if (alignment_strings[i].empty()) { 155 | continue; 156 | } 157 | 158 | auto thread_data = new ThreadPredictionData(alignment_strings[i], queries[i], 159 | subst_path, sequence_identity, out_path); 160 | 161 | thread_tasks[i] = threadPoolSubmit(threadSiftPredictions, (void*) thread_data); 162 | } 163 | 164 | for (int32_t i = 0; i < queries_length; ++i) { 165 | threadPoolTaskWait(thread_tasks[i]); 166 | threadPoolTaskDelete(thread_tasks[i]); 167 | queryLog(i + 1, queries_length); 168 | } 169 | 170 | fprintf(stderr, "\n\n"); 171 | } 172 | 173 | /***************************************************************************** 174 | *****************************************************************************/ 175 | 176 | void* threadSiftPredictions(void* params) { 177 | 178 | auto thread_data = (ThreadPredictionData*) params; 179 | 180 | std::string subst_extension = ".subst"; 181 | std::string out_extension = ".SIFTprediction"; 182 | 183 | // only keep first 399 hits, erase more distant ones 184 | if (thread_data->alignment_strings.size() > kMaxSequences - 1) { 185 | for (uint32_t j = kMaxSequences - 1; j < thread_data->alignment_strings.size(); ++j) { 186 | chainDelete(thread_data->alignment_strings[j]); 187 | } 188 | thread_data->alignment_strings.resize(kMaxSequences - 1); 189 | } 190 | 191 | int query_length = chainGetLength(thread_data->query); 192 | remove_seqs_percent_identical_to_query(thread_data->query, 193 | thread_data->alignment_strings, thread_data->sequence_identity); 194 | 195 | // add query sequence to the beginning of the alignment 196 | thread_data->alignment_strings.insert(thread_data->alignment_strings.begin(), 197 | chainCreateView(thread_data->query, 0, query_length - 1, 0)); 198 | int total_seq = thread_data->alignment_strings.size(); 199 | 200 | std::vector> matrix(query_length, std::vector(26, 0.0)); 201 | std::vector> SIFTscores(query_length, std::vector(26, 0.0)); 202 | 203 | std::vector weights_1(thread_data->alignment_strings.size(), 1.0); 204 | std::vector aas_stored(query_length, 0.0); 205 | 206 | createMatrix(thread_data->alignment_strings, thread_data->query, weights_1, matrix, aas_stored); 207 | 208 | calcSIFTScores(thread_data->alignment_strings, thread_data->query, matrix, SIFTscores); 209 | 210 | int num_seqs_in_alignment = thread_data->alignment_strings.size(); 211 | std::vector seq_weights (num_seqs_in_alignment); 212 | std::vector number_of_diff_aas (query_length); 213 | calcSeqWeights(thread_data->alignment_strings, matrix, aas_stored, seq_weights, number_of_diff_aas); 214 | 215 | char* subst_file_name = createFileName(chainGetName(thread_data->query), 216 | thread_data->subst_path, subst_extension); 217 | char* out_file_name = createFileName(chainGetName(thread_data->query), 218 | thread_data->out_path, out_extension); 219 | 220 | if (isExtantPath(subst_file_name) == 1) { 221 | std::list subst_list; 222 | std::unordered_map medianSeqInfoForPos; 223 | 224 | readSubstFile(subst_file_name, subst_list); 225 | hashPredictedPos(subst_list, medianSeqInfoForPos); 226 | addPosWithDelRef(thread_data->query, SIFTscores, medianSeqInfoForPos); 227 | addMedianSeqInfo(thread_data->alignment_strings, thread_data->query, 228 | matrix, medianSeqInfoForPos); 229 | printSubstFile(subst_list, medianSeqInfoForPos, SIFTscores, aas_stored, 230 | total_seq, thread_data->query, out_file_name); 231 | } else { 232 | // printMatrix(SIFTscores, out_file_name); 233 | printMatrixOriginalFormat(SIFTscores, out_file_name); 234 | } 235 | 236 | delete[] out_file_name; 237 | delete[] subst_file_name; 238 | 239 | delete thread_data; 240 | 241 | return nullptr; 242 | } 243 | -------------------------------------------------------------------------------- /sift4g/src/select_alignments.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file select_alignments.cpp 3 | * 4 | * @brief Alignment selection source file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "utils.hpp" 15 | #include "constants.hpp" 16 | #include "select_alignments.hpp" 17 | 18 | class ThreadSelectionData { 19 | public: 20 | ThreadSelectionData(std::vector& _dst, DbAlignment** _alignments, int _alignments_length, 21 | Chain* _query, float _threshold): 22 | dst(_dst), alignments(_alignments), alignments_length(_alignments_length), 23 | query(_query), threshold(_threshold) { 24 | } 25 | 26 | std::vector& dst; 27 | DbAlignment** alignments; 28 | int alignments_length; 29 | Chain* query; 30 | float threshold; 31 | }; 32 | 33 | void aligmentStr(char** query_str, char** target_str, Alignment* alignment, const char gap_item); 34 | 35 | void alignmentsExtract(std::vector& dst, Chain* query, DbAlignment** alignments, 36 | int alignments_length); 37 | 38 | int alignmentsSelect(std::vector& alignment_strings, Chain* query, float threshold); 39 | 40 | void* threadSelectAlignments(void* params); 41 | 42 | /***************************************************************************** 43 | *****************************************************************************/ 44 | 45 | void selectAlignments(std::vector>& dst, DbAlignment*** alignments, 46 | int32_t* alignments_lengths, Chain** queries, int32_t queries_length, 47 | float threshold) { 48 | 49 | dst.resize(queries_length); 50 | 51 | fprintf(stderr, "** Selecting alignments with median threshold: %.2f **\n", threshold); 52 | 53 | std::vector thread_tasks(queries_length, nullptr); 54 | 55 | for (int32_t i = 0; i < queries_length; ++i) { 56 | 57 | if (alignments_lengths[i] == 0) { 58 | continue; 59 | } 60 | 61 | auto thread_data = new ThreadSelectionData(dst[i], alignments[i], 62 | alignments_lengths[i], queries[i], threshold); 63 | 64 | thread_tasks[i] = threadPoolSubmit(threadSelectAlignments, (void*) thread_data); 65 | } 66 | 67 | for (int32_t i = 0; i < queries_length; ++i) { 68 | threadPoolTaskWait(thread_tasks[i]); 69 | threadPoolTaskDelete(thread_tasks[i]); 70 | queryLog(i + 1, queries_length); 71 | } 72 | 73 | fprintf(stderr, "\n\n"); 74 | } 75 | 76 | void outputSelectedAlignments(std::vector>& alignment_strings, 77 | Chain** queries, int32_t queries_length, const std::string& out_path) { 78 | 79 | std::string out_extension = ".aligned.fasta"; 80 | 81 | for (uint32_t i = 0; i < alignment_strings.size(); ++i) { 82 | 83 | std::ofstream out_file; 84 | char* out_file_name = createFileName(chainGetName(queries[i]), out_path, out_extension); 85 | 86 | int query_len = chainGetLength(queries[i]); 87 | 88 | out_file.open(out_file_name); 89 | out_file << ">QUERY" << std::endl; 90 | 91 | for (int j = 1; j < query_len + 1; ++j) { 92 | out_file << chainGetChar(queries[i], j - 1); 93 | if (j % 60 == 0) out_file << std::endl; 94 | } 95 | out_file << std::endl; 96 | 97 | for (uint32_t j = 0; j < alignment_strings[i].size(); ++j) { 98 | out_file << ">" << chainGetName(alignment_strings[i][j]) << std::endl; 99 | 100 | for (int k = 1; k < query_len + 1; ++k) { 101 | out_file << chainGetChar(alignment_strings[i][j], k - 1); 102 | if (k % 60 == 0) out_file << std::endl; 103 | } 104 | out_file << std::endl; 105 | } 106 | 107 | out_file.close(); 108 | delete[] out_file_name; 109 | } 110 | } 111 | 112 | void deleteSelectedAlignments(std::vector>& alignment_strings) { 113 | for (uint32_t i = 0; i < alignment_strings.size(); ++i) { 114 | for (uint32_t j = 0; j < alignment_strings[i].size(); ++j) { 115 | if (alignment_strings[i][j] != nullptr) { 116 | chainDelete(alignment_strings[i][j]); 117 | alignment_strings[i][j] = nullptr; 118 | } 119 | } 120 | std::vector().swap(alignment_strings[i]); 121 | } 122 | } 123 | 124 | /***************************************************************************** 125 | *****************************************************************************/ 126 | 127 | void alignmentsExtract(std::vector& dst, Chain* query, DbAlignment** alignments, 128 | int alignments_length) { 129 | 130 | int query_len = chainGetLength(query); 131 | 132 | char* alignment_str = new char[query_len]; 133 | int query_start, length; 134 | 135 | Chain* target = nullptr; 136 | 137 | char* query_str = nullptr; 138 | char* target_str = nullptr; 139 | Alignment* alignment = nullptr; 140 | 141 | const char gap_item = '-'; 142 | 143 | for (int i = 0; i < alignments_length; ++i) { 144 | 145 | target = dbAlignmentGetTarget(alignments[i]); 146 | 147 | query_start = dbAlignmentGetQueryStart(alignments[i]); 148 | length = dbAlignmentGetPathLen(alignments[i]); 149 | 150 | alignment = dbAlignmentToAlignment(alignments[i]); 151 | aligmentStr(&query_str, &target_str, alignment, gap_item); 152 | alignmentDelete(alignment); 153 | 154 | int j = 0; 155 | for (; j < query_start; ++j) { 156 | alignment_str[j] = 'X'; 157 | } 158 | 159 | for (int k = 0; k < length; ++k) { 160 | if (query_str[k] != gap_item) { 161 | if (target_str[k] != gap_item) { 162 | alignment_str[j++] = target_str[k]; 163 | } else { 164 | alignment_str[j++] = 'X'; 165 | } 166 | } 167 | } 168 | 169 | for (; j < query_len; ++j) { 170 | alignment_str[j] = 'X'; 171 | } 172 | 173 | dst.push_back(chainCreate((char*) chainGetName(target), strlen(chainGetName(target)), 174 | alignment_str, query_len)); 175 | 176 | delete[] query_str; 177 | delete[] target_str; 178 | } 179 | 180 | delete[] alignment_str; 181 | } 182 | 183 | int alignmentsSelect(std::vector& alignment_strings, Chain* query, float threshold) { 184 | 185 | int amino_acid_num = 26; 186 | float median = kLog_2_20; 187 | 188 | int* amino_acid_nums = new int[amino_acid_num]; 189 | for (int i = 0; i < amino_acid_num; ++i) { 190 | amino_acid_nums[i] = 0; 191 | } 192 | 193 | int query_len = chainGetLength(query); 194 | 195 | float* pos_freq = new float[query_len]; 196 | for (int i = 0; i < query_len; ++i) { 197 | pos_freq[i] = 0.0; 198 | } 199 | 200 | char c; 201 | int i, valid; 202 | for (i = 1; median > threshold && i <= (int) alignment_strings.size(); ++i) { 203 | 204 | for (int j = 0; j < query_len; ++j) { 205 | valid = 0; 206 | 207 | for (int k = 0; k < i; ++k) { 208 | c = chainGetChar(alignment_strings[k], j); 209 | if (c != 'X') { 210 | valid++; 211 | amino_acid_nums[(int) c - 'A']++; 212 | } 213 | } 214 | 215 | for (int k = 0; k < amino_acid_num; ++k) { 216 | if (amino_acid_nums[k] != 0) { 217 | pos_freq[j] += amino_acid_nums[k] / (float) valid * 218 | log2f(amino_acid_nums[k] / (float) valid); 219 | } 220 | } 221 | 222 | pos_freq[j] += kLog_2_20; 223 | 224 | for (int k = 0; k < amino_acid_num; ++k) { 225 | if (amino_acid_nums[k] != 0) { 226 | amino_acid_nums[k] = 0; 227 | } 228 | } 229 | } 230 | 231 | median = getMedian(pos_freq, query_len); 232 | 233 | for (int j = 0; j < query_len; ++j) { 234 | pos_freq[j] = 0.0; 235 | } 236 | } 237 | 238 | delete[] pos_freq; 239 | delete[] amino_acid_nums; 240 | 241 | return i - 1; 242 | } 243 | 244 | void aligmentStr(char** query_str, char** target_str, Alignment* alignment, const char gap_item) { 245 | /* code take from SW# (its a private function)*/ 246 | Chain* query = alignmentGetQuery(alignment); 247 | int query_start = alignmentGetQueryStart(alignment); 248 | 249 | Chain* target = alignmentGetTarget(alignment); 250 | int target_start = alignmentGetTargetStart(alignment); 251 | 252 | int path_len = alignmentGetPathLen(alignment); 253 | 254 | int query_idx = query_start; 255 | int target_idx = target_start; 256 | 257 | *query_str = new char[path_len]; 258 | *target_str = new char[path_len]; 259 | 260 | int i; 261 | for (i = 0; i < path_len; ++i) { 262 | 263 | char query_chr; 264 | char target_chr; 265 | 266 | switch (alignmentGetMove(alignment, i)) { 267 | case MOVE_LEFT: 268 | 269 | query_chr = gap_item; 270 | target_chr = chainGetChar(target, target_idx); 271 | 272 | target_idx++; 273 | 274 | break; 275 | case MOVE_UP: 276 | 277 | query_chr = chainGetChar(query, query_idx); 278 | target_chr = gap_item; 279 | 280 | query_idx++; 281 | 282 | break; 283 | case MOVE_DIAG: 284 | 285 | query_chr = chainGetChar(query, query_idx); 286 | target_chr = chainGetChar(target, target_idx); 287 | 288 | query_idx++; 289 | target_idx++; 290 | 291 | break; 292 | default: 293 | // error 294 | return; 295 | } 296 | 297 | (*query_str)[i] = query_chr; 298 | (*target_str)[i] = target_chr; 299 | } 300 | } 301 | 302 | void* threadSelectAlignments(void* params) { 303 | 304 | auto thread_data = (ThreadSelectionData*) params; 305 | 306 | thread_data->dst.clear(); 307 | 308 | alignmentsExtract(thread_data->dst, thread_data->query, thread_data->alignments, 309 | thread_data->alignments_length); 310 | 311 | uint32_t selected_alignments_length = alignmentsSelect(thread_data->dst, 312 | thread_data->query, thread_data->threshold); 313 | 314 | for (uint32_t i = selected_alignments_length; i < thread_data->dst.size(); ++i) { 315 | chainDelete(thread_data->dst[i]); 316 | } 317 | thread_data->dst.resize(selected_alignments_length); 318 | 319 | delete thread_data; 320 | 321 | return nullptr; 322 | } 323 | -------------------------------------------------------------------------------- /sift4g/src/database_search.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file database_search.cpp 3 | * 4 | * @brief Database search source file 5 | * 6 | * @author: rvaser 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "hash.hpp" 14 | #include "utils.hpp" 15 | #include "database_search.hpp" 16 | 17 | constexpr uint32_t database_chunk = 250000000; /* ~250MB */ 18 | constexpr float log_step_percentage = 2.5; 19 | 20 | class Candidate { 21 | public: 22 | Candidate(float _score, int _id) : 23 | score(_score), id(_id) { 24 | } 25 | 26 | bool operator<(const Candidate& other) const { 27 | return this->score > other.score; 28 | } 29 | 30 | float score; 31 | int32_t id; 32 | }; 33 | 34 | class ThreadSearchData { 35 | public: 36 | ThreadSearchData(std::shared_ptr _query_hash, uint32_t _queries_length, 37 | std::vector& _min_scores, Chain** _database, 38 | uint32_t _database_begin, uint32_t _database_end, 39 | uint32_t _kmer_length, uint32_t _max_candidates, 40 | std::vector>& _candidates, 41 | bool _log, uint32_t _part, float _part_size): 42 | query_hash(_query_hash), queries_length(_queries_length), min_scores(_min_scores), 43 | database(_database), database_begin(_database_begin), database_end(_database_end), 44 | kmer_length(_kmer_length), max_candidates(_max_candidates), candidates(_candidates), 45 | log(_log), part(_part), part_size(_part_size) { 46 | } 47 | 48 | std::shared_ptr query_hash; 49 | uint32_t queries_length; 50 | std::vector& min_scores; 51 | Chain** database; 52 | uint32_t database_begin; 53 | uint32_t database_end; 54 | uint32_t kmer_length; 55 | uint32_t max_candidates; 56 | std::vector>& candidates; 57 | bool log; 58 | uint32_t part; 59 | float part_size; 60 | }; 61 | 62 | void* threadSearchDatabase(void* params); 63 | 64 | int32_t longestIncreasingSubsequence(const std::vector& src); 65 | 66 | uint64_t searchDatabase(std::vector>& dst, 67 | const std::string& database_path, Chain** queries, int32_t queries_length, 68 | uint32_t kmer_length, uint32_t max_candidates, uint32_t num_threads) { 69 | 70 | fprintf(stderr, "** Searching database for candidate sequences **\n"); 71 | 72 | std::shared_ptr query_hash = createHash(queries, queries_length, 0, 73 | queries_length, kmer_length); 74 | 75 | Chain** database = nullptr; 76 | int database_length = 0; 77 | int database_start = 0; 78 | 79 | FILE* handle = nullptr; 80 | int serialized = 0; 81 | readFastaChainsPartInit(&database, &database_length, &handle, &serialized, 82 | database_path.c_str()); 83 | 84 | uint64_t database_cells = 0; 85 | 86 | std::vector min_scores(queries_length, 1000000.0); 87 | std::vector>> candidates(num_threads); 88 | 89 | uint32_t part = 1; 90 | float part_size = database_chunk / (float) 1000000000; 91 | 92 | while (true) { 93 | 94 | int status = 1; 95 | 96 | status &= readFastaChainsPart(&database, &database_length, handle, 97 | serialized, database_chunk); 98 | 99 | databaseLog(part, part_size, 0); 100 | 101 | uint32_t database_split_size = (database_length - database_start) / num_threads; 102 | std::vector database_splits(num_threads + 1, database_start); 103 | for (uint32_t i = 1; i < num_threads; ++i) { 104 | database_splits[i] += i * database_split_size; 105 | } 106 | database_splits[num_threads] = database_length; 107 | 108 | std::vector thread_tasks(num_threads, nullptr); 109 | 110 | for (uint32_t i = 0; i < num_threads; ++i) { 111 | 112 | auto thread_data = new ThreadSearchData(query_hash, queries_length, min_scores, 113 | database, database_splits[i], database_splits[i + 1], kmer_length, 114 | max_candidates, candidates[i], i == num_threads - 1, part, 115 | part_size); 116 | 117 | thread_tasks[i] = threadPoolSubmit(threadSearchDatabase, (void*) thread_data); 118 | } 119 | 120 | for (uint32_t i = 0; i < num_threads; ++i) { 121 | threadPoolTaskWait(thread_tasks[i]); 122 | threadPoolTaskDelete(thread_tasks[i]); 123 | } 124 | 125 | for (int i = database_start; i < database_length; ++i) { 126 | database_cells += chainGetLength(database[i]); 127 | chainDelete(database[i]); 128 | database[i] = nullptr; 129 | } 130 | 131 | // merge candidates from all threads 132 | for (int32_t i = 0; i < queries_length; ++i) { 133 | for (uint32_t j = 1; j < num_threads; ++j) { 134 | if (candidates[j][i].empty()) { 135 | continue; 136 | } 137 | candidates[0][i].insert(candidates[0][i].end(), 138 | candidates[j][i].begin(), candidates[j][i].end()); 139 | std::vector().swap(candidates[j][i]); 140 | } 141 | 142 | if (num_threads > 1) { 143 | std::sort(candidates[0][i].begin(), candidates[0][i].end()); 144 | if (candidates[0][i].size() > max_candidates) { 145 | std::vector tmp(candidates[0][i].begin(), 146 | candidates[0][i].begin() + max_candidates); 147 | candidates[0][i].swap(tmp); 148 | } 149 | } 150 | 151 | if (!candidates[0][i].empty()) { 152 | min_scores[i] = candidates[0][i].back().score; 153 | } 154 | } 155 | 156 | databaseLog(part, part_size, 100); 157 | ++part; 158 | 159 | if (status == 0) { 160 | break; 161 | } 162 | 163 | database_start = database_length; 164 | } 165 | fprintf(stderr, "\n\n"); 166 | 167 | fclose(handle); 168 | deleteFastaChains(database, database_length); 169 | 170 | dst.clear(); 171 | dst.resize(queries_length); 172 | 173 | for (int32_t i = 0; i < queries_length; ++i) { 174 | dst[i].reserve(candidates[0][i].size()); 175 | for (uint32_t j = 0; j < candidates[0][i].size(); ++j) { 176 | dst[i].emplace_back(candidates[0][i][j].id); 177 | } 178 | std::vector().swap(candidates[0][i]); 179 | std::sort(dst[i].begin(), dst[i].end()); 180 | } 181 | 182 | return database_cells; 183 | } 184 | 185 | void* threadSearchDatabase(void* params) { 186 | 187 | auto thread_data = (ThreadSearchData*) params; 188 | 189 | thread_data->candidates.resize(thread_data->queries_length); 190 | 191 | std::vector kmer_vector; 192 | std::vector> hits(thread_data->queries_length); 193 | std::vector min_scores(thread_data->min_scores); 194 | 195 | uint32_t log_counter = 0; 196 | uint32_t log_size = (thread_data->database_end - thread_data->database_begin) / (100. / log_step_percentage); 197 | float log_percentage = log_step_percentage; 198 | 199 | for (uint32_t i = thread_data->database_begin; i < thread_data->database_end; ++i) { 200 | 201 | if (thread_data->log && log_percentage < 100.0) { 202 | ++log_counter; 203 | if (log_size != 0 && log_counter % log_size == 0) { 204 | databaseLog(thread_data->part, thread_data->part_size, log_percentage); 205 | log_percentage += log_step_percentage; 206 | } 207 | } 208 | 209 | createKmerVector(kmer_vector, thread_data->database[i], thread_data->kmer_length); 210 | 211 | for (uint32_t j = 0; j < kmer_vector.size(); ++j) { 212 | if (j != 0 && kmer_vector[j] == kmer_vector[j - 1]) { 213 | continue; 214 | } 215 | 216 | Hash::Iterator begin, end; 217 | thread_data->query_hash->hits(begin, end, kmer_vector[j]); 218 | for (; begin != end; ++begin) { 219 | hits[begin->id].emplace_back(begin->position); 220 | } 221 | } 222 | 223 | for (uint32_t j = 0; j < thread_data->queries_length; ++j) { 224 | if (hits[j].empty()) { 225 | continue; 226 | } 227 | 228 | float similartiy_score = longestIncreasingSubsequence(hits[j]) / 229 | (float) chainGetLength(thread_data->database[i]); 230 | 231 | if (thread_data->candidates[j].size() < thread_data->max_candidates || similartiy_score > min_scores[j]) { 232 | thread_data->candidates[j].emplace_back(similartiy_score, i); 233 | min_scores[j] = std::min(min_scores[j], similartiy_score); 234 | } 235 | 236 | std::vector().swap(hits[j]); 237 | } 238 | } 239 | 240 | for (uint32_t i = 0; i < thread_data->queries_length; ++i) { 241 | std::sort(thread_data->candidates[i].begin(), thread_data->candidates[i].end()); 242 | 243 | if (thread_data->candidates[i].size() > thread_data->max_candidates) { 244 | std::vector tmp(thread_data->candidates[i].begin(), 245 | thread_data->candidates[i].begin() + thread_data->max_candidates); 246 | thread_data->candidates[i].swap(tmp); 247 | } 248 | } 249 | 250 | delete thread_data; 251 | 252 | return nullptr; 253 | } 254 | 255 | int32_t longestIncreasingSubsequence(const std::vector& src) { 256 | 257 | int32_t score = 0, l, u, temp; 258 | 259 | std::vector help(src.size() + 1, std::numeric_limits::max()); 260 | help[0] = std::numeric_limits::min(); 261 | 262 | for (uint32_t i = 0; i < src.size(); ++i) { 263 | l = 0; 264 | u = src.size(); 265 | 266 | while (u > l) { 267 | temp = std::floor((l + u) / 2.0f); 268 | if (help[temp] < src[i]) { 269 | l = temp + 1; 270 | } else { 271 | u = temp; 272 | } 273 | } 274 | 275 | help[l] = src[i]; 276 | score = score > l ? score : l; 277 | } 278 | 279 | return score; 280 | } 281 | -------------------------------------------------------------------------------- /sift4g/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "utils.hpp" 7 | #include "database_search.hpp" 8 | #include "database_alignment.hpp" 9 | #include "select_alignments.hpp" 10 | #include "sift_prediction.hpp" 11 | 12 | #include "swsharp/evalue.h" 13 | #include "swsharp/swsharp.h" 14 | 15 | #define CHAR_INT_LEN(x) (sizeof(x) / sizeof(CharInt)) 16 | typedef struct CharInt { 17 | const char* format; 18 | const int code; 19 | } CharInt; 20 | 21 | static struct option options[] = { 22 | {"query", required_argument, 0, 'q'}, 23 | {"database", required_argument, 0, 'd'}, 24 | {"kmer-length", required_argument, 0, 'k'}, 25 | {"max-candidates", required_argument, 0, 'C'}, 26 | {"median-threshold", required_argument, 0, 'T'}, 27 | {"cards", required_argument, 0, 'c'}, 28 | {"gap-extend", required_argument, 0, 'e'}, 29 | {"gap-open", required_argument, 0, 'g'}, 30 | {"matrix", required_argument, 0, 'm'}, 31 | {"out", required_argument, 0, 'o'}, 32 | {"subst", required_argument, 0, 'S'}, 33 | {"seq-id", required_argument, 0, 'I'}, 34 | {"sub-results", no_argument, 0, 's'}, 35 | {"outfmt", required_argument, 0, 'f'}, 36 | {"evalue", required_argument, 0, 'E'}, 37 | {"max-aligns", required_argument, 0, 'M'}, 38 | {"algorithm", required_argument, 0, 'A'}, 39 | {"threads", required_argument, 0, 't'}, 40 | {"help", no_argument, 0, 'h'}, 41 | {0, 0, 0, 0} 42 | }; 43 | 44 | static CharInt outFormats[] = { 45 | { "bm0", SW_OUT_DB_BLASTM0 }, 46 | { "bm8", SW_OUT_DB_BLASTM8 }, 47 | { "bm9", SW_OUT_DB_BLASTM9 }, 48 | { "light", SW_OUT_DB_LIGHT } 49 | }; 50 | 51 | static CharInt algorithms[] = { 52 | { "SW", SW_ALIGN }, 53 | { "NW", NW_ALIGN }, 54 | { "HW", HW_ALIGN }, 55 | { "OV", OV_ALIGN } 56 | }; 57 | 58 | static void getCudaCards(int** cards, int* cardsLen, char* optarg); 59 | static int getOutFormat(char* optarg); 60 | static int getAlgorithm(char* optarg); 61 | static void help(); 62 | 63 | int main(int argc, char* argv[]) { 64 | 65 | std::string query_path; 66 | std::string database_path; 67 | 68 | uint32_t kmer_length = 5; 69 | uint32_t max_candidates = 5000; 70 | 71 | int32_t gap_open = 10; 72 | int32_t gap_extend = 1; 73 | char* matrix = "BLOSUM_62"; 74 | 75 | uint32_t max_alignments = 400; 76 | double max_evalue = 0.0001; 77 | 78 | int32_t* cards = nullptr; 79 | int32_t cards_length = 0; 80 | 81 | std::string out_path = ""; 82 | bool sub_results = false; 83 | int32_t out_format = SW_OUT_DB_BLASTM9; 84 | 85 | int32_t algorithm = SW_ALIGN; 86 | 87 | float median_threshold = 2.75; 88 | std::string subst_path = ""; 89 | int32_t sequence_identity = 100; 90 | 91 | uint32_t num_threads = 8; 92 | 93 | while (1) { 94 | 95 | char argument = getopt_long(argc, argv, "q:d:g:e:t:h", options, NULL); 96 | 97 | if (argument == -1) { 98 | break; 99 | } 100 | 101 | switch (argument) { 102 | case 'q': 103 | query_path = optarg; 104 | break; 105 | case 'd': 106 | database_path = optarg; 107 | break; 108 | case 'k': 109 | kmer_length = atoi(optarg); 110 | break; 111 | case 'C': 112 | max_candidates = atoi(optarg); 113 | break; 114 | case 'T': 115 | median_threshold = atof(optarg); 116 | break; 117 | case 'g': 118 | gap_open = atoi(optarg); 119 | break; 120 | case 'e': 121 | gap_extend = atoi(optarg); 122 | break; 123 | case 'c': 124 | getCudaCards(&cards, &cards_length, optarg); 125 | break; 126 | case 'o': 127 | out_path = optarg; 128 | break; 129 | case 'S': 130 | subst_path = optarg; 131 | break; 132 | case 's': 133 | sub_results = true; 134 | break; 135 | case 'I': 136 | sequence_identity = atoi(optarg); 137 | break; 138 | case 'f': 139 | out_format = getOutFormat(optarg); 140 | break; 141 | case 'M': 142 | max_alignments = atoi(optarg); 143 | break; 144 | case 'E': 145 | max_evalue = atof(optarg); 146 | break; 147 | case 'm': 148 | matrix = optarg; 149 | break; 150 | case 'A': 151 | algorithm = getAlgorithm(optarg); 152 | break; 153 | case 't': 154 | num_threads = atoi(optarg); 155 | break; 156 | case 'h': 157 | default: 158 | help(); 159 | return -1; 160 | } 161 | } 162 | 163 | ASSERT(!query_path.empty(), "missing option -q (query file)"); 164 | ASSERT(isExtantPath(query_path.c_str()) == 1, "invalid query file path '%s'", query_path.c_str()); 165 | 166 | ASSERT(!database_path.empty(), "missing option -d (database file)"); 167 | ASSERT(isExtantPath(database_path.c_str()) == 1, "invalid database file path '%s'", database_path.c_str()); 168 | 169 | ASSERT(kmer_length > 2 && kmer_length < 6, "kmer_length possible values = 3,4,5"); 170 | ASSERT(max_candidates > 0, "invalid max candidates number"); 171 | 172 | ASSERT(max_evalue > 0, "invalid evalue"); 173 | ASSERT(num_threads > 0, "invalid thread number"); 174 | 175 | if (!out_path.empty()) { 176 | ASSERT(isExtantPath(out_path.c_str()) == 0, "invalid out directory path '%s'", out_path.c_str()); 177 | } 178 | 179 | if (!subst_path.empty()) { 180 | ASSERT(isExtantPath(subst_path.c_str()) == 0, "invalid substitutions directory path '%s'", subst_path.c_str()); 181 | } 182 | 183 | if (cards_length == -1) { 184 | cudaGetCards(&cards, &cards_length); 185 | } 186 | ASSERT(cudaCheckCards(cards, cards_length), "invalid cuda cards"); 187 | 188 | threadPoolInitialize(num_threads); 189 | 190 | Chain** queries = nullptr; 191 | int32_t queries_length = 0; 192 | readFastaChains(&queries, &queries_length, query_path.c_str()); 193 | checkData(queries, queries_length, subst_path); 194 | 195 | if (queries_length == 0) { 196 | fprintf(stderr, "** EXITING! No valid queries to process. **\n"); 197 | threadPoolTerminate(); 198 | free(queries); 199 | free(cards); 200 | return -1; 201 | } 202 | 203 | std::vector> indices; 204 | uint64_t cells = searchDatabase(indices, database_path, queries, queries_length, 205 | kmer_length, max_candidates, num_threads); 206 | 207 | Scorer* scorer = nullptr; 208 | scorerCreateMatrix(&scorer, matrix, gap_open, gap_extend); 209 | 210 | EValueParams* evalue_params = createEValueParams(cells, scorer); 211 | 212 | DbAlignment*** alignments = nullptr; 213 | int* alignments_lenghts = nullptr; 214 | 215 | Chain** database = nullptr; 216 | int32_t database_length = 0; 217 | 218 | alignDatabase(&alignments, &alignments_lenghts, &database, &database_length, 219 | database_path, queries, queries_length, indices, algorithm, evalue_params, 220 | max_evalue, max_alignments, scorer, cards, cards_length); 221 | 222 | deleteEValueParams(evalue_params); 223 | scorerDelete(scorer); 224 | 225 | if (sub_results) { 226 | char* alignments_path = createFileName("alignments", out_path, ".txt"); 227 | outputShotgunDatabase(alignments, alignments_lenghts, queries_length, alignments_path, out_format); 228 | delete[] alignments_path; 229 | } 230 | 231 | std::vector> alignment_strings; 232 | selectAlignments(alignment_strings, alignments, alignments_lenghts, queries, queries_length, median_threshold); 233 | 234 | deleteShotgunDatabase(alignments, alignments_lenghts, queries_length); 235 | deleteFastaChains(database, database_length); 236 | 237 | if (sub_results) { 238 | outputSelectedAlignments(alignment_strings, queries, queries_length, out_path); 239 | } 240 | 241 | siftPredictions(alignment_strings, queries, queries_length, subst_path, 242 | sequence_identity, out_path); 243 | 244 | deleteSelectedAlignments(alignment_strings); 245 | deleteFastaChains(queries, queries_length); 246 | 247 | threadPoolTerminate(); 248 | 249 | free(cards); 250 | 251 | return 0; 252 | } 253 | 254 | static void getCudaCards(int** cards, int* cardsLen, char* optarg) { 255 | 256 | *cardsLen = strlen(optarg); 257 | *cards = (int*) malloc(*cardsLen * sizeof(int)); 258 | 259 | for (int32_t i = 0; i < *cardsLen; ++i) { 260 | (*cards)[i] = optarg[i] - '0'; 261 | } 262 | } 263 | 264 | static int getOutFormat(char* optarg) { 265 | 266 | for (uint32_t i = 0; i < CHAR_INT_LEN(outFormats); ++i) { 267 | if (strcmp(outFormats[i].format, optarg) == 0) { 268 | return outFormats[i].code; 269 | } 270 | } 271 | 272 | ASSERT(false, "unknown out format '%s'", optarg); 273 | } 274 | 275 | static int getAlgorithm(char* optarg) { 276 | 277 | for (uint32_t i = 0; i < CHAR_INT_LEN(algorithms); ++i) { 278 | if (strcmp(algorithms[i].format, optarg) == 0) { 279 | return algorithms[i].code; 280 | } 281 | } 282 | 283 | ASSERT(false, "unknown algorithm '%s'", optarg); 284 | } 285 | 286 | static void help() { 287 | printf( 288 | "usage: sift4g -q -d [arguments ...]\n" 289 | "\n" 290 | "arguments:\n" 291 | " -q, --query \n" 292 | " (required)\n" 293 | " input fasta query file\n" 294 | " -d, --database \n" 295 | " (required)\n" 296 | " input fasta database file\n" 297 | " -g, --gap-open \n" 298 | " default: 10\n" 299 | " gap opening penalty, must be given as a positive integer \n" 300 | " -e, --gap-extend \n" 301 | " default: 1\n" 302 | " gap extension penalty, must be given as a positive integer and\n" 303 | " must be less or equal to gap opening penalty\n" 304 | " --matrix \n" 305 | " default: BLOSUM_62\n" 306 | " similarity matrix, can be one of the following:\n" 307 | " BLOSUM_45\n" 308 | " BLOSUM_50\n" 309 | " BLOSUM_62\n" 310 | " BLOSUM_80\n" 311 | " BLOSUM_90\n" 312 | " BLOSUM_30\n" 313 | " BLOSUM_70\n" 314 | " BLOSUM_250\n" 315 | " --evalue \n" 316 | " default: 0.0001\n" 317 | " evalue threshold, alignments with higher evalue are filtered,\n" 318 | " must be given as a positive float\n" 319 | " --max-aligns \n" 320 | " default: 400\n" 321 | " maximum number of alignments to be outputted\n" 322 | " --algorithm \n" 323 | " default: SW\n" 324 | " algorithm used for alignment, must be one of the following: \n" 325 | " SW - Smith-Waterman local alignment\n" 326 | " NW - Needleman-Wunsch global alignment\n" 327 | " HW - semiglobal alignment\n" 328 | " OV - overlap alignment\n" 329 | " --cards \n" 330 | " default: all available CUDA cards\n" 331 | " list of cards should be given as an array of card indexes delimited with\n" 332 | " nothing, for example usage of first two cards is given as --cards 01\n" 333 | " --out \n" 334 | " default: current directory\n" 335 | " output directory for SIFT predictions\n" 336 | " --sub-results\n" 337 | " prints sub results (alignment file and a file per query containing\n" 338 | " its selected alignments forp rediction) to same directory defined\n" 339 | " with --out\n" 340 | " --outfmt \n" 341 | " default: bm9\n" 342 | " out format for the alignment file, must be one of the following:\n" 343 | " bm0 - blast m0 output format\n" 344 | " bm8 - blast m8 tabular output format\n" 345 | " bm9 - blast m9 commented tabular output format\n" 346 | " light - score-name tabbed output\n" 347 | " --kmer-length \n" 348 | " default: 5\n" 349 | " length of kmers used for database search\n" 350 | " possible values: 3, 4, 5\n" 351 | " --max-candidates \n" 352 | " default: 5000\n" 353 | " number of database sequences passed on to the Smith-Waterman part\n" 354 | " --median-threshold \n" 355 | " default: 2.75\n" 356 | " represents alignment diversity, used to output only a set of alignments\n" 357 | " --subst \n" 358 | " default: current directory\n" 359 | " directory containing substitution files for each query (extension .subst)\n" 360 | " files must have the same name as their corresponding query in FASTA file\n" 361 | " --seq-id \n" 362 | " default: 100\n" 363 | " -t, --threads \n" 364 | " default: 8\n" 365 | " number of threads used in thread pool\n" 366 | " -h, -help\n" 367 | " prints out the help\n"); 368 | } 369 | -------------------------------------------------------------------------------- /sift4g/src/sift_scores.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file sift_prediction.cpp 3 | * 4 | * @brief SIFT predictions source file 5 | * 6 | * @author: pauline-ng, angsm 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "utils.hpp" 20 | #include "constants.hpp" 21 | #include "sift_scores.hpp" 22 | 23 | constexpr uint32_t kMaxSequences = 400; 24 | constexpr double TOLERANCE_PROB_THRESHOLD = 0.05; 25 | constexpr double ADEQUATE_SEQ_INFO =3.25; 26 | // init_frq_qij 27 | constexpr double LOCAL_QIJ_RTOT = 5.0; 28 | 29 | void scale_matrix_to_max_aa(std::vector>& matrix, std::vector& max_aa_array) { 30 | 31 | int query_length = matrix.size(); 32 | int aas = matrix[0].size(); 33 | 34 | for (int pos = 0; pos < query_length; pos++) { 35 | int max_aa = max_aa_array[pos]; 36 | double max_aa_score = matrix[pos][max_aa]; 37 | for (int aa_index = 0; aa_index < aas; aa_index++) { 38 | matrix[pos][aa_index] = matrix[pos][aa_index] / max_aa_score; 39 | } 40 | } 41 | } 42 | 43 | void find_max_aa_in_matrix(std::vector>& matrix, std::vector& max_aa_index) { 44 | int query_length = matrix.size(); 45 | int aas = matrix[0].size(); 46 | 47 | for (int pos = 0; pos < query_length; pos++) { 48 | int max_aa = -1; 49 | double max_count = -1.0; 50 | for (int aa_index =0; aa_index < aas; aa_index++) { 51 | if (matrix[pos][aa_index] > max_count) { 52 | max_aa = aa_index; 53 | max_count = matrix[pos][aa_index]; 54 | } 55 | } 56 | max_aa_index[pos] = max_aa; 57 | } 58 | } 59 | 60 | void calcEpsilon(std::vector>& weighted_matrix, std::vector& max_aa_array, 61 | std::vector& number_of_diff_aas, std::vector& epsilon) { 62 | 63 | int query_length = weighted_matrix.size(); 64 | /*vector max_aa_array (query_length); 65 | find_max_aa_in_matrix (weighted_matrix, max_aa_array);*/ 66 | 67 | for (int pos =0; pos < query_length; pos++) { 68 | if (number_of_diff_aas[pos] == 1) { 69 | epsilon[pos] = 0; 70 | } else { 71 | int max_aa = max_aa_array[pos]; 72 | double sum = 0.0; 73 | double pos_tot = 0.0; 74 | for (char aa = 'A'; aa <= 'Z'; aa++) { 75 | if (valid_amino_acid(aa)) { 76 | int aa_index = int(aa) - int('A'); 77 | int rank = rank_matrix[max_aa][aa_index]; 78 | sum += (double) rank * weighted_matrix[pos][aa_index]; 79 | pos_tot += weighted_matrix[pos][aa_index]; 80 | } 81 | } 82 | sum = sum / pos_tot; 83 | epsilon[pos] = exp((double) sum); 84 | } 85 | } 86 | } 87 | 88 | void readSubstFile(char* substFilename, std::list& substList) { 89 | 90 | std::ifstream infile(substFilename); 91 | std::string line; 92 | 93 | if (infile.good()) { 94 | while (std::getline(infile, line)) { 95 | substList.push_back(line); 96 | } 97 | } 98 | infile.close(); 99 | } 100 | 101 | void addMedianSeqInfo(std::vector& alignment_string, Chain* query, std::vector>& matrix, 102 | std::unordered_map& medianSeqInfoForPos) { 103 | 104 | int query_length = matrix.size(); 105 | std::vector number_of_diff_aas(query_length); 106 | 107 | for (auto it = medianSeqInfoForPos.begin(); it != medianSeqInfoForPos.end(); ++it) { 108 | int pos = stoi(it->first); // position in protein, start count at 1 109 | pos = pos -1; // position in array 110 | if (it->second == -1) { 111 | /* get sequences that don't have X or invalid aa */ 112 | std::unordered_map invalidSeq; 113 | for (int i = 0; i < (int) alignment_string.size(); i++) { 114 | char c = chainGetChar(alignment_string[i], pos); 115 | if (!valid_amino_acid(c)) { 116 | invalidSeq.insert({i,1}); //invalidSeq[i] = 1; 117 | } 118 | } 119 | std::vector alignment_with_noX_at_pos; 120 | seqs_without_X(alignment_string, pos, alignment_with_noX_at_pos ); 121 | 122 | int num_seqs_in_alignment = alignment_with_noX_at_pos.size(); 123 | if (num_seqs_in_alignment == 0) { 124 | medianSeqInfoForPos[it->first] = 0.0; 125 | continue; 126 | } 127 | 128 | /* have to recalculate sequence weights */ 129 | std::vector weights_1 (num_seqs_in_alignment, 1.0); 130 | /* raw counts of valid amino acids */ 131 | std::vector aas_stored_at_each_pos (query_length); 132 | std::vector> matrix_noX_raw(query_length, std::vector(26, 0.0)); 133 | 134 | createMatrix(alignment_with_noX_at_pos, query, weights_1, matrix_noX_raw, aas_stored_at_each_pos); 135 | std::vector seq_weights (num_seqs_in_alignment); 136 | /* calcSeqWeights is the only function where all sequences 137 | and all positions have to be looked at at the same time. 138 | all other routines treat each position independently */ 139 | calcSeqWeights (alignment_with_noX_at_pos,matrix_noX_raw, aas_stored_at_each_pos, seq_weights, 140 | number_of_diff_aas); 141 | std::vector> matrix_noX (query_length, std::vector(26, 0.0)); 142 | 143 | basic_matrix_construction(alignment_with_noX_at_pos, seq_weights, matrix_noX); 144 | // printMatrix (matrix, "tmp_basicmatrix.txt"); 145 | double medianSeqInfo = calculateMedianSeqInfo(alignment_with_noX_at_pos, invalidSeq, seq_weights, matrix_noX); 146 | medianSeqInfoForPos[it->first] = medianSeqInfo; 147 | } 148 | } 149 | } 150 | 151 | double calculateMedianSeqInfo(std::vector& alignment_strings, std::unordered_map& invalidSeq, 152 | std::vector& seq_weights, std::vector>& matrix) { 153 | 154 | int query_len = chainGetLength(alignment_strings[0]); 155 | int amino_acid_num = 26; 156 | float median = kLog_2_20; 157 | double tmp = 0.0; 158 | 159 | double* amino_acid_nums = new double[amino_acid_num]; 160 | for (int i = 0; i < amino_acid_num; ++i) { 161 | amino_acid_nums[i] = 0.0; 162 | } 163 | 164 | float* pos_freq = new float[query_len]; 165 | for (int i = 0; i < query_len; ++i) { 166 | pos_freq[i] = 0.0; 167 | } 168 | 169 | // char c; 170 | // double valid_aa; 171 | for (int pos_index = 0; pos_index < query_len; pos_index++) { 172 | // valid_aa = 0.0; 173 | // re-initialize to 0 174 | double total_weight = 0.0; 175 | double r = 0.0; 176 | for (char aa = 'A'; aa <= 'Z'; aa++) { 177 | if (valid_amino_acid(aa)) { 178 | int idx = aa_to_idx(aa); 179 | amino_acid_nums[idx] = 0.0; 180 | total_weight += matrix[pos_index][idx]; 181 | } 182 | } 183 | 184 | for (char aa= 'A';aa <= 'Z'; aa++) { 185 | int idx = aa_to_idx(aa); 186 | tmp = matrix[pos_index][idx]/total_weight; 187 | if (tmp > 0.0 && valid_amino_acid(aa)) { 188 | r += tmp * log(tmp); 189 | } 190 | } 191 | r = r / log(2.0); 192 | pos_freq[pos_index] = r + kLog_2_20; 193 | } /* end pos_index */ 194 | median = getMedian(pos_freq, query_len); 195 | 196 | delete[] pos_freq; 197 | delete[] amino_acid_nums; 198 | 199 | return median; 200 | } 201 | 202 | void hashPredictedPos(std::list& substList, std::unordered_map& medianSeqInfoForPos) { 203 | 204 | std::list::const_iterator iterator; 205 | 206 | std::regex regexSubst ("^[A-Z]([0-9]+)[A-Z]"); /*, std::regex_constants::basic); */ 207 | std::smatch m; 208 | 209 | for (iterator = substList.begin(); iterator != substList.end(); ++iterator) { 210 | std::string substLine = *iterator; 211 | if (regex_search(substLine, m, regexSubst)) { 212 | std::string string_pos = m[1]; 213 | medianSeqInfoForPos[string_pos] = -1; 214 | } 215 | } 216 | } 217 | 218 | void addPosWithDelRef(Chain* query, std::vector>& SIFTscores, 219 | std::unordered_map& medianSeqInfoForPos) { 220 | 221 | int query_length = SIFTscores.size(); 222 | 223 | for (int pos = 0; pos < query_length; pos++) { 224 | char ref_aa = chainGetChar(query, pos); 225 | int ref_aa_index = (int) ref_aa - (int) 'A'; 226 | 227 | if (SIFTscores[pos][ref_aa_index] < TOLERANCE_PROB_THRESHOLD) { 228 | medianSeqInfoForPos[std::to_string(pos + 1)] = -1; // pos + 1 because the substitution files are 1-based 229 | } 230 | } 231 | } 232 | 233 | void check_refaa_against_query(char ref_aa, int aa_pos, Chain* query, std::ofstream& outfp) { 234 | char aa = chainGetChar(query, aa_pos); 235 | if (aa != ref_aa) { 236 | outfp << "WARNING! Amino acid " << aa << " is at position " << 237 | std::to_string(aa_pos + 1) << ", but your list of substitutions assumes it's a " << ref_aa << std::endl; 238 | } 239 | } 240 | 241 | std::string print_double(double num, int precision) { 242 | std::stringstream stream; 243 | stream << std::fixed << std::setprecision(precision) << num; 244 | return stream.str(); 245 | } 246 | 247 | void printSubstFile(const std::list& substList, std::unordered_map& medianSeqInfoForPos, 248 | const std::vector>& SIFTscores, const std::vector& aas_stored, 249 | const int total_seq, Chain* query, const std::string outfile) { 250 | 251 | std::list::const_iterator iterator; 252 | std::regex regexSubst ("^([A-Z])([0-9]+)([A-Z])"); /*, std::regex_constants::basic); */ 253 | std::smatch m; 254 | std::ofstream outfp; 255 | outfp.open(outfile, std::ios::out); 256 | int query_length = SIFTscores.size(); 257 | 258 | for (int pos = 0; pos < query_length; pos++) { 259 | char ref_aa = chainGetChar(query, pos); 260 | int ref_aa_index = (int) ref_aa - (int) 'A'; 261 | 262 | if (SIFTscores[pos][ref_aa_index] < TOLERANCE_PROB_THRESHOLD) { 263 | auto search = medianSeqInfoForPos.find(std::to_string(pos + 1)); 264 | if (search == medianSeqInfoForPos.end()) { 265 | // missing position 266 | continue; 267 | } 268 | double median = search->second; 269 | if (median < ADEQUATE_SEQ_INFO) { 270 | outfp << "WARNING! " << ref_aa << std::to_string(pos+1) << " not allowed! score: " << 271 | print_double( SIFTscores[pos][ref_aa_index],2) << " median: " << 272 | print_double(medianSeqInfoForPos[std::to_string(pos)],2) << " # of sequence: " << 273 | std::to_string((int) aas_stored[pos]) << std::endl; 274 | } 275 | } /* end if less than TOLERANCE_PROB_THRESHOLD */ 276 | } 277 | 278 | for (iterator = substList.begin(); iterator != substList.end(); ++iterator) { 279 | std::string substLine = *iterator; 280 | std::stringstream ss(std::stringstream::in | std::stringstream::out); 281 | ss << substLine; 282 | std::string cleanSubst; 283 | ss >> cleanSubst; 284 | 285 | if (regex_search(substLine, m, regexSubst)) { 286 | char tmp[10]; 287 | std::string tmp_string = m[1]; 288 | std::strncpy(tmp, tmp_string.c_str(), tmp_string.size()+1); 289 | char ref_aa = tmp[0]; //.c_str()[0]; 290 | std::string aa_pos_string = m[2]; 291 | int aa_pos = stoi ( aa_pos_string); //.c_str()); 292 | aa_pos = aa_pos -1; // position in array 293 | tmp_string = m[3]; 294 | std::strncpy(tmp, tmp_string.c_str(), tmp_string.size()+1); 295 | char new_aa = tmp[0]; //.c_str()[0]; 296 | 297 | int new_aa_index = (int) new_aa - (int) 'A'; 298 | double score = SIFTscores[aa_pos][new_aa_index]; 299 | 300 | check_refaa_against_query(ref_aa, aa_pos, query, outfp); 301 | outfp << cleanSubst << "\t"; 302 | if (score >= TOLERANCE_PROB_THRESHOLD) { 303 | outfp << "TOLERATED\t" << print_double(score,2); 304 | } else { 305 | outfp << "DELETERIOUS\t" << print_double(score,2); 306 | } 307 | auto search = medianSeqInfoForPos.find(aa_pos_string); 308 | outfp << "\t" << print_double(search->second,2) << "\t" << 309 | std::to_string((int) aas_stored[aa_pos]) << "\t" << std::to_string(total_seq) << std::endl; 310 | } 311 | } 312 | 313 | outfp.close(); 314 | } 315 | 316 | bool valid_amino_acid(char aa) { 317 | if (aa == 'B' or aa == 'Z' or aa == 'J' or aa == 'O' or aa == 'U' or aa == 'X' or aa == '-' or aa == '*') { 318 | return false; 319 | } else { 320 | return true; 321 | } 322 | } 323 | 324 | void calcSIFTScores(std::vector& alignment_string, Chain* query, std::vector>& matrix, 325 | std::vector>& SIFTscores) { 326 | 327 | int query_length = matrix.size(); 328 | 329 | std::vector max_aa_array(query_length); 330 | std::vector number_of_diff_aas(query_length); 331 | std::vector epsilon(query_length); 332 | 333 | int num_seqs_in_alignment = alignment_string.size(); 334 | std::vector> raw_count_matrix(query_length, std::vector(26, 0.0)); 335 | 336 | /* initialize an array with weight 1 */ 337 | std::vector weights_1(num_seqs_in_alignment, 1.0); 338 | 339 | /* raw counts of valid amino acids */ 340 | std::vector aas_stored_at_each_pos(query_length); 341 | createMatrix(alignment_string, query, weights_1, raw_count_matrix, aas_stored_at_each_pos); 342 | 343 | std::vector seq_weights (num_seqs_in_alignment); 344 | 345 | /* calcSeqWeights is the only function where all sequences 346 | and all positions have to be looked at at the same time. 347 | all other routines treat each position independently */ 348 | calcSeqWeights (alignment_string, matrix, aas_stored_at_each_pos, seq_weights, 349 | number_of_diff_aas); 350 | 351 | /* now construct matrix with weighted sequence values */ 352 | std::vector> seq_weighted_matrix(query_length, std::vector(26,0.0)); 353 | std::vector tot_weights_each_pos(query_length); 354 | 355 | createMatrix(alignment_string, query, seq_weights, seq_weighted_matrix, tot_weights_each_pos); 356 | 357 | find_max_aa_in_matrix(seq_weighted_matrix, max_aa_array); 358 | 359 | calcEpsilon(seq_weighted_matrix, max_aa_array, number_of_diff_aas, epsilon ); 360 | 361 | /* pseudo_diri */ 362 | std::vector> diric_matrix(query_length, std::vector(26, 0.0)); 363 | calcDiri(seq_weighted_matrix, diric_matrix); 364 | //printMatrix (diric_matrix, "diri_matrix.txt"); 365 | 366 | for (int pos= 0; pos < query_length; pos++) { 367 | for (char aa = 'A'; aa <= 'Z'; aa++) { 368 | int aa_index = (int) aa - (int) 'A'; 369 | SIFTscores[pos][aa_index] = seq_weighted_matrix[pos][aa_index] + epsilon[pos] * diric_matrix[pos][aa_index]; 370 | SIFTscores[pos][aa_index] /= (tot_weights_each_pos[pos] + epsilon[pos]); 371 | } 372 | } 373 | /* have to find max aa again because it'schanged with newly added weights */ 374 | find_max_aa_in_matrix(SIFTscores, max_aa_array); 375 | scale_matrix_to_max_aa(SIFTscores, max_aa_array); 376 | // printMatrix(SIFTscores, "siftscores_matrix.txt"); 377 | } 378 | 379 | void calcDiri(std::vector>& count_matrix, std::vector>& diric_matrix) { 380 | 381 | int query_length = count_matrix.size(); 382 | for (int pos = 0; pos < query_length; pos++) { 383 | add_diric_values(count_matrix[pos], diric_matrix[pos]); 384 | } 385 | } 386 | 387 | double add_logs (double logx, double logy) { 388 | if (logx > logy) { 389 | return (logx + log (1.0 + exp (logy -logx))); 390 | } else { 391 | return (logy + log (1.0 + exp (logx - logy))); 392 | } 393 | } 394 | 395 | void add_diric_values(std::vector& count_col, std::vector& diric_col) { 396 | 397 | double tmp; 398 | int diri_comp_num = (int) (sizeof (diri_altot)/sizeof (diri_altot[0])); 399 | std::vector probn(diri_comp_num, 0.0); 400 | std::vector probj(diri_comp_num,0.0); 401 | 402 | double pos_count_tot = 0.0; 403 | for (int j=0; j < (int) count_col.size(); j++) { 404 | pos_count_tot += count_col[j]; 405 | } 406 | 407 | /*----------- compute equation (3), Prob(n|j) ------------ */ 408 | for (int j = 0; j < diri_comp_num; j++) { 409 | probn[j] = lgamma(pos_count_tot+1.0) + lgamma(diri_altot[j]); 410 | probn[j] -= lgamma(pos_count_tot + diri_altot[j]); 411 | 412 | for (char aa = 'A'; aa <= 'Z'; aa++) { 413 | int aa_index = (int) aa - (int) 'A'; 414 | if (valid_amino_acid(aa)) { 415 | tmp = lgamma(count_col[aa_index] + diri_alpha[j][aa_index]); 416 | tmp -= lgamma(count_col[aa_index] + 1.0); 417 | tmp -= lgamma(diri_alpha[j][aa_index]); 418 | probn[j] += tmp; 419 | } /* end if greater than 0 */ 420 | } /* end all amino acids */ 421 | } /* end for all Diri components */ 422 | 423 | /*------ compute sum qk * p(n|k) using logs & exponents ----------*/ 424 | double denom = log(diri_q[0]) + probn[0]; 425 | 426 | for (int j =1; j < diri_comp_num; j++) { 427 | double tmp = log(diri_q[j]) + probn[j]; 428 | denom = add_logs(denom, tmp); 429 | } 430 | 431 | /* compute equation (3), Prob(j|n) */ 432 | for (int j = 0; j < diri_comp_num; j++) { 433 | probj[j] = log(diri_q[j]) + probn[j] - denom; 434 | } 435 | 436 | double totreg = 0.0; 437 | for (char aa = 'A'; aa <= 'Z'; aa++) { 438 | if (valid_amino_acid(aa)) { 439 | int aa_index = (int) aa - (int) 'A'; 440 | for (int j =0; j < (int) probj.size(); j++) { 441 | diric_col[aa_index] += exp (probj[j]) * diri_alpha[j][aa_index]; 442 | } /* gone through all components */ 443 | totreg += diric_col[aa_index]; 444 | } /* end valid amino acid */ 445 | } 446 | /* now normalize */ 447 | for (char aa= 'A'; aa <= 'Z'; aa++) { 448 | int aa_index = (int) aa - (int) 'A'; 449 | diric_col[aa_index] /= totreg; 450 | } 451 | } 452 | 453 | void calcSeqWeights(const std::vector& alignment_string, std::vector>& matrix, 454 | std::vector& amino_acids_present, std::vector& seq_weights, std::vector& number_of_diff_aas) { 455 | 456 | int query_length = matrix.size(); 457 | /* first calculate number of different aas at each position */ 458 | /* std::vector number_of_diff_aas (query_length); */ 459 | 460 | /* initialize */ 461 | for (int pos = 0; pos < query_length; pos++) { 462 | number_of_diff_aas[pos] = 0; 463 | } 464 | for (uint32_t seq_index = 0; seq_index < alignment_string.size(); seq_index++) { 465 | seq_weights[seq_index] = 0.0; 466 | } 467 | 468 | /* now tabulate # of unique amino acids at each position */ 469 | for (int pos = 0; pos < query_length; pos++) { 470 | for (char aa = 'A'; aa <= 'Z'; aa++) { 471 | int aa_index = int (aa) - int ('A'); 472 | if (valid_amino_acid(aa) && matrix[pos][aa_index] > 0.0) { 473 | number_of_diff_aas[pos] += 1.0f; 474 | } 475 | } 476 | } 477 | 478 | double tot = 0.0; 479 | /* now calculate position-based weights */ 480 | for (uint32_t seq_index = 0; seq_index < alignment_string.size(); seq_index++) { 481 | for (int pos = 0; pos < query_length; pos++) { 482 | char aa = chainGetChar(alignment_string[seq_index], pos); 483 | int aa_index = (int) aa - (int) 'A'; 484 | if (valid_amino_acid(aa) && matrix[pos][aa_index] > 0.0) { 485 | double tmp = number_of_diff_aas[pos] * matrix[pos][aa_index]; 486 | seq_weights[seq_index] += 1.0/tmp; 487 | } 488 | } 489 | tot += seq_weights[seq_index]; 490 | } 491 | 492 | /* normalize so weights sum up to the number of sequences */ 493 | double new_tot_weight = 0.0; 494 | for (uint32_t seq_index = 0; seq_index& alignment_string, double seq_identity) { 501 | 502 | double identity; 503 | double seqTotal; 504 | int lenOfQuery = chainGetLength(queries); 505 | int currPos = 0; 506 | 507 | /*iterate through elements in vector of chains*/ 508 | while (currPos < int(alignment_string.size())){ 509 | 510 | identity = 0; 511 | seqTotal = 0; 512 | int lenOfAlign = chainGetLength(alignment_string[currPos]); 513 | 514 | /*see if alignment_string is indeed made to match query seq length*/ 515 | if (lenOfQuery == lenOfAlign){ 516 | for (int m = 0; m < lenOfAlign; m++){ 517 | /*compare each char of query to alignment_string char at same position in chain*/ 518 | char qChar = chainGetChar(queries, m); 519 | char aChar = chainGetChar(alignment_string[currPos], m); 520 | 521 | if (aChar != 'X'){ 522 | if (valid_amino_acid(aChar) && valid_amino_acid(qChar)){ 523 | seqTotal++; 524 | if (qChar == aChar){ 525 | identity++; 526 | } 527 | } 528 | } 529 | } 530 | } else { 531 | std::cout << "Length does not match!" << std::endl; 532 | exit(1); 533 | } 534 | 535 | double perc_similar = (identity / seqTotal) * 100; 536 | /*Read curr element, delete if beyond threshold, else move to next pos*/ 537 | if (perc_similar >= seq_identity) { 538 | chainDelete(alignment_string[currPos]); 539 | alignment_string.erase(alignment_string.begin() + currPos); 540 | } else { 541 | currPos++; 542 | } 543 | } 544 | } 545 | 546 | void printSeqNames (std::vector& alignment_string) { 547 | for (int i=0; i < int(alignment_string.size()); i++){ 548 | std::cout << "printName in here " << std::to_string(i) << std::endl; 549 | std::cout << "printName" << chainGetName(alignment_string[i]) << std::endl; 550 | } 551 | } 552 | 553 | /* add it with seq_weights 1 or pb weights 554 | with 1 it's raw count, with pb_weights it's weighted */ 555 | void createMatrix(const std::vector& alignment_string, Chain* query, 556 | const std::vector& seq_weights, std::vector>& matrix, 557 | std::vector& tot_pos_weight) { 558 | 559 | int query_len = chainGetLength(query); 560 | for (uint32_t seq_index = 0; seq_index < alignment_string.size(); seq_index++) { 561 | for (int pos = 0; pos < query_len; pos++) { 562 | char aa = chainGetChar(alignment_string[seq_index], pos); 563 | if (valid_amino_acid(aa)) { 564 | int aa_index = (int) aa - (int) 'A'; 565 | matrix[pos][aa_index] += seq_weights[seq_index] ; /*seq_weight[seq_index];*/ 566 | tot_pos_weight[pos] += seq_weights[seq_index]; 567 | } 568 | } 569 | } 570 | } 571 | 572 | void printMatrix(std::vector>& matrix, std::string filename) { 573 | 574 | int query_length = matrix.size(); 575 | int aas = matrix[0].size(); 576 | std::ofstream out_file; 577 | 578 | out_file.open(filename); 579 | 580 | /* print out header */ 581 | for (int aa_index = 0; aa_index< aas; aa_index++) { 582 | out_file << "\t"; 583 | out_file << (char) (aa_index + (int) 'A'); 584 | } 585 | out_file << std::endl; 586 | 587 | for (int pos = 0; pos < query_length; pos++) { 588 | out_file << (pos + 1); 589 | for (int aa_index =0; aa_index < aas; aa_index++) { 590 | out_file << "\t" << matrix[pos][aa_index]; 591 | } 592 | out_file << std::endl; 593 | } 594 | out_file.close(); 595 | } 596 | 597 | void printMatrixOriginalFormat(std::vector>& matrix, std::string filename) { 598 | 599 | auto fp = fopen(filename.c_str(), "w"); 600 | 601 | int query_length = matrix.size(); 602 | int aas = matrix[0].size(); 603 | 604 | // print out header 605 | fprintf(fp, "ID UNK_ID; MATRIX\nAC UNK_AC\nDE UNK_DE\nMA UNK_BL\n"); 606 | fprintf(fp, " "); 607 | for (int aa_index = 0; aa_index < aas; aa_index++) { 608 | // ignore J O U 609 | if (aa_index != 9 && aa_index != 14 && aa_index != 20) { 610 | fprintf(fp, " %c ", aa_index + 'A'); 611 | } 612 | } 613 | fprintf(fp, " * -\n"); 614 | 615 | for (int pos = 0; pos < query_length; pos++) { 616 | for (int aa_index = 0; aa_index < aas; aa_index++) { 617 | if (aa_index != 9 && aa_index != 14 && aa_index != 20) { 618 | fprintf(fp, " %6.4f ", matrix[pos][aa_index]); 619 | } 620 | } 621 | fprintf(fp, " %6.4f %6.4f\n", 0.0, 0.0); 622 | } 623 | fprintf(fp, "//\n"); 624 | 625 | fclose(fp); 626 | } 627 | 628 | int aa_to_idx(char character){ 629 | // A = 0 and etc 630 | return int(character) - int('A'); 631 | } 632 | 633 | void basic_matrix_construction(const std::vector& alignment_string, const std::vector& seq_weights, 634 | std::vector>& matrix) { 635 | 636 | // do partial calculation on aa that can be represented by B or Z as well 637 | double part_D = aa_frequency[aa_to_idx('D')] / ( aa_frequency[aa_to_idx('D')] + aa_frequency[aa_to_idx('N')] ); 638 | double part_N = aa_frequency[aa_to_idx('N')] / ( aa_frequency[aa_to_idx('D')] + aa_frequency[aa_to_idx('N')] ); 639 | double part_E = aa_frequency[aa_to_idx('E')] / ( aa_frequency[aa_to_idx('E')] + aa_frequency[aa_to_idx('Q')] ); 640 | double part_Q = aa_frequency[aa_to_idx('Q')] / ( aa_frequency[aa_to_idx('E')] + aa_frequency[aa_to_idx('Q')] ); 641 | 642 | int proteinLen = chainGetLength(alignment_string[0]); 643 | int aaLen = matrix[0].size(); 644 | 645 | // loop every position in chain length 646 | for (int pos = 0; pos < proteinLen; pos++){ 647 | double total = 0.0; 648 | 649 | // read pos in each chain 650 | for (int seq = 0; seq < int(alignment_string.size()); seq++){ 651 | char currChar = chainGetChar(alignment_string[seq],pos); 652 | 653 | // parition B between D and N 654 | if (currChar == 'B') { 655 | if (aa_frequency[aa_to_idx('D')] != 0.0) { /* try to protect div by zero */ 656 | double num = (part_D * seq_weights[seq]) / aa_frequency[aa_to_idx('D')]; 657 | matrix[pos][aa_to_idx('D')] += num; 658 | total += num; 659 | } 660 | 661 | if (aa_frequency[aa_to_idx('N')] != 0.0) { /* try to protect div by zero */ 662 | double num = (part_N * seq_weights[seq]) / aa_frequency[aa_to_idx('N')]; 663 | matrix[pos][aa_to_idx('N')] += num; 664 | total += num; 665 | } 666 | /* partition Z between E and Q */ 667 | } else if (currChar == 'Z') { 668 | if (aa_frequency[aa_to_idx('E')] != 0.0) { /* try to protect div by zero */ 669 | double num = (part_E * seq_weights[seq]) / aa_frequency[aa_to_idx('E')]; 670 | matrix[pos][aa_to_idx('E')] += num; 671 | total += num; 672 | } 673 | 674 | if (aa_frequency[aa_to_idx('Q')] != 0.0) { /* try to protect div by zero */ 675 | double num = (part_Q * seq_weights[seq]) / aa_frequency[aa_to_idx('Q')]; 676 | matrix[pos][aa_to_idx('Q')] += num; 677 | total += num; 678 | } 679 | 680 | } else { 681 | if (aa_frequency[aa_to_idx(currChar)] != 0.0){ 682 | if (currChar != 'X' && currChar != '-' && currChar != '*'){ 683 | double num = seq_weights[seq] / aa_frequency[aa_to_idx(currChar)]; 684 | matrix[pos][aa_to_idx(currChar)] += num; 685 | total += num; 686 | } 687 | } 688 | } 689 | } 690 | 691 | // loop to calculate percentage, excludes index 26 (-) and index 27 (*) 692 | for (int n = 0; n < aaLen; n++){ 693 | // A to Z excluding 'X' (alignment filler) 694 | if (n <= aa_to_idx('Z') || n != aa_to_idx('X')) { 695 | matrix[pos][n] = matrix[pos][n] * 100.0 / total; // X and * and - 696 | } else { 697 | matrix[pos][n] = aa_frequency[n]; 698 | } 699 | } 700 | 701 | // lastly, tali B and Z rows 702 | matrix[pos][aa_to_idx('B')] = (matrix[pos][aa_to_idx('D')] * part_D) + (matrix[pos][aa_to_idx('N')] * part_N); 703 | matrix[pos][aa_to_idx('Z')] = (matrix[pos][aa_to_idx('E')] * part_E) + (matrix[pos][aa_to_idx('Q')] * part_Q); 704 | } 705 | } 706 | 707 | void seqs_without_X(const std::vector& alignment_string, int pos, std::vector& out_alignment) { 708 | 709 | char posChar; 710 | /*iterate vector of aligment and check pos*/ 711 | for( int n = 0; n < int(alignment_string.size()); n++){ 712 | posChar = chainGetChar(alignment_string[n], pos); 713 | 714 | /*check if char in pos is valid, and push it into new vector*/ 715 | if (valid_amino_acid(posChar)){ 716 | out_alignment.push_back(alignment_string[n]); 717 | } 718 | } 719 | } 720 | -------------------------------------------------------------------------------- /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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | -------------------------------------------------------------------------------- /sift4g/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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | --------------------------------------------------------------------------------