1

I need to run an awk script to detect local maxima(peaks) in a spectrum. This spectrum is in the form of two columns in a text file.

This is part of a larger project which will identify whether these peaks represent certain compounds(metabolites really).

I have searched on Google and myriad other places i really cant find anything for running awk scripts in R.

EDIT: The awk script i am using looks something like this-

awk 'BEGIN{dydx = 0;}
{ 
  if(NR > 1)
     { dydx = ($2 - y0)/($1 - x0); } 
  if(NR > 2 && last * dydx < 0)
     { printf( "%.4f  %.4f\n", (x0 + $1)/2, log((dydx<0)?-dydx:dydx)); } ;
  last=dydx; x0=$1; y0=$2
}' /home/chaitanya/Work/nmr_spectra/caffeine/pdata/1/spectrumtext.txt  | awk '$2 > 17'

You can also see this question i had asked yesterday. Please note it is NOT the same question.

Community
  • 1
  • 1
Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
  • are you [asking the same question as yesterday](http://stackoverflow.com/q/9060569/471093)? – baptiste Jan 31 '12 at 06:28
  • @baptiste Not really the same question. No. I have found one solution for yesterday's problem. And i am trying to make it work. I am still looking for other possible solutions. So please dont consider this a duplicate and down vote this or the other question out of that reason. – Chaitanya Nettem Jan 31 '12 at 06:31
  • it's just not clear if you actually followed up on yesterday's idea. If you found a solution, please add it to the discussion, and explain better the real problem you're trying to solve. – baptiste Jan 31 '12 at 06:36
  • @baptiste is right (as always). You should always be transparent. Post as much information as you can and link to any relevant material. – Xu Wang Jan 31 '12 at 06:42
  • @baptiste I changed both the questions. Please have a look. I think they are pretty clear right now. – Chaitanya Nettem Jan 31 '12 at 06:58

3 Answers3

7

you should be able to do

system('your awk command here')

If you have single quotes in your awk command, either do as Hong Ooi suggests and put your awk command in a separate file and then just run that from the system command or escape the single quotes:

> system('echo \'hello guy!\' ')
hello guy!
Xu Wang
  • 10,199
  • 6
  • 44
  • 78
3

Update for Windows 10 users now able to use AWK natively:

To Windows 10 users with WSL (windows subsystem for linux) installed, you can run AWK in windows from an R console with:

>  system( 'wsl awk \'BEGIN { print "Hello, world" }\' ')
Hello, world
kabammi
  • 326
  • 3
  • 14
1

If you have reasonably smooth spectral data, there are several functions available which do a good job of locating local maxima and minima (aka "peaks"). I like the tools in pastecs , but take a look at the following packages (in my rough order of choice) :

pastecs,
Peaks,
seewave,
ppc

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • interesting, thanks for the list. I've only ever used PRocess and custom functions, I should check these out. – baptiste Jan 31 '12 at 18:40