10.2 years ago by
United States
One-line solution
ruby -e 'first_line = true; while line = STDIN.gets; line.chomp!; if line =~ /^>/; puts unless first_line; print line[1..-1]; print ","; else; print line; end; first_line = false; end; puts' < s001.fasta
Simple script
Here is a Ruby script that does this:
#!/usr/bin/ruby
first_line = true
while line = STDIN.gets
line.chomp!
if line =~ /^>/
puts unless first_line
print line[1..-1]
print "," # <-- Change this to "\t" and it's a convert-fasta-to-tab
else
print line
end
first_line = false
end
puts
- Just save it to a file.
- Name the file
convert-fasta-to-csv
- to make it executable, run
chmod +x ./convert-fasta-to-csv
Usage
./convert-fasta-to-csv < f001.fasta > f001.fasta.csv
To do it in batch run all .fasta
files in current folder:
for i in *.fasta; do ./convert-fasta-to-csv < $i > $i.csv; done
System Requirements
You probably already have Ruby but it may not always be installed by default.
- To install it on Ubuntu or Debian run:
sudo apt-get install ruby
- On RedHat or CentOS, run:
sudo yum install ruby
- On Windows, install from http://rubyinstaller.org/
- On Mac, you already have it (it's a part of the operating system).
What have you tried so far ?
@Pierre Lindenbaum: Ended up just using Regex in TextPad. Problem is solved. Thanks!