File: OrderedBAMWriter.cpp

package info (click to toggle)
tvc 5.0.3%2Bgit20151221.80e144e%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,548 kB
  • sloc: cpp: 24,088; ansic: 3,933; python: 260; makefile: 16
file content (90 lines) | stat: -rw-r--r-- 2,207 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
/* Copyright (C) 2013 Ion Torrent Systems, Inc. All Rights Reserved */

/* Author: Alex Artyomenko <aartyomenko@cs.gsu.edu> */

#include "OrderedBAMWriter.h"


bool alignment_left_after_right (Alignment *lhs, Alignment *rhs)
{
  if (lhs == rhs)
    return false;

  if (!rhs->processed)
    return false;
  if (!lhs->processed)
    return true;

  if (!rhs->alignment.IsMapped())
    return false;
  if (!lhs->alignment.IsMapped())
    return true;

  if (lhs->alignment.RefID > rhs->alignment.RefID)
    return true;
  if (lhs->alignment.RefID < rhs->alignment.RefID)
    return false;

  return lhs->alignment.Position > rhs->alignment.Position;
}


bool AlignmentComporator::operator()(Alignment *lhs, Alignment *rhs) {
  return alignment_left_after_right(lhs, rhs);
}

bool OrderedBAMWriter::cond(Alignment *current) {
  if (!current)
    return !empty();

  if (current == top())
    return false;

  if (!top()->processed)
    return false;
  if (!current->processed)
    return true;

  if (!top()->alignment.IsMapped())
    return false;
  if (!current->alignment.IsMapped())
    return true;

  if (current->alignment.RefID > top()->alignment.RefID)
    return true;
  if (current->alignment.RefID < top()->alignment.RefID)
    return false;

  return current->original_position > top()->alignment.Position;

  /*
  return current ? 1 == cmp_pairs(top()->alignment.RefID, top()->alignment.Position,
                                  current->alignment.RefID, current->original_position) : !empty();
                                  */
}

Alignment* OrderedBAMWriter::process_new_etries(Alignment *list) {
  if (!list) return NULL;
  Alignment * last = list;
  for (Alignment* current = list;current; last = current, current = current->next) {
    push(current);
  }
  return wrap_items_in_linkedlist(last);
}

Alignment* OrderedBAMWriter::flush() {
  return wrap_items_in_linkedlist(NULL);
}

Alignment* OrderedBAMWriter::wrap_items_in_linkedlist(Alignment * current) {
  if (empty()) return NULL;
  Alignment* result = NULL, *c = top();
  while (cond(current)) {
    if (!result) result = top();
    c = top();
    pop();
    c->next = top();
  }
  if (result) c->next = NULL;
  return result;
}