I have a matrix that I wish to delete columns from but for which I would like to retain the original column numbers. To keep track of the original columns numbers I create a new matrix of rules that give the {row,col} position in the original matrix to which I assign a value (either "A","G","C","T","-") depending on its value in the original matrix. I do not know beforehand which columns need to be deleted, nor which of these 5 values will appear at any position of the matrix, but I do know the criteria which determine whether or not a column of the matrix should be deleted.
Thus, using a subsample of the matrix as an example I have originally a matrix m, (all elements stored as single characters [with enclosing "" not shown]).
m = {{G,T,T,A,A,C,G,A,C,-},
{G,T,T,A,G,C,G,T,C,-},
{C,A,A,T,T,C,G,T,C,G},
{T,A,A,T,T,C,G,T,C,A},
{C,A,T,A,C,C,C,G,A,A},
{-,A,T,A,C,C,G,G,A,A}}
I then convert these entries into a matrix of rules, the keys being the {row,col} position in original matrix and the values are those values assigned to these positions. I do this by using the following commands:
{row,col} = Dimensions[m]
m2 = ArrayRules[m];
m3 = Partition[m2,col];
at any element of m3 we have a two element list as a key and the original value at that position as a value. Thus, for a given position say 6th row, fist column the rule defined at m[[6,1]] would be {6,1}-> "-", whereas that of the 8th row 10th column would be {8,10}-> "A". This permits one to extract either the key (row,col} pairs or the value, its assigned value. The col value would then be used to label the columns in the new submatrix, when representing the matrix in as a grid.
Now, I wish to delete columns of this new matrix based on several criteria, yet still retain the original column number in matrix m.
Criteria 1: Delete any column that contains one or more "-" as a value.
Criteria 2: Having deleted the columns containing "-" values, delete any of the remaining columns that either have 3 or 4 distinct values (leaving columns with only two distinct values, neither of which is a "-".
Criteria 3: Delete any columns in the original matrix for which all row entries are the same.
Criteria 4: Delete any column with two values but one is a singleton (ie represented only once in the column).
Thus, for the matrix m above, the new matrix would delete columns 1 and 10 (because they contain a "-" (col 1) or more than one "-" (col 10). Column 5 is deleted as it has 4 different values (A,G,T,C). Column 6 is deleted because all elements have the value "C". Column 7 is deleted because although it contains 2 values only, one of them is expressed as a singleton (all "G" or "C", but only 1 "C"). No singletons are allowed. Column 8 is deleted because it has 3 different values ("A","T","G") Cols 2, 3, 4, 9 are retained as they each contain just exactly 2 distinct elements (neither singletons) to become the four columns of the resultant matrix. A peek at the key for any element gives the original column number as the second element of an ordered pair forming the key.
Thus, the values for the new resultant matrix say m4 would again be rules, whose keys are as before {row, col} (original) and the new/same values as follows (again showing only the values of the key->value pair without the enclosing ""):
m4 = {{T,T,A,C},
{T,T,A,C},
{A,A,T,C},
{A,A,T,C},
{A,T,A,A},
{A,T,A,A}}
Thus, each column of the new matrix is formed from those columns of the original matrix satisfying the four criteria above (all have entries with only two different characters, neither a "-" and neither represented as a singleton.
The original column numbers would then be obtained from the second element of the ordered pair {row col} specified by Keys. These would be displayed vertically (above or below) for each column and used to refer to original column numbers as column labels and not the new column numbers generated by the resultant matrix.
Retaining the original column numbers is critical because they represent the original column positions in the original matrix, information that would be lost looking solely at the column numbers in the resultant submatrix.
Is there a way to structure a set of DeleteCases commands to do this?
I have syntax problems for my DeleteCases corresponding to each of the criteria that I haven't been able to code my way past.
Most examples in other posts and those that I can find elsewhere require one to know which columns are to be deleted beforehand, which is not possible here. Likewise, I can find no other code that will permit the original column numbers (labels/positions) to be retained for labeling in a grid. Hence, my storing this information as part of the key in the key->value pair for each element (row,col position) of the original matrix.
I would be interested in any other approaches to this problem that would be the quickest, as the original matrices are large (rows = hundreds x col = thousands) and hence many deletions would be necessary for subsequent analysis to begin. Any help would be much appreciated.