annotate tools/maf/interval_maf_to_merged_fasta.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Reads an interval or gene BED and a MAF Source.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 Produces a FASTA file containing the aligned intervals/gene sequences, based upon the provided coordinates
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 Alignment blocks are layered ontop of each other based upon score.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 usage: %prog maf_file [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -d, --dbkey=d: Database key, ie hg17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -c, --chromCol=c: Column of Chr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -s, --startCol=s: Column of Start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -e, --endCol=e: Column of End
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 -S, --strandCol=S: Column of Strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 -G, --geneBED: Input is a Gene BED file, process and join exons as one region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 -t, --mafSourceType=t: Type of MAF source to use
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 -m, --mafSource=m: Path of source MAF file, if not using cached version
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 -I, --mafIndex=I: Path of precomputed source MAF file index, if not using cached version
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 -i, --interval_file=i: Input interval file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 -o, --output_file=o: Output MAF file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 -p, --species=p: Species to include in output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 -O, --overwrite_with_gaps=O: Overwrite bases found in a lower-scoring block with gaps interior to the sequence for a species.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 -z, --mafIndexFileDir=z: Directory of local maf_index.loc file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 usage: %prog dbkey_of_BED comma_separated_list_of_additional_dbkeys_to_extract comma_separated_list_of_indexed_maf_files input_gene_bed_file output_fasta_file cached|user GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 from galaxy.tools.util import maf_utilities
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 import bx.intervals.io
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 mincols = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 strand_col = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if options.dbkey:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 primary_species = options.dbkey
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 primary_species = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 if primary_species in [None, "?", "None"]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 stop_err( "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." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 include_primary = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 secondary_species = maf_utilities.parse_species_option( options.species )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if secondary_species:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 species = list( secondary_species ) # make copy of species list
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if primary_species in secondary_species:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 secondary_species.remove( primary_species )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 include_primary = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 species = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if options.interval_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 interval_file = options.interval_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 stop_err( "Input interval file has not been specified." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if options.output_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 output_file = options.output_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 stop_err( "Output file has not been specified." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if not options.geneBED:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if options.chromCol:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 chr_col = int( options.chromCol ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 stop_err( "Chromosome column not set, click the pencil icon in the history item to set the metadata attributes." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if options.startCol:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 start_col = int( options.startCol ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 stop_err( "Start column not set, click the pencil icon in the history item to set the metadata attributes." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 if options.endCol:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 end_col = int( options.endCol ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 stop_err( "End column not set, click the pencil icon in the history item to set the metadata attributes." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 if options.strandCol:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 strand_col = int( options.strandCol ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 mafIndexFile = "%s/maf_index.loc" % options.mafIndexFileDir
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 overwrite_with_gaps = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 if options.overwrite_with_gaps and options.overwrite_with_gaps.lower() == 'false':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 overwrite_with_gaps = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 #Finish parsing command line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 #get index for mafs based on type
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 index = index_filename = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 #using specified uid for locally cached
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 if options.mafSourceType.lower() in ["cached"]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 index = maf_utilities.maf_index_by_uid( options.mafSource, mafIndexFile )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 if index is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 stop_err( "The MAF source specified (%s) appears to be invalid." % ( options.mafSource ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 elif options.mafSourceType.lower() in ["user"]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 #index maf for use here, need to remove index_file when finished
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 index, index_filename = maf_utilities.open_or_build_maf_index( options.mafSource, options.mafIndex, species = [primary_species] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 if index is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 stop_err( "Your MAF file appears to be malformed." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 stop_err( "Invalid MAF source type specified." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 #open output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 output = open( output_file, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 if options.geneBED:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 region_enumerator = maf_utilities.line_enumerator( open( interval_file, "r" ).readlines() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 region_enumerator = enumerate( bx.intervals.io.NiceReaderWrapper( open( interval_file, 'r' ), chrom_col = chr_col, start_col = start_col, end_col = end_col, strand_col = strand_col, fix_strand = True, return_header = False, return_comments = False ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 #Step through intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 regions_extracted = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 line_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 for line_count, line in region_enumerator:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if options.geneBED: #Process as Gene BED
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 starts, ends, fields = maf_utilities.get_starts_ends_fields_from_gene_bed( line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 #create spliced alignment object
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 alignment = maf_utilities.get_spliced_region_alignment( index, primary_species, fields[0], starts, ends, strand = '+', species = species, mincols = mincols, overwrite_with_gaps = overwrite_with_gaps )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 primary_name = secondary_name = fields[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 alignment_strand = fields[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 print "Error loading exon positions from input line %i: %s" % ( line_count, e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 else: #Process as standard intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 #create spliced alignment object
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 alignment = maf_utilities.get_region_alignment( index, primary_species, line.chrom, line.start, line.end, strand = '+', species = species, mincols = mincols, overwrite_with_gaps = overwrite_with_gaps )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 primary_name = "%s(%s):%s-%s" % ( line.chrom, line.strand, line.start, line.end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 secondary_name = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 alignment_strand = line.strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 print "Error loading region positions from input line %i: %s" % ( line_count, e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 #Write alignment to output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 #Output primary species first, if requested
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 if include_primary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 output.write( ">%s.%s\n" %( primary_species, primary_name ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 if alignment_strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 output.write( alignment.get_sequence_reverse_complement( primary_species ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 output.write( alignment.get_sequence( primary_species ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 output.write( "\n" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 #Output all remainging species
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 for spec in secondary_species or alignment.get_species_names( skip = primary_species ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 if secondary_name:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 output.write( ">%s.%s\n" % ( spec, secondary_name ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 output.write( ">%s\n" % ( spec ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 if alignment_strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 output.write( alignment.get_sequence_reverse_complement( spec ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 output.write( alignment.get_sequence( spec ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 output.write( "\n" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 output.write( "\n" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 regions_extracted += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 print "Unexpected error from input line %i: %s" % ( line_count, e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 #close output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 output.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 #remove index file if created during run
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 maf_utilities.remove_temp_index_file( index_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 #Print message about success for user
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 if regions_extracted > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 print "%i regions were processed successfully." % ( regions_extracted )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 print "No regions were processed successfully."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 if line_count > 0 and options.geneBED:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 print "This tool requires your input file to conform to the 12 column BED standard."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 if __name__ == "__main__": __main__()