-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_preprocess.py
More file actions
95 lines (82 loc) · 2.25 KB
/
Copy pathclass_preprocess.py
File metadata and controls
95 lines (82 loc) · 2.25 KB
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
import sys
import os, inspect
#class Foo():
# def __init__(self):
# self.path = os.path.relpath(inspect.getfile(self.__class__))
#foo = Foo()
#print(foo.path)
class PreProcessing(object):
_filename = None
#defining the constructor
def __init__(self, path):
self.path = path
#self.path = os.path.relpath(inspect.getfile(self.__class__))
#print (f"{self.path}")
#would be set once the class is called
#not working unless the file is in the same directory as the Class
#there is another way to do that? don't know.
@property
def filename(self):
if self._filename is None:
for file in os.listdir(self.path):
#print (file)
if file.endswith(".fasta"):
#print (file)
self._filename = file
return self._filename
#Creation of single fasta file from multi-fasta file input
#Removing also the multi-fasta file
def separator(self):
#print (self.filename)
#print (self.path)
infile = open(self.filename)
outfile = []
for line in infile:
if line.startswith(">"):
if (outfile != []):
outfile.close()
genename = line.strip().split('|')[1].split('.')[0]
filename = genename+".fasta"
outfile = open(filename,'w')
outfile.write(line)
else:
outfile.write(line)
outfile.close()
#os.remove(self.filename)
def oneliner(self):
for file in os.listdir(self.path):
if file.endswith(".fasta"):
fasta = open(file)
fasta_one = []
headers, sequences = [],[]
for line in fasta:
if line.startswith('>'):
genename = line.strip().split('|')[1].split('.')[0]
output = genename+"_one.fasta"
fasta_one = open(output, 'w')
#print (line.strip())
fasta_one.write(line)
else :
seq = line.strip()
#print (seq)
if len(seq) > 0:
fasta_one.write(seq)
fasta_one.close()
#os.remove(file)
def checklenght(self):
for file in os.listdir(self.path):
if file.endswith("_one.fasta"):
with open(file) as oneliner:
for line in oneliner:
seq = oneliner.readline()
print (seq)
#seq = oneliner.readline()
#print (seq)
if len(seq) <= 40:
print ('Error')
else :
print (file)
prova = PreProcessing("/home/mariapaola/Desktop/Pymol/class")
prova.separator()
prova.oneliner()
prova.checklenght()