Advertisement
ZaynerTech

NMR Peak Assigner Perl

Jan 17th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.19 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. #  For NMR Peak Assignment using known assignments
  4. #  tries to match picked peaks to assignments based on
  5. #  peak closest distance
  6. #
  7. #
  8. # ./nmrasser.pl <picked peaks> <peak assignments>
  9. #
  10. #
  11. #
  12. # picked peaks file should be from NMRviewJ
  13. # Assigned peaks file should be of the format
  14. # Kind of Ugly but it works
  15. # Residue #. HN/N Chemicalshift
  16. #
  17. # ex.
  18. # 1. HN 9.3
  19. # 1. N  120.2
  20.  
  21. use Math::Complex;
  22.  
  23. my %hn = ();
  24. my %n = ();
  25. my %npeaks = ();
  26. my %hnpeaks  = ();
  27. $x = 0;
  28.  
  29. if(!$ARGV[0]){  die "Need your picked peaks to parse!"; }
  30. if(!$ARGV[1]) { die "Need your peak assignments!"; }
  31.  
  32. open(PEAKS, $ARGV[0]);
  33. open(ASS, $ARGV[1]);
  34. open(OUTPUT, "$ARGV[0].out");
  35.  
  36. if($ARGV[2])
  37.   {open(SEQ, $ARGV[2]);
  38.    @seq = <SEQ>;
  39.    }
  40.  
  41. @peaks = <PEAKS>;
  42. @ass = <ASS>;
  43.  
  44.  
  45.  
  46. #find 1Hppm and N15 ppm for each peak
  47. foreach $line (@peaks)
  48. {
  49.      
  50.    if($line =~ m/\{\}/)
  51.    {
  52.     @ln = split(/ /, $line);
  53.      $hnpeaks{ $ln[0] } = $ln[2];
  54.      $npeaks{ $ln[0] } = $ln[9];  
  55.      #print "$ln[2] : $ln[9] \n";
  56.    }
  57.    #  else { print "BALLS\n!"; }
  58. }
  59.  
  60. #Finds location of 1H N15 peaks from assignments
  61.  
  62. $a=1;
  63. foreach $line1 (@ass)
  64. {
  65.    
  66.    if($line1 =~ m/\d{1,3}\.N /)
  67.    {  
  68.       @nit = split(/\s{1,6}/, $line1);
  69.        #print "$nit[0] $nit[1]\n";  
  70.       @stuff = split(/\./, $nit[0]);
  71.        #Assign H peak to a number
  72.        $n{ $stuff[0] } = $nit[1];
  73.        #$n{ $stuff[0] }->{ seq } = substr($seq[0],$a,1);
  74.        $a++;
  75.    }
  76.  
  77.    if($line1 =~ m/\d{1,3}\.HN /)
  78.    {   @hnit = split(/\s{1,7}/, $line1);
  79.        #print "$hnit[0] $hnit[1]\n";
  80.        @stuff1 = split(/\./, $hnit[0]);
  81.        $hn{ $stuff1[0] } = $hnit[1];
  82.    }
  83.  
  84.  
  85.      #else { print "CRAP!\n"; }
  86.  
  87.  
  88. }
  89.  
  90. #print "assigns: $a\n\n";
  91. #foreach $key (keys %n) {
  92. #print "$key $n{ $key } $hn{ $key }\n";
  93. #}
  94.  
  95.  
  96.  
  97. foreach $keay (sort keys %npeaks)
  98. {
  99.  
  100.   &matcher($keay,0);
  101.  
  102.  
  103. }
  104.  
  105. foreach $keyr (keys %npeaks) {
  106.   &removedups($keyr,1);
  107.  
  108. }
  109.  
  110. foreach $keyr (keys %npeaks) {
  111.   &removedups($keyr,2);
  112.  
  113. }
  114.  
  115. foreach $keyr (keys %npeaks) {
  116.   &removedups($keyr,3);
  117.  
  118. }
  119.  
  120. foreach $keyr (keys %npeaks) {
  121.  
  122.   &removedups1($keyr);
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. $x=0;
  133. foreach $key (sort keys %npeaks) {
  134.  
  135. if($npeaks->{$key}->{ winname }->{0} > 0) { $x++; }
  136.  printf("%d N:%f HN:%f : %d N:%f HN:%f ::: %f \n",$key,  $npeaks{$key}, $hnpeaks{$key}, $npeaks->{$key}->{ winname }->{0}, $n{$npeaks->{$key}->{ winname }->{0} }, $hn{$npeaks->{$key}->{ winname }->{0}}, $npeaks-> {$key}->{ winner }->{0});
  137.  
  138. }
  139.  
  140.  
  141. print "Number matches: $x\n";
  142.  
  143.  
  144. close(OUTPUT);
  145. close(PEAKS);
  146. close(ASS);
  147. #close(SEQ);
  148.  
  149. exit;
  150.  
  151. sub matcher
  152. {
  153.  
  154. # print "###### $_[0]\n\n\n\n\n\n\n\n";
  155.  
  156. $num = $_[0];
  157.  
  158.  
  159.  
  160.  $npeaks->{ $num }->{ winner }->{0} = 1;
  161.  
  162.  foreach $keym (sort keys %hn )
  163.  {
  164.     #print "$npeaks{ $num } ** $n{ $keym } ** $hnpeaks{ $num }  ** $hn{ $keym}\n";
  165.  
  166.    
  167.     $rise = abs($npeaks{ $num } - $n{ $keym });
  168.     $run = abs($hnpeaks{ $num } - $hn{ $keym});
  169.     $c = sqrt($rise**2 + $run**2);
  170.     #print "$rise **** $run ***** $c\n\n\n";
  171.  
  172.     if($npeaks->{ $num }->{ winner }->{0} > $c && $c < 0.25)
  173.      {  
  174.  
  175.         $npeaks->{ $num }->{ winner }->{1} = $npeaks->{ $num }->{ winner }->{0}; $npeaks->{ $num }->{ winname }->{1} = $npeaks->{ $num }->{ winname }->{0};
  176.         $npeaks->{ $num }->{ winner }->{2} = $npeaks->{ $num }->{ winner }->{1}; $npeaks->{ $num }->{ winname }->{2} = $npeaks->{ $num }->{ winname }->{1};
  177.         $npeaks->{ $num }->{ winner }->{3} = $npeaks->{ $num }->{ winner }->{2}; $npeaks->{ $num }->{ winname }->{3} = $npeaks->{ $num }->{ winname }->{2};
  178.         $npeaks->{ $num }->{ winner }->{0} = $c; $npeaks->{ $num }->{ winname }->{0} = $keym;    
  179. }
  180.  
  181.     #print "$n{$keym} : $hn{$keym}\n";
  182.  
  183.    
  184.  
  185.  
  186.  }
  187.  
  188.  
  189.  
  190. #print "$npeaks->{ $num }->{ winname }->{3}\n\n\n";
  191.  
  192.  
  193. }#sub
  194.  
  195. sub removedups
  196. {
  197.  
  198.  
  199. my($keyp,$nt)=@_;
  200.  
  201. foreach $key1 (keys %npeaks) {
  202.  
  203. if($keyp != $key1 && $npeaks->{ $keyp }->{ winname }->{0} == $npeaks->{ $key1 }->{ winname }->{0})
  204.   {
  205.    if($npeaks->{ $keyp }->{ winner }->{0} > $npeaks->{ $key1 }->{ winner }->{0})
  206.    {
  207.        
  208.       $npeaks->{ $keyp }->{ winner }->{0} = $npeaks->{ $keyp }->{ winner }->{$nt};
  209.       $npeaks->{ $keyp }->{ winname }->{0} = $npeaks->{ $keyp }->{ winname }->{$nt};
  210.      
  211.        
  212.  
  213.    }
  214.  
  215.       else {  
  216.            $npeaks->{ $key1 }->{ winner }->{0} = $npeaks->{ $key1 }->{ winner }->{$nt};
  217.            $npeaks->{ $key1 }->{ winname }->{0} = $npeaks->{ $key1 }->{ winname }->{$nt};
  218.            
  219.             #if(removedups1($key1,$nt+1) == -1){ print "POOP\n\n"; return 0;}
  220.            }
  221.  
  222.   }
  223. }#foreach
  224.  
  225.  
  226. }#sub
  227.  
  228.  
  229.  
  230. sub removedups1
  231. {
  232.  
  233. my($keyp) = $_[0];
  234.  
  235.  
  236. my($key1);
  237.  
  238. foreach $key1 (keys %npeaks) {
  239.  
  240.  
  241. if($keyp != $key1 && $npeaks->{ $keyp }->{ winname }->{0} == $npeaks->{ $key1 }->{ winname }->{0})
  242.   {
  243.    if($npeaks->{ $keyp }->{ winner }->{0} > $npeaks->{ $key1 }->{ winner }->{0})
  244.    {
  245.        
  246.       $npeaks->{ $keyp }->{ winner }->{0} = 0;
  247.       $npeaks->{ $keyp }->{ winname }->{0} = 0;
  248.      
  249.        
  250.  
  251.    }
  252.  
  253.       else {  
  254.            $npeaks->{ $key1 }->{ winner }->{0} = 0;
  255.            $npeaks->{ $key1 }->{ winname }->{0} = 0;
  256.            
  257.            }
  258.   }
  259. }#foreach
  260.  
  261.  
  262. }#sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement