So i have been tasked with joining text in a text file but its like whatever i try is not working. I tried split but it needs strings and not arrays and join doesn't help me at all, since i have code that already does that job.
The text file with the words is as follows (filename = demo_fasta_file_2019.fsa):
>sequence_1
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
>sequence_2
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
GATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGAGATCGATCGA
>sequence_3
TTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAA
TTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAATTTTGGAAAA
>sequence_4
GGTTAACCATGGATC
And the code that i have is as follows:
#def Read_FastA_Names_And_Sequences(filepath):
#############
filepath=str("demo_fasta_file_2019.fsa")
##sequence_names,sequences = Read_FastA_Names_And_Sequences(filepath)
sequence_names=[]
sequences=[]
number_of_sequences=4
#############
textfile = open(filepath, 'r')
sequence = textfile.readlines()
for i in sequence:
if i.__contains__('>'):
a=i[1:]
sequence_names.append(a[:a.__len__()-1])
i=+1
print(sequence)
#list1 = sequence
#s = "\n"
#s = s.join(list1)
#print(s)
list2 = sequence
words2 = list2.split(">")
print(words2)
So my question is, how can i join only the text without >sequence_1, >sequence_2, >sequence_3, >sequence_4 ?
This can be easily achieved using Biopython , which might also be useful for further tasks on fasta files:
from Bio import SeqIO
concatenated_sequence = ""
fasta_sequences = SeqIO.parse(open(input_file),'fasta')
for fasta in fasta_sequences:
# id is stored in fasta.id
# the sequence is stored in fasta.seq, and need to be transformed to str()
concatenated_sequence += str(fasta.seq)
您可以使用生成器表达式过滤不以>
开头的行,并使用str.join
将它们连接起来:
print(''.join(line for line in open("demo_fasta_file_2019.fsa") if not line.startswith('>')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.