1
$\begingroup$

I have the following code:

names = {"Gly", "Ala", "Ser", "Pro",  "Val", "Thr", "Cys", "Leu/Ile", 
   "Asn", "Asp/isoAsp", "Gln", "Lys", "Glu", "Met", "His", "Phe", 
   "Arg", "Tyr", "Trp", "4-hydroxyPro", "5-hydroxyLys", 
   "6-N-methylLys", "\[Gamma]-carboxyGlu", "selenoCys", "phosphoSer", 
   "phosphoThr", "phosphoTyr", "\[Sigma]-N-methylArg", 
   "6-N-acetylLys", "Glu \[Gamma]-methyl ester", "Ornithine", 
   "Citrulline", "3-methylHis", "N,N,N-trimethylLys", "N-acetylAla", 
   "3-sulfinoAla", "N-acetylCys", "pyroGlu", "N-acetylGly", 
   "Met sulfoxide", "Met sulfone", "N-acetylSer", "N-acetylThr", 
   "Kynurenine", "Tyr O-sulfate"};
list = {57.0215, 71.0371, 87.0320, 97.0528, 99.0684, 101.0477, 
   103.0092, 113.0841, 114.0429, 115.0269, 128.0586, 128.0950, 
   129.0426, 131.0405, 137.0589, 147.0684, 156.1011, 163.0633, 
   186.0793, 113.048, 144.089, 142.110, 173.032, 150.954, 166.998, 
   181.014, 243.029, 170.116, 170.105, 143.058, 114.079, 157.085, 
   151.074, 170.141, 113.047, 134.999, 145.019, 111.032, 99.032, 
   147.035, 163.030, 129.042, 143.058, 190.047, 243.020};
subsets = 
 DeleteDuplicates@
  Select[Subsets[list, {3}], 344.160 <= Total[#] <= 344.163 &]

which produces the expression:

{{71.0371, 103.009, 170.116}}

To display the parts of this expression as their names, I'm using:

seqNam = names[[Position[list, subsets[[1]][[1]]] [[1]][[1]]]]
seqNam = names[[Position[list, subsets[[1]][[2]]] [[1]][[1]]]] 
seqNam = names[[Position[list, subsets[[1]][[3]]] [[1]][[1]]]]

but I would like do display all parts as one line of output, and I don't know how. Any help would be appreciated!

$\endgroup$
1
  • 1
    $\begingroup$ names[[Position[list, #][[1, 1]]]] & /@ subsets[[1]] $\endgroup$
    – Bob Hanlon
    Nov 27 '19 at 2:57
2
$\begingroup$

Another method

Pick[names, Thread[list == #]] & /@ subsets[[1]] // Flatten

{"Ala", "Cys", "σ-N-methylArg"}

$\endgroup$
1
$\begingroup$

Given names and list as you have defined them. I would add Flatten your definition of subsets to simplify retrieval. Like so:

subsets =
 DeleteDuplicates @
   Select[Subsets[list, {3}], 344.160 <= Total[#] <= 344.163 &] // Flatten

{71.0371, 103.009, 170.116}

Also, because Position is designed to used with Extract, one way to get the names is:

Extract[names, Position[list, #]] & /@ subsets // Flatten

{"Ala", "Cys", "σ-N-methylArg"}

However, if you are going to doing this kind of lookup a lot, I reccomend making a dictionary (an Association) and doing the lookup with the buit-in function Lookup, which gives very readable code.

dictionary = AssociationThread[list, names];
Lookup[dictionary, subsets]

{"Ala", "Cys", "σ-N-methylArg"}

$\endgroup$
1
$\begingroup$

You may make use of Association with the names as keys and list as values.

rules = Thread[names -> list];
Select[Association @@@ Subsets[rules, {3}], 344.160 <= Total[#] <= 344.163 &]
{<|"Ala" -> 71.0371, "Cys" -> 103.009, "σ-N-methylArg" -> 170.116|>}
Select[Association @@@ Subsets[rules, {2}], 103.4 <= Total[#] <= 144.8 &]
{<|"Gly" -> 57.0215, "Ala" -> 71.0371|>, <|"Gly" -> 57.0215, "Ser" -> 87.032|>}

Hope this helps.

$\endgroup$
1
$\begingroup$
Pick[names, list, Alternatives @@ subsets[[1]]]
 {"Ala", "Cys", "σ-N-methylArg"}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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