rnaeye has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am a geneticist/biochemist and trying to use Perl to analyze DNA sequence data. I have a giant text file (99 million lines; ~ 7GB). The file contains DNA sequences. I need to extract duplicate lines based on the BeadIDs from input file and print it into different file. BeadIDs are written in field one (first column), for example followings are BeadIDs:
99_99_992 99_999_930 99_999_930 99_99_999 99_99_999 99_99_9992 99_99_9992 99_99_9992
Real Input file looks like this (total ~99 million lines):
99_99_992 chr16.fsa 11064 11088 TAGGTAAACGAGGAGTCTTTCAATA + - 25 99_999_930 chr04.fsa 148776 148800 TGCAAACGAGGACAACCTGTTTG +TG + 25 99_999_930 chr04.fsa 148882 148916 CCGGAAAAATTTGCTATTGGAAG +AGGTGGCGCTGG - 35 99_99_999 chr12.fsa 468017 468049 TTTTCGGTGACGGAAATACGCTTC +AGAGACCCT + 33 99_99_999 chr12.fsa 468138 468162 CTATGTTTTCTTACTCCTATGTCT +A - 25 99_99_9992 chr12.fsa 468138 468162 CTATGTTTTCTTACTCCTATGTC +TA - 25 99_99_9992 chr12.fsa 468138 468162 CTATGTTTTCTTACTCCTATGTC +TA - 25 99_99_9992 chr12.fsa 468138 468162 CTATGTTTTCTTACTCCTATGTC +TA - 25 ...
The output that I would like to have is shown below (BeadIDs must be repeated exactly TWICE, not unique, not triplicate, not quadruplicate, but I want the entire line not only column one):
99_999_930 chr04.fsa 148776 148800 TGCAAACGAGGACAACCTGTTTG +TG + 25 99_999_930 chr04.fsa 148882 148916 CCGGAAAAATTTGCTATTGGAAG +AGGTGGCGCTGG - 35 99_99_999 chr12.fsa 468017 468049 TTTTCGGTGACGGAAATACGCTTC +AGAGACCCT + 33 99_99_999 chr12.fsa 468138 468162 CTATGTTTTCTTACTCCTATGTCT +A - 25
I have written a code that would choose duplicate lines if my input file had only single column(field). However, as you can see my actual file has 7 fields, and I need to choose the duplicate lines based on first field, and print the entire line. Sor far, I could come up with following script that would work on a single column file. However, my Perl knowledge is not advance enough to come up for a solution when input file is in 7 field format.
My code so far:
#!/usr/bin/perl #use strict; use warnings; $numArgs = $#ARGV + 1; if ($numArgs < 2) { print "USAGE: extract_duplicate_rows <input file> <# of duplicate +rows>\n\n"; exit(0); } $finput = $ARGV[0]; $duplicate_count = $ARGV[1]; open(IFILE, "<", $finput) or die $!; chomp($prev_line = <IFILE>); $match_count = 1; while (1) { chomp($line = <IFILE>); if ($line eq $prev_line) { $match_count++; } else { if ($match_count == $duplicate_count) { print "$prev_line\n"; } $prev_line = $line; $match_count = 1; if (eof(IFILE)) { last; } } } close IFILE;
I was wondering if you guys could make me suggestions. Thanks for your help. ~Erbay
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to extract duplicate lines from a text file?
by Eliya (Vicar) on Mar 01, 2011 at 06:42 UTC | |
by rnaeye (Friar) on Mar 01, 2011 at 13:30 UTC | |
|
Re: How to extract duplicate lines from a text file?
by NetWallah (Canon) on Mar 01, 2011 at 06:26 UTC | |
by rnaeye (Friar) on Mar 01, 2011 at 13:28 UTC |