annotate utils/gff_util.py @ 5:0d97b11ed3d5 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author devteam
date Thu, 22 Jun 2017 18:51:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
1 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
2 Provides utilities for working with GFF files.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
3 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
4 import copy
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
5
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
6 from bx.intervals.io import GenomicInterval, GenomicIntervalReader, MissingFieldError, NiceReaderWrapper
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
7 from bx.tabular.io import Comment, Header, ParseError
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
8
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
9 from .odict import odict
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
10
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
11
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
12 class GFFInterval( GenomicInterval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
13 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
14 A GFF interval, including attributes. If file is strictly a GFF file,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
15 only attribute is 'group.'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
16 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
17 def __init__( self, reader, fields, chrom_col=0, feature_col=2, start_col=3, end_col=4,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
18 strand_col=6, score_col=5, default_strand='.', fix_strand=False ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
19 # HACK: GFF format allows '.' for strand but GenomicInterval does not. To get around this,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
20 # temporarily set strand and then unset after initing GenomicInterval.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
21 unknown_strand = False
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
22 if not fix_strand and fields[ strand_col ] == '.':
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
23 unknown_strand = True
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
24 fields[ strand_col ] = '+'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
25 GenomicInterval.__init__( self, reader, fields, chrom_col, start_col, end_col, strand_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
26 default_strand, fix_strand=fix_strand )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
27 if unknown_strand:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
28 self.strand = '.'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
29 self.fields[ strand_col ] = '.'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
30
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
31 # Handle feature, score column.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
32 self.feature_col = feature_col
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
33 if self.feature_col >= self.nfields:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
34 raise MissingFieldError( "No field for feature_col (%d)" % feature_col )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
35 self.feature = self.fields[ self.feature_col ]
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
36 self.score_col = score_col
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
37 if self.score_col >= self.nfields:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
38 raise MissingFieldError( "No field for score_col (%d)" % score_col )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
39 self.score = self.fields[ self.score_col ]
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
40
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
41 # GFF attributes.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
42 self.attributes = parse_gff_attributes( fields[8] )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
43
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
44 def copy( self ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
45 return GFFInterval(self.reader, list( self.fields ), self.chrom_col, self.feature_col, self.start_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
46 self.end_col, self.strand_col, self.score_col, self.strand)
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
47
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
48
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
49 class GFFFeature( GFFInterval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
50 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
51 A GFF feature, which can include multiple intervals.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
52 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
53 def __init__( self, reader, chrom_col=0, feature_col=2, start_col=3, end_col=4,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
54 strand_col=6, score_col=5, default_strand='.', fix_strand=False, intervals=[],
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
55 raw_size=0 ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
56 # Use copy so that first interval and feature do not share fields.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
57 GFFInterval.__init__( self, reader, copy.deepcopy( intervals[0].fields ), chrom_col, feature_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
58 start_col, end_col, strand_col, score_col, default_strand,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
59 fix_strand=fix_strand )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
60 self.intervals = intervals
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
61 self.raw_size = raw_size
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
62 # Use intervals to set feature attributes.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
63 for interval in self.intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
64 # Error checking. NOTE: intervals need not share the same strand.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
65 if interval.chrom != self.chrom:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
66 raise ValueError( "interval chrom does not match self chrom: %s != %s" %
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
67 ( interval.chrom, self.chrom ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
68 # Set start, end of interval.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
69 if interval.start < self.start:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
70 self.start = interval.start
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
71 if interval.end > self.end:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
72 self.end = interval.end
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
73
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
74 def name( self ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
75 """ Returns feature's name. """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
76 name = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
77 # Preference for name: GTF, GFF3, GFF.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
78 for attr_name in ['gene_id', 'transcript_id', # GTF
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
79 'ID', 'id', # GFF3
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
80 'group' ]: # GFF (TODO)
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
81 name = self.attributes.get( attr_name, None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
82 if name is not None:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
83 break
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
84 return name
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
85
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
86 def copy( self ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
87 intervals_copy = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
88 for interval in self.intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
89 intervals_copy.append( interval.copy() )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
90 return GFFFeature(self.reader, self.chrom_col, self.feature_col, self.start_col, self.end_col, self.strand_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
91 self.score_col, self.strand, intervals=intervals_copy )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
92
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
93 def lines( self ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
94 lines = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
95 for interval in self.intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
96 lines.append( '\t'.join( interval.fields ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
97 return lines
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
98
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
99
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
100 class GFFIntervalToBEDReaderWrapper( NiceReaderWrapper ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
101 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
102 Reader wrapper that reads GFF intervals/lines and automatically converts
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
103 them to BED format.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
104 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
105
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
106 def parse_row( self, line ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
107 # HACK: this should return a GFF interval, but bx-python operations
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
108 # require GenomicInterval objects and subclasses will not work.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
109 interval = GenomicInterval( self, line.split( "\t" ), self.chrom_col, self.start_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
110 self.end_col, self.strand_col, self.default_strand,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
111 fix_strand=self.fix_strand )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
112 interval = convert_gff_coords_to_bed( interval )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
113 return interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
114
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
115
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
116 class GFFReaderWrapper( NiceReaderWrapper ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
117 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
118 Reader wrapper for GFF files.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
119
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
120 Wrapper has two major functions:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
121
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
122 1. group entries for GFF file (via group column), GFF3 (via id attribute),
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
123 or GTF (via gene_id/transcript id);
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
124 2. convert coordinates from GFF format--starting and ending coordinates
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
125 are 1-based, closed--to the 'traditional'/BED interval format--0 based,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
126 half-open. This is useful when using GFF files as inputs to tools that
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
127 expect traditional interval format.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
128 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
129
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
130 def __init__( self, reader, chrom_col=0, feature_col=2, start_col=3,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
131 end_col=4, strand_col=6, score_col=5, fix_strand=False, convert_to_bed_coord=False, **kwargs ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
132 NiceReaderWrapper.__init__( self, reader, chrom_col=chrom_col, start_col=start_col, end_col=end_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
133 strand_col=strand_col, fix_strand=fix_strand, **kwargs )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
134 self.feature_col = feature_col
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
135 self.score_col = score_col
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
136 self.convert_to_bed_coord = convert_to_bed_coord
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
137 self.last_line = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
138 self.cur_offset = 0
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
139 self.seed_interval = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
140 self.seed_interval_line_len = 0
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
141
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
142 def parse_row( self, line ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
143 interval = GFFInterval( self, line.split( "\t" ), self.chrom_col, self.feature_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
144 self.start_col, self.end_col, self.strand_col, self.score_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
145 self.default_strand, fix_strand=self.fix_strand )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
146 return interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
147
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
148 def __next__( self ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
149 """ Returns next GFFFeature. """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
150
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
151 #
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
152 # Helper function.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
153 #
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
154
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
155 def handle_parse_error( parse_error ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
156 """ Actions to take when ParseError found. """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
157 if self.outstream:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
158 if self.print_delegate and hasattr(self.print_delegate, "__call__"):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
159 self.print_delegate( self.outstream, e, self )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
160 self.skipped += 1
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
161 # no reason to stuff an entire bad file into memmory
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
162 if self.skipped < 10:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
163 self.skipped_lines.append( ( self.linenum, self.current_line, str( e ) ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
164
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
165 # For debugging, uncomment this to propogate parsing exceptions up.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
166 # I.e. the underlying reason for an unexpected StopIteration exception
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
167 # can be found by uncommenting this.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
168 # raise e
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
169
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
170 #
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
171 # Get next GFFFeature
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
172 #
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
173 raw_size = self.seed_interval_line_len
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
174
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
175 # If there is no seed interval, set one. Also, if there are no more
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
176 # intervals to read, this is where iterator dies.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
177 if not self.seed_interval:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
178 while not self.seed_interval:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
179 try:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
180 self.seed_interval = GenomicIntervalReader.next( self )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
181 except ParseError as e:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
182 handle_parse_error( e )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
183 # TODO: When no longer supporting python 2.4 use finally:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
184 # finally:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
185 raw_size += len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
186
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
187 # If header or comment, clear seed interval and return it with its size.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
188 if isinstance( self.seed_interval, ( Header, Comment ) ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
189 return_val = self.seed_interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
190 return_val.raw_size = len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
191 self.seed_interval = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
192 self.seed_interval_line_len = 0
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
193 return return_val
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
194
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
195 # Initialize feature identifier from seed.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
196 feature_group = self.seed_interval.attributes.get( 'group', None ) # For GFF
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
197 # For GFF3
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
198 feature_id = self.seed_interval.attributes.get( 'ID', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
199 # For GTF.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
200 feature_transcript_id = self.seed_interval.attributes.get( 'transcript_id', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
201
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
202 # Read all intervals associated with seed.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
203 feature_intervals = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
204 feature_intervals.append( self.seed_interval )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
205 while True:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
206 try:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
207 interval = GenomicIntervalReader.next( self )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
208 raw_size += len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
209 except StopIteration as e:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
210 # No more intervals to read, but last feature needs to be
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
211 # returned.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
212 interval = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
213 raw_size += len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
214 break
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
215 except ParseError as e:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
216 handle_parse_error( e )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
217 raw_size += len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
218 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
219 # TODO: When no longer supporting python 2.4 use finally:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
220 # finally:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
221 # raw_size += len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
222
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
223 # Ignore comments.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
224 if isinstance( interval, Comment ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
225 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
226
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
227 # Determine if interval is part of feature.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
228 part_of = False
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
229 group = interval.attributes.get( 'group', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
230 # GFF test:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
231 if group and feature_group == group:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
232 part_of = True
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
233 # GFF3 test:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
234 parent_id = interval.attributes.get( 'Parent', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
235 cur_id = interval.attributes.get( 'ID', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
236 if ( cur_id and cur_id == feature_id ) or ( parent_id and parent_id == feature_id ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
237 part_of = True
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
238 # GTF test:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
239 transcript_id = interval.attributes.get( 'transcript_id', None )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
240 if transcript_id and transcript_id == feature_transcript_id:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
241 part_of = True
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
242
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
243 # If interval is not part of feature, clean up and break.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
244 if not part_of:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
245 # Adjust raw size because current line is not part of feature.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
246 raw_size -= len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
247 break
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
248
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
249 # Interval associated with feature.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
250 feature_intervals.append( interval )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
251
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
252 # Last interval read is the seed for the next interval.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
253 self.seed_interval = interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
254 self.seed_interval_line_len = len( self.current_line )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
255
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
256 # Return feature.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
257 feature = GFFFeature( self, self.chrom_col, self.feature_col, self.start_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
258 self.end_col, self.strand_col, self.score_col,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
259 self.default_strand, fix_strand=self.fix_strand,
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
260 intervals=feature_intervals, raw_size=raw_size )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
261
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
262 # Convert to BED coords?
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
263 if self.convert_to_bed_coord:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
264 convert_gff_coords_to_bed( feature )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
265
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
266 return feature
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
267 next = __next__ # This line should be removed once the bx-python port to Python3 is finished
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
268
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
269
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
270 def convert_bed_coords_to_gff( interval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
271 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
272 Converts an interval object's coordinates from BED format to GFF format.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
273 Accepted object types include GenomicInterval and list (where the first
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
274 element in the list is the interval's start, and the second element is
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
275 the interval's end).
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
276 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
277 if isinstance( interval, GenomicInterval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
278 interval.start += 1
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
279 if isinstance( interval, GFFFeature ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
280 for subinterval in interval.intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
281 convert_bed_coords_to_gff( subinterval )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
282 elif type( interval ) is list:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
283 interval[ 0 ] += 1
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
284 return interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
285
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
286
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
287 def convert_gff_coords_to_bed( interval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
288 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
289 Converts an interval object's coordinates from GFF format to BED format.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
290 Accepted object types include GFFFeature, GenomicInterval, and list (where
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
291 the first element in the list is the interval's start, and the second
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
292 element is the interval's end).
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
293 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
294 if isinstance( interval, GenomicInterval ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
295 interval.start -= 1
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
296 if isinstance( interval, GFFFeature ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
297 for subinterval in interval.intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
298 convert_gff_coords_to_bed( subinterval )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
299 elif type( interval ) is list:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
300 interval[ 0 ] -= 1
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
301 return interval
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
302
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
303
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
304 def parse_gff_attributes( attr_str ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
305 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
306 Parses a GFF/GTF attribute string and returns a dictionary of name-value
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
307 pairs. The general format for a GFF3 attributes string is
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
308
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
309 name1=value1;name2=value2
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
310
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
311 The general format for a GTF attribute string is
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
312
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
313 name1 "value1" ; name2 "value2"
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
314
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
315 The general format for a GFF attribute string is a single string that
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
316 denotes the interval's group; in this case, method returns a dictionary
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
317 with a single key-value pair, and key name is 'group'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
318 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
319 attributes_list = attr_str.split(";")
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
320 attributes = {}
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
321 for name_value_pair in attributes_list:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
322 # Try splitting by '=' (GFF3) first because spaces are allowed in GFF3
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
323 # attribute; next, try double quotes for GTF.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
324 pair = name_value_pair.strip().split("=")
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
325 if len( pair ) == 1:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
326 pair = name_value_pair.strip().split("\"")
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
327 if len( pair ) == 1:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
328 # Could not split for some reason -- raise exception?
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
329 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
330 if pair == '':
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
331 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
332 name = pair[0].strip()
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
333 if name == '':
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
334 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
335 # Need to strip double quote from values
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
336 value = pair[1].strip(" \"")
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
337 attributes[ name ] = value
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
338
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
339 if len( attributes ) == 0:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
340 # Could not split attributes string, so entire string must be
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
341 # 'group' attribute. This is the case for strictly GFF files.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
342 attributes['group'] = attr_str
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
343 return attributes
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
344
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
345
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
346 def gff_attributes_to_str( attrs, gff_format ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
347 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
348 Convert GFF attributes to string. Supported formats are GFF3, GTF.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
349 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
350 if gff_format == 'GTF':
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
351 format_string = '%s "%s"'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
352 # Convert group (GFF) and ID, parent (GFF3) attributes to transcript_id, gene_id
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
353 id_attr = None
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
354 if 'group' in attrs:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
355 id_attr = 'group'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
356 elif 'ID' in attrs:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
357 id_attr = 'ID'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
358 elif 'Parent' in attrs:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
359 id_attr = 'Parent'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
360 if id_attr:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
361 attrs['transcript_id'] = attrs['gene_id'] = attrs[id_attr]
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
362 elif gff_format == 'GFF3':
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
363 format_string = '%s=%s'
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
364 attrs_strs = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
365 for name, value in attrs.items():
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
366 attrs_strs.append( format_string % ( name, value ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
367 return " ; ".join( attrs_strs )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
368
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
369
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
370 def read_unordered_gtf( iterator, strict=False ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
371 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
372 Returns GTF features found in an iterator. GTF lines need not be ordered
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
373 or clustered for reader to work. Reader returns GFFFeature objects sorted
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
374 by transcript_id, chrom, and start position.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
375 """
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
376
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
377 # -- Get function that generates line/feature key. --
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
378
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
379 def get_transcript_id(fields):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
380 return parse_gff_attributes( fields[8] )[ 'transcript_id' ]
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
381
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
382 if strict:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
383 # Strict GTF parsing uses transcript_id only to group lines into feature.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
384 key_fn = get_transcript_id
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
385 else:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
386 # Use lenient parsing where chromosome + transcript_id is the key. This allows
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
387 # transcripts with same ID on different chromosomes; this occurs in some popular
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
388 # datasources, such as RefGenes in UCSC.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
389 def key_fn(fields):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
390 return fields[0] + '_' + get_transcript_id( fields )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
391
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
392 # Aggregate intervals by transcript_id and collect comments.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
393 feature_intervals = odict()
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
394 comments = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
395 for count, line in enumerate( iterator ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
396 if line.startswith( '#' ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
397 comments.append( Comment( line ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
398 continue
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
399
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
400 line_key = key_fn( line.split('\t') )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
401 if line_key in feature_intervals:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
402 feature = feature_intervals[ line_key ]
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
403 else:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
404 feature = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
405 feature_intervals[ line_key ] = feature
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
406 feature.append( GFFInterval( None, line.split( '\t' ) ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
407
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
408 # Create features.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
409 chroms_features = {}
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
410 for count, intervals in enumerate( feature_intervals.values() ):
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
411 # Sort intervals by start position.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
412 intervals.sort( lambda a, b: cmp( a.start, b.start ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
413 feature = GFFFeature( None, intervals=intervals )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
414 if feature.chrom not in chroms_features:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
415 chroms_features[ feature.chrom ] = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
416 chroms_features[ feature.chrom ].append( feature )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
417
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
418 # Sort features by chrom, start position.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
419 chroms_features_sorted = []
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
420 for chrom_features in chroms_features.values():
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
421 chroms_features_sorted.append( chrom_features )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
422 chroms_features_sorted.sort( lambda a, b: cmp( a[0].chrom, b[0].chrom ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
423 for features in chroms_features_sorted:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
424 features.sort( lambda a, b: cmp( a.start, b.start ) )
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
425
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
426 # Yield comments first, then features.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
427 # FIXME: comments can appear anywhere in file, not just the beginning.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
428 # Ideally, then comments would be associated with features and output
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
429 # just before feature/line.
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
430 for comment in comments:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
431 yield comment
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
432
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
433 for chrom_features in chroms_features_sorted:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
434 for feature in chrom_features:
0d97b11ed3d5 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
diff changeset
435 yield feature