-
Notifications
You must be signed in to change notification settings - Fork 47
/
make_links.py
executable file
·180 lines (158 loc) · 7.01 KB
/
make_links.py
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
#!/usr/bin/env python2
import operator
import math
import sys
import argparse
contig_lengths = {}
RF_counts_left = {}
RF_counts_right = {}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-b","--mapping", help="mapping of read to contigs in bed format")
parser.add_argument("-i","--iteration", help="iteration number")
parser.add_argument("-d",'--directory',help="output directory")
parser.add_argument("-x","--dup",help="Duplicated links")
args = parser.parse_args()
iteration = str(args.iteration)
dup_links = {}
if args.dup != 'abc' and iteration == 1:
with open(args.dup,'r') as f:
for line in f:
attrs = line.split()
dup_links[attrs[0]+'$'+attrs[1]] = 1
dup_links[attrs[1]+'$'+attrs[0]] = 1
avoid_links = {}
if int(iteration) > 1:
with open(args.directory+'/links_avoid_iteration_'+str(iteration),'r') as f:
for line in f:
attrs = line.split()
avoid_links[attrs[0]+'$'+attrs[1]] = 1
avoid_links[attrs[1]+'$'+attrs[0]] = 1
with open(args.directory+'/scaffold_length_iteration_'+str(iteration),'r') as cfile:
lines = cfile.readlines()
for line in lines:
attrs = line.split()
contig_lengths[attrs[0]] = float(attrs[1])
with open(args.directory+'/re_counts_iteration_'+str(iteration),'r') as f:
#lines = f.readlines()
for line in f:
attrs = line.split()
RF_counts_left[attrs[0]] = int(attrs[1])
RF_counts_right[attrs[0]] = int(attrs[2])
contig_links = {}
exp_links = {}
norm_score = {}
all_counts = {}
coordinates_map = {}
first_in_pair = {}
second_in_pair = {}
print >> sys.stderr, 'bedfile started'
prev_line = ''
with open(args.mapping,'r') as f:
for line in f:
attrs = line.split()
# if len(attrs) < 6:
# continue
# try:
if prev_line == '':
prev_line = line
continue
prev_attrs = prev_line.split()
if prev_attrs[0] != attrs[0]:
ks = prev_attrs[0] + '$' + attrs[0]
if ks in dup_links or ks in avoid_links:
continue
prev_read = prev_attrs[3]
curr_read = attrs[3]
#print prev_read.split('/')[0]
if prev_read.split('/')[0] == curr_read.split('/')[0]:
#print 'here'
pos1 = (long(prev_attrs[1]) + long(prev_attrs[2]))/2.0
pos2 = (long(attrs[1]) + long(attrs[2]))/2.0
# l1 = 300000.0
# l2 = 300000.0
#print pos1, pos2
key = ''
#print key
first = ''
second = ''
if prev_attrs[0] < attrs[0]:
first = prev_attrs[0]
second = attrs[0]
else:
first = attrs[0]
second = prev_attrs[0]
pos1,pos2 = pos2, pos1
t_key = first+'$'+second
if t_key not in all_counts:
all_counts[t_key] = 0
all_counts[t_key] += 1
len1 = contig_lengths[first]
len2 = contig_lengths[second]
#print len1,len2
l1 = len1/2
l2 = len2/2
r1 = l1 / len1
r2 = l2 / len2
if pos1 <= l1 and pos2 <= l2:
#print 'here1'
key = first + ":B$" + second + ':B'
#key1 = rec2[0] + ":B$" + rec1[0] + ':B'
#norm_score[key] = RF_counts_left[first]*r1 + RF_counts_left[second]*r2
norm_score[key] = RF_counts_left[first]*r1 + RF_counts_left[second]*r2
#norm_score[key1] = RF_counts_left[rec1[0]] + RF_counts_left[rec2[0]]
if pos1 <= l1 and pos2 > len2 - l2:
#print 'here2'
key = first + ":B$" + second + ':E'
#key1 = rec2[0] + ":E$" + rec1[0] + ':B'
#norm_score[key] = RF_counts_left[first]*r1 + RF_counts_right[second]*r2
norm_score[key] = RF_counts_left[first]*r1 + RF_counts_right[second]*r2
#norm_score[key1] = RF_counts_left[rec1[0]] + RF_counts_left[rec2[0]]
#exp_links[key] = exp_num
if pos1 > len1 - l1 and pos2 <= l2:
#print 'here3'
key = first + ":E$" + second + ':B'
#print key
#key1 = rec2[0] + ":B$" + rec1[0] + ':E'
#norm_score[key] = RF_counts_right[first]*r1 + RF_counts_left[second]*r2
norm_score[key] = RF_counts_right[first]*r1 + RF_counts_left[second]*r2
#norm_score[key1] = RF_counts_left[rec1[0]] + RF_counts_left[rec2[0]]
#exp_links[key] = exp_num
if pos1 > len1 - l1 and pos2 > len2 - l2:
#print 'here4'
key = first + ":E$" + second + ':E'
#key1 = rec2[0] + ":E$" + rec1[0] + ':E'
#norm_score[key] = RF_counts_right[first]*r1 + RF_counts_right[second]*r2
norm_score[key] = RF_counts_right[first]*r1 + RF_counts_right[second]*r2
#norm_score[key1] = RF_counts_left[rec1[0]] + RF_counts_left[rec2[0]]
#exp_links[key] = exp_num
if key not in contig_links and key != '':
contig_links[key] = 0
if key != '':
#print 'adding'
contig_links[key] += 1
prev_line = line
#print >> sys.stderr, len(first_in_pair)
# except:
# print 'here'
# continue
print >> sys.stderr, 'bedfile loaded'
print len(contig_links)
ofile = open(args.directory+'/contig_links_iteration_'+str(iteration),'w')
for key in contig_links:
if key != '':
edge = key.split('$')
#rf_sum = RF_counts[edge[0].split(':')[0]] + RF_counts[edge[1].split(':')[0]]
if norm_score[key] == 0:
score = 0
else:
score = contig_links[key]*1.0/norm_score[key]
ofile.write(edge[0]+'\t'+edge[1]+'\t'+str(score)+'\t'+str(contig_links[key])+'\n')
#write all count links
ofile.close()
# ofile = open(args.directory+'/contig_links_raw_iteration_1','w')
# for key in all_counts:
# contigs = key.split('$')
# ofile.write(contigs[0]+'\t'+contigs[1]+'\t'+str(all_counts[key])+'\n')
if __name__ == '__main__':
main()