.fasta 파일이있는 폴더와 fasta 폴더를 수정해야하는 수정 목록이있는 csv 파일이 있습니다.
일반적인 fasta 파일 (두 번째 줄은 실제 데이터) :
> TCTCG (this is called the header)
TAGACTGTGTCGATATGCAATAAACATATTAACTACAGGTATTCGGGTAT
csv 파일에는 세 개의 열이 있습니다. 첫 번째 열은 이전 디렉토리의 파일 이름에 해당하는 이름이고 세 번째 열에는 새 헤더가되어야하는 새 헤더가 있으므로 >TCTCG
세 번째 열의 항목으로 대체되고 두 번째 행은 fasta 파일은 동일하게 유지됩니다.
이 작업을 수행하는 가장 좋은 방법은 csv 파일의 첫 번째 열 (이전 파일 이름)을 추출하고 첫 번째 열의 이름을 사용하여 이전 폴더를 반복하고 두 번째 줄을 모두 복사하는 것입니다. 그런 다음 csv 파일의 세 번째 열에서 모든 새 헤더를 복사 한 다음 새 디렉토리를 만들고 세 번째 열 항목과 이전 파일의 두 번째 줄을 각각의 새 파일 (이전 이름 사용)에 붙여 넣습니다.
모든 이전 파일에서 두 번째 줄을 추출하고 csv 파일의 세 번째 열을 읽을 수 있지만 새 디렉터리를 만들고 새 줄을 "쓰기"/ "추가"하려고하면 아무 일도 일어나지 않습니다. 파일 I / O에 대한 경험이 거의 없기 때문에 지금은 무엇이 잘못되었는지 알 수 없습니다.
import glob, os
from typing import Counter
def main():
header_changes_knife = open('header_changes.csv','r') # the name of the csv file where the first column is the list of names of files and the third columns is the headers
for i in header_changes_knife:
firstColumn = [line.split(',')[0] for line in header_changes_knife] # makes a list of the first column of the header changes file, the name of the fasta file
header_changes_knife.seek(0)
third_column_read = [line.split(',')[2] for line in header_changes_knife] #makes a list of the third column of the header changes file, the new headers
my_pass_to_fasta_opener = my_fasta_opener(firstColumn) # passes the first column to the function that actually reads and opens the fasta files
for my_new_dir in header_changes_knife:
os.chdir('C:\\Users\\dhaka\\OneDrive\\Desktop\\Semester material\\Data Skills class\\All Homework\\two\\10\\pauls_dna_seqs\\Updated directory')
make_new_file = open(firstColumn,"w")
make_new_file.writelines(firstColumn)
make_new_file.writelines(third_column_read)
def my_fasta_opener(my_list):
counter = 0
for my_file in my_list:
os.chdir('C:\\Users\\dhaka\\OneDrive\\Desktop\\Semester material\\Data Skills class\\All Homework\\two\\10\\pauls_dna_seqs')
file_open = open(my_list[counter])
file_open.readline()
second_line = file_open.readline()
return second_line
counter += 1
main()
내장 csv
모듈 을 사용하여 많은 노력없이 CSV 파일을 구문 분석 할 수 있습니다 . 여기에서는 CSV 파일에 각 헤더가있는 열이 두 개 이상 있다고 가정합니다.
FASTA 파일은 우리가 관심있는 두 가지로 나눌 수 있습니다 : header
와 data
. 모든 파일에 대해 CSV에 따라 헤더를 업데이트합니다. 그런 다음 업데이트 된 데이터 구조를 디스크에 저장합니다.
import csv
def read_csv(filename):
""" Returns a list of rows, where each row is a dict """
print(f'Reading csv file {filename}')
with open(filename) as f:
return list(csv.DictReader(f))
def read_fasta_file(filename):
""" Returns a string tuple (header, rest_of_the_file) """
print(f'Reading fasta file {filename}')
with open(filename) as f:
content = f.read()
lines = content.spltlines()
# header is the first line
# data is the rest of the file
header, data = lines[0], '\n'.join(lines[1:])
return header, data
# Assumption: We have at least the columns
# target_file: The file you want to update
# new_header: The updated header
updates = read_csv('myfile.csv')
fasta_files = ['data1.fasta', 'data2.fasta']
# This is dict of the form
# {
# 'data1.fasta': ('TCTCG', '...'),
# 'data2.fasta': ('TCTCG', '...'),
# }
fasta_files = {filename: read_fasta_file(filename) for filename in fasta_files}
# Produce a new dict with the same format, but updated header values
updated_files = {}
for update in updates:
target_filename = updates['target_file']
old_header, data = fasta_files[target_filename]
new_header = updates['new_header']
updated_files[target_filename] = (new_header, data)
# Write the changes to disk
for filename, (header, data) in updated_files:
print(f'Outputting to updated_{filename}')
content = header + '\n' + data
with open(f'updated_{filename}', 'w') as f:
f.write(content)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다