PageRenderTime 36ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/filters/ucsc_gene_table_to_intervals.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 106 lines | 71 code | 13 blank | 22 comment | 25 complexity | 6026a813e2982105c780e308163b6f69 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. Read a table dump in the UCSC gene table format and print a tab separated
  4. list of intervals corresponding to requested features of each gene.
  5. usage: ucsc_gene_table_to_intervals.py [options]
  6. options:
  7. -h, --help show this help message and exit
  8. -rREGION, --region=REGION
  9. Limit to region: one of coding, utr3, utr5, transcribed [default]
  10. -e, --exons Only print intervals overlapping an exon
  11. -i, --input=inputfile input file
  12. -o, --output=outputfile output file
  13. """
  14. import optparse, string, sys
  15. assert sys.version_info[:2] >= ( 2, 4 )
  16. def main():
  17. # Parse command line
  18. parser = optparse.OptionParser( usage="%prog [options] " )
  19. parser.add_option( "-r", "--region", dest="region", default="transcribed",
  20. help="Limit to region: one of coding, utr3, utr5, transcribed [default]" )
  21. parser.add_option( "-e", "--exons", action="store_true", dest="exons",
  22. help="Only print intervals overlapping an exon" )
  23. parser.add_option( "-s", "--strand", action="store_true", dest="strand",
  24. help="Print strand after interval" )
  25. parser.add_option( "-i", "--input", dest="input", default=None,
  26. help="Input file" )
  27. parser.add_option( "-o", "--output", dest="output", default=None,
  28. help="Output file" )
  29. options, args = parser.parse_args()
  30. assert options.region in ( 'coding', 'utr3', 'utr5', 'transcribed' ), "Invalid region argument"
  31. try:
  32. out_file = open (options.output,"w")
  33. except:
  34. print >> sys.stderr, "Bad output file."
  35. sys.exit(0)
  36. try:
  37. in_file = open (options.input)
  38. except:
  39. print >> sys.stderr, "Bad input file."
  40. sys.exit(0)
  41. print "Region:", options.region+";"
  42. print "Only overlap with Exons:",
  43. if options.exons:
  44. print "Yes"
  45. else:
  46. print "No"
  47. # Read table and handle each gene
  48. for line in in_file:
  49. try:
  50. if line[0:1] == "#":
  51. continue
  52. # Parse fields from gene tabls
  53. fields = line.split( '\t' )
  54. name = fields[0]
  55. chrom = fields[1]
  56. strand = fields[2].replace(" ","_")
  57. tx_start = int( fields[3] )
  58. tx_end = int( fields[4] )
  59. cds_start = int( fields[5] )
  60. cds_end = int( fields[6] )
  61. # Determine the subset of the transcribed region we are interested in
  62. if options.region == 'utr3':
  63. if strand == '-': region_start, region_end = tx_start, cds_start
  64. else: region_start, region_end = cds_end, tx_end
  65. elif options.region == 'utr5':
  66. if strand == '-': region_start, region_end = cds_end, tx_end
  67. else: region_start, region_end = tx_start, cds_start
  68. elif options.region == 'coding':
  69. region_start, region_end = cds_start, cds_end
  70. else:
  71. region_start, region_end = tx_start, tx_end
  72. # If only interested in exons, print the portion of each exon overlapping
  73. # the region of interest, otherwise print the span of the region
  74. if options.exons:
  75. exon_starts = map( int, fields[8].rstrip( ',\n' ).split( ',' ) )
  76. exon_ends = map( int, fields[9].rstrip( ',\n' ).split( ',' ) )
  77. for start, end in zip( exon_starts, exon_ends ):
  78. start = max( start, region_start )
  79. end = min( end, region_end )
  80. if start < end:
  81. if strand: print_tab_sep(out_file, chrom, start, end, name, "0", strand )
  82. else: print_tab_sep(out_file, chrom, start, end )
  83. else:
  84. if strand: print_tab_sep(out_file, chrom, region_start, region_end, name, "0", strand )
  85. else: print_tab_sep(out_file, chrom, region_start, region_end )
  86. except:
  87. continue
  88. def print_tab_sep(out_file, *args ):
  89. """Print items in `l` to stdout separated by tabs"""
  90. print >>out_file, string.join( [ str( f ) for f in args ], '\t' )
  91. if __name__ == "__main__": main()