File position and buffers

C

Cee Joe

Hi Jake,

I would still need the header intact, which should be away from the rest
of the entry:
gi|329295464|ref|NM_2005745.3Acc1| Def1 zgc:65895 (zgc:65895), mRNA
AGCTCGGGGGCTCTAGCGATTTAAGGAGCGATGCGATCGAGCTGACCGTCGCG

instead of:

">gi|329295464|ref|NM_2005745.3Acc1| Def1 zgc:65895 (zgc:65895),
mRNAAGCTCGGGGGCTCTAGCGATTTAAGGAGCGATGCGATCGAGCTGACCGTCGCG>"

Still need the header line so I can extract information from that and
the lines after that in each entry. Your delete() will delete all the
newlines, which would not be beneficial in this scenario. Thanks for the
input, appreciate it.

-Cee
 
7

7stud --

Cee Joe wrote in post #995830:
7stud -- wrote in post #995821:

I understand the logic, it makes sense. What if the file looked like
this, where there is one newline seperating the entries? :

What if you had presented that possibility from the very beginning?


require 'stringio'

str =<<ENDOFSTRING
gi|329295464|ref|NM_2005745.3Acc1| Def1 zgc:65895 (zgc:65895), mRNA AGCTCGGGGGCTCTAGCGATTTAAGGAGCGATGCGATCGAGCTGACCGTCGCG

gi|456299107|ref|NM_2342343.3Acc2| Def2 zgc:65895 (zgc:65895), mRNA GTCGCTGGGTCGAAAAGTGGTGCTATATCGCGGCTCGCGTCGATGTCGCGATG
CGTGCGCGCGAGAGCGCGCTATGATGAAAGGATGAGAGAG

gi|3542945647|ref|NM_7453343.5Acc3| Def3 zgc:65895 (zgc:65895), mRNA
CGTGCGGGGABCCGTACGTGCCGTGGGGGTTT
AATAGCGCGCCATCTGAGCAG
TTAGTCGCTGACGCATGCACG

ENDOFSTRING

input = StringIO.new(str)
buffer = ''

input.each do |line|
if line[0, 1] == '>'
if buffer != ''
puts buffer #or do something else to buffer
puts '-' * 20
end

buffer = ''
buffer << line
else
buffer << line.sub(/ \n+ \z /xms, '')
end

end

puts buffer #or do something else to buffer

--output:--
gi|329295464|ref|NM_2005745.3Acc1| Def1 zgc:65895 (zgc:65895), mRNA AGCTCGGGGGCTCTAGCGATTTAAGGAGCGATGCGATCGAGCTGACCGTCGCG
CGTGCGGGGABCCGTACGTGCCGTGGGGGTTTAATAGCGCGCCATCTGAGCAGTTAGTCGCTGACGCATGCACG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top