/// <summary> /// Parses the provided FASTA sequence data for a single sequence. /// </summary> /// <param name="data">The string to parse.</param> /// <returns> /// A new <see cref="IActualSequence"/> object parsed from the provided /// data string. Depending on the data passed in, this could either be /// an <see cref="AminoAcidSequence"/> object or a /// <see cref="NucleicAcidSequence"/> object. If the sequence is /// ambiguous, the latter type is assumed and returned. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="data"/> is <see langword="null"/>. /// </exception> /// <exception cref="FormatException"> /// <paramref name="data"/> is empty or all whitespace. /// -or- /// <paramref name="data"/> is in an incorrect format. /// </exception> public static IActualSequence Parse(string data) { if (data == null) { throw new ArgumentNullException(nameof(data), "The sequence data to parse cannot be null."); } if (string.IsNullOrWhiteSpace(data)) { throw new FormatException("The sequence data to parse cannot be empty or all whitespace."); } if (NucleicAcidSequence.TryParse(data, out NucleicAcidSequence nucleicAcidSequenceData)) { return(nucleicAcidSequenceData); } else if (AminoAcidSequence.TryParse(data, out AminoAcidSequence aminoAcidSequenceData)) { return(aminoAcidSequenceData); } else { throw new FormatException("The provided string is not a valid FASTA amino or nucleic acid sequence."); } }
/// <summary> /// Converts a string representation of a FASTA file nucelic acid /// sequence to its <see cref="NucleicAcidSequence"/> equivalent. /// A return value indicates whether the conversion succeeded. /// </summary> /// <param name="s">A string containing the sequence to convert.</param> /// <param name="result"> /// When this method returns, contains the /// <see cref="NucleicAcidSequence"/> value equivalent of the /// sequence contained in <paramref name="s"/>, if the conversion /// succeeded, or an empty sequence if the conversion failed. The /// conversion fails if the <paramref name="s"/> parameter is /// <see langword="null"/>, empty, all whitespace, or is not of the /// correct format. This parameter is passed uninitialized; any value /// originally supplied in result will be overwritten. /// </param> /// <returns> /// <see langword="true"/> if <paramref name="s"/> was converted /// successfully; otherwise, <see langword="false"/>. /// </returns> /// <seealso cref="Parse(string)"/> public static bool TryParse(string s, out NucleicAcidSequence result) { if (s == null) { result = default; return(false); } string cleanedUpCharacters = s.RemoveAllWhitespace(); if (!IsValidSequence(cleanedUpCharacters)) { result = default; return(false); } result = new NucleicAcidSequence(cleanedUpCharacters.ToUpperInvariant()); return(true); }