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




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"]
  • 1
    $\begingroup$ How about c = Classify[Join@@Map[Thread[StringSplit[Import["txt"<>#<>".txt"], "\n"]-> "Gene"<>#]&, ToString/@Range[2]]]? $\endgroup$ – b3m2a1 Nov 21 '18 at 7:09
  • $\begingroup$ 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]? $\endgroup$ – Laughing Man Nov 22 '18 at 5:49
  • $\begingroup$ <> 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. $\endgroup$ – b3m2a1 Nov 22 '18 at 5:50
  • $\begingroup$ 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! $\endgroup$ – Laughing Man Nov 23 '18 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.