MATLAB Answers

fastawrite error with string array as input datatype

1 view (last 30 days)
Dear MATLAB community,
I would like to create a fasta file from a cell array of character vectors. I have used the fastawrite function in Bioinformatics toolbox but I encounter the following error:
a=top50.dat.aa{2,1};
% 'a' is 50 X 1 cell array of character vectors
fastawrite('test.fasta',a)
Error using fastawrite (line 123)
Data is not a valid sequence or structure.
I have also tried to convert 'a' to a string but the error is still the same:
fastawrite('test.fasta',string(a))
Error using fastawrite (line 123)
Data is not a valid sequence or structure.
I have solved this issue by using a loop (since fasta write appends an existing file instead of overwriting it) but I would like to know if a more direct solution exists.
for i=1:50
fastawrite('test.fasta', string(a(i,1)))
end
Considering that since MATLAB 2018 all functions which take character array as an input work with string arrays, I would like to know if there is an issue with my code or with fastawrite.
Thank you,
Akshay
IIT D

  2 Comments

Akshay Chenna
Akshay Chenna on 20 Jun 2020
Thank you @madhan for the quick reply, I was refering to this page https://www.mathworks.com/help/matlab/matlab_prog/update-your-code-to-accept-strings.html
which states that any function that can accept character vector as input also accepts strings. So shouldnt this string array:
fastawrite('test.fasta',string(a))
work inplace of character array?

Sign in to comment.

Accepted Answer

Arthur Goldsipe
Arthur Goldsipe on 20 Jun 2020
You generally should be able to cell arrays of character vectors and string vectors interchangeably, and a character vector interchangeably with a single string. Both of these are two ways of encoding text. But if a function only excepts one piece of text, then you should expect to see the same error if you specify more than one piece of text. That is the case in the error you get when calling fastawrite with a cell of character vector or with a vector of strings.
In other words, (and as described in the documentation that madhan ravi points to), fastawrite only accepts a single piece of text as the data. This piece of text can be specified as a character vector or as a single string. If you want to write several pieces of text into a single FASTA file, then you first need to join them into a single piece of text. For example, you could try the following:
a=top50.dat.aa{2,1};
% 'a' is 50 X 1 cell array of character vectors
fastawrite('test.fasta',join(a, ''))

  1 Comment

Akshay Chenna
Akshay Chenna on 22 Jun 2020
Thank you for making it clear. This worked for me:
a=top50.dat.aa{2,1};
% 'a' is 50 X 1 cell array of character vectors
fastawrite('test.fasta',join(string(a), ''))

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!