This is a test version of Biostars. For the public version, visit https://www.biostars.org.
intersect two file

I have 2 transcript file - one contain only transcript id and other file contain transcript id, gene name and gene description.

1st file looks like----

ENST00000009589.8
ENST00000084795.9
ENST00000196551.8
ENST00000202773.13
ENST00000211372.9
ENST00000216146.9
ENST00000222247.10
ENST00000225430.9
ENST00000225655.6

2nd file looks like ----

ENST00000387314.1   MT-TF   mitochondrially encoded tRNA-Phe (UUU/C) [Source:HGNC Symbol;Acc:HGNC:7481]
ENST00000389680.2   MT-RNR1 mitochondrially encoded 12S rRNA [Source:HGNC Symbol;Acc:HGNC:7470]
ENST00000387342.1   MT-TV   mitochondrially encoded tRNA-Val (GUN) [Source:HGNC Symbol;Acc:HGNC:7500]
ENST00000387347.2   MT-RNR2 mitochondrially encoded 16S rRNA [Source:HGNC Symbol;Acc:HGNC:7471]
ENST00000386347.1   MT-TL1  mitochondrially encoded tRNA-Leu (UUA/G) 1 [Source:HGNC Symbol;Acc:HGNC:7490]
ENST00000361390.2   MT-ND1  mitochondrially encoded NADH:ubiquinone oxidoreductase core subunit 1 [Source:HGNC Symbol;Acc:HGNC:7455]
ENST00000387365.1   MT-TI   mitochondrially encoded tRNA-Ile (AUU/C) [Source:HGNC Symbol;Acc:HGNC:7488]
ENST00000387372.1   MT-TQ   mitochondrially encoded tRNA-Gln (CAA/G) [Source:HGNC Symbol;Acc:HGNC:7495]
ENST00000387377.1   MT-TM   mitochondrially encoded tRNA-Met (AUA/G) [Source:HGNC Symbol;Acc:HGNC:7492]

the 2nd file is download from biomart(ensembl) and i just want to know that my 1st file where intersect to 2nd file then i can know the gene name of my 1st transcript file.

thanks in advance

intersect transcript_id

thanks for you valuable suggestion.

4 answers

quick n' dirty: use grep -f file1 file2

right way: use sort to sort both files on the ensembl ID and the join to get the instersection.

A simple modification of Pierre's suggestion, building the proper pattern before the query, would be more than enough:

perl -lne 'print "^$_\\s"' file1.txt | grep -f - file2.txt

Or You can use dplyr R package to do the intersection.

Base R works as well:

joint = df2[df2$ens_id %in% df1$ens_id,]

Since the transcript ids do not overlap, you can use:

cat file1 | while read line ; do grep "${line}" file2 >> file3; done

If you're interested in a few columns of the output, say col1 and col2:

cat file1 | while read line ; do grep "${line}" file2 | awk '{print $1,$2}' >> file3; done

That's a very slow solution compared to grep -f . Furthermore I said above that it was a quick 'n dirty general solution for example if you're searching for ENST00005678 in lines like:

ENST00001234 MT-RNR2 mitochondrially encoded 16S rRNA similar to ENST00005678

I think you meant to comment under Jorge's comment?

No he didn't. Pierre's suggestion is to include all possible patterns into a single grep query, which is much faster than to perform one grep per pattern, which implies reading the second file entirely on each query.

Aha, okay, thanks. I'll delete my answer then.

Log in to answer this question.