-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddGeneName.py
More file actions
39 lines (29 loc) · 1.21 KB
/
Copy pathAddGeneName.py
File metadata and controls
39 lines (29 loc) · 1.21 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
import csv
# Open the CSV file
file='file.csv'
with open(file, 'r') as csv_file:
reader = csv.reader(csv_file)
rows = list(reader)
# Modify the header row to add the new column
header = rows[0]
header.append('Description')
# Iterate over the rows (excluding the header)
for row in rows[1:]:
gene_id = row[0]
# Open the FASTA file
with open('IcePlant_ProteinSequence.fasta', 'r') as fasta_file:
fasta_lines = fasta_file.readlines()
# Search for the corresponding gene_id in the FASTA file
for i in range(len(fasta_lines)):
if fasta_lines[i].startswith('>' + gene_id):
# Extract the description text
description = fasta_lines[i].split('Similar to', 1)[-1].strip()
# Exclude text after and including "AED" in the description
description = description.split('AED', 1)[0].strip()
# Add the description to the current row
row.append(description)
break
# Write the modified CSV file
with open(file, 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerows(rows)