File: ScaffoldIwormContigs.cpp

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (216 lines) | stat: -rwxr-xr-x 5,083 bytes parent folder | download | duplicates (4)
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
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <stdexcept>
#include "error_checker.h"

#ifndef WIN32
#define HTS
#endif

#ifdef HTS
#include "htslib/sam.h"
#endif

#ifdef _DEBUG
ofstream SAM_OFH("scaffolding_entries.sam");
#endif

using namespace std;


void examine_frags(const set<string>& iworm_left, const set<string>& iworm_right, map<pair<string, string>, int>& paired_iworm_contigs)
{
	if (iworm_left.size() == 0 || iworm_right.size() == 0)
		return;	// no pairs

	if (iworm_left.size() == 1 && iworm_right.size() == 1)
	{
		string k1 = *iworm_left.begin();
		string k2 = *iworm_right.begin();
		if (k1 != k2)
		{
			if (k2 < k1) swap(k1, k2);

			pair<string, string> key(k1, k2);
			if (paired_iworm_contigs.find(key) == paired_iworm_contigs.end())
				paired_iworm_contigs[key] = 1;
			else
				paired_iworm_contigs[key]++;

#ifdef _DEBUG
			SAM_OFH << k1 << k2 << endl;
#endif

		}
	}
}

struct map_key
{
	template <typename T>
	typename T::first_type operator()(T keyValuePair) const
	{
		return keyValuePair.first;
	}
};

template <typename T>
struct sort_pred {
	map<T, int> &_hash;
	sort_pred(map<T, int>& h) : _hash(h) {}
	bool operator()(const T &left, const T &right) {
		return _hash[left] > _hash[right];
	}
};

template <typename T>
void copy_to_out(map<T, int>& paired_iworm_contigs, map<string, int>& iworm_acc_to_fasta_index)
{
	vector<T> outvec;
	const char *my_endl = "\n";

	outvec.reserve(paired_iworm_contigs.size());
	transform(paired_iworm_contigs.begin(), paired_iworm_contigs.end(), back_inserter(outvec), map_key());
	sort(outvec.begin(), outvec.end(), sort_pred<T >(paired_iworm_contigs));
	for (typename vector<T>::const_iterator a = outvec.begin(); a != outvec.end(); ++a)
	{
		const int index_A = iworm_acc_to_fasta_index[a->first];
		const int index_B = iworm_acc_to_fasta_index[a->second];

		cout << a->first << '\t' << index_A << '\t';
		cout << a->second << '\t' << index_B << '\t';
		cout << paired_iworm_contigs[*a] << my_endl;
	}
}

void index_iworm(string iworm_fasta, map<string, int>& aMap)
{
	int counter = 0;

	ifstream fh(iworm_fasta.c_str());
	while (fh)
	{
		string line;
		fh >> line;
		if (line[0] == '>')
			aMap[line.substr(1, string::npos)] = counter++;
	}
}

void validate_iworm(string iworm_acc)
{
	pair<int, int> a;

	if (iworm_acc[0] == 'a') iworm_acc[0] = ' ';
	replace(iworm_acc.begin(), iworm_acc.end(), ';', ' ');
	istringstream iss2(iworm_acc);
	iss2 >> a.first >> a.second;
	if (iss2.fail())
		throw bad_name_exception();
}

pair<string, int> parse_read_name(string read_acc)
{
	pair<string, int> result;
	size_t s = read_acc.rfind('/');
	result.first = read_acc.substr(0, s);
	result.second = read_acc[s + 1] - '1';

	if (s == string::npos || (result.second != 0 && result.second != 1))
		throw single_read_exception();

	return result;
}

void process_line(string read_acc, string iworm_acc, set<string>& iworm_left, 
	set<string>& iworm_right, string& prev_core_acc,
	map<pair<string, string>, int>& paired_iworm_contigs,
	error_checker& checker)
{
	try
	{
		validate_iworm(iworm_acc);

		pair<string, int> read = parse_read_name(read_acc);
		const string core_acc = read.first;
		int frag_end = read.second;

		if (core_acc != prev_core_acc)
		{
			examine_frags(iworm_left, iworm_right, paired_iworm_contigs);
			iworm_left.clear();
			iworm_right.clear();
		}

		if (frag_end == 0)
			iworm_left.insert(iworm_acc);
		if (frag_end == 1)
			iworm_right.insert(iworm_acc);

		prev_core_acc = core_acc;
	}
	catch (const bad_name_exception&)
	{
		checker.add_bad_name(iworm_acc, read_acc);
	}
	catch (const single_read_exception&)
	{
		checker.add_single_read(read_acc);
	}
}

int main(int argc, char* argv[])
{
	string usage = "\n\nusage: ScaffoldIwormContigs nameSorted.sam iworm_fasta_file\n\n";
	if (argc != 3)
	{
		cerr << usage;
		return -1;
	}

	string name_sorted_sam_file(argv[1]);
	map<string, int> iworm_acc_to_fasta_index;
	index_iworm(argv[2], iworm_acc_to_fasta_index);

	string prev_core_acc;
	map<pair<string,string>, int> paired_iworm_contigs;
	set<string> iworm_left;
	set<string> iworm_right;
	error_checker errors;

	samFile *in = sam_open(name_sorted_sam_file.c_str(), "r");
	bam_hdr_t *h = sam_hdr_read(in);
	bam1_t *b = bam_init1();

	while (sam_read1(in, h, b) > 0)
	{
		string read_acc(bam_get_qname(b));
		if (b->core.tid >= 0)
		{
			string iworm_acc(h->target_name[b->core.tid]);
			process_line(read_acc, iworm_acc, iworm_left, iworm_right, prev_core_acc, paired_iworm_contigs, errors);
		}
	}

	int r = sam_close(in);
	if (r < 0) {
		throw runtime_error("Error closing input");
	}

	// get last one
	examine_frags(iworm_left, iworm_right, paired_iworm_contigs);

	errors.report();

	copy_to_out(paired_iworm_contigs, iworm_acc_to_fasta_index);

	return 0;
}