<p>I have a file (fasta file to be specific) that I would like to index, so that I can quickly locate any substring within the file and then find the location within the original fasta file.</p>

<p>This would be easy to do in many cases, using a Trie or substring array, unfortunately the strings I need to index are 800+ MBs which means that doing them in memory in unacceptable, so I'm looking for a reasonable way to create this index on disk, with minimal memory usage.</p>

<p>(edit for clarification)</p>

<p>I am only interested in the headers of proteins, so for the largest database I'm interested in, this is about 800 MBs of text.</p>

<p>I would like to be able to find an exact substring within O(N) time based on the input string. This must be useable on 32 bit machines as it will be shipped to random people, who are not expected to have 64 bit machines.</p>

<p>I want to be able to index against any word break within a line, to the end of the line (though lines can be several MBs long).</p>

<p>Hopefully this clarifies what is needed and why the current solutions given are not illuminating.</p>

<p>I should also add that this needs to be done from within java, and must be done on client computers on various operating systems, so I can't use any OS Specific solution, and it must be a programatic solution.</p>

[{'answer_id': 53195, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 0, 'selected': False, 'text': "<p>I talked to a few co-workers and they just use VIM/Grep to search when they need to. Most of the time I wouldn't expect someone to search for a substring like this though.</p>\n\n<p>But I don't see why MS Desktop search or spotlight or google's equivalent can't help you here.</p>\n\n<p>My recommendation is splitting the file up --by gene or species, hopefully the input sequences aren't interleaved. </p>\n"}, {'answer_id': 53541, 'author': 'Stu Thompson', 'author_id': 2961, 'author_profile': 'https://Stackoverflow.com/users/2961', 'pm_score': 1, 'selected': False, 'text': '<p>In some languages programmers have access to <strong><em>"direct byte arrays"</em></strong> or <strong><em>"<a href="http://en.wikipedia.org/wiki/Memory-mapped_file" rel="nofollow noreferrer">memory maps</a>"</em></strong>, which are provided by the OS.  In java we have <strong><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/MappedByteBuffer.html" rel="nofollow noreferrer">java.nio.MappedByteBuffer</a></strong>.  This allows one to work with the data as if it were a byte array in memory, when in fact it is on the disk.  The size of the file one can work with is only limited by the OS\'s virtual memory capabilities, and is typically ~&lt;4GB for 32-bit computers.  64-bit?  In theory 16 exabytes (17.2 billion GBs), but I think modern CPUs are limited to a 40-bit (1TB) or 48-bit (128TB) address space.</p>\n\n<p>This would let you easily work with the one big file.</p>\n'}, {'answer_id': 60533, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 1, 'selected': False, 'text': '<p>The <a href="http://en.wikipedia.org/wiki/Fasta_format" rel="nofollow noreferrer">FASTA file format</a> is very sparse.  The first thing I would do is generate a compact binary format, and index <em>that</em> - it should be maybe 20-30% the size of your current file, and the process for coding/decoding the data should be fast enough (even with 4GB) that it won\'t be an issue.</p>\n\n<p>At that point, your file should fit within memory, even on a 32 bit machine.  Let the OS page it, or make a ramdisk if you want to be certain it\'s all in memory.</p>\n\n<p>Keep in mind that memory is only around $30 a GB (and getting cheaper) so if you have a 64 bit OS then you can even deal with the complete file in memory without encoding it into a more compact format.</p>\n\n<p>Good luck!</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 2790340, 'author': 'Erik Garrison', 'author_id': 238609, 'author_profile': 'https://Stackoverflow.com/users/238609', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t imagine that the original poster still has this problem, but anyone needing FASTA file indexing and subsequence extraction should check out fastahack: <a href="http://github.com/ekg/fastahack" rel="nofollow noreferrer">http://github.com/ekg/fastahack</a></p>\n\n<p>It uses an index file to count newlines and sequence start offsets.  Once the index is generated you can rapidly extract subsequences; the extraction is driven by fseek64.</p>\n\n<p>It will work very, very well in the case that your sequences are as long as the poster\'s.  However, if you have many thousands or millions of sequences in your FASTA file (as is the case with the outputs from short-read sequencing or some <em>de novo</em> assemblies) you will want to use another solution, such as a disk-backed key-value store.</p>\n'}]
['https://Stackoverflow.com/questions/53172', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3900/']