-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCleanFasta.py
executable file
·149 lines (121 loc) · 4.25 KB
/
CleanFasta.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
#################################################
## run mothur alignments
#################################################
import sys, time, random, shlex, os, itertools, gc
from optparse import OptionParser
from Bio import SeqIO, Seq
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from threading import *
from subprocess import *
_author="Sebastian Szpakowski"
_date="2011/06/01"
_version="Version 1"
#################################################
## Classes
##
class CommandWorker (Thread) :
def __init__(self, command):
Thread.__init__(self)
self.command = command
def run(self):
pool_sema.acquire()
gc.collect()
p = Popen(shlex.split(self.command), stdout=PIPE)
gc.collect()
pool_sema.release()
def __str__(self):
otpt = "%s\n%s" % (self.command, len(result))
return (otpt)
#################################################
### Iterator over input fasta file.
### Only reading when requested
### Useful for very large FASTA files
### with many sequences
class FastaParser:
def __init__ (self, x, quals=False):
self.filename = x
self.fp = open(x, "r")
self.currline = ""
self.currentFastaName = ""
self.currentFastaSequence = ""
self.lastitem=False
if quals:
self.linesep=" "
else:
self.linesep=""
def __iter__(self):
return(self)
#####
def next(self):
for self.currline in self.fp:
if self.currline.startswith(">"):
self.currline = self.currline[1:]
if self.currentFastaName == "":
self.currentFastaName = self.currline
else:
otpt = (self.currentFastaName.strip(), self.currentFastaSequence.strip())
self.currentFastaName = self.currline
self.currentFastaSequence = ""
self.previoustell = self.fp.tell()
return (otpt)
else:
self.addSequence(self.currline)
if not self.lastitem:
self.lastitem=True
return (self.currentFastaName.strip(), self.currentFastaSequence.strip())
else:
raise StopIteration
def addSequence(self, x):
self.currentFastaSequence = "%s%s%s" % (self.currentFastaSequence,self.linesep, x.strip())
def __str__():
return ("reading file: %s" %self.filename)
#################################################
## Functions
##
def cleanup(recordlist):
otpt=list()
for rec in recordlist:
seq = str(rec.seq).replace(".", "").replace("-", "").replace(" ", "").replace("\n", "")
id = rec.id.strip().split()[0]
if len(id.split("|"))==5:
id = id.split("|")[1]
tmp = SeqRecord(seq=Seq(seq), id=id, name=id, description=id)
otpt.append(tmp)
return (otpt)
#################################################
## Arguments
##
parser = OptionParser()
#parser.add_option("-f", "--file", dest="filename",
# help="write report to FILE", metavar="FILE")
#parser.add_option("-q", "--quiet",
# action="store_false", dest="verbose", default=True,
# help="don't print status messages to stdout")
parser.add_option("-i", "--input", dest="fn_input",
help="load and clean fasta FILE", metavar="FILE")
parser.add_option("-o", "--output", dest="fn_output",
help="load and clean fasta FILE", metavar="FILE")
(options, args) = parser.parse_args()
#################################################
## Begin
##
if options.fn_input != None :
print "cleaning up..."
#C = list(SeqIO.parse(open(options.fn_input), "fasta"))
#cleana = cleanup(C)
output_handle = open(options.fn_output, "w")
#SeqIO.write(cleana, output_handle, "fasta")
for head, seq in FastaParser(options.fn_input):
seq = seq.replace(".", "").replace("-", "").replace(" ", "").replace("\n", "")
head = head.strip().split()[0]
output_handle.write(">%s\n%s\n" % (head, seq))
output_handle.close()
#################################################
## Finish
#################################################