-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpair.py
More file actions
70 lines (61 loc) · 1.95 KB
/
Copy pathpair.py
File metadata and controls
70 lines (61 loc) · 1.95 KB
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
import sys
import subprocess
import cStringIO
import copy
def setup_pairing(f, group):
# encode sam output as paired end
# using only adjacent mappings as pairs
id = group[0][0].replace("/ccs", "")
for i in range(len(group)-1):
j = i+1
c1 = copy.copy(group[i])
c2 = copy.copy(group[j])
c1[1] = str(int(c1[1]) | 0x1 | 0x2 | 0x40)
c2[1] = str(int(c2[1]) | 0x1 | 0x2 | 0x80)
c1[0] = id
c2[0] = id
c1[6] = '='
c2[6] = '='
c1[7] = c2[3]
c2[7] = c1[3]
print >>f, '\t'.join(c1)
print >>f, '\t'.join(c2)
def run(input_filename, output_filename, genomic_coordinates):
target_chrom, coord_range = genomic_coordinates.split(':')
coords = coord_range.split('-')
target_start = int(coords[0].replace(',', ''))
target_end = int(coords[1].replace(',', ''))
print target_chrom, target_start, target_end
f = open(output_filename, 'w')
# copy header
for line in open(input_filename):
if line.startswith('@'):
print >>f, line,
else:
break
# sort by read id
# sort -k1,1 -k14.7n all.sam
p = subprocess.Popen(["sort", "-k1,1", "-k14.7n", input_filename], stdout = subprocess.PIPE)
fh = cStringIO.StringIO(p.communicate()[0])
last_id = None
group = []
for line in fh:
if line.startswith('@'): continue
cols = line.strip().split()
# use ontarget alignments only
chrom = cols[2]
pos = int(cols[3])
if chrom != target_chrom or pos < target_start or pos > target_end: continue
id = cols[0]
if id != last_id:
if len(group) > 1:
setup_pairing(f, group)
elif len(group) == 1:
print >>f, '\t'.join(group[0])
group = [cols]
last_id = id
else:
group.append(cols)
f.close()
if __name__ == '__main__':
run(*sys.argv[1:])