annotate fasta_concatenate_by_species.py @ 0:a63b082a26eb

Imported from capsule None
author devteam
date Mon, 19 May 2014 10:59:37 -0400
parents
children c5311b7718d1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
2 #Dan Blankenberg
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
3 """
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
4 Takes a Multiple Alignment FASTA file and concatenates
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
5 sequences for each species, resulting in one sequence
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
6 alignment per species.
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
7 """
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
8
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
9 import sys, tempfile
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
10 from utils.maf_utilities import iter_fasta_alignment
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
11 from utils.odict import odict
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
12
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
13 def __main__():
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
14 input_filename = sys.argv[1]
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
15 output_filename = sys.argv[2]
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
16 species = odict()
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
17 cur_size = 0
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
18 for components in iter_fasta_alignment( input_filename ):
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
19 species_not_written = species.keys()
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
20 for component in components:
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
21 if component.species not in species:
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
22 species[component.species] = tempfile.TemporaryFile()
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
23 species[component.species].write( "-" * cur_size )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
24 species[component.species].write( component.text )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
25 try:
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
26 species_not_written.remove( component.species )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
27 except ValueError:
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
28 #this is a new species
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
29 pass
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
30 for spec in species_not_written:
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
31 species[spec].write( "-" * len( components[0].text ) )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
32 cur_size += len( components[0].text )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
33 out = open( output_filename, 'wb' )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
34 for spec, f in species.iteritems():
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
35 f.seek( 0 )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
36 out.write( ">%s\n%s\n" % ( spec, f.read() ) )
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
37 out.close()
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
38
a63b082a26eb Imported from capsule None
devteam
parents:
diff changeset
39 if __name__ == "__main__" : __main__()