-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_maf.py
More file actions
214 lines (196 loc) · 8.05 KB
/
read_maf.py
File metadata and controls
214 lines (196 loc) · 8.05 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
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
# -*- coding: utf-8 -*-
# Copyright 2013 by Petra Kubincova
import array
import sys
import seq_structures as seq
import bgzf_tool as bgzftool
# Correctly adds genome name 'name' to 'genome_map' if it wasn't already added
def add_genome(genome_map, chr_maps, name):
if not name[0] in genome_map:
genome_map[name[0]] = len(genome_map)
while genome_map[name[0]] >= len(chr_maps):
chr_maps.append({})
# Parses line 'line' from file of .maf format
# Returns Reference structure or id of informant and Informant structure
def parse_line(line, genome_map, chr_maps):
# Split the line into non-whitespace parts
parts = [x for x in line.split(' ') if x != ""]
name = parts[1].split('.')
# Complete genome_map and chr_maps
if len(name) == 1:
name.append('?')
elif len(name) > 2:
name[1] = ".".join(name[1:])
add_genome(genome_map, chr_maps, name)
if not name[1] in chr_maps[genome_map[name[0]]]:
chr_maps[genome_map[name[0]]][name[1]] =\
(len(chr_maps[genome_map[name[0]]]), int(parts[5]))
# Extract position, parse sequence
position = int(parts[2])
plus_strand = True
if parts[4] == '-':
plus_strand = False
# Conversion of position for '-' strand: not needed
#position = int(parts[5]) - position - int(parts[3])
sequence = array.array('c')
counter = 0
for letter in parts[6]:
if letter == '-':
sequence.append('0')
else:
sequence.append('1')
counter += 1
# Return reference/informant data
if genome_map[name[0]] == 0:
return seq.Reference(sequence.tostring(),
chr_maps[genome_map[name[0]]][name[1]][0], position, plus_strand,
counter)
else:
return genome_map[name[0]], seq.Informant(sequence.tostring(),
chr_maps[genome_map[name[0]]][name[1]][0], position, plus_strand,
counter, 0)
# Parses block of alignment
# Returns Reference structure from this block
def parse_block(block_lines, genome_map, chr_maps):
# If there is something to parse, parse it
if len(block_lines) <= 0:
raise BaseException(("no block lines"))
reference = parse_line(block_lines[0], genome_map, chr_maps)
for i in range(1,len(block_lines)):
inf_id, informant = parse_line(block_lines[i], genome_map, chr_maps)
reference.add_informant(inf_id, informant)
return reference
def write_bin_block(writer, block, ref_chrs, current_chr):
block.finalize()
index_items = []
for reference in block.ref_blocks:
index_items.append(seq.IndexItem(reference))
if reference.chr_id != current_chr:
current_chr = reference.chr_id
ref_chrs[current_chr] = 1
else:
ref_chrs[current_chr] += 1
bgzf_starts = bgzftool.write_bgzf_block(writer, block)
return current_chr, index_items, bgzf_starts
# Parses .maf file, reads it into BinBlock structures
# Returns list of BinBlock structures
def read(filename, genome_map, chr_maps, ref_chrs, writer):
block_lines = []
last_bin = current_bin = seq.BinBlock(0)
next_block = False
current_chr = -1
to_index = []
for line in open(filename, 'r'):
# In case of Windows-like ends of lines:
line = line.strip() + "\n"
# Encountered irrelevant line, skip it
if not (line == "\n" or line[0] == 'a' or line[0] == 's'):
continue
# Encountered next block of alignment
if line[0] == 'a':
next_block = True
# Read next line of alignment block, add it to block_lines
elif line[0] == 's' and next_block:
block_lines.append(line.strip())
# Encountered end of alignment block, add it to bin
elif line == '\n':
next_block = False
if len(block_lines) > 0:
current_bin = current_bin.add(parse_block(block_lines,
genome_map, chr_maps))
# If a new bin following the old bin was created
if last_bin.idn < current_bin.idn:
current_chr, index_items, bgzf_starts =\
write_bin_block(writer, last_bin, ref_chrs, current_chr)
to_index.append((index_items, bgzf_starts))
last_bin = current_bin
# Clear list of lines
del block_lines[:]
# Add to bin last alignment block in file
if len(block_lines) > 0:
current_bin = current_bin.add(parse_block(block_lines,
genome_map, chr_maps))
current_chr, index_items, bgzf_starts =\
write_bin_block(writer, last_bin, ref_chrs, current_chr)
to_index.append((index_items, bgzf_starts))
if last_bin.idn != current_bin.idn:
current_chr, index_items, bgzf_starts =\
write_bin_block(writer, current_bin, ref_chrs, current_chr)
to_index.append((index_items, bgzf_starts))
bgzftool.close_writer(writer)
return to_index
# Writes header information from 'genome_map' and 'chr_maps' to 'filestream'
def write_header(filestream, genome_map, chr_maps, ref_chrs, to_index):
data = array.array("B")
# Number of genomes
seq.write_bytes_to(data, len(genome_map), seq.INF_NUM_SIZE)
# Genome map
for name in genome_map:
# Size of name
seq.write_bytes_to(data, len(name), seq.NAME_SIZE_SIZE)
# Name itself
for letter in name:
data.append(ord(letter))
# Genome id
seq.write_bytes_to(data, genome_map[name], seq.INF_ID_SIZE)
# Chromosome maps:
for chr_map in chr_maps:
# Number of chromosomes in this maps
seq.write_bytes_to(data, len(chr_map), seq.CHR_ID_SIZE)
for name in chr_map:
# Size of name
seq.write_bytes_to(data, len(name), seq.NAME_SIZE_SIZE)
# Name itself
for letter in name:
data.append(ord(letter))
# Chromosome id
seq.write_bytes_to(data, chr_map[name][0], seq.CHR_ID_SIZE)
# Chromosome size
seq.write_bytes_to(data, chr_map[name][1], seq.LEN_SIZE)
# Index
current_chr = -1
for j in range(len(to_index)):
for i in range(len(to_index[j][1])):
if to_index[j][0][i].chr_id != current_chr:
current_chr = to_index[j][0][i].chr_id
# Chromosome id and number of references within this chromosome
seq.write_bytes_to(data, current_chr, seq.CHR_ID_SIZE)
seq.write_bytes_to(data, ref_chrs[current_chr],
seq.REF_COUNT_SIZE)
# Strand
seq.write_bytes_to(data, to_index[j][0][i].plus_strand,
seq.STRAND_SIZE)
# Position of this reference in chromosome
seq.write_bytes_to(data, abs(to_index[j][0][i].chr_pos),
seq.POS_SIZE)
# Bases count
seq.write_bytes_to(data, to_index[j][0][i].bases_count,
seq.POS_SIZE)
# Pointer
seq.write_bytes_to(data, to_index[j][1][i],
seq.FILE_OFFSET_SIZE)
data.tofile(filestream)
if filestream != sys.stdout:
filestream.close()
# Executes all the reading and parsing and writing using 'arguments'
# (as from command line)
def do_all_the_magic(arguments):
src_file = arguments[1]
bgzf_file = arguments[2]
header_file = arguments[3]
genome_map = {}
chr_maps = []
ref_chrs = {}
writer = bgzftool.get_writer(bgzf_file)
to_index = read(src_file, genome_map, chr_maps, ref_chrs, writer)
print "Successfully read", src_file
print "Compressed data were written into", bgzf_file
write_header(open(header_file, "wb"), genome_map, chr_maps, ref_chrs,
to_index)
print "Binary representation of header was written into", header_file
if __name__ == "__main__":
if len(sys.argv) < 4:
print "Usage: python {0} <from.maf> <to.bgzf> <header.bin>".\
format(sys.argv[0])
sys.exit(0)
do_all_the_magic(sys.argv)