File: JoinTransByPairs.cc

package info (click to toggle)
trinityrnaseq 2.6.6%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 346,416 kB
  • sloc: perl: 47,542; cpp: 20,209; java: 12,484; python: 2,766; sh: 1,665; makefile: 895; ansic: 90; xml: 83
file content (447 lines) | stat: -rw-r--r-- 9,460 bytes parent folder | download | duplicates (5)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#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 "util/mutil.h"
#include "analysis/KmerTable.h"


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


int FindPartner(svec<IDS> & ids, const string & name) 
{
  char tmp[256];
  strcpy(tmp, name.c_str());
  tmp[strlen(tmp)-1] = '1';

  IDS dummy;
  dummy.SetName(tmp);

  int index = BinSearch(ids, dummy);
  return index;
}

int FindPartnerRead(vecDNAVector & dna, const string & name) 
{
  char tmp[256];
  strcpy(tmp, name.c_str());
  tmp[strlen(tmp)-1] = '1';


  int index = dna.NameIndex(tmp);
  return index;
}

bool IsFW(const string & name) {
  if (name[name.size()-1] == '2') {
    //cout << "Add fw " << name << endl;
    return true;
  }
  return false;
}


class ReadPlaces
{
public:
  ReadPlaces() {
  }

  void AddFW(int i, int pos) {
    m_fw.push_back(i);
    m_fwPos.push_back(pos);
  }
  void AddRC(int i, int pos) {
    m_rc.push_back(i);
    m_rcPos.push_back(pos);
  }

  int GetFWCount() const {return m_fw.isize();}
  int GetFW(int i) const {return m_fw[i];}
  int GetFWPos(int i) const {return m_fwPos[i];}
  int GetRCCount() const {return m_rc.isize();}
  int GetRC(int i) const {return m_rc[i];}
  int GetRCPos(int i) const {return m_rcPos[i];}

  int GetRCPosByRead(int read) const {
    for (int i=0; i<m_rc.isize(); i++) {
      if (m_rc[i] == read)
	return m_rcPos[i];
    }
    cout << "Error: read not found!" << endl;
    return 10000;
  }

  void Clear() {
    m_fw.clear();
    m_rc.clear();
    m_fwPos.clear();
    m_rcPos.clear();
  }

  void Absorb(const ReadPlaces & pl, int len1, int len2) {
    int i;
    for (i=0; i<pl.GetFWCount(); i++) {
      m_fw.push_back(pl.GetFW(i));
      m_fwPos.push_back(pl.GetFWPos(i)+len2);
    }
    
    for (i=0; i<pl.GetRCCount(); i++) {
      m_rc.push_back(pl.GetRC(i));
      m_rcPos.push_back(pl.GetRCPos(i)+len1);
    }
  }

  void Write(CMWriteFileStream & f) const {
    int i;
    f.Write(m_fw.isize());
    for (i=0; i<m_fw.isize(); i++) {
      f.Write(m_fw[i]);
      f.Write(m_fwPos[i]);
    }
    f.Write(m_rc.isize());
    for (i=0; i<m_rc.isize(); i++) {
      f.Write(m_rc[i]);
      f.Write(m_rcPos[i]);
    }
  }

  void Read(CMReadFileStream & f) {
    int i, n;
    f.Read(n);
    m_fw.resize(n);
    m_fwPos.resize(n);
    for (i=0; i<m_fw.isize(); i++) {
      f.Read(m_fw[i]);
      f.Read(m_fwPos[i]);
    }
    f.Read(n);
    m_rc.resize(n);
    m_rcPos.resize(n);
    for (i=0; i<m_rc.isize(); i++) {
      f.Read(m_rc[i]);
      f.Read(m_rcPos[i]);
    }
    
  }
  
  
private:
  svec<int> m_fw;
  svec<int> m_fwPos;
  
  svec<int> m_rc;
  svec<int> m_rcPos;
};


void ReadThePlaces(svec<ReadPlaces> & plac, const string & file) {
  CMReadFileStream f;
  f.Open(file.c_str());
  int n;  
  f.Read(n);
  plac.resize(n);
  for (int i=0; i<n; i++)
    plac[i].Read(f);
  f.Close();

}

void WriteThePlaces(const svec<ReadPlaces> & plac, const string & file) {
  CMWriteFileStream f;
  f.Open(file.c_str());
  
  f.Write(plac.isize());

  for (int i=0; i<plac.isize(); i++)
    plac[i].Write(f);
  f.Close();

}

bool DevOK(double a, double a_dev, double b, double b_dev) 
{
  if ((a-a_dev)/(b+b_dev) < 2.5 && (b-b_dev)/(a+a_dev) < 2.5)
    return true;
  
  return false;
}

bool IsJoinOK(double a, double b, double c) 
{
  double a_dev = sqrt(a);
  double b_dev = sqrt(b);
  double c_dev = sqrt(c);

  cout << "Try this - contig a: mean=" << a << " +/-" << a_dev;
  cout << "  contig b: mean=" << b << " +/-" << b_dev;
  cout << "  joins: mean=" << c << " +/-" << c_dev << endl;
  
  return DevOK(a, a_dev, b, b_dev) && DevOK((a+b)/2, a_dev, c, c_dev);

}



int main(int argc,char** argv)
{

  commandArg<string> aStringCmmd("-i","read fasta file");
  commandArg<string> fStringCmmd("-f","fasta file");
  commandArg<string> oStringCmmd("-o","fasta output");
  commandArg<int> kCmmd("-k","kmer size", 24);
  commandArg<bool> strandCmmd("-doublestrand","not strand-specific", false);
  commandArg<bool> padCmmd("-pad","pad witn N's if no merge", false);
  commandArg<bool> contCmmd("-cont","continue w/ previous run", false);
  //commandArg<bool> cCmmd("-nc","do not fully connected graph", false);

  commandLineParser P(argc,argv);
  P.SetDescription("Assembles k-mer sequences.");
  P.registerArg(aStringCmmd);
  P.registerArg(fStringCmmd);
  P.registerArg(oStringCmmd);
  P.registerArg(kCmmd);
  P.registerArg(strandCmmd);
  P.registerArg(padCmmd);
  P.registerArg(contCmmd);
  //P.registerArg(cCmmd);


  P.parse();

  string aString = P.GetStringValueFor(aStringCmmd);
  string fString = P.GetStringValueFor(fStringCmmd);
  string oString = P.GetStringValueFor(oStringCmmd);
  int k = P.GetIntValueFor(kCmmd);
  bool bStrand = P.GetBoolValueFor(strandCmmd);

  bool bCont = P.GetBoolValueFor(contCmmd);

  bool bPad = P.GetBoolValueFor(padCmmd);


  string placementFile = fString + ".placements";

  int i, j;
  //vecbasevector contigBases;
 
  vecDNAVector seq;
  seq.Read(aString);

  vecDNAVector dna;
  dna.Read(fString);

  int readlen = seq[0].isize();
 
  KmerSequence kmers(k, &seq);

  if (!bCont) {
    kmers.AddRestrict(seq, dna);
    //kmers.Add(seq);
  } else {
    cout << "Continuing..." << endl;
  }

  long long m = kmers.GetBoundValue();


  vecDNAVector out;
  //dna.Read(fString);

  //svec<int> used;
  //used.resize(seq.isize(), 0);

  int offset = seq[0].isize();

  int broken = 0;

  int extra = 100;

  svec<int> contigs;
  contigs.resize(seq.size(), -1);

  svec<ReadPlaces> places;

  if (!bCont) {
    places.resize(dna.size());
  } else {
    cout << "Reading placements..." << endl;
    ReadThePlaces(places, placementFile);
    cout << "done." << endl;
  }
   
  int maxDist = 400;


  cout << "Assigning contigs to reads." << endl;
  for (i=0; i<dna.size(); i++) {
    const DNAVector & one = dna[i];
    double all = 0.;
    broken = 0;
    //cout << "Sequence length: " << one.isize() << " seq: " << i << endl;
   
    ReadPlaces & pl = places[i];

    if (bCont) {
      for (j=0; j<pl.GetFWCount(); j++) {
	contigs[pl.GetFW(j)] = i;
      }
      for (j=0; j<pl.GetRCCount(); j++) {
	contigs[pl.GetRC(j)] = i;
      }

    } else {
      for (j=0; j<=one.isize()-k; j++) {
	DNAVector sub;
	sub.SetToSubOf(one, j, k);
	long long n1, n2;
	
	svec<IDS> ids;
	
	int edge = 0;
	kmers.BasesToNumberCountPlus(ids, n1, sub, edge);
	
	for (int x=0; x<ids.isize(); x++) {
	  if (contigs[ids[x].ID()] > 0)
	    continue;
	  
	  double ff = FracAlign(one, j, seq[ids[x].ID()], ids[x].Start());
	  if (ff > 0.98) {
	    if (IsFW(seq.Name(ids[x].ID()))) {
	      pl.AddFW(ids[x].ID(), one.isize()-j);
	    } else {
	      pl.AddRC(ids[x].ID(), j+readlen);
	    }
	    contigs[ids[x].ID()] = i;
	  }
	}
      }
    }
  }

  //if (!bCont)
  //WriteThePlaces(places, placementFile);
  
  for (i=0; i<dna.size(); i++) {

    DNAVector & d = dna[i];
    if (d.isize() == 0)
      continue;
    if (d.isize() < 100)
      continue;

    svec<int> possible;
    possible.resize(dna.size(), 0);

    //cout << "Checking contig " << i << endl;
    ReadPlaces & pl = places[i];

    double currCov = (double)(pl.GetFWCount() + pl.GetRCCount() * readlen)/(double)d.isize();

    int best = 0;
    int max = 0;
    for (j=0; j<pl.GetFWCount(); j++) {
      int o = pl.GetFW(j);
      int dist = pl.GetFWPos(j); 
      const string & name = seq.Name(o);
      int r = FindPartnerRead(seq, name);
      if (r == -1)
	continue;
      
      int cont = contigs[r];
      if (cont == i || cont == -1)
	continue;
      //cout << "Found possible: " << cont << endl;
      
      const ReadPlaces & plOther = places[cont];
      dist += plOther.GetRCPosByRead(r);

      if (dist > maxDist)
	continue;

      possible[cont]++;
      if (possible[cont] > max) {
	max = possible[cont];
	best = cont;
      }
    }
    if (max > 2) {
      DNAVector & second = dna[best];
      double otherCov = (double)(places[best].GetFWCount() + places[best].GetRCCount() * readlen)/(double)second.isize();

      if (IsJoinOK(currCov, otherCov, max)) {

	cout << "Joining " << dna.Name(i) << " to " << dna.Name(best) << " support: " << max << endl;
	int firstLen = d.isize();
	int secondLen = second.isize();

	bool bMerged = false;

	if (d.Append(second, 12, 25, 0.95)) {
	  cout << "Merged sequences w/o N's." << endl;
	  second.resize(0);
	  bMerged = true;
	} else {
	  if (bPad) {
	    cout << "Padding with N's" << endl;
	    DNAVector nn = d;	
	    
	    nn.resize(d.isize() + 8);
	    for (j=d.isize(); j<nn.isize(); j++)
	      nn[j] = 'N';
	    int off = nn.isize();
	    nn.resize(off+second.isize());
	    for (j=off; j<nn.isize(); j++)
	      nn[j] = second[j-off];
	    second.resize(0);
	    dna[i] = nn;
	    bMerged = true;
	  }
	}

	if (bMerged) {
	  string newName = dna.Name(i);
	  newName += "_";
	  newName += dna.Name(best);
	  dna.SetName(i, newName);
	  
	  for (j=0; j<places[best].GetFWCount(); j++) {
	    contigs[places[best].GetFW(j)] = i;
	  }
	  for (j=0; j<places[best].GetRCCount(); j++) {
	    contigs[places[best].GetRC(j)] = i;
	  }
	  
	  
	  pl.Absorb(places[best], firstLen, secondLen);
	  places[best].Clear();
	  i--;
	}
      }
    }
  }


  

  dna.Write(oString, true);
 
  return 0;

}