| 1 | """ |
|---|
| 2 | Eland file transformer. Transforms an eland file |
|---|
| 3 | to a gff file format. |
|---|
| 4 | |
|---|
| 5 | python eland2gff.py |
|---|
| 6 | |
|---|
| 7 | As a python module:: |
|---|
| 8 | |
|---|
| 9 | python -m genetrack.scripts.eland2gff |
|---|
| 10 | |
|---|
| 11 | Or in other python scripts:: |
|---|
| 12 | |
|---|
| 13 | >>> |
|---|
| 14 | >>> from genetrack.scripts import eland2gff |
|---|
| 15 | >>> eland2gff.transform(inpname, outname) |
|---|
| 16 | >>> |
|---|
| 17 | |
|---|
| 18 | Run the script with no parameters to see the options that it takes. |
|---|
| 19 | |
|---|
| 20 | **Observed runtime**: tranformation rate of 5 million lines per minute |
|---|
| 21 | |
|---|
| 22 | """ |
|---|
| 23 | import os, sys, csv |
|---|
| 24 | from itertools import * |
|---|
| 25 | from genetrack import logger, conf, util |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | def transform(inpname, size, outname=None): |
|---|
| 29 | """ |
|---|
| 30 | Transforms reads stored in bedfile to a genetrack input file. |
|---|
| 31 | Requires at least 6 bed columns to access the strand. |
|---|
| 32 | """ |
|---|
| 33 | logger.debug('input %s' % inpname) |
|---|
| 34 | logger.debug('output %s' % outname) |
|---|
| 35 | |
|---|
| 36 | reader = csv.reader(open(inpname), delimiter='\t') |
|---|
| 37 | |
|---|
| 38 | # unwind the iterator |
|---|
| 39 | list(takewhile( lambda x: x[0].startswith('#'), reader)) |
|---|
| 40 | |
|---|
| 41 | output = file(outname, 'wt') |
|---|
| 42 | output.write('##gff-version 3\n') |
|---|
| 43 | output.write('# created with eland2gff on %s\n' % inpname) |
|---|
| 44 | output.write('# fixed read lenght of %s\n' % size) |
|---|
| 45 | for row in reader: |
|---|
| 46 | chrom, start, strand = row[10], int(row[12]), row[13] |
|---|
| 47 | end = start + size |
|---|
| 48 | if strand == 'F': |
|---|
| 49 | strand = '+' |
|---|
| 50 | else: |
|---|
| 51 | strand = '-' |
|---|
| 52 | result = map(str, [chrom, '.', '.', start, end, '.', strand, '.', '.']) |
|---|
| 53 | output.write("%s\n" % '\t'.join(result)) |
|---|
| 54 | |
|---|
| 55 | output.close() |
|---|
| 56 | |
|---|
| 57 | def option_parser(): |
|---|
| 58 | "The option parser may be constructed in other tools invoking this script" |
|---|
| 59 | import optparse |
|---|
| 60 | |
|---|
| 61 | usage = "usage: %prog -i inputfile -o outputfile" |
|---|
| 62 | |
|---|
| 63 | parser = optparse.OptionParser(usage=usage) |
|---|
| 64 | |
|---|
| 65 | # setting the input file name |
|---|
| 66 | parser.add_option( |
|---|
| 67 | '-i', '--input', action="store", |
|---|
| 68 | dest="inpname", type='str', default=None, |
|---|
| 69 | help="the input file name (required)" |
|---|
| 70 | ) |
|---|
| 71 | |
|---|
| 72 | # setting the output file name |
|---|
| 73 | parser.add_option( |
|---|
| 74 | '-o', '--output', action="store", |
|---|
| 75 | dest="outname", type='str', default=None, |
|---|
| 76 | help="output file name (optional)" |
|---|
| 77 | ) |
|---|
| 78 | |
|---|
| 79 | # verbosity can be 0,1 and 2 (increasing verbosity) |
|---|
| 80 | parser.add_option( |
|---|
| 81 | '-v', '--verbosity', action="store", |
|---|
| 82 | dest="verbosity", type="int", default=1, |
|---|
| 83 | help="sets the verbosity (0, 1) (default=1)", |
|---|
| 84 | ) |
|---|
| 85 | |
|---|
| 86 | # correction shift added in 5' direction for start/end coordinates |
|---|
| 87 | parser.add_option( |
|---|
| 88 | '-s', '--size', action="store", |
|---|
| 89 | dest="size", type="int", default=36, |
|---|
| 90 | help="readlength (fixed) default 36", |
|---|
| 91 | ) |
|---|
| 92 | |
|---|
| 93 | return parser |
|---|
| 94 | |
|---|
| 95 | if __name__ == '__main__': |
|---|
| 96 | |
|---|
| 97 | parser = option_parser() |
|---|
| 98 | |
|---|
| 99 | options, args = parser.parse_args() |
|---|
| 100 | |
|---|
| 101 | # set verbosity |
|---|
| 102 | logger.disable(options.verbosity) |
|---|
| 103 | |
|---|
| 104 | # missing file names |
|---|
| 105 | if not options.inpname: |
|---|
| 106 | parser.print_help() |
|---|
| 107 | else: |
|---|
| 108 | options.outname=options.outname or '%s.gff' % options.inpname |
|---|
| 109 | transform(inpname=options.inpname, outname=options.outname, size=options.size) |
|---|