File: QuantifyGraph.cc

package info (click to toggle)
trinityrnaseq 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212,452 kB
  • ctags: 5,067
  • sloc: perl: 45,552; cpp: 19,678; java: 11,865; sh: 1,485; makefile: 613; ansic: 427; python: 313; xml: 83
file content (428 lines) | stat: -rw-r--r-- 12,518 bytes parent folder | download
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//#ifndef FORCE_DEBUG
//#define NDEBUG
//#endif

#include <string.h>

#include "base/CommandLineParser.h"

#include "aligns/KmerAlignCore.h"
#include "analysis/DNAVector.h"
#include <math.h>
#include "base/FileParser.h"
#include "analysis/KmerTable.h"

#include "analysis/CompMgr.h"

extern "C"
{
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
}


static bool DEBUG = false;


// need to add Execute() and Exists() to a common library, since used in multiple chrysalis components (including Chrysalis.cc)
void Execute(const char * command) {
    int ret = system(command);
    if (ret != 0) {
        cout << "COMMAND: " << command << endl;
        cout << "Died with exit code " << ret << endl;
        cout << "Exiting." << endl;
        exit(-1);
    }
    
}
bool Exists(const string & s) 
{
    FILE * p = fopen(s.c_str(), "r");
    if (p != NULL) {
        fclose(p);
        return true;
    }
    // cout << "FATAL ERROR: Could not open file for read: " << s << endl;
    // cout << "Please make sure to enter the correct file name(s). Exiting now." << endl;
    
    return false;
}



void SortPrint(FILE * pReads, svec<IDS> & ids, const vecDNAVector & seq) 
{
    long long i;
    
    Sort(ids);
    int lastID = -1;
    int id = -1;
    int start = -1;
    int edge = -1;
    int lastStart = -1;
    int lastEdge = -1;
    int ori;
    
    string line;
    char tmp[1024*10];
    
    int lastStartTemp = -1;
    int lastOri = 1;
    
    for (i=0; i<ids.lsize(); i++) {
        id = ids[i].ID();
        ori = ids[i].Ori();
        start = ids[i].Start();
        edge = ids[i].Edge();
        //cout << id << "\t" << start << "\t" << edge << endl;
        if (id != lastID
#ifndef NO_REVERSE_OUT	  
            || ori != lastOri
#endif
            ) {
            if (lastID != -1) {
                
                sprintf(tmp, "%d\t%d\t", lastStart, lastEdge);	
                line += tmp;
                if (lastStart > lastStartTemp) {
                    fprintf(pReads, "%s\t", line.c_str());
                    //const DNAVector &d = seq[lastID];
                    
#ifndef NO_REVERSE_OUT	  
                    DNAVector d = seq[lastID];
                    if (lastOri == -1) {
                        d.ReverseComplement();
                        //cout << "Reversing" << endl;
                    } else {
                        //cout << "Forward" << endl;
                    }
#endif 
                    
                    for (int j=0; j<d.isize(); j++) {
                        tmp[1] = 0;
                        tmp[0] = d[j];
                        fprintf(pReads, "%s", tmp);
                    }
                    
                    if (lastOri == -1)
                        fprintf(pReads, "\t-");
                    else
                        fprintf(pReads, "\t+");
                    fprintf(pReads, "\n");
                }
                //fprintf(pReads, "%d\t%d\n", lastStart, lastEdge);
                line = "";
            }
            //fprintf(pReads, "%s\t%d\t%d\t", seq.Name(id).c_str(), start, edge);
            sprintf(tmp, "%s\t%d\t%d\t", seq.Name(id).c_str(), start, edge);
            line = tmp;
            
            lastStartTemp = start;
        }
        lastID = id;
        lastStart = start;
        lastEdge = edge;
        lastOri = ori;
        
    }
    
    if (id != -1) {
        sprintf(tmp, "%d\t%d\t", start, edge);	
        line += tmp;
        if (lastStart > lastStartTemp) {
            fprintf(pReads, "%s\t", line.c_str());
            DNAVector d = seq[id];
            if (ori == -1)
                d.ReverseComplement();
            for (int j=0; j<d.isize(); j++) {
                tmp[1] = 0;
                tmp[0] = d[j];
                fprintf(pReads, "%s", tmp);
            }
            if (lastOri == 1)
                fprintf(pReads, "\t+");
            else
                fprintf(pReads, "\t-");
            
            
            fprintf(pReads, "\n");
        }
        //fprintf(pReads, "%d\t%d\n", start, edge);
    }
}

//========================================================================
//========================================================================
//========================================================================


bool Irregular(char l)
{
    if (l == 'A' || l == 'C' || l == 'G' || l == 'T')
        return false;
    //cout << "Irregular char: " << l << endl;
    return true;
}



string ReadsExt(const string & in) 
{
    char tmp[1024*10];
    strcpy(tmp, in.c_str());
    int n = strlen(tmp);
    
    
    for (int i=n-1; i>=0; i--) {
        if (n-i > 6) {
            break;
        }
        if (tmp[i] == '.') {
            tmp[i] = 0;
            string out = tmp;
            out += ".reads";
            return out;
        }
        
    }
    string out = in + ".reads";
    return out;
}

int main(int argc,char** argv)
{
    
    commandArg<string> aStringCmmd("-i","read fasta file");
    commandArg<string> gStringCmmd("-g","graph file");
    commandArg<string> oStringCmmd("-o","graph output");
    commandArg<int> kCmmd("-k","kmer size", 24);
    commandArg<bool> strandCmmd("-strand","strand specific", false);
    //commandArg<bool> cCmmd("-nc","do not fully connected graph", false);
    commandArg<long> maxReadsCmd("-max_reads", "max number of reads to map to graph", -1);
    commandArg<bool> debugCmmd("-debug", "verbosely describe operations", false);
    commandArg<bool> no_cleanupCmmd ("-no_cleanup", "retain input files on success", false);
    
    commandLineParser P(argc,argv);
    P.SetDescription("Assembles k-mer sequences.");
    P.registerArg(aStringCmmd);
    P.registerArg(gStringCmmd);
    P.registerArg(oStringCmmd);
    P.registerArg(kCmmd);
    P.registerArg(strandCmmd);
    //P.registerArg(cCmmd);
    P.registerArg(maxReadsCmd);
    P.registerArg(debugCmmd);
    P.registerArg(no_cleanupCmmd);

    P.parse();
    
    string aString = P.GetStringValueFor(aStringCmmd); // reads
    string gString = P.GetStringValueFor(gStringCmmd); // graph input
    string oString = P.GetStringValueFor(oStringCmmd); // graph output
    bool sStrand = P.GetBoolValueFor(strandCmmd);
    int k = P.GetIntValueFor(kCmmd)+1;
    long max_reads = P.GetLongValueFor(maxReadsCmd); 
    bool NO_CLEANUP = P.GetBoolValueFor(no_cleanupCmmd);
    
    DEBUG = P.GetBoolValueFor(debugCmmd);
    


    
    if (Exists(oString) && (! Exists(gString)) && (! Exists(aString)) ) {
        cerr << "Quantify graph previously finished successfully on " << aString << ".  Not rerunning here." << endl;
        return(0);
    }
    else if (! (Exists(gString) && Exists(aString)) ) {
        cerr << "ERROR: missing either: " << gString << " or " << aString << ", cannot run QuantifyGraph here." << endl;
        return(1);
    }
    
    
    int i, j;
    //vecbasevector contigBases;
    
    vecDNAVector seq;

    if (max_reads > 0) {
        // std::cerr << "*Restricting number of input reads to " << max_reads << endl;
        seq.setMaxSeqsToRead(max_reads);
    }
    
    seq.Read(aString, false, true, true, 1000); // parse the reads from the fasta file
    
    KmerSequence kmers(k, &seq); // kmer catalog based on the fasta reads
    kmers.Add(seq);
    
    long long m = kmers.GetBoundValue();
    
    FlatFileParser parser; // read the raw graph
    parser.Open(gString);
    
    FILE * pOut = fopen(oString.c_str(), "w");  // output graph
    string reads = ReadsExt(oString);
    FILE * pReads = fopen(reads.c_str(), "w");  // output reads in context of graph
    
    svec<IDS> ids;
    ids.reserve(seq.size());
    
    //string last;
    //int lastNode = -1;
    
    svec<char> first;
    first.resize(100000, 'N');

    // do an initial scan to set up the node identities and linkage info
    while (parser.ParseLine()) {
        
        if (parser.GetItemCount() >= 4) {
            const string & s = parser.AsString(3); // kmer
            int node = parser.AsInt(0);
            int prevNode = parser.AsInt(1);
            
            const char * p2 = s.c_str();
            if (node >= first.isize())
                first.resize(node + 10000, 'N');

            first[node] = p2[0]; // first letter of the kmer stored
        }
    }

    
    // now, do a second pass:
    parser.Open(gString);

    while (parser.ParseLine()) {
        
        if (parser.GetItemCount() < 4) {

            fprintf(pOut, "%s\n", parser.Line().c_str()); // component header line

            // processing of component data from previously processed component
            
            if (ids.lsize() > 0) {
                SortPrint(pReads, ids, seq);
                //UniqueSort(ids);
                //for (i=0; i<ids.isize(); i++) {
                //fprintf(pReads, "%s\n", seq.Name(ids[i]).c_str());
                //}
            }
            fprintf(pReads, "%s\n", parser.Line().c_str());
            ids.clear();
            
            //first.clear();
            //first.reserve(50000);
            continue;
        }
        
        const string & s = parser.AsString(3); // kmer
        int node = parser.AsInt(0);
        int prevNode = parser.AsInt(1);
        
        const char * p2 = s.c_str();
        //if (node >= first.isize())
        //    first.resize(node + 10000, 'N');
        //first[node] = p2[0];
        
        //if (prevNode >= 0 && last == "" ) {
        //  cout << "Potential ERROR" << endl;
        //}
        //if(prevNode >= 0 && prevNode != lastNode) {
        //  cout << "Potential ERROR (2): prevNode = " << prevNode;
        //  cout << " lastNode=" << lastNode << endl;      
        //}
        //int edge = parser.AsInt(0);
        long long edge = prevNode;
        long long n1 = 0;
        long long n2 = 0;
        if (prevNode >= 0) {
            
            //  building the whole kmer sequence in 'sub'
            DNAVector sub;
            sub.resize(strlen(s.c_str())+1);
            const char * p = s.c_str();
            for (i=0; i<sub.isize()-1; i++)
                sub[i+1] = p[i];
            
            //const char * p2 = last.c_str();
            if (first[prevNode] == 'N')
                cout << "ERROR!! first[prevNode] where prevNode = " << prevNode << " unset" << endl;

            sub[0] = first[prevNode];
            


            kmers.BasesToNumberCountPlus(ids, n1, sub, edge);
            
            if (!sStrand) {
                sub.ReverseComplement();
                long long from = ids.lsize();
                kmers.BasesToNumberCountPlus(ids, n2, sub, edge);
                
                if (n1 + n2 < 0x7FFFFFFF) {
                    
                    for (long long x=from; x<ids.lsize(); x++) {
                        ids[x].SetOri(-1);
                        int len = seq[ids[x].ID()].isize();
                        int pos = ids[x].Start()+1;
                        //cout << "len=" << len << " pos=" << pos;
#ifndef NO_REVERSE_OUT	  
                        ids[x].SetStart(len-pos-k+1);
                        //cout << " new=" << len-pos-k+1 << endl;
#else
                        ids[x].SetStart(pos+1);
#endif
                    }
                } else {
                    cout << "WARNING: k-mer overflow, n=" << n1 + n2 << ". Discarding." << endl;
                    n1 = n2 = 0;
                    //ids.resize(0);	
                }
            }
            
            /*  if (s == "ATATCACAAAACAATCTTCATTCG") {
                for (int x = 0; x<sub.isize(); x++)
                cout << sub[x];
                cout << endl;
                
                cout << "Count=" << n1 + n2 << endl;
                }*/
        }
        
        for (i=0; i<parser.GetItemCount(); i++) {
            if (i>0)
                fprintf(pOut, "\t");
            if (i == 2) {
                fprintf(pOut, "%d", (int)(n1+n2));
            } else {
                fprintf(pOut, "%s", parser.AsString(i).c_str());
            }
        }
        fprintf(pOut, "\n");
        //lastNode = node;
        //last = s;
    }
    if (ids.lsize() > 0) {
        SortPrint(pReads, ids, seq);
    }
    
    fclose(pOut);
    fclose(pReads);

           
    
    
    // only remove the input files once the outputs have been successfully generated.
    if (! NO_CLEANUP) {

        // remove inputs to reduce file counts.
        unlink(aString.c_str());
        unlink(gString.c_str());
    }

    return 0;
    
}