comparison fasta_utils/__init__.py @ 0:d04fa5201f51 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/rna_commander/tools/rna_tools/rna_commender commit 7ad344d108076116e702e1c1e91cea73d8fcadc4
author rnateam
date Thu, 28 Jul 2016 05:56:54 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d04fa5201f51
1 """Util functions for FASTA format."""
2
3 __author__ = "Gianluca Corrado"
4 __copyright__ = "Copyright 2016, Gianluca Corrado"
5 __license__ = "MIT"
6 __maintainer__ = "Gianluca Corrado"
7 __email__ = "gianluca.corrado@unitn.it"
8 __status__ = "Production"
9
10
11 def import_fasta(fasta_file):
12 """Import a fasta file as a dictionary."""
13 dic = {}
14 f = open(fasta_file)
15 fasta = f.read().strip()
16 f.close()
17 for a in fasta.split('>'):
18 k = a.split('\n')[0]
19 v = ''.join(a.split('\n')[1:])
20 if k != '':
21 dic[k] = v
22 return dic
23
24
25 def export_fasta(dic):
26 """Export a dictionary."""
27 fasta = ""
28 for (k, v) in dic.iteritems():
29 fasta += ">%s\n%s\n" % (k, v)
30 return fasta
31
32
33 def seq_names(fasta_file):
34 """Get sequence names from fasta file."""
35 names = []
36 f = open(fasta_file)
37 fasta = f.read()
38 f.close()
39 for a in fasta.split('>'):
40 names.append(a.split('\n')[0])
41 return [a for a in names if a != '']
42
43
44 def stockholm2fasta(stockholm):
45 """Convert alignment in stockholm format to fasta format."""
46 fasta = ""
47 for line in stockholm.split("\n"):
48 # comment line
49 if line[0] == "#":
50 continue
51 # termination line
52 elif line == "//":
53 return fasta
54 # alignment line
55 else:
56 name, align = line.split()
57 seq = align.replace(".", "")
58 fasta += ">%s\n%s\n" % (name, seq)