-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenome.py
282 lines (239 loc) · 10.7 KB
/
Genome.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import pandas as pd
from Gene import Gene
class Genome:
"""Genome files, information and methods for analysis.
Attributes
----------
files : dict
Dictionary with keys containing names of all possible files avialable for a genome on NCBI FTP-site.
https://www.ncbi.nlm.nih.gov/genome/doc/ftpfaq/ chapter 11. Values contain path to this file
on a local computer or None.
raised_errors : bool
False by default, True if any error occured while working with a genome.
log : str
Log with all information about errors.
folder_path : str
Path to a folder, where files for genome can be found.
files_prefix : str
Prefix for a files in folder. Usually contains assembly number.
I.e. for a path '/home/user/E_coli/GCF_000007445.1_ASM744v1_protein.faa' `folder_path` would be
'/home/user/E_coli/' and `files_prefix` - 'GCF_000007445.1_ASM744v1_'
TODO: delete this attribute. Can check file_name.endswith('protein.faa')
feature_table : pandas.DataFrame
feature_table.txt file in dataframe format from pandas after self.read_feature_table() method called.
genes : list
List of genes from protein.faa file for a genome. Each gene is an instance of Gene() class.
Filled after self.read_protein_faa() methid called.
Methods
-------
read_feature_table()
Read feature_table.txt in a `folder_path`. Save pandas.DataFrame in `self.feature_table`.
read_protein_faa()
Read protein.faa in a `folder_path`. Save set of Gene() objects in `self.genes`.
__repr__
Return `folder_path` and `files_prefix` of a genome
get_gene_by_id(gene_id)
Get gene, which `id` is the same as `gene_id` parameter.
<<<<<<< HEAD
set_gene_positions()
Set `start` and `end` for each gene in `self.genes`. Requires `self.feature_table` read.
write_genes_to_file(path)
Create a file and write all genes sequences in fasta format in it.
=======
set_gene_positions()
Sets `start` and `end` for each gene in `self.genes`. Requires `self.feature_table` read.
"""
def __init__(self, folder_path, files_prefix):
"""Set genome folder path and prefix for files.
Parameters
----------
folder_path : str
Path to a folder, where files for genome can be found.
files_prefix : str
Prefix for a files in folder. Usually contains assembly number.
I.e. for a path '~/Documents/E_coli/GCF_000007445.1_ASM744v1_protein.faa' `folder_path` would be
'~/Documents/E_coli/' and `files_prefix` - 'GCF_000007445.1_ASM744v1_'
"""
self.files = {'assembly_report.txt': None,
'assembly_stats.txt': None,
'assembly_regions.txt': None,
'assembly_structure directory': None,
'cds_from_genomic.fna': None,
'feature_count.txt': None,
'feature_table.txt': None,
'genomic.fna': None,
'genomic.gbff': None,
'genomic.gff': None,
'genomic.gtf': None,
'genomic_gaps.txt': None,
'protein.faa': None,
'protein.gpff': None,
'rm.out': None,
'rm.run': None,
'rna.fna': None,
'rna.gbff': None,
'rna_from_genomic.fna': None,
'translated_cds.faa': None,
'wgsmaster.gbff': None,
}
self.raised_errors = False
self.log = ''
self.folder_path = folder_path
self.files_prefix = files_prefix
self.genes = []
self.feature_table = None
for key in self.files:
self.files[key] = self.folder_path + self.files_prefix + key
def __repr__(self):
"""Return `folder_path` and `files_prefix` of a genome"""
return 'Path: {} Prefix: {}'.format(self.folder_path, self.files_prefix)
def __getitem__(self, index):
"""Get gene by it's name through an index."""
return self.get_gene_by_id(index)
def read_feature_table(self, only_protein_coding=True):
"""Read feature_table.txt in a `folder_path`. Save pandas.DataFrame in `self.feature_table`.
Parameters
----------
only_protein_coding : bool, optional
If True, feature_table will be filtered as following:
'# feature' in ['CDS', 'gene', 'operon']
'class' in ['protein_coding', 'with_protein']
"""
try:
feature_table = pd.read_csv(self.files['feature_table.txt'], sep='\t')
if only_protein_coding:
feature_table = feature_table[(feature_table['# feature'].isin(['CDS', 'gene', 'operon'])) &
(feature_table['class'].isin(['protein_coding', 'with_protein']))]
self.feature_table = feature_table.dropna(subset=['product_accession'])
except FileNotFoundError:
self.raised_errors = True
self.log += 'Cannot get feature_table\n'
def read_protein_faa(self, filename=None):
"""Read protein.faa in a `self.folder_path`. Save list of Gene() objects in `self.genes`.
Parameters
----------
filename : str
Path to a file with protein sequence. If not set, `self.files['protein.faa']` will be used.
Examples
--------
>>> K12_PATH = '~/Documents/E_coli/K12/'
>>> K12_PREFIX = 'GCF_000005845.2_ASM584v2_'
>>> k12 = Genome(K12_PATH, K12_PREFIX)
>>> print(len(k12.genes))
0
>>> k12.read_protein_faa()
>>> print(len(k12.genes))
4242
>>> print(k12.genes[0])
>NP_414542.1 thr operon leader peptide [Escherichia coli str. K-12 substr. MG1655]
MKRISTTITTTITITTGNGAG
"""
filename = filename if filename else self.files['protein.faa']
try:
with open(filename, 'r') as f:
genes = f.read()
genes = [x for x in genes.split('>') if x != '']
for gene in genes:
info = gene.split('\n')[0]
gene_id = info.split(' ')[0]
info = ' '.join(info.split(' ')[1:])
seq = ''.join(gene.split('\n')[1:-1]) # The last element is always ''
self.genes.append(Gene(gene_id=gene_id,
info=info,
sequence=seq,
genome=self.files_prefix))
except FileNotFoundError:
self.raised_errors = True
self.log += 'Cannot get protein.faa\n'
def get_gene_positions(self, gene=None, gene_id=None):
"""Return start and end for a given gene.
Parameters
----------
gene : Gene
Instance of an object Gene() for which to return positions. If None, `gene_id` must be given.
Ignored, if `gene_id` is given.
gene_id : str
Particular object of class Gene() `id` attribute
Returns
-------
start
Integer number with start position of a gene or None if gene or position was not found.
end
Integer number with end position of a gene or None if gene or position was not found.
Examples
--------
>>> K12_PATH = '~/Documents/E_coli/K12/'
>>> K12_PREFIX = 'GCF_000005845.2_ASM584v2_'
>>> k12 = Genome(K12_PATH, K12_PREFIX)
>>> k12.read_protein_faa()
>>> k12.read_feature_table()
>>> print( k12.get_gene_positions(gene=k12.genes[0]) )
(190, 255)
>>> print( k12.get_gene_positions(gene_id='NP_414542.1') )
(190, 255)
"""
if gene_id:
gene = self.get_gene_by_id(gene_id)
row = self.feature_table[(self.feature_table['product_accession'] == gene.id)]
row = row.dropna(subset=['start', 'end'])
if row.shape[0] > 0:
row = row.iloc[0]
else:
return None, None
start = row['start']
end = row['end']
return start, end
def set_gene_positions(self, from_fasta=False): # TODO: this works ages! Do something
"""Set `start` and `end` for each gene in `self.genes`.
Parameters
----------
from_fasta : bool
If False, requires `self.feature_table` read. If True, information will be searched in `self.info`.
"""
if from_fasta:
params = ['start:', 'end:', 'genome:']
for gene in self.genes:
for param in params:
param_start = gene.info.find(param)
param_end = gene.info[param_start:].find(']')
info = gene.info[param_start + len(param):param_start + param_end]
if param == 'start:':
gene.start = info
elif param == 'end:':
gene.end = info
elif param == 'genome:':
gene.genome = info
else:
for gene in self.genes:
gene.start, gene.end = self.get_gene_positions(gene=gene)
def get_gene_by_id(self, gene_id):
"""Get gene, which `id` is the same as `gene_id` parameter."""
for gene in self.genes:
if gene.id == gene_id:
return gene
def write_genes_to_file(self, path):
"""Create file with `self.genes` in fasta format."""
with open(path, 'w') as f:
for gene in self.genes:
f.write(gene.get_fasta())
def read_genomic_fna(self, filename=None): # TODO: document
filename = filename if filename else self.files['genomic.fna']
try:
with open(filename, 'r') as f:
sequence = f.read()
sequence = ''.join(sequence.split('\n')[1:]) # Split the first string with info
self.sequence = sequence
except FileNotFoundError:
self.raised_errors = True
self.log += 'Cannot get genomic.fna\n'
def set_nucleotide_sequences_to_genes(self): # TODO: document
for gene in self.genes:
start = gene.start - 1 # String indexes start with 0
end = gene.end # String slice doesn't include end, so that's ok
gene.nucleotide_sequence = self.sequence[start:end]
def set_everything(self): # TODO: document
self.read_feature_table()
self.read_protein_faa()
self.set_gene_positions()
self.read_genomic_fna()
self.set_nucleotide_sequences_to_genes()