annotate maf_stats.py @ 0:5a92f4c476b5 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
author iuc
date Thu, 13 Aug 2020 09:30:47 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
2 # Dan Blankenberg
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
3 """
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
4 Reads a list of intervals and a maf. Outputs a new set of intervals with statistics appended.
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
5 """
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
6 from __future__ import print_function
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
7
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
8 import sys
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
9
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
10 import bx.intervals.io
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
11 from bx.bitset import BitSet
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
12 from galaxy.tools.util import maf_utilities
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
13
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
14
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
15 def __main__():
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
16 maf_source_type = sys.argv.pop(1)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
17 input_maf_filename = sys.argv[1].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
18 input_interval_filename = sys.argv[2].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
19 output_filename = sys.argv[3].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
20 dbkey = sys.argv[4].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
21 try:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
22 chr_col = int(sys.argv[5].strip()) - 1
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
23 start_col = int(sys.argv[6].strip()) - 1
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
24 end_col = int(sys.argv[7].strip()) - 1
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
25 except Exception:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
26 print("You appear to be missing metadata. You can specify your metadata by clicking on the pencil icon associated with your interval file.", file=sys.stderr)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
27 sys.exit()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
28 summary = sys.argv[8].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
29 if summary.lower() == "true":
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
30 summary = True
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
31 else:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
32 summary = False
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
33
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
34 mafIndexFile = "%s/maf_index.loc" % sys.argv[9]
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
35 try:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
36 maf_index_filename = sys.argv[10].strip()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
37 except Exception:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
38 maf_index_filename = None
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
39 index = index_filename = None
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
40 if maf_source_type == "user":
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
41 # index maf for use here
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
42 index, index_filename = maf_utilities.open_or_build_maf_index(input_maf_filename, maf_index_filename, species=[dbkey])
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
43 if index is None:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
44 print("Your MAF file appears to be malformed.", file=sys.stderr)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
45 sys.exit()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
46 elif maf_source_type == "cached":
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
47 # access existing indexes
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
48 index = maf_utilities.maf_index_by_uid(input_maf_filename, mafIndexFile)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
49 if index is None:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
50 print("The MAF source specified (%s) appears to be invalid." % (input_maf_filename), file=sys.stderr)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
51 sys.exit()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
52 else:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
53 print('Invalid source type specified: %s' % maf_source_type, file=sys.stdout)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
54 sys.exit()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
55
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
56 out = open(output_filename, 'w')
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
57
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
58 num_region = None
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
59 num_bad_region = 0
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
60 species_summary = {}
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
61 total_length = 0
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
62 # loop through interval file
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
63 for num_region, region in enumerate(bx.intervals.io.NiceReaderWrapper(open(input_interval_filename, 'r'), chrom_col=chr_col, start_col=start_col, end_col=end_col, fix_strand=True, return_header=False, return_comments=False)):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
64 src = "%s.%s" % (dbkey, region.chrom)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
65 region_length = region.end - region.start
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
66 if region_length < 1:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
67 num_bad_region += 1
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
68 continue
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
69 total_length += region_length
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
70 coverage = {dbkey: BitSet(region_length)}
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
71
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
72 for block in index.get_as_iterator(src, region.start, region.end):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
73 for spec in maf_utilities.get_species_in_block(block):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
74 if spec not in coverage:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
75 coverage[spec] = BitSet(region_length)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
76 for block in maf_utilities.iter_blocks_split_by_species(block):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
77 if maf_utilities.component_overlaps_region(block.get_component_by_src(src), region):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
78 # need to chop and orient the block
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
79 block = maf_utilities.orient_block_by_region(maf_utilities.chop_block_by_region(block, src, region), src, region, force_strand='+')
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
80 start_offset, alignment = maf_utilities.reduce_block_by_primary_genome(block, dbkey, region.chrom, region.start)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
81 for i in range(len(alignment[dbkey])):
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
82 for spec, text in alignment.items():
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
83 if text[i] != '-':
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
84 coverage[spec].set(start_offset + i)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
85 if summary:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
86 # record summary
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
87 for key in coverage.keys():
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
88 if key not in species_summary:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
89 species_summary[key] = 0
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
90 species_summary[key] = species_summary[key] + coverage[key].count_range()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
91 else:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
92 # print coverage for interval
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
93 coverage_sum = coverage[dbkey].count_range()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
94 out.write("%s\t%s\t%s\t%s\n" % ("\t".join(region.fields), dbkey, coverage_sum, region_length - coverage_sum))
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
95 keys = list(coverage.keys())
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
96 keys.remove(dbkey)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
97 keys.sort()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
98 for key in keys:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
99 coverage_sum = coverage[key].count_range()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
100 out.write("%s\t%s\t%s\t%s\n" % ("\t".join(region.fields), key, coverage_sum, region_length - coverage_sum))
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
101 if summary:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
102 out.write("#species\tnucleotides\tcoverage\n")
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
103 for spec in species_summary:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
104 out.write("%s\t%s\t%.4f\n" % (spec, species_summary[spec], float(species_summary[spec]) / total_length))
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
105 out.close()
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
106 if num_region is not None:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
107 print("%i regions were processed with a total length of %i." % (num_region + 1, total_length))
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
108 if num_bad_region:
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
109 print("%i regions were invalid." % (num_bad_region))
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
110 maf_utilities.remove_temp_index_file(index_filename)
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
111
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
112
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
113 if __name__ == "__main__":
5a92f4c476b5 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/interval2maf/ commit f7b04f8c6edca90b1e9bc867a1569996aec76d7a"
iuc
parents:
diff changeset
114 __main__()