File: multireads.cpp

package info (click to toggle)
cufflinks 1.3.0-2
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 3,864 kB
  • sloc: cpp: 48,999; ansic: 12,297; sh: 3,381; python: 432; makefile: 209
file content (131 lines) | stat: -rw-r--r-- 2,801 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
/*
 *  multireads.cpp
 *  cufflinks
 *
 *  Created by Adam Roberts on 3/6/11.
 *  Copyright 2011 Adam Roberts. All rights reserved.
 *
 */


#include "hits.h"
#include "multireads.h"

void MultiRead::add_hit(RefID r_id, int left, int right)
{
	_hits.push_back(MultiHit(r_id, left, right));
}

MultiHit* MultiRead::get_hit(RefID r_id, int left, int right)
{
    for (size_t i = 0; i < num_hits(); ++i)
	{
		MultiHit& hit = _hits[_curr_index];
		if (hit.r_id == r_id && hit.left == left && hit.right == right)
		{
			return &hit;
        }
		_curr_index = (_curr_index + 1) % num_hits();
	}  
    fprintf(stderr, "\nWARNING: Multi-Hit not found (%d,%d).\n", left, right); 
    return NULL;
}

void MultiRead::add_expr(RefID r_id, int left, int right, double expr)
{
	MultiHit* hit = get_hit(r_id, left, right);
    if (hit)
    {
        hit->expr += expr;
        _tot_expr += expr;
    }
}

double MultiRead::get_mass(RefID r_id, int left, int right, bool valid_mass)
{
	if (!valid_mass)
	{
		return 1.0/num_hits();
	}
	
	if (_tot_expr == 0.0)
		return 0.0;
	
	MultiHit* hit = get_hit(r_id, left, right);
    if (hit)
        return hit->expr/_tot_expr;
    else
        return 1.0/num_hits();
}

MultiRead* MultiReadTable::get_read(InsertID mr_id)
{
	MultiReadMap::iterator it;
	it = _read_map.find(mr_id);
	if (it == _read_map.end())
	{
		return NULL;
	}
	else 
	{
		return &(it->second);
	}
}

void MultiReadTable::add_hit(const MateHit& hit)
{
	add_hit(hit.ref_id(), hit.left(), hit.right(), hit.insert_id(), hit.num_hits());
}

void MultiReadTable::add_hit(RefID r_id, int left, int right, InsertID mr_id, int exp_num_hits)
{
#if ENABLE_THREADS
	boost::mutex::scoped_lock lock(_lock);
#endif
	MultiRead* mr = get_read(mr_id);
	if (!mr)
	{
		mr = &((_read_map.insert(std::make_pair(mr_id, MultiRead(mr_id, exp_num_hits)))).first->second); 
	}
	mr->add_hit(r_id, left, right);
}

void MultiReadTable::add_expr(const MateHit& hit, double expr)
{
	add_expr(hit.ref_id(), hit.left(), hit.right(), hit.insert_id(), expr);
}

void MultiReadTable::add_expr(RefID r_id, int left, int right, InsertID mr_id, double expr)
{
#if ENABLE_THREADS
	boost::mutex::scoped_lock lock(_lock);
#endif
	MultiRead* mr = get_read(mr_id);
	mr->add_expr(r_id, left, right, expr);
}

double MultiReadTable::get_mass(const MateHit& hit)
{
#if ENABLE_THREADS
	boost::mutex::scoped_lock lock(_lock);
#endif	
	MultiRead* mr = get_read(hit.insert_id());
	if(!mr)
		return 1.0;
	return mr->get_mass(hit.ref_id(), hit.left(), hit.right(), _valid_mass);
}

size_t MultiReadTable::num_multireads()
{
	return (int)_read_map.size();
}

size_t MultiReadTable::num_multihits()
{
	size_t count = 0;
	for (MultiReadMap::iterator it=_read_map.begin() ; it != _read_map.end(); it++ )
	{
		count += it->second.num_hits();
	}
	return count;
}