Created
December 28, 2023 15:59
-
-
Save pjmagee/68116175cd13f23199daf9ac06df9744 to your computer and use it in GitHub Desktop.
my DNA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var data = Load(@"C:\Users\patri\OneDrive\Desktop\AncestryDNA.txt"); | |
| data.Dump(); | |
| } | |
| public class GeneticData | |
| { | |
| private ReadOnlyMemory<char> Line { get; set; } | |
| public ReadOnlyMemory<char> Rsid => GetField(0); | |
| public ReadOnlyMemory<char> Chromosome => GetField(1); | |
| public ReadOnlyMemory<char> Position => GetField(2); | |
| public ReadOnlyMemory<char> Allele1 => GetField(3); | |
| public ReadOnlyMemory<char> Allele2 => GetField(4); | |
| private ReadOnlyMemory<char> GetField(int index) | |
| { | |
| var span = Line.Span; | |
| var start = 0; | |
| for (var i = 0; i < index; i++) | |
| { | |
| start = span.IndexOf('\t') + 1; | |
| span = span.Slice(start); | |
| } | |
| var end = span.IndexOf('\t'); | |
| if (end == -1) end = span.Length; // Last field | |
| return Line.Slice(start, end); | |
| } | |
| private GeneticData(ReadOnlyMemory<char> line) | |
| { | |
| Line = line; | |
| } | |
| public static GeneticData FromLine(string line) => new GeneticData(line.AsMemory()); | |
| } | |
| public static ImmutableArray<GeneticData> Load(string filePath) | |
| { | |
| var lines = File.ReadAllLines(filePath); | |
| var builder = ImmutableArray.CreateBuilder<GeneticData>(lines.Length - 1); | |
| foreach (var line in lines.Skip(1)) | |
| { | |
| builder.Add(GeneticData.FromLine(line)); | |
| } | |
| return builder.ToImmutableArray(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment