Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to grep several words in file1, and use each word to grep what follows after its match in file2. And then I want to add the string that followed the match to the word I used into file03, so that file03 contains

word1 [the thing that was found using word1 in a grep in file2]
word2 [the thing that was found using word1 in a grep in file2]

Part of files I have are: file1:

JAN1319964: PGSC|PGSC0003DMP400068385_PGSC0003DMT400096710  PGSC|PGSC0003DMP400062633_PGSC0003DMT400090958 PGSC|PGSC0003DMP400066271_PGSC0003DMT400094596 PGSC|PGSC0003DMP400064671_PGSC0003DMT400092996 PGSC|PGSC0003DMP400068967_PGSC0003DMT400097292
JAN1327159: PGSC|PGSC0003DMP400016823_PGSC0003DMT400024599 PGSC|PGSC0003DMP400017933_PGSC0003DMT400026257 Dul|Dul_comp58749_c0_seq2-1
JAN1330513: Des|Des_g36886.t1 PGSC|PGSC0003DMP400049952_PGSC0003DMT400073802

File2:

>Dul|Dul_g997.t1
ESECRVQYFSDDEVSPVTEVTGRRGSICVVCRLVPKASVSESSFLK
>Dul|Dul_g998.t1
MDDKRLWEEEERRRIAVRQREERGKIYERQKALEEQEKLAAIESYQDAIRREREEEERLKEKKKKKKKTEIRDDYLDDFLPRRNDRRIPDRDRSVKRRQTFESGRHAKEHAPPTKRRRGGEVGLSNILEEIVDTLKNNVNVSYLFLKPVTRKEAPDYHKYVKRPMDLSTIKERARKLEYKNRGQFRHDVAQITINAHLYNDGRNPGIPPLADQLLEICDYLLEENESILAEAESAI
>Dul|Dul_g999.t1
MDDKRLWEEEERRRIAVRQREERGKIYERQKALEEQEKLAAIESYQDAIRREREEEERLKEKKKKKKKTEIRDDYLDDFLPRRNDRRIPDRDRSVKRRQTFESGRHAKEHAPPTKRRRGGEVGLSNILEEIVDTLKNNVNVSYLFLKPVTRKEAPDYHKYVKRPMDLSTIKERARKLEYKNRGQFRHDVAQITINAHLYNDGRNPGIPPLADQLLEICDYLLEENESILAEAESGIEQ
>Des|Des_g1.t1
FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK

The output I want is for this example:

JAN1319964: PGSC|PGSC0003DMP400068385_PGSC0003DMT400096710 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400062633_PGSC0003DMT400090958 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400066271_PGSC0003DMT400094596 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400064671_PGSC0003DMT400092996 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400068967_PGSC0003DMT400097292  [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
JAN1327159: PGSC|PGSC0003DMP400016823_PGSC0003DMT400024599 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400017933_PGSC0003DMT400026257 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
Dul|Dul_comp58749_c0_seq2-1
JAN1330513: Des|Des_g36886.t1  [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK
PGSC|PGSC0003DMP400049952_PGSC0003DMT400073802 [the line after the match to this name]: FRKQTVELSESDDTSISVETEDAEIENGNSPPAGLSNTTKVQLKPLYRSTIQLTPHPDGLSNTNEIK

As you can see, I simply have some information missing in file1, which is contained in file2, and needs to be added to file1. If anyone knows how to do this I would greatly appreciate it!

share|improve this question
    
I don't understand, the first line of your file1 has no identifiers that are present in the FASTA file2, what do you want to grep for? Are you trying to convert FASTA to tbl? –  terdon Jun 13 '13 at 12:31

1 Answer 1

up vote 0 down vote accepted

I don't really understand your question, so I'll answer what I think you're asking. If you have a file of identifiers of interest like this (I assume the first field is never an identifier, I also assume that at least some of the IDs are present in the sequence file, none of the ones of your example are):

Jan12345: ID1 ID2 ... IDN1
Jan67899: ID11 ID12 ... IDN2

And a Fasta file like this:

>ID1
ABCDEFG
>ID2
HIJKLMN
>IDN1
OPQRSTU
>ID11
WXYZABC
>ID12
DEFGHIJ
>IDN2
KLMNOPQ

And you want an output file like this:

Jan12345 ID1 ABCDEFG ID2 HIJKLMN ... IDN OPQRSTU

You could do something like this:

  1. Save this script as FastaToTbl and make it executable (chmod 744 FastaToTbl):

    #! /bin/sh
    gawk '{
            if (substr($1,1,1)==">")
           if (NR>1)
                 printf "\n%s\t", substr($0,2,length($0)-1)
          else 
             printf "%s\t", substr($0,2,length($0)-1)
           else 
              printf "%s", $0
    }END{printf "\n"}'  "$@"
    

    This will convert FASTA to tbl, (ID<TAB>SEQUENCE).

  2. Use FastaToTbl combined with this script to extract the IDs from file1 and the sequences from file2:

    FastaToTbl file2 | 
      perl -ne 'chomp;@a=split(/\t/); $k{$a[0]}=$a[1]; ## Collect the sequences
                                                       ## $k{ID}=SEQUENCE
          END{open(A,"file1");   ## Open ID file
             while(<A>){         ## and process it line by line
               @a=split(/\s+/);  ## Gather the IDs in array @a
               print shift(@a);  ## Print the first element (Jan123:)
               print " $_ $k{$_}" for @a; ## Print each ID and its seq
               print "\n";
     }}' 
    Jan12345:ID1 ABCDEFG ID2 HIJKLMN IDN1 OPQRSTU
    Jan67899:ID11 WXYZABC ID12 DEFGHIJ IDN2 KLMNOPQ
    
share|improve this answer
    
I tried this (for quite some time), although for some reason the first (FastaToTbl) script did not separate the "Jan..." from the rest with a \t (tab), but with a normal space. –  Stenemo Jun 13 '13 at 21:36
    
Also, I am not completely sure, but after adding a tab, I am still not getting the sequences into my output (the last 5 lines looks like this: PGSC|PGSC0003DMP400017933_PGSC0003DMT400026257 Dul|Dul_comp58749_c0_seq2-1 JAN1330513:Des|Des_g36886.t1 PGSC|PGSC0003DMP400049952_PGSC0003DMT400073802 SWtf|SW_g16502.t1 –  Stenemo Jun 13 '13 at 21:58
    
@Stenemo the FastaToTbl should be run on the FASTA file. In my example, file2 is the fasta file and file1 is the one with the ids. –  terdon Jun 13 '13 at 22:03
    
Ok, it works now, thank you so much for your help! –  Stenemo Jun 14 '13 at 8:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.