-
Notifications
You must be signed in to change notification settings - Fork 1
/
repbase-to-fasta.py
71 lines (57 loc) · 2.03 KB
/
repbase-to-fasta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
from Bio.Seq import Seq
from Bio import SeqIO
import glob
import os
orig_repbase_dir = '' # directory containing raw repbase files
out_file ='' # fasta file output name
new_fasta = open(out_file, 'a')
rec_count = 1
repbase_files = glob.glob(orig_repbase_dir + "*.ref")
print "\nBeginning converson with the following parameters:"
print "-"*65
print "\nIn Dir: %s" %orig_repbase_dir
print "Repbase Files: %s" %len(repbase_files)
print "Out File: %s\n" %out_file
print "-"*65
print "\nConverting Repbase to makeblastdb friendly fasta:"
temp_no_tabs = open('repbase_notabs.fasta', 'a')
print " -Removing blast-unfriendly characters from record descriptions..."
for f in repbase_files:
rep_f = open(f, 'r')
for line in rep_f:
new_line = line.replace('\t', ':')
temp_no_tabs.write(new_line+'\n')
temp_no_tabs.close()
no_tabs_handle = open('repbase_notabs.fasta', 'r')
print " -Removing gaps and converting ambiguous sequence characters to blast-friendly values..."
for line in no_tabs_handle:
seq = None
if line.startswith('>') :
id_list = line.split(':')
if len(id_list) == 1:
id = id_list[0][1:].strip('\n')
elif len(id_list) == 2:
id = id_list[0][1:]
element = id_list[1].strip('\n')
elif len(id_list) >=3:
id = id_list[0][1:]
element = id_list[1]
organism = id_list[2].strip('\n')
else:
seq = line.replace('*', 'n').strip('\n')
seq = seq.replace('-', '').strip('\n')
if seq:
if element and organism:
record = SeqRecord(Seq(seq, IUPAC.unambiguous_dna), id=id, name=element, description=element+" "+organism)
elif element and not organism:
record = Seqrecord(Seq(seq, IUPAC.unambiguous_dna), id=id, name=element, description=element)
else:
record = Seqrecord(Seq(seq, IUPAC.unambiguous_dna), id=id)
SeqIO.write(record, new_fasta, "fasta")
rec_count += 1
print "Cleaning up temp files..."
os.remove('repbase_notabs.fasta')
print "Conversion Complete."
print "%s records successfully converted." %rec_count