| 1 | /* esl_sq : a single biological sequence |
| 2 | */ |
| 3 | #ifndef eslSQ_INCLUDED |
| 4 | #define eslSQ_INCLUDED |
| 5 | #include <esl_config.h> |
| 6 | |
| 7 | #include "esl_alphabet.h" |
| 8 | #include "esl_msa.h" |
| 9 | #include "esl_random.h" /* random, randomseq add ability to sample random sq objects for unit tests */ |
| 10 | #include "esl_randomseq.h" /* random, randomseq add ability to sample random sq objects for unit tests */ |
| 11 | |
| 12 | /* ESL_SQ - a biosequence |
| 13 | * |
| 14 | * Can be either in text mode <seq>, or digital mode <dsq>. |
| 15 | * One of them has to be NULL, and the other contains the data. |
| 16 | * |
| 17 | * When in text mode, <ss> and <seq> can hold up to <n=salloc-1> |
| 18 | * residues and a terminal '\0', and both are indexed <0..n-1>. |
| 19 | * |
| 20 | * When in digital mode, <ss> and <dsq> can hold up to <n=salloc-2> |
| 21 | * residues; both are indexed <1..n>, and positions 0 and n+1 are |
| 22 | * sentinel bytes. The digital sequence <dsq> uses <eslDSQ_SENTINEL> |
| 23 | * as its sentinels; as a hack, <ss> uses '\0' as sentinels. This |
| 24 | * means that <sq->ss+1> is a valid NUL-terminated C string, but |
| 25 | * <sq->ss> itself would be a string of length 0 because of the |
| 26 | * leading NUL sentinel. Watch out for this. |
| 27 | * |
| 28 | * To save on allocation calls, the structure is designed to be reused |
| 29 | * for subsequent sequences, rather than free'd and reallocated - |
| 30 | * thus, we keep track of the allocated sizes of all the strings. |
| 31 | * |
| 32 | * Notes on when we need to reallocate: |
| 33 | * - In a text mode sequence (seq 0..n-1), byte salloc-1 is |
| 34 | * reserved for the NUL, so the sequence is full when |
| 35 | * n == salloc-1. |
| 36 | * |
| 37 | * - In a digital mode sequence (dsq 1..n), bytes 0 and salloc-1 |
| 38 | * are reserved for sentinel bytes, so the reallocation condition |
| 39 | * is when n == salloc-2. |
| 40 | * |
| 41 | * At least for now, the only way to set the <ss> structure annotation |
| 42 | * field is by a CreateFrom(), by extraction from an MSA, or manually |
| 43 | * (by directly allocating a sequence's <ss> field). |
| 44 | * |
| 45 | * A sequence object will usually be holding a complete (full length) |
| 46 | * sequence. Three other cases arise less frequently: |
| 47 | * |
| 48 | * 1. We're a subsequence extracted from a source sequence. |
| 49 | * <sourcename> is the name of the source. |
| 50 | * <L> is the length of the source (and coords are 1..L). |
| 51 | * The subsequence is from <start>..<end> on the source. |
| 52 | * The length of the subsequence <n> is abs(<end>-<start>)+1. |
| 53 | * <start> can be greater than <end> for a nucleic acid sequence; |
| 54 | * in this case, the subsequence is reverse complemented. |
| 55 | * |
| 56 | * 2. We're a window on a source sequence. |
| 57 | * This is similar to being a subsequence, with the added |
| 58 | * wrinkle that we're scanning over a long source sequence |
| 59 | * in overlapping segments, defined by a "previous context" |
| 60 | * <C> and a "new window" <W> (the whole sequence is n=C+W |
| 61 | * residues long): |
| 62 | * s C W e |
| 63 | * current window: |------||------------| |
| 64 | * next window read: |------||------------| |
| 65 | * s C W e |
| 66 | * Here, dsq[1..n] is source[s..e]; each newly read |
| 67 | * window starts at dsq[C+1], and is preceded by C |
| 68 | * residues of context. |
| 69 | * |
| 70 | * 3. We're just after information about the sequence, not the |
| 71 | * sequence itself; everything except the per-residue information |
| 72 | * (such as <dsq/seq> and <ss>). We do this when SSI indexing, |
| 73 | * for example, so we don't have to read entire huge seqs into |
| 74 | * memory just to calculate their lengths for the index. |
| 75 | * |
| 76 | * Note/TODO: use of "\0" empty string to indicate lack of optional |
| 77 | * acc, desc info is now deprecated. Cannot distinguish empty string |
| 78 | * from lack of annotation. Should use NULL ptr instead. Fix this in |
| 79 | * future. (21 Nov 09 xref J5/114) |
| 80 | * |
| 81 | * Easel dsq coordinates are "1-offset, closed-interval, no-flag". |
| 82 | * 1-offset means 1..n, as opposed to 0..n-1. Closed-interval means |
| 83 | * that start/end coords i..j refers to subseq x_i..x_j, not |
| 84 | * x_i..x_j-1 (as in half-open coords of Python lists, for example). |
| 85 | * No-flag means that we don't keep a flag for forward vs. reverse |
| 86 | * strand on DNA/RNA sequences; instead, start>end means reverse |
| 87 | * strand. A flaw in such a coord system is that we cannot |
| 88 | * unambiguously represent fwd/rev strand for a subsequence of length |
| 89 | * 1, where start=end. So in the case of start=end, Easel always |
| 90 | * assumes fwd strand; if rev strand is needed, there will be some |
| 91 | * sort of patchy workaround. |
| 92 | */ |
| 93 | typedef struct { |
| 94 | /*::cexcerpt::sq_sq::begin::*/ |
| 95 | char *name; /* name; one word, no whitespace ("\0" if no name) */ |
| 96 | char *acc; /* optional accession (1 word) ("\0" if none) */ |
| 97 | char *desc; /* description line ("\0" if no description) */ |
| 98 | int32_t tax_id; /* NCBI taxonomy id (-1 if none) */ |
| 99 | char *seq; /* sequence [0..n-1], or NULL if digital */ |
| 100 | ESL_DSQ *dsq; /* digitized sequence [1..n], or NULL if text */ |
| 101 | char *ss; /* optional sec structure [0..n-1], [1..n], or NULL */ |
| 102 | int64_t n; /* length of seq (or dsq) and ss */ |
| 103 | /*::cexcerpt::sq_sq::end::*/ |
| 104 | |
| 105 | /* Source-tracking coordinate info for: seq subseq window info */ |
| 106 | /* ---- ------ ------ ----- */ |
| 107 | int64_t start; /* coord of seq[0],dsq[1] on source [1..L] 1 1<=i<=L 1<=i<=L 0 */ |
| 108 | int64_t end; /* coord of seq[n-1],dsq[n] on source[1..L] L 1<=j<=L 1<=j<=L 0 */ |
| 109 | int64_t C; /* # of context residues for a window 0 0 n-W 0 */ |
| 110 | int64_t W; /* window width L n n-C 0 */ |
| 111 | int64_t L; /* source sequence length in residues L L (or -1) L (or -1) L */ |
| 112 | /* and n: length of seq (or dsq) and ss actually stored: L abs(j-i)+1 C+W 0 */ |
| 113 | /* In all the above bookkeeping, a -1 means "unknown" */ |
| 114 | char *source; /* name of the source of a subseq/window; or MSA name; or ""*/ |
| 115 | |
| 116 | /* Memory allocation bookkeeping: (all inclusive of \0; >= strlen()+1) */ |
| 117 | int nalloc; /* allocated length of name */ |
| 118 | int aalloc; /* allocated length of accession */ |
| 119 | int dalloc; /* allocated length of description */ |
| 120 | int64_t salloc; /* alloc for seq or dsq, and ss if present */ |
| 121 | int srcalloc; /* allocated length for source name */ |
| 122 | |
| 123 | /* Disk-tracking offset bookkeeping: */ |
| 124 | int64_t idx; /* ctr for which # seq this is; -1 if not counting */ |
| 125 | off_t roff; /* record offset (start of record); -1 if none */ |
| 126 | off_t hoff; /* offset to last byte of header; -1 if unknown */ |
| 127 | off_t doff; /* data offset (start of sequence data); -1 if none */ |
| 128 | off_t eoff; /* offset to last byte of record; -1 if unknown */ |
| 129 | |
| 130 | /* Optional information for extra residue markups. |
| 131 | * The number of them, and their tags are arbitrary |
| 132 | */ |
| 133 | char **xr_tag; /* markup tags for extra residue markups [0..ntr-1][free-text], [0..ntr-1][free-text], or NULL */ |
| 134 | char **xr; /* annotations for extra residue markups [0..ntr-1][0..n-1], [0..ntr-1][1..n], or NULL */ |
| 135 | int nxr; /* number of extra residue markups */ |
| 136 | |
| 137 | /* Copy of a pointer to the alphabet, if digital mode */ |
| 138 | const ESL_ALPHABET *abc; /* reference to the alphabet for <dsq> */ |
| 139 | } ESL_SQ; |
| 140 | |
| 141 | typedef struct { |
| 142 | int count; /* number of <ESL_SQ> objects in the block */ |
| 143 | int listSize; /* maximum number elements in the list */ |
| 144 | int complete; /*TRUE if the the final ESL_SQ element on the block is complete, FALSE if it's only a partial winow of the full sequence*/ |
| 145 | int64_t first_seqidx;/*unique identifier of the first ESL_SQ object on list; the seqidx of the i'th entry on list is first_seqidx+i */ |
| 146 | ESL_SQ *list; /* array of <ESL_SQ> objects */ |
| 147 | } ESL_SQ_BLOCK; |
| 148 | |
| 149 | /* These control default initial allocation sizes in an ESL_SQ. */ |
| 150 | #define eslSQ_NAMECHUNK 32 // allocation unit for name, source |
| 151 | #define eslSQ_ACCCHUNK 32 // allocation unit for accession |
| 152 | #define eslSQ_DESCCHUNK 128 // allocation unit for description |
| 153 | #define eslSQ_SEQCHUNK 256 // allocation unit for seqs |
| 154 | // .. dsqdata assumes _SEQCHUNK >= 4 |
| 155 | |
| 156 | extern ESL_SQ *esl_sq_Create(void); |
| 157 | extern ESL_SQ *esl_sq_CreateFrom(const char *name, const char *seq, |
| 158 | const char *desc, const char *acc, const char *ss); |
| 159 | extern int esl_sq_Grow (ESL_SQ *sq, int64_t *ret_nsafe); |
| 160 | extern int esl_sq_GrowTo(ESL_SQ *sq, int64_t n); |
| 161 | extern int esl_sq_Copy(const ESL_SQ *src, ESL_SQ *dst); |
| 162 | extern int esl_sq_Compare (ESL_SQ *sq1, ESL_SQ *sq2); |
| 163 | extern int esl_sq_Reuse (ESL_SQ *sq); |
| 164 | extern int esl_sq_IsDigital(const ESL_SQ *sq); |
| 165 | extern int esl_sq_IsText (const ESL_SQ *sq); |
| 166 | extern void esl_sq_Destroy (ESL_SQ *sq); |
| 167 | |
| 168 | extern int esl_sq_SetName (ESL_SQ *sq, const char *name); |
| 169 | extern int esl_sq_SetAccession (ESL_SQ *sq, const char *acc); |
| 170 | extern int esl_sq_SetDesc (ESL_SQ *sq, const char *desc); |
| 171 | extern int esl_sq_SetSource (ESL_SQ *sq, const char *source); |
| 172 | extern int esl_sq_FormatName (ESL_SQ *sq, const char *name, ...) ESL_ATTRIBUTE_FORMAT(printf, 2, 3); |
| 173 | extern int esl_sq_FormatAccession(ESL_SQ *sq, const char *acc, ...) ESL_ATTRIBUTE_FORMAT(printf, 2, 3); |
| 174 | extern int esl_sq_FormatDesc (ESL_SQ *sq, const char *desc, ...) ESL_ATTRIBUTE_FORMAT(printf, 2, 3); |
| 175 | extern int esl_sq_FormatSource (ESL_SQ *sq, const char *source, ...) ESL_ATTRIBUTE_FORMAT(printf, 2, 3); |
| 176 | extern int esl_sq_AppendDesc (ESL_SQ *sq, const char *desc); |
| 177 | extern int esl_sq_SetCoordComplete(ESL_SQ *sq, int64_t L); |
| 178 | extern int esl_sq_CAddResidue (ESL_SQ *sq, char c); |
| 179 | extern int esl_sq_ReverseComplement(ESL_SQ *sq); |
| 180 | extern int esl_sq_Checksum(const ESL_SQ *sq, uint32_t *ret_checksum); |
| 181 | extern int esl_sq_CountResidues(const ESL_SQ *sq, int start, int L, float *f); |
| 182 | |
| 183 | extern ESL_SQ *esl_sq_CreateDigital(const ESL_ALPHABET *abc); |
| 184 | extern ESL_SQ *esl_sq_CreateDigitalFrom(const ESL_ALPHABET *abc, const char *name, const ESL_DSQ *dsq, |
| 185 | int64_t L, const char *desc, const char *acc, const char *ss); |
| 186 | extern int esl_sq_Digitize(const ESL_ALPHABET *abc, ESL_SQ *sq); |
| 187 | extern int esl_sq_Textize(ESL_SQ *sq); |
| 188 | extern int esl_sq_GuessAlphabet(ESL_SQ *sq, int *ret_type); |
| 189 | extern int esl_sq_XAddResidue(ESL_SQ *sq, ESL_DSQ x); |
| 190 | extern int esl_sq_ConvertDegen2X(ESL_SQ *sq); |
| 191 | |
| 192 | extern int esl_sq_GetFromMSA (const ESL_MSA *msa, int which, ESL_SQ *sq); |
| 193 | extern int esl_sq_FetchFromMSA(const ESL_MSA *msa, int which, ESL_SQ **ret_sq); |
| 194 | |
| 195 | extern ESL_SQ_BLOCK *esl_sq_CreateBlock(int count); |
| 196 | extern int esl_sq_BlockGrowTo(ESL_SQ_BLOCK *sqblock, int newsize, int do_digital, const ESL_ALPHABET *abc); |
| 197 | extern ESL_SQ_BLOCK *esl_sq_CreateDigitalBlock(int count, const ESL_ALPHABET *abc); |
| 198 | extern void esl_sq_DestroyBlock(ESL_SQ_BLOCK *sqBlock); |
| 199 | extern int esl_sq_BlockReallocSequences(ESL_SQ_BLOCK *block); |
| 200 | |
| 201 | extern int esl_sq_Validate(ESL_SQ *sq, char *errmsg); |
| 202 | extern int esl_sq_Sample(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, int maxL, ESL_SQ **ret_sq); |
| 203 | |
| 204 | #endif /*eslSQ_INCLUDED*/ |
| 205 |