-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFASTA_Reader.py
More file actions
36 lines (31 loc) · 1.08 KB
/
Copy pathFASTA_Reader.py
File metadata and controls
36 lines (31 loc) · 1.08 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
import pandas as pd
import os
class FastaReader:
@staticmethod
def check_fasta(filename):
_, extension = os.path.splitext(filename)
if extension not in [".fasta", ".fna", ".fa"]:
return False
path = os.path.abspath(filename)
bases = ["A", "C", "G", "T"]
with open(path, "r") as file:
if file.readlines()[0][0] != ">":
return False
for line in file.readlines()[1:]:
for base in line:
if base not in bases:
return False
return True
@staticmethod
def scanfile(filename):
path = os.path.join("fasta_file/", filename)
if FastaReader.check_fasta(path):
with open(path, "r") as file:
nt = "" # String of all the nucleotides
for line in file.readlines()[1:]:
nt += line
nt = nt.replace('\n', '')
nt_series = pd.Series(list(nt))
return nt_series
else:
raise TypeError("File is not a FASTA file")