http://www.perlmonks.org?node_id=1020241

pickleswarlz has asked for the wisdom of the Perl Monks concerning the following question:

I am pretty new to Perl, but have what I think is a complex problem that is way beyond my abilities.

I have one .csv file that contains the locations of genes in the genome that looks like this:

Chromosome Start End chr1 220 250 chr1 259 298 chr2 225 263 chr3 310 344 [...]

where the first column indicates the chromosome number that the locations map to, second column is the start position of the gene in the genome, and the third column is the end position of the gene in the genome.

I have a second .csv file that gives a set of other locations in the genome with identification numbers:

ID# position ID1 214 ID2 222 ID3 258 [...]

where the first column is the identifier (alphanumeric), and the second column is the position in the genome.

What I would like to do is find the two locations from the second file that are closest to either side of location given in the first file, and output these to a new file. For example, for this line in .csv file 1:

chr1   220   250

search CSV file 2 for the two closest values on either side of 220 AND the two closest values on either side of 250, given the following output .csv file:

Chromosome Start End Start(-) Start(+) End (-) End (+) chr1 220 250 214 222 222 258

I'm a Windows user, and relatively competent with the command line. I'll have to run this Perl script on multiple files, so I'd love to be able to specify the input and output files in the command line as I go rather than specifying them in the script itself. The files containing the ID information is huge, around 500,000 lines, so I'm wondering if it might be better to transform that file into a hash first.

Thank you!