annotate tools/extract/extract_genomic_dna.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 usage: %prog $input $out_file1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 -1, --cols=N,N,N,N: Columns for start, end, strand in input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 -d, --dbkey=N: Genome build of input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 -o, --output_format=N: the data type of the output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -g, --GALAXY_DATA_INDEX_DIR=N: the directory containing alignseq.loc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -I, --interpret_features: if true, complete features are interpreted when input is GFF
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -F, --fasta=<genomic_sequences>: genomic sequences to use for extraction
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -G, --gff: input and output file, when it is interval, coordinates are treated as GFF format (1-based, half-open) rather than 'traditional' 0-based, closed format.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 import sys, string, os, re, tempfile, subprocess
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 from bx.intervals.io import Header, Comment
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 import bx.seq.nib
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 import bx.seq.twobit
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 from galaxy.datatypes.util import gff_util
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 def reverse_complement( s ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 complement_dna = {"A":"T", "T":"A", "C":"G", "G":"C", "a":"t", "t":"a", "c":"g", "g":"c", "N":"N", "n":"n" }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 reversed_s = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 for i in s:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 reversed_s.append( complement_dna[i] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 reversed_s.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 return "".join( reversed_s )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 def check_seq_file( dbkey, GALAXY_DATA_INDEX_DIR ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 seq_file = "%s/alignseq.loc" % GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 seq_path = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 for line in open( seq_file ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if line and not line.startswith( "#" ) and line.startswith( 'seq' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 if len( fields ) < 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 if fields[1] == dbkey:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 seq_path = fields[2].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 return seq_path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 # Parse options, args.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 chrom_col, start_col, end_col, strand_col = parse_cols_arg( options.cols )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 dbkey = options.dbkey
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 output_format = options.output_format
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 gff_format = options.gff
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 interpret_features = options.interpret_features
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 GALAXY_DATA_INDEX_DIR = options.GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 fasta_file = options.fasta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 input_filename, output_filename = args
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 doc_optparse.exception()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 includes_strand_col = strand_col >= 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 strand = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 nibs = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 twobits = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 # Set path to sequence data.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if fasta_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 # Need to create 2bit file from fasta file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 seq_path = tempfile.NamedTemporaryFile( dir="." ).name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 cmd = "faToTwoBit %s %s" % ( fasta_file, seq_path )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 tmp_name = tempfile.NamedTemporaryFile( dir="." ).name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 tmp_stderr = open( tmp_name, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 returncode = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 # Get stderr, allowing for case where it's very large.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 tmp_stderr = open( tmp_name, 'rb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 stderr = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 buffsize = 1048576
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 stderr += tmp_stderr.read( buffsize )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if not stderr or len( stderr ) % buffsize != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 except OverflowError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 tmp_stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 # Error checking.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 if returncode != 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 raise Exception, stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 stop_err( 'Error running faToTwoBit. ' + str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 seq_path = check_seq_file( dbkey, GALAXY_DATA_INDEX_DIR )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if not os.path.exists( seq_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 # If this occurs, we need to fix the metadata validator.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 stop_err( "No sequences are available for '%s', request them by reporting this error." % dbkey )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 # Fetch sequences.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 # Get feature's line(s).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 def get_lines( feature ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 if isinstance( feature, gff_util.GFFFeature ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 return feature.lines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 return [ feature.rstrip( '\r\n' ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 invalid_lines = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 fout = open( output_filename, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 warnings = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 warning = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 twobitfile = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 file_iterator = open( input_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 if gff_format and interpret_features:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 file_iterator = gff_util.GFFReaderWrapper( file_iterator, fix_strand=False )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 line_count = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 for feature in file_iterator:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 # Ignore comments, headers.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 if isinstance( feature, ( Header, Comment ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 line_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if gff_format and interpret_features:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 # Processing features.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 gff_util.convert_gff_coords_to_bed( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 chrom = feature.chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 start = feature.start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 end = feature.end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 strand = feature.strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 # Processing lines, either interval or GFF format.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 line = feature.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 if line and not line.startswith( "#" ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 chrom = fields[chrom_col]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 start = int( fields[start_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 end = int( fields[end_col] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 if gff_format:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 start, end = gff_util.convert_gff_coords_to_bed( [start, end] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 if includes_strand_col:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 strand = fields[strand_col]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 warning = "Invalid chrom, start or end column values. "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 if start > end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 warning = "Invalid interval, start '%d' > end '%d'. " % ( start, end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 if strand not in ['+', '-']:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 strand = '+'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 sequence = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 # Open sequence file and get sequence for feature/interval.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 if seq_path and os.path.exists( "%s/%s.nib" % ( seq_path, chrom ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 # TODO: improve support for GFF-nib interaction.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 if chrom in nibs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 nib = nibs[chrom]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 nibs[chrom] = nib = bx.seq.nib.NibFile( file( "%s/%s.nib" % ( seq_path, chrom ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 sequence = nib.get( start, end-start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 warning = "Unable to fetch the sequence from '%d' to '%d' for build '%s'. " %( start, end-start, dbkey )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 elif seq_path and os.path.isfile( seq_path ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 if not(twobitfile):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 twobitfile = bx.seq.twobit.TwoBitFile( file( seq_path ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 if options.gff and interpret_features:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 # Create sequence from intervals within a feature.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 sequence = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 for interval in feature.intervals:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 sequence += twobitfile[interval.chrom][interval.start:interval.end]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 sequence = twobitfile[chrom][start:end]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 warning = "Unable to fetch the sequence from '%d' to '%d' for chrom '%s'. " %( start, end-start, chrom )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 warning = "Chromosome by name '%s' was not found for build '%s'. " % ( chrom, dbkey )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 if sequence == '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 warning = "Chrom: '%s', start: '%s', end: '%s' is either invalid or not present in build '%s'. " \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 % ( chrom, start, end, dbkey )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 if not invalid_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 invalid_lines = get_lines( feature )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 skipped_lines += len( invalid_lines )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 if includes_strand_col and strand == "-":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 sequence = reverse_complement( sequence )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 if output_format == "fasta" :
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 l = len( sequence )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 c = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 if gff_format:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 start, end = gff_util.convert_bed_coords_to_gff( [ start, end ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 fields = [dbkey, str( chrom ), str( start ), str( end ), strand]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 meta_data = "_".join( fields )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 fout.write( ">%s\n" % meta_data )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 while c < l:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 b = min( c + 50, l )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 fout.write( "%s\n" % str( sequence[c:b] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 c = b
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 else: # output_format == "interval"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 if gff_format and interpret_features:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 # TODO: need better GFF Reader to capture all information needed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 # to produce this line.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 meta_data = "\t".join(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 [feature.chrom, "galaxy_extract_genomic_dna", "interval", \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 str( feature.start ), str( feature.end ), feature.score, feature.strand,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 ".", gff_util.gff_attributes_to_str( feature.attributes, "GTF" ) ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 meta_data = "\t".join( fields )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 if gff_format:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 format_str = "%s seq \"%s\";\n"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 format_str = "%s\t%s\n"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 fout.write( format_str % ( meta_data, str( sequence ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 # Update line count.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 if isinstance( feature, gff_util.GFFFeature ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 line_count += len( feature.intervals )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 line_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 fout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 if warnings:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 warn_msg = "%d warnings, 1st is: " % len( warnings )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 warn_msg += warnings[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 print warn_msg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 if skipped_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 # Error message includes up to the first 10 skipped lines.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 print 'Skipped %d invalid lines, 1st is #%d, "%s"' % ( skipped_lines, first_invalid_line, '\n'.join( invalid_lines[:10] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 if __name__ == "__main__": __main__()