Postgres documentation seems to only show how to check if two arrays have overlapping elements.
I need to know if the two array have non-overlapping elements.
So if I have 2 arrays:
array_1 = '{1, 2, 3, 4}'
array_2 = '{0, 3, 4, 5}'
This should return false. Checking for not equal doesn't work because they may not be equal in that one array might have the same integer repeat several times.
Is this comparison possible?
Two sets are non-overlapping if there are no elements in common, so using the && operator for arrays and negating the result give you what you want.
# select NOT (ARRAY[1,2,3,4] && ARRAY[0,3,3,3,3,4,5]) AS non_overlapping;
non_overlapping
-----------------
f
(1 row)
# select NOT (ARRAY[1,2,8,9] && ARRAY[0,3,3,3,3,4,5]) AS non_overlapping;
non_overlapping
-----------------
t
(1 row)
I would like to count the number of element in a field of a structure in Matlab
The file name is "data.m"
I have something like this:
The file has 3 fields (Columns): x, y and z
The file has maximal 6 lines, which is the number of line of the y-column
x has 5 elements (0, 9, 5, 6, 6)
y has 6 elements (6, 1, 2, 2, 8, 2)
z has 4 elements (8, 8, 4, 9)
Using:
number_of_element = numel(data.x);
returns 1.
It only takes the first element (The first line, which is "0" here)
I would like to have the number of element of the column x, which is "5" in this case.
Then I tried this:
number_of_element = numel(data(:,x));
But it doesn't work. I though Matlab could recognise "x" as a field name.
This also did not work:
count = 0;
for i = 1:end % I get an error because of this "end". Why is it not recognised here ?
number_of_element = numel(data(i).x);
count = count+number_of_element;
end
How could I get the number of element in the x-column ?
Thank you in advance.
My question is probably really easy, but I am a mathematica beginner.
I have a dataset, lets say:
Column: Numbers from 1 to 10
Column Signs
Column Other signs.
{{1,2,3,4,5,6,7,8,9,10},{d,t,4,/,g,t,w,o,p,m},{g,h,j,k,l,s,d,e,w,q}}
Now I want to extract all rows for which column 1 provides an odd number. In other words I want to create a new dataset.
I tried to work with Select and OddQ as well as with the IF function, but I have absolutely no clue how to put this orders in the right way!
Taking a stab at what you might be asking..
(table = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ,
Characters["abcdefghij"],
Characters["ABCDEFGHIJ"]}) // MatrixForm
table[[All, 1 ;; -1 ;; 2]] // MatrixForm
or perhaps this:
Select[table, OddQ[#[[1]]] &]
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
The convention in Mathematica is the reverse of what you use in your description.
Rows are first level sublists.
Let's take your original data
mytable = {{1,2,3,4,5,6,7,8,9,10},{d,t,4,"/",g,t,w,o,p,m},{g,h,j,k,l,s,d,e,w,q}}
Just as you suggested, Select and OddQ can do what you want, but on your table, transposed. So we transpose first and back:
Transpose[Select[Transpose[mytable], OddQ[First[#]]& ]]
Another way:
Mathematica functional command MapThread can work on synchronous lists.
DeleteCases[MapThread[If[OddQ[#1], {##}] &, mytable], Null]
The inner function of MapThread gets all elements of what you call a 'row' as variables (#1, #2, etc.). So it test the first column and outputs all columns or a Null if the test fails. The enclosing DeleteCases suppresses the unmatching "rows".
I don't know how to explain this better than by giving you an example.
Suppose I have the following array:
a = magic(6)
And then I take a 'slice' of that like this:
a(:,1)
It will print:
35
3
31
8
30
4
Now I want the first number, so I want to write:
a(:,1)(1)
Instead of:
b = a(:,1)
b(1)
Also, is there a way to do something like this (assignment and comparison, i.e. set b, then evaluate against it):
(b = a(:,1))(1)
Ok, here's an update with a function where it isn't trivial to use a(1, 1)
come_on = sprintf('%i, ', magic(3));
come_on(1:end-2)
8, 3, 4, 1, 5, 9, 6, 7, 2
Also, what if I only want the first 4 numbers on magic(3)?
It would be better to write
sprintf('%i, ', magic(3)(1:4))(1:end-2)
instead of tens of lines, MHO.
You cannot concatenate indexing as foo(1)(2)(3). However, you can index multiple dimensions at once. So in this case, a(1,1) will give you what you want.
I need to generate a random walk based on the DNA sequence of a virus, given its base pair sequence of 2k base pairs. The sequence looks like "ATGCGTCGTAACGT". The path should turn right for an A, left for a T, go upwards for a G and downwards for a C.
How can I use either Matlab, Mathematica or SPSS for this purpose?
I did not previously know of Mark McClure's blog about Chaos Game representation of gene sequences, but it reminded me of an article by Jose Manuel Gutiérrez (The Mathematica Journal Vol 9 Issue 2), which also gives a chaos game algorithm for an IFS using (the four bases of) DNA sequences. A detailed description may be found here (the original article).
The method may be used to produce plots such as the following. Just for the hell of it, I've included (in the RHS panels) the plots generated with the corresponding complementary DNA strand (cDNA).
Mouse Mitochondrial DNA (LHS) and its
complementary strand (cDNA) (RHS).
These plots were generated from GenBank Identifier gi|342520. The sequence contains 16295 bases.
(One of the examples used by Jose Manuel Gutiérrez. If anyone is interested, plots for the human equivalent may be generated from gi|1262342).
Human Beta Globin Region (LHS) and its cDNA (RHS)
Generated from gi|455025| (the example
used my Mark McClure). The sequence contains 73308 bases
There are pretty interesting plots! The (sometimes) fractal nature of such plots is known, but the symmetry obvious in the LHS vs RHS (cDNA) versions was very surprising (at least to me).
The nice thing is that such plots for any DNA sequence may be very easily generated by directly importing the sequence (from, say, Genbank), and then using the power of Mma.
All you need it the accession number! ('Unknown' nucleotides such as "R" may need to be zapped) (I am using Mma v7).
The Original Implimenation (slightly modified) (by Jose Manuel Gutiérrez)
Important Update
On the advise of Mark McClure, I have changed Point/#Orbit[s, Union[s]] to Point#Orbit[s, Union[s]].
This speeds things up very considerably. See Mark's comment below.
Orbit[s_List, {a_, b_, c_, d_}] :=
OrbitMap[s /. {a -> {0, 0}, b -> {0, 1}, c -> {1, 0},
d -> {1, 1}}];
OrbitMap =
Compile[{{m, _Real, 2}}, FoldList[(#1 + #2)/2 &, {0, 0}, m]];
IFSPlot[s_List] :=
Show[Graphics[{Hue[{2/3, 1, 1, .5}], AbsolutePointSize[2.5],
Point # Orbit[s, Union[s]]}], AspectRatio -> Automatic,
PlotRange -> {{0, 1}, {0, 1}},
GridLines -> {Range[0, 1, 1/2^3], Range[0, 1, 1/2^3]}]
This gives a blue plot. For green, change Hue[] to Hue[{1/3,1,1,.5}]
The following code now generates the first plot (for mouse mitochondrial DNA)
IFSPlot[Flatten#
Characters#
Rest#Import[
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=\
nucleotide&id=342520&rettype=fasta&retmode=text", "Data"]]
To get a cDNA plot I used the follow transformation rules (and also changed the Hue setting)
IFSPlot[ .... "Data"] /. {"A" -> "T", "T" -> "A", "G" -> "C",
"C" -> "G"}]
Thanks to Sjoerd C. de Vries and telefunkenvf14 for help in directly importing sequences from the NCBI site.
Splitting things up a bit, for the sake of clarity.
Import a Sequence
mouseMitoFasta=Import["http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=342520&rettype=fasta&retmode=text","Data"];
The method given for importing sequences in the original Mathematica J. article is dated.
A nice check
First#mouseMitoFasta
Output:
{>gi|342520|gb|J01420.1|MUSMTCG Mouse mitochondrion, complete genome}
Generation of the list of bases
mouseMitoBases=Flatten#Characters#Rest#mouseMitoFasta
Some more checks
{Length#mouseMitoBases, Union#mouseMitoBases,Tally#mouseMitoBases}
Output:
{16295,{A,C,G,T},{{G,2011},{T,4680},{A,5628},{C,3976}}}
The second set of plots was generated in a similar manner from gi|455025. Note that the sequence is long!
{73308,{A,C,G,T},{{G,14785},{A,22068},{T,22309},{C,14146}}}
One final example (containing 265922 bp), also showing fascinating 'fractal' symmetry. (These were generated with AbsolutePointSize[1] in IFSPlot).
The first line of the fasta file:
{>gi|328530803|gb|AFBL01000008.1| Actinomyces sp. oral taxon 170 str. F0386 A_spOraltaxon170F0386-1.0_Cont9.1, whole genome shotgun sequence}
The corresponding cDNA plot is again shown in blue on RHS
Finally, Mark's method also gives very beautiful plots (for example with gi|328530803), and may be downloaded as a notebook.
Not that I really understand the "graph" you want, but here is one literal interpretation.
None of the following code in necessarily in a final form. I want to know if this is right before I try to refine anything.
rls = {"A" -> {1, 0}, "T" -> {-1, 0}, "G" -> {0, 1}, "C" -> {0, -1}};
Prepend[Characters#"ATGCGTCGTAACGT" /. rls, {0, 0}];
Graphics[Arrow /# Partition[Accumulate#%, 2, 1]]
Prepend[Characters#"TCGAGTCGTGCTCA" /. rls, {0, 0}];
Graphics[Arrow /# Partition[Accumulate#%, 2, 1]]
3D Options
i = 0;
Prepend[Characters#"ATGCGTCGTAACGT" /. rls, {0, 0}];
Graphics[{Hue[i++/Length#%], Arrow##} & /#
Partition[Accumulate#%, 2, 1]]
i = 0;
Prepend[Characters#"ATGCGTCGTAACGT" /.
rls /. {x_, y_} :> {x, y, 0.3}, {0, 0, 0}];
Graphics3D[{Hue[i++/Length#%], Arrow##} & /#
Partition[Accumulate#%, 2, 1]]
Now that I know what you want, here is a packaged version of the first function:
genePlot[s_String] :=
Module[{rls},
rls =
{"A" -> { 1, 0},
"T" -> {-1, 0},
"G" -> {0, 1},
"C" -> {0, -1}};
Graphics[Arrow /# Partition[#, 2, 1]] & #
Accumulate # Prepend[Characters[s] /. rls, {0, 0}]
]
Use it like this:
genePlot["ATGCGTCGTAACGT"]
It sounds like you might be talking about CGR, or the so called Chaos Game Representation of a gene sequence described in the 1990 paper "Chaos game representation of gene structure" by Joel Jefferey. Here's an implementation in Mathematica:
cgrPic[s_String] := Module[
{},
chars = StringCases[s, "G"|"A"|"T"|"C"];
f[x_, "A"] := x/2;
f[x_, "T"] := x/2 + {1/2, 0};
f[x_, "G"] := x/2 + {1/2, 1/2};
f[x_, "C"] := x/2 + {0, 1/2};
pts = FoldList[f, {0.5, 0.5}, chars];
ListPlot[pts, AspectRatio -> Automatic]]
Here's how to apply it to a gene sequence taken from Mathematica's GenomeData command:
cgrPic[GenomeData["FAT4", "FullSequence"]]
You might also try something like this...
RandomDNAWalk[seq_, path_] :=
RandomDNAWalk[StringDrop[seq, 1],
Join[path, getNextTurn[StringTake[seq, 1]]]];
RandomDNAWalk["", path_] := Accumulate[path];
getNextTurn["A"] := {{1, 0}};
getNextTurn["T"] := {{-1, 0}};
getNextTurn["G"] := {{0, 1}};
getNextTurn["C"] := {{0, -1}};
ListLinePlot[
RandomDNAWalk[
StringJoin[RandomChoice[{"A", "T", "C", "G"}, 2000]], {{0, 0}}]]
Assuming that the sequence S has been mapped already*) to integer array then the actual computation of movements is straightforward based on rules R:
R =
1 -1 0 0
0 0 1 -1
S =
1 2 3 4 3 2 4 3 2 1 1 4 3 2
T= cumsum(R(:, S), 2)
T =
1 0 0 0 0 -1 -1 -1 -2 -1 0 0 0 -1
0 0 1 0 1 1 0 1 1 1 1 0 1 1
*) You need to elaborate more on the actual sequence. Is it represented as single string, or perhaps as cell array, or something else?
Edit:
Assuming your sequence is represented as string, then you'll map it to integer sequence S like:
r= zeros(1, 84);
r(double("ATGC"))= [1 2 3 4];
S= r(double("ATGCGTCGTAACGT"))
And to plot it:
plot([0 T(1, :)], [0 T(2, :)], linespec)
where linespec is the desired line specification.
This question seems to have been well answered already, but I thought I'd add that what you are describing has been previously published under the banner of DNA walks among a collection of numerical representation methods for DNA sequences, which are discussed in our preprint.
It turns out that DNA walks aren't very useful in practice, yet permit intuitive visualisation. I don't have it to hand, but I'd imagine my colleague would be more than happy to provide the Matlab code used to generate the below figure.