# How to parse through a .txt file in the Classify function and assign a class to each item?

I have 2 txt files that contain 500 DNA sequences each. Both files use 2 hard returns to separate the sequences like so:

ATCGATCT...

ATCGATCT...

ATCGATCT...


I want to parse these two files into the Classify function such that each item in the txt file is assigned a class, Gene1 and Gene2. I have already separated the two classes of DNA in their respective txt files.

Because of this, I was thinking I can classify all the items in txt1 as Gene1 and all the items in txt2 as Gene2. As for how to do this, I am not sure. Does Mathematica allow parsing of items from a txt file into the Classify function?

I was hoping it would be a simple line of code such as this:

c = Classify[{Import["txt1.txt"]} -> "Gene1", {Import["txt2.txt"]} -> "Gene2"]

• How about c = Classify[Join@@Map[Thread[StringSplit[Import["txt"<>#<>".txt"], "\n"]-> "Gene"<>#]&, ToString/@Range[2]]]? – b3m2a1 Nov 21 '18 at 7:09
• Thank you for the response! I was wondering if you could explain what the <># symbols in [Import["txt" <> # <> ".txt"], "\n"] -> "Gene" <> #] &, ToString /@ Range[2]] means. Also, why are you using @Range[2]? – Laughing Man Nov 22 '18 at 5:49
• <> is Mathematica shorthand for StringJoin and # is shorthand for Slot in a Function (worth looking all these terms up in the docs as understanding them will be very helpful moving forward). Pro-tip: you can select a symbol and use Command-Shift-F (or Control-F1 on Windows) to look up what it does. – b3m2a1 Nov 22 '18 at 5:50
• Hey b3m, I've been trying out the code you provided in its different components and I've been getting stuck at the import part. Specifically, I don't understand Import["txt"<>#<>".txt"], "\n"]. Does "\n" tell the function that 1 hard enter signifies the separation between each item in the txt file? I am also not sure where I input the filepath for the 2 txt files. I thought I enter the filepaths in the "txt"sections like so Import["C:\\filepath\\test1.txt"<>#<>"C:\\filepath\\test2.txt"], but this gives me a File not found during import error. Thank you for your help in advance! – Laughing Man Nov 23 '18 at 6:59