| 1 | #!/usr/bin/python2.6 |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Read a maf and print the text as a fasta file, concatenating blocks. A |
|---|
| 5 | specific subset of species can be chosen. |
|---|
| 6 | |
|---|
| 7 | usage %prog [options] species1,species2,... < maf_file > fasta_file |
|---|
| 8 | --fill="expression": Insert this between blocks |
|---|
| 9 | --wrap=columns: Wrap FASTA to this many columns |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | from optparse import OptionParser |
|---|
| 13 | |
|---|
| 14 | import textwrap |
|---|
| 15 | import sys |
|---|
| 16 | from bx.align import maf |
|---|
| 17 | |
|---|
| 18 | def __main__(): |
|---|
| 19 | # Parse command line arguments |
|---|
| 20 | |
|---|
| 21 | parser = OptionParser() |
|---|
| 22 | parser.add_option( "--fill", action="store", default=None, type="string", help="" ) |
|---|
| 23 | parser.add_option( "--wrap", action="store", default=None, type="int", help="" ) |
|---|
| 24 | parser.add_option( "--nowrap", action="store_true", default=False, dest="nowrap", help="" ) |
|---|
| 25 | |
|---|
| 26 | ( options, args ) = parser.parse_args() |
|---|
| 27 | |
|---|
| 28 | species = [] |
|---|
| 29 | for arg in args: species.extend(arg.split(',')) |
|---|
| 30 | |
|---|
| 31 | fill = "" |
|---|
| 32 | if options.fill: fill = eval( options.fill ) |
|---|
| 33 | |
|---|
| 34 | wrap = 50 |
|---|
| 35 | if (options.wrap != None): wrap = options.wrap |
|---|
| 36 | elif (options.nowrap): wrap = 0 |
|---|
| 37 | |
|---|
| 38 | # create the concatenated sequences |
|---|
| 39 | |
|---|
| 40 | texts = {} |
|---|
| 41 | for s in species: texts[s] = [] |
|---|
| 42 | maf_reader = maf.Reader( sys.stdin ) |
|---|
| 43 | for m in maf_reader: |
|---|
| 44 | for s in species: |
|---|
| 45 | c = m.get_component_by_src_start( s ) |
|---|
| 46 | if c: texts[s].append( c.text ) |
|---|
| 47 | else: texts[s].append( "-" * m.text_size ) |
|---|
| 48 | for s in species: |
|---|
| 49 | print ">" + s |
|---|
| 50 | print_n( fill.join( texts[s] ), wrap ) |
|---|
| 51 | |
|---|
| 52 | def print_n( s, n, f = sys.stdout ): |
|---|
| 53 | if (n <= 0): |
|---|
| 54 | print >> f, s |
|---|
| 55 | else: |
|---|
| 56 | p = 0 |
|---|
| 57 | while p < len( s ): |
|---|
| 58 | print >> f, s[p:min(p+n,len(s))] |
|---|
| 59 | p += n |
|---|
| 60 | |
|---|
| 61 | if __name__ == "__main__": __main__() |
|---|