File: nucleotideMapping.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (144 lines) | stat: -rw-r--r-- 3,311 bytes parent folder | download | duplicates (7)
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
#include <BALL/STRUCTURE/nucleotideMapping.h>

#include <BALL/COMMON/exception.h>
#include <BALL/KERNEL/residue.h>
#include <BALL/KERNEL/protein.h>

namespace BALL
{
	NucleotideMapping::NucleotideMapping()
		: a_(0), b_(0)
	{
	}

	NucleotideMapping::NucleotideMapping(Chain& a, Chain& b, const NucleotideMap& a_to_b, const NucleotideMap& b_to_a)
		: a_(&a), b_(&b), a_to_b_(a_to_b), b_to_a_(b_to_a)
	{
	}

	Chain* NucleotideMapping::getFirstStrand() const
	{
		return a_;
	}

	Chain* NucleotideMapping::getSecondStrand() const
	{
		return b_;
	}

	Residue* NucleotideMapping::firstToSecond(Residue* a) const
	{
		NucleotideMap::const_iterator it = a_to_b_.find(a);

		if (it == a_to_b_.end())
		{
			return 0;
		}

		return it->second;
	}

	Residue* NucleotideMapping::secondToFirst(Residue* b) const
	{
		NucleotideMap::const_iterator it = b_to_a_.find(b);

		if (it == b_to_a_.end())
		{
			return 0;
		}

		return it->second;
	}

	NucleotideMapping NucleotideMapping::assignNaively(Chain& a, Chain& b, unsigned int offset_a, unsigned int offset_b)
	{
		ResidueIterator at = a.beginResidue();
		for(unsigned int i = 0; i < offset_a; ++i, ++at) {
			if(at == a.endResidue()) {
				return NucleotideMapping();
			}
		}

		ResidueIterator bt = b.beginResidue();
		bt.toRBegin();
		ResidueIterator bREnd = b.beginResidue();
		bREnd.toREnd();
		for(unsigned int i = 0; i < offset_b; ++i, --bt) {
			if(bt == bREnd) {
				return NucleotideMapping();
			}
		}

		NucleotideMap a_to_b;
		NucleotideMap b_to_a;

		for (; (at != a.endResidue()) && (bt != bREnd); ++at, --bt) {
			a_to_b.insert(NucleotideMap::value_type(&*at, &*bt));
			b_to_a.insert(NucleotideMap::value_type(&*bt, &*at));
		}

		return NucleotideMapping(a, b, a_to_b, b_to_a);
	}

	NucleotideMapping NucleotideMapping::assignFromDistances(Chain& a, Chain& b)
	{
		NucleotideMap a_to_b;
		NucleotideMap b_to_a;

		throw Exception::NotImplemented(__FILE__, __LINE__);

		return NucleotideMapping(a, b, a_to_b, b_to_a);
	}

	NucleotideMapping NucleotideMapping::assignFromAlignment(Chain& a, Chain& b, const Alignment& alignment)
	{
		//An iterator for the fist chain
		ResidueIterator at = a.beginResidue();

		//A pair of iterators for the second chain
		ResidueIterator bt = b.beginResidue();
		bt.toRBegin();
		ResidueIterator bREnd = b.beginResidue();
		bREnd.toREnd();

		if(alignment.first.length() != alignment.second.length())
		{
			throw Exception::InvalidArgument(__FILE__, __LINE__, "Strings in the alignment have differing lengths");
		}

		NucleotideMap a_to_b;
		NucleotideMap b_to_a;

		for(size_t i = 0; (i < alignment.first.length()) && (at != a.endResidue()) && (bt != bREnd); ++i)
		{
			//This should not happen with a true alignment, but better to be safe...
			if(alignment.first[i] == '-' && alignment.second[i] == '-')
			{
				continue;
			}

			//If the first residue is a gap, advance the iterator of the second chain
			if(alignment.first[i] == '-')
			{
				--bt;
				continue;
			}

			//If the second residue is a gap, advance the iterator of the first chain
			if(alignment.second[i] == '-')
			{
				++at;
				continue;
			}

			a_to_b.insert(NucleotideMap::value_type(&*at, &*bt));
			b_to_a.insert(NucleotideMap::value_type(&*bt, &*at));

			++at; --bt;
		}

		return NucleotideMapping(a, b, a_to_b, b_to_a);
	}

}