File: graph.hh

package info (click to toggle)
augustus 3.5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 777,052 kB
  • sloc: cpp: 80,066; perl: 21,491; python: 4,368; ansic: 1,244; makefile: 1,141; sh: 171; javascript: 32
file content (411 lines) | stat: -rw-r--r-- 11,907 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
353
354
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
/*
 * graph.hh
 *
 * License: Artistic License, see file LICENSE.TXT or 
 *          https://opensource.org/licenses/artistic-license-1.0
 */

#ifndef _GRAPH_HH
#define _GRAPH_HH

#include <map>
#include <sstream>
#include <queue>
#include <iostream>
#include <stack>
#include "gene.hh"
#include "properties.hh"
#include "exoncand.hh"

using namespace std;

#define NUM_STATENAMES 6

/**
 * @author Stefanie Nachtweide
 */
enum Statename{type_unknown=-1, CDS, utr3, utr5, intron, utr3Intron, utr5Intron};

extern string stateNameIdentifiers[NUM_STATENAMES];

/* types of nodes:
 * sampled exons and introns: sampled
 * additional exons, which are not sampled: unsampled_exons
 * neutral nodes: IR, plus0, plus1, plus2, minus0, minus1, minus2 (for each of the 7 neutral lines one type)
 * NOT_KNOWN: default type, for example head and tail
 */
#define NUM_NODETYPES 27

/**
 * @brief types of nodes
 * @details sampled exons and introns: sampled <br>
 * additional exons, which are not sampled: unsampled_exons <br>
 * neutral nodes: IR, plus0, plus1, plus2, minus0, minus1, minus2 (for each of the 7 neutral lines one type) <br>
 * NOT_KNOWN: default type, for example head and tail <br>
 * 
 * @author Stefanie Nachtweide
 */
enum NodeType{NOT_KNOWN=-1, IR,
	      plus0, plus1, plus2, minus0, minus1, minus2, T_plus1, TA_plus2, TG_plus2, T_minus1, C_minus1, YY_minus0, // intron types between two CDS exons
	      ncintr, rncintr, // intron between two non-coding exons
	      utr5intr, TLstart, TLstop, utr3intr, rutr5intr, rTLstart, rTLstop, rutr3intr, utrExon, // utr types
	      sampled, unsampled_exon}; // CDS types

extern string nodeTypeIdentifiers[NUM_NODETYPES];

/**
 * @brief Status stores all the relevant information (for states) from the program specific datastructure
 * 
 * @author Stefanie Nachtweide
 */
class Status;

/**
 * @author Stefanie Nachtweide
 */
class Node;

/**
 * @author Stefanie Nachtweide
 */
class Edge;

/**
 * @author Stefanie Nachtweide
 */
class Graph;

class Status{
public:
    Status(Statename s=type_unknown, int b=0, int e=0, double sc=0.0, const void *it=NULL, Status *n=NULL):
	name(s),
	begin(b),
	end(e),
	score(sc),
	next(n),
	item(it)
    {}
    Statename name;
    int begin, end;
    double score;
    Status *next;
    const void *item;

    bool isIntron() const {return (name >= intron);}
    bool isExon() const {return (name >= CDS && name <= utr5);}
    bool isCDS() const {return (name == CDS);}
    bool isUTR() const {return (name == utr3 || name == utr5);}    
    float getPostProb() const {return (item)? ((State*)item)->apostprob : 0.0;}
    int getLen() const {return end-begin+1;}
    int getFrame() const {return isCDS()? ((State*)item)->frame() : -1;}
    bool hasEvidence() const {return ((State*)item)->evidence;}
    bool hasEvidence(string srcname) const; //returns true if the exon/intron has evidence from src="srcname"
    int numEvidence() const {return hasEvidence()? ((State*)item)->evidence->numEvidence : 0;}
};

bool isTlstartOrstop(Status *predExon, Status *succExon);

class Node{
public:
    Node(int s=0, int e=0, float sc=0.0, const void *it=NULL, NodeType t=NOT_KNOWN, Node *p=NULL, bool b=0, Node *nn=NULL, Node *pn=NULL):
	begin(s),
	end(e),
	score(sc),
	item(it),
	n_type(t),
	pred(p),
	label(b),
	nextNontrivialNeutNode(nn),
	prevNontrivialNeutNode(pn)
    {}
    int begin, end;
    float score;
    const void *item;
    NodeType n_type;
    Node *pred;
    bool label;           // label is 1, if node is in path, else label is 0
    list<Edge> edges;
    Node *nextNontrivialNeutNode;   // pointer to Node on neutral line, which is the nearest that can be reached by a nontrivial way (used for new back edges)
    Node *prevNontrivialNeutNode;   // pointer to Node on neutral line, from which the actual node is the nearest that can be reached by a nontrivial way (used for new back edges)
    size_t index;                   // (used for new back edges) (used for tarjan)
    size_t lowlink;                 // (used for tarjan)

    StateType castToStateType(); //casts void* back to State* and returns the StateType
    void addWeight(float weight); //add weight to all outgoing edges
    Edge* getEdge(Node* succ);
    State* getIntron(Node* succ);
    bool isSampled() const {return(n_type == sampled || n_type == utrExon);}

};

class Edge{
public:
    Edge(Node *t=NULL, bool n=true, float sc=0.0, const void *it=NULL):
	to(t),
	score(sc),
	neutral(n),
	item(it)
    {}
    Node *to;
    float score;
    bool neutral;
    const void *item;

    inline bool isSampledIntron(){
	return item;
    }
};

//print functions for Nodes and Edges
ostream& operator<<(ostream& ostrm, Node *node);
ostream& operator<<(ostream& ostrm, const Edge &edge);

/*
 * ordering needed by statelist: 
 * the states of the sampled genes need to be together ordered after their beginning for each gene. 
 * example: 5'UTR-CDS-intron-CDS-intron-CDS-3'UTR-5'UTR-CDS-intron-CDS-3'UTR
 * here we have 2 Genes and states in order of appearance
 */

class Graph{
public:
    Graph(list<Status> *states) : statelist(states) {}
    virtual ~Graph(); 
    void addBackEdges();
    void addBackEdgesComp();
    void tarjan();          // Tarjan's strongly connected components algorithm
    void tarjanAlg(Node* from, stack<Node*> &S, size_t &index);

    list<Node*> nodelist;      //stores all nodes belonging to the graph
    list<Status> *statelist;
    int min, max;
    map<string,Node*> existingNodes;
    Node *head;
    Node *tail;
 
    void buildGraph(); //needs to be called in constructor of derived class

    template<class T> inline bool alreadyProcessed(T *temp){
	return(existingNodes[getKey(temp)]!=NULL);    
    }
    inline bool alreadyProcessed(string key){
	return(existingNodes[key]!=NULL);    
    }
    template<class T> inline Node* getNode(T *temp){
	return existingNodes[getKey(temp)];
    }
    inline Node* getNode(string key){
	return existingNodes[key];
    } 
    // functions needed to build the graph
protected:
    bool edgeExists(Node *e1, Node *e2);
    inline void addToHash(Node *n){
	existingNodes[getKey(n)] = n;
    }
    Node* addExon(Status *exon, vector<Node*> &neutralLine);
    void addPair(Status *exon1, Status *exon2, vector<Node*> &neutralLine);
    void createNeutralLine(vector<Node*> &neutralLine, double weight=0.0, bool onlyComplete=false); 
    void addCompatibleEdges();
    void insertIntron(Node *exon1, Node *exon2);  
    int minInQueue(queue<Node*> *q);
    bool nonneutralIncomingEdge(Node *exon);
    void printGraphToShell();
    void getSizeNeutralLine();
    void addWeightToEdge();

    // program specific functions
    virtual bool exonAtGeneStart(Status *st)=0;
    virtual bool exonAtGeneEnd(Status *st)=0;
    virtual string getKey(Node *n)=0;
    virtual string getKey(Status *st)=0;
    virtual string getKey(State *st)=0;
    virtual string getKey(ExonCandidate *exoncand)=0;
    virtual double getIntronScore(Status *predExon, Status *nextExon)=0;
    virtual void addEdgeFromHead(Status *exon)=0;
    virtual void addEdgeToTail(Status *exon)=0; 
    virtual bool compatible(Node *exon1, Node *exon2)=0;
    virtual double setScore(Status *st)=0;
    virtual void calculateBaseScores()=0;
    virtual void printGraph(string filename)=0;   
    virtual void printGraph2(string filename)=0;  
    virtual bool mergedStopcodon(Node* exon1, Node* exon2)=0;
    virtual bool mergedStopcodon(Status* exon1, Status* exon2)=0;
    virtual bool mergedStopcodon(StateType type1, StateType type2, int end1, int begin2)=0;
    virtual float getAvgBaseProb(Status *st) =0;
    virtual float getAvgBaseProb(ExonCandidate *ec)=0;

};

/**
 * @author Stefanie Nachtweide
 */
class AugustusGraph : public Graph{

public:
    AugustusGraph(list<Status> *states, const char* dna) : Graph(states), sequence(dna), seqlength(strlen(dna)){
	try {
	    utr = Properties::getBoolProperty("UTR");
	} catch (...) {
	    utr = false;
	}
	try {
	    alpha_e = Properties::getdoubleProperty("/MeaPrediction/alpha_E");
	} catch (...) {
	    alpha_e = 1;
	}
	try {
	    alpha_i = Properties::getdoubleProperty("/MeaPrediction/alpha_I");
	} catch (...) {
	    alpha_i = 1;
	}
	try {
	    x0_e = Properties::getdoubleProperty("/MeaPrediction/x0_E");
	} catch (...) {
	    x0_e = -10;
	}
	try {
	    x0_i = Properties::getdoubleProperty("/MeaPrediction/x0_I");
	} catch (...) {
	    x0_i = -10;
	}
	try {
	    x1_e = Properties::getdoubleProperty("/MeaPrediction/x1_E");
	} catch (...) {
	    x1_e = 10;
	}
	try {
	    x1_i = Properties::getdoubleProperty("/MeaPrediction/x1_I");
	} catch (...) {
	    x1_i = 10;
	}
	try {
	    y0_e = Properties::getdoubleProperty("/MeaPrediction/y0_E");
	} catch (...) {
	    y0_e = 0.5;
	}
	try {
	    y0_i = Properties::getdoubleProperty("/MeaPrediction/y0_I");
	} catch (...) {
	    y0_i = 0.5;
	}
	try {
	    i1_e = Properties::getdoubleProperty("/MeaPrediction/i1_E");
	} catch (...) {
	    i1_e = 0.25;
	}	
	try {
	    i1_i = Properties::getdoubleProperty("/MeaPrediction/i1_I");
	} catch (...) {
	    i1_i = 0.25;
	}
	try {
	    i2_e = Properties::getdoubleProperty("/MeaPrediction/i2_E");
	} catch (...) {
	    i2_e = 0.75;
	}
	try {
	    i2_i = Properties::getdoubleProperty("/MeaPrediction/i2_I");
	} catch (...) {
	    i2_i = 0.75;
	}
	try {
	    j1_e = Properties::getdoubleProperty("/MeaPrediction/j1_E");
	} catch (...) {
	    j1_e = -5;
	}	
	try {
	    j1_i = Properties::getdoubleProperty("/MeaPrediction/j1_I");
	} catch (...) {
	    j1_i = -5;
	}
	try {
	    j2_e = Properties::getdoubleProperty("/MeaPrediction/j2_E");
	} catch (...) {
	    j2_e = 5;
	}
	try {
	    j2_i = Properties::getdoubleProperty("/MeaPrediction/j2_I");
	} catch (...) {
	    j2_i = 5;
	}
	try {
	    r_be = Properties::getdoubleProperty("/MeaPrediction/r_be"); // threshold for exons on base level
	} catch (...) {
	    r_be = 0.5;
	}
	try {
	    r_bi = Properties::getdoubleProperty("/MeaPrediction/r_bi"); // threshold for introns on base level
	} catch (...) {
	    r_bi = 0.5;
	}
	
	for(int i = 0; i < seqlength*10; i++)
	    baseScore.push_back(0);

    }
    bool exonAtGeneStart(Status *st);
    bool exonAtGeneEnd(Status *st);
    bool exonAtCodingStart(Node *st);
    bool exonAtCodingEnd(Node *st);
    string getKey(Node *n);
    string getKey(Status *st);
    string getKey(State *st);
    string getKey(ExonCandidate *exoncand);
    double getIntronScore(Status *predExon, Status *nextExon);  
    void addEdgeFromHead(Status *exon);
    void addEdgeToTail(Status *exon);
    bool compatible(Node *exon1, Node *exon2);
    bool sameStrand(StateType typeA, StateType typeB);
    bool sameReadingFrame(Node *e1, Node *e2);
    void calculateBaseScores();
    double setScore(Status *st);
    int getBasetype(Status *st, int pos);
    void printGraph(string filename); 
    void printGraph2(string filename);
    bool mergedStopcodon(Node* exon1, Node* exon2);
    bool mergedStopcodon(Status* exon1, Status* exon2);
    bool mergedStopcodon(StateType type1, StateType type2, int end1, int begin2);
    void getPoints(Status *st, double p, double *a1, double *a2, double *b1, double *b2);
    float getAvgBaseProb(Status *st);
    float getAvgBaseProb(ExonCandidate *ec);

    const char* sequence;
    int seqlength;
    vector<double> baseScore;
    bool utr;

  // parameters for scores
  double alpha_e;
  double alpha_i;
  double x0_e;
  double x0_i;
  double x1_e;
  double x1_i;
  double y0_e;
  double y0_i;
  double i1_e;
  double i1_i;
  double i2_e;
  double i2_i;
  double j1_e;
  double j1_i;
  double j2_e;
  double j2_i;
  double r_be;
  double r_bi;  
};

bool compareNodes(Node *first, Node *second);
bool compareEdges(Edge first, Edge second);
bool compareNodeEnds(const Node *first, const Node *second);

template <class T>
inline string to_string (const T& t)
{
    stringstream ss;
    ss << t;
    return ss.str();
}

#endif