annotate interval2maf.py @ 0:d38f317fa9d4 draft default tip

Uploaded
author dave
date Fri, 10 Jul 2020 16:05:40 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
1 #!/usr/bin/env python
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
2 """
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
3 Reads a list of intervals and a maf. Produces a new maf containing the
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
4 blocks or parts of blocks in the original that overlapped the intervals.
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
5
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
6 If a MAF file, not UID, is provided the MAF file is indexed before being processed.
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
7
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
8 NOTE: If two intervals overlap the same block it will be written twice.
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
9
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
10 usage: %prog maf_file [options]
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
11 -d, --dbkey=d: Database key, ie hg17
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
12 -c, --chromCol=c: Column of Chr
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
13 -s, --startCol=s: Column of Start
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
14 -e, --endCol=e: Column of End
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
15 -S, --strandCol=S: Column of Strand
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
16 -t, --mafType=t: Type of MAF source to use
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
17 -m, --mafFile=m: Path of source MAF file, if not using cached version
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
18 -I, --mafIndex=I: Path of precomputed source MAF file index, if not using cached version
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
19 -i, --interval_file=i: Input interval file
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
20 -o, --output_file=o: Output MAF file
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
21 -p, --species=p: Species to include in output
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
22 -P, --split_blocks_by_species=P: Split blocks by species
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
23 -r, --remove_all_gap_columns=r: Remove all Gap columns
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
24 -l, --indexLocation=l: Override default maf_index.loc file
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
25 -z, --mafIndexFile=z: Directory of local maf index file ( maf_index.loc or maf_pairwise.loc )
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
26 """
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
27 # Dan Blankenberg
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
28 from __future__ import print_function
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
29
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
30 import bx.align.maf
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
31 import bx.intervals.io
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
32 from bx.cookbook import doc_optparse
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
33
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
34 from galaxy.tools.util import maf_utilities
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
35
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
36
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
37 def __main__():
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
38 index = index_filename = None
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
39
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
40 # Parse Command Line
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
41 options, args = doc_optparse.parse(__doc__)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
42
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
43 if options.dbkey:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
44 dbkey = options.dbkey
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
45 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
46 dbkey = None
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
47 if dbkey in [None, "?"]:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
48 maf_utilities.tool_fail("You must specify a proper build in order to extract alignments. You can specify your genome build by clicking on the pencil icon associated with your interval file.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
49
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
50 species = maf_utilities.parse_species_option(options.species)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
51
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
52 if options.chromCol:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
53 chromCol = int(options.chromCol) - 1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
54 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
55 maf_utilities.tool_fail("Chromosome column not set, click the pencil icon in the history item to set the metadata attributes.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
56
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
57 if options.startCol:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
58 startCol = int(options.startCol) - 1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
59 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
60 maf_utilities.tool_fail("Start column not set, click the pencil icon in the history item to set the metadata attributes.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
61
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
62 if options.endCol:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
63 endCol = int(options.endCol) - 1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
64 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
65 maf_utilities.tool_fail("End column not set, click the pencil icon in the history item to set the metadata attributes.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
66
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
67 if options.strandCol:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
68 strandCol = int(options.strandCol) - 1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
69 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
70 strandCol = -1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
71
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
72 if options.interval_file:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
73 interval_file = options.interval_file
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
74 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
75 maf_utilities.tool_fail("Input interval file has not been specified.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
76
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
77 if options.output_file:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
78 output_file = options.output_file
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
79 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
80 maf_utilities.tool_fail("Output file has not been specified.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
81
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
82 split_blocks_by_species = remove_all_gap_columns = False
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
83 if options.split_blocks_by_species and options.split_blocks_by_species == 'split_blocks_by_species':
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
84 split_blocks_by_species = True
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
85 if options.remove_all_gap_columns and options.remove_all_gap_columns == 'remove_all_gap_columns':
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
86 remove_all_gap_columns = True
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
87 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
88 remove_all_gap_columns = True
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
89 # Finish parsing command line
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
90
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
91 # Open indexed access to MAFs
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
92 if options.mafType:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
93 if options.indexLocation:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
94 index = maf_utilities.maf_index_by_uid(options.mafType, options.indexLocation)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
95 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
96 index = maf_utilities.maf_index_by_uid(options.mafType, options.mafIndexFile)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
97 if index is None:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
98 maf_utilities.tool_fail("The MAF source specified (%s) appears to be invalid." % (options.mafType))
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
99 elif options.mafFile:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
100 index, index_filename = maf_utilities.open_or_build_maf_index(options.mafFile, options.mafIndex, species=[dbkey])
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
101 if index is None:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
102 maf_utilities.tool_fail("Your MAF file appears to be malformed.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
103 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
104 maf_utilities.tool_fail("Desired source MAF type has not been specified.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
105
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
106 # Create MAF writter
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
107 out = bx.align.maf.Writer(open(output_file, "w"))
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
108
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
109 # Iterate over input regions
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
110 num_blocks = 0
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
111 num_regions = None
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
112 for num_regions, region in enumerate(bx.intervals.io.NiceReaderWrapper(open(interval_file, 'r'), chrom_col=chromCol, start_col=startCol, end_col=endCol, strand_col=strandCol, fix_strand=True, return_header=False, return_comments=False)):
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
113 src = maf_utilities.src_merge(dbkey, region.chrom)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
114 for block in index.get_as_iterator(src, region.start, region.end):
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
115 if split_blocks_by_species:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
116 blocks = [new_block for new_block in maf_utilities.iter_blocks_split_by_species(block) if maf_utilities.component_overlaps_region(new_block.get_component_by_src_start(dbkey), region)]
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
117 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
118 blocks = [block]
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
119 for block in blocks:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
120 block = maf_utilities.chop_block_by_region(block, src, region)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
121 if block is not None:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
122 if species is not None:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
123 block = block.limit_to_species(species)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
124 block = maf_utilities.orient_block_by_region(block, src, region)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
125 if remove_all_gap_columns:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
126 block.remove_all_gap_columns()
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
127 out.write(block)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
128 num_blocks += 1
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
129
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
130 # Close output MAF
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
131 out.close()
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
132
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
133 # remove index file if created during run
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
134 maf_utilities.remove_temp_index_file(index_filename)
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
135
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
136 if num_blocks:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
137 print("%i MAF blocks extracted for %i regions." % (num_blocks, (num_regions + 1)))
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
138 elif num_regions is not None:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
139 print("No MAF blocks could be extracted for %i regions." % (num_regions + 1))
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
140 else:
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
141 print("No valid regions have been provided.")
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
142
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
143
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
144 if __name__ == "__main__":
d38f317fa9d4 Uploaded
dave
parents:
diff changeset
145 __main__()