Cassiopee.h 9.85 KB
Newer Older
1 2 3
#include <iostream>
#include <fstream>
#include <list>
4
#include <map>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

#include "tree/tree.hh"

#include "CassiopeeConfig.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
// Provide an implementation of serialize for std::list
#include <boost/serialization/list.hpp>

using namespace std;




/**
 * Object to manage insertion and deletions
 */
class Match {
public:

	/**
	 * define equality between matches
	 */
	bool operator==(const Match& p) const {
	        return pos == p.pos && in+del == p.in + p.del;
	}

	/**
	 * Number of insertion error
	 */
	int in;
	/**
	 * Number of deletion error
	 */
	int del;
	/**
	 * Number of substitution error
	 */
	int subst;


	long pos;

	Match();


};

/**
 * Node in the suffix tree
 *
 * If tree is reduced (DO_REDUCTION=1), memory usage is highly reduced, keeping in memory only the branching nodes.
 * Search is made after that looking at branch nodes and sequence content between nodes.
 * In addition, data from file are analysed per chunk, limiting again loaded data in memory.
 *
 * This treatment requires however more disk usage (reads) and tree manipulations, increasing the index and search time for a gain on memory requirements.
 */
class TreeNode {
public:
	/**
	 * Character to match
	 */
	char c;


	/**
	 * List of positions in sequence matching this node
	 */
	list<long> positions;

	/**
	 * When tree is reduced, store the location of next characters in input sequence file.
	 * Reduction can be made from a branch up to the leaf or between 2 branches.
	 * If next_pos is equal to -1, then no reduction is made.
	 */
	long next_pos;

	/**
	 * Length of remaining data to read in sequence file
	 */
	long next_length;


	/**
	 * Creates a node from a char
	 *
	 * \param nc character for this node
	 */
	TreeNode(char nc);

	/**
	 * Creates a node from a char
	 *
	 * \param nc character for this node
	 * \param pos position of the character in the sequence
	 */
	TreeNode(char nc, long pos);
	TreeNode();


private:
	  friend class boost::serialization::access;
	  template<class Archive>
	  void serialize(Archive & ar, const unsigned int /*version*/)
	  {
	    ar & c;
	    ar & next_pos;
	    ar & next_length;
	    ar & positions;
	  }

};



#ifndef __CASSIOPEE_H_
#define __CASSIOPEE_H_
//inline std::ostream& operator<<(std::ostream &strm, const TreeNode &a) {
//  return strm << "TreeNode(" << a.c << ")";
//}
#endif


/**
 * Cassiopee indexer
 *
 * Index an input file
 */
class CassieIndexer {
public:


	/**
	 * Main contructor
	 *
	 * \param path Path to the sequence file, sequence must be a one-line sequence.
	 */
	CassieIndexer(const char* path);

	~CassieIndexer();

	/**
	 * Save the index in a file
	 */
	void save();

	/**
	 * Load index from a file
	 */
	void load();


	/**
	 * Generates a dot file representing the tree.
	 */
	void graph();

	/**
	 * Generates a dot file representing the tree up to depth
	 *
	 * \param depth Max depth to print
	 */
	void graph(int depth);

	/**
	 * Get the suffix content from a position
	 *
	 * \param pos Position in the file
	 * \return the suffix string
	 */
	string getSuffix(long pos);

	/**
	 * Launch the indexation of the input sequence
	 */
	void index();

	/**
	 * Get the tree matching the indexed sequence
	 */
	tree<TreeNode>* getTree();

	/**
	 * List of positions in original sequence matching the search
	 */
	list<Match> matches;

	/**
	 * Allow tree reduction
	 */
	bool do_reduction;

	/**
	 * Get char from suffix located at position pos.
	 */
	char getCharAtSuffix(long pos);

	/**
	 * Fills suffix tree with input suffix
	 *
	 * \param pos Position of suffix in sequence
	 */
	void filltree(long pos);

	/**
	 * Max depth to graph
	 */
	long max_depth;

    /**
     * Max depth to index, can limit index size if pattern to search are known
     * of a maximum size
     */
    long max_index_depth;

	long seq_length;

	/**
	 * Checks if index has been loaded from a saved index or if index has been build dynamically
	 */
	bool index_loaded_from_file();

private:
	list<TreeNode> serialized_nodes;

	bool loaded_from_file;

	const char* filename;
	ifstream seqstream;
	tree<TreeNode> tr;

	const long MAX_SUFFIX;
	long suffix_position;
	char* suffix;

	/**
	 * Graph the children of node
	 */
	long graphNode(tree<TreeNode>::iterator node, long counter, ofstream& myfile, int maxdepth);

	/**
	 * Extract parts of suffix located at pos from stream with a max size of MAX_SUFFIX.
	 */
	char* loadSuffix(long pos);


	/**
	 * Reset current suffix; Mandatory before calling getCharAtSuffix on a new suffix
	 */
	void reset_suffix();



	/**
	 * Fills a tree branch with the suffix starting at suffix_pos
	 *
	 * \param sib Insert at node sib.
	 * \param suffix_pos Starting inserting from suffix position index
	 * \param pos Position of suffix in sequence
	 */
	void fillTreeWithSuffix(tree<TreeNode>::iterator sib, long suffix_pos, long pos);
	void fillTreeWithSuffix(long suffix_pos, long pos);



};


/**
 * Utility functions
 */
class CassiopeeUtils {
public:
	/**
	 * Transform a Fasta input sequence in a sequence manageable by index.
	 */
	static void transform_fasta(const string in, const string out);
};

/**
 * Ambiguity character matches for DNA
 */
class Ambiguous {
public:
	/**
	 * Checks if a char is equal to an other according to ambiguity
	 *
	 * \param a input char
	 * \param b input char
	 * \return true if characters match
	 */
	static bool isequal(char a, char b);
private:
	/**
	 * Compares input character with a list of possible matches
	 *
	 * \param a input char, non ambiguous
	 * \param b list of possible matches for ambiguity
	 * \param len length of b array
	 * \return true if a match one character of b
	 */
	static bool ismatchequal(char a, const char b[], int len);
	static const char K_MATCH[];
	static const char M_MATCH[];
	static const char R_MATCH[];
	static const char Y_MATCH[];
	static const char S_MATCH[];
	static const char W_MATCH[];
	static const char B_MATCH[];
	static const char V_MATCH[];
	static const char H_MATCH[];
	static const char D_MATCH[];
	static const char N_MATCH[];
};


struct per_position
{
    inline bool operator() (const Match* struct1, const Match* struct2)
    {
        return (struct1->pos < struct2->pos);
    }
};

/**
 * Search object
 *
 */
class CassieSearch {

public:
	/**
	 * Main constructor
	 *
	 * \param index_ref Indexer for sequence
	 */
	CassieSearch(CassieIndexer* index_ref);

	~CassieSearch();

	/**
	 * Remove matches with same position and same length.
	 */
	void removeDuplicates();

353 354
	map<std::string, string> morphisms;

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	/**
	 * Used to store max errors allowed
	 */
	Match* match_limits;

	/**
	 * List of positions in original sequence matching the search
	 */
	list<Match*> matches;

	/**
	 * Allow alphabet ambiguity search. False by default.
	 * N are by default ignored. To allow N matching, the nmax attribute must be set
	 */
	bool ambiguity;

	/**
	 * Allow N matching with alphabet ambiguity. nmax defines the maximum number of consecutive N allowed in match.
	 */
	int nmax;

	/**
	 * Search mode:
	 *
	 *  - 0: DNA
	 *  - 1: RNA
	 *  - 2: Protein
	 */
	int mode;

	/**
	 * Search for a string in the indexed sequence
	 *
	 * \param suffix pattern to search
	 * \param clear Clear existing matches?
	 */
	void search(string suffix, bool clear);


	/**
	 * Search for a string in the indexed sequence.
	 * Clear all previous matches.
	 *
	 * \param suffix pattern to search
	 */
	void search(string suffix);

	/**
	 * Search for multiple strings
	 *
	 * \param array of patterns to search
	 */
	void search(string suffixes[]);

	/**
	 * Compare two chars, with ambiguity is option is set
	 */
	bool isequal(char a,char b);


	/**
	 * Maximum number of insertion or deletion
	 */
	int max_indel;
	/**
	 * Maximum number of substitution
	 */
	int max_subst;

    long pattern_length;

	/**
	 * Sort matches according to positions i nascending order
	 */
	void sort();

private:

	/**
	 * Compare two Match pointers
	 */
	static bool same_match (Match* first, Match* second)
	{ return ( *first == *second ); }

	CassieIndexer* indexer;

	/**
	 * Sets all matching positions in matches from node
	 *
	 * \param sib Base node to search through in the tree
	 * \param nbSubst Number of substition found
	 * \param nbIn Number of insertion found
	 * \param nbDel Number of deletion found
	 */
	void getMatchesFromNode(tree<TreeNode>::iterator sib, const int nbSubst, const int nbIn, const int nbDel);



	/**
	 * Compare suffix at a specific start root node (check with root children).
	 *
	 * \param suffix Suffix to analyse
	 * \param suffix_pos position on suffix
	 * \param root Tree node root to analyse, starts with first child
	 * \param nbSubst current number of substitutions found
	 * \param nbIn current number of insertions found
	 * \param nbDel current number of deletions found
	 * \param nbN current consecutive N found
	 */
	void searchAtNode(string suffix, const long suffix_pos, const tree<TreeNode>::iterator root, int nbSubst, int nbIn, int nbDel, int nbN);

	/**
	 * Compare suffix at a specific start root node (check with root children).
	 * \param suffix Suffix to analyse
	 * \param suffix_pos position on suffix
	 * \param root Tree node root to analyse
	 * \param start_node root child node to start with (no first element)
	 * \param nbSubst current number of substitutions found
	 * \param nbIn current number of insertions found
	 * \param nbDel current number of deletions found
	 * \param nbN current consecutive N found
	 */
	void searchAtNode(string suffix, const long suffix_pos, const tree<TreeNode>::iterator root, const tree<TreeNode>::iterator start_node, int nbSubst, int nbIn, int nbDel, int nbN);

	/**
	 * Compare suffix in reducted string at node sib.
	 * \return true if matched
	 */
	bool searchAtreduction(const string suffix, const tree<TreeNode>::iterator sib, long counter, long tree_reducted_pos, int nbSubst, int nbIn, int nbDel, int nbN);


};