forked from reefgenomics/SymPortal_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
99 lines (82 loc) · 2.72 KB
/
general.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
import os
import pickle
def writeListToDestination(destination, listToWrite):
# print('Writing list to ' + destination)
try:
os.makedirs(os.path.dirname(destination))
except FileExistsError:
pass
with open(destination, mode='w') as writer:
i = 0
while i < len(listToWrite):
if i != len(listToWrite) - 1:
writer.write(listToWrite[i] + '\n')
elif i == len(listToWrite) - 1:
writer.write(listToWrite[i])
i += 1
def readDefinedFileToList(filename):
temp_list = []
with open(filename, mode='r') as reader:
temp_list = [line.rstrip() for line in reader]
return temp_list
def convert_interleaved_to_sequencial_fasta(fasta_in):
fasta_dict = {}
list_seq_names = []
list_seq_sequences = []
num_seqs = int(fasta_in[0].split()[0])
fasta_cropped = []
# Get rid of the first line and get rid of the blank lines
for line in fasta_in[1:]:
if line != '':
fasta_cropped.append(line)
for i in range(len(fasta_cropped)):
if i < num_seqs:
# Then we are on one of the inital lines
list_seq_names.append(fasta_cropped[i].split()[0])
list_seq_sequences.append(''.join(fasta_cropped[i].split()[1:]))
else:
index = i % num_seqs
list_seq_sequences[index] += ''.join(fasta_cropped[i].split()[1:])
out_fasta = []
for name, seq in zip(list_seq_names, list_seq_sequences):
out_fasta.extend(['>{}'.format(name), seq])
return out_fasta
def readByteObjectFromDefinedDirectory(directory):
f = open(directory, 'rb')
return pickle.load(f)
def writeByteObjectToDefinedDirectory(directory,object):
f = open(directory , 'wb+')
pickle.dump(object, f)
def createNoSpaceFastaFile(fastaList):
tempList = []
i = 0
while i < len(fastaList):
tempList.extend([fastaList[i].split('\t')[0], fastaList[i+1]])
i += 2
return tempList
def createDictFromFasta(fastaList):
tempDict = {}
i = 0
while i < len(fastaList):
sequence = fastaList[i][1:]
tempDict[sequence] = fastaList[i+1]
i += 2
return tempDict
def createNewFile(pathtofile):
try:
os.makedirs(os.path.dirname(pathtofile))
except FileExistsError:
pass
open(pathtofile, mode='w')
def writeLinesToFile(pathToFile, listoflines):
with open(pathToFile, mode='a') as f:
for line in listoflines:
f.write(line + '\n')
def checkIfFileEmpty(filepath):
count = 0
with open(filepath, mode='r') as reader:
for line in reader:
if count != 0:
return False # not empty
count += 1
return True