Changes between Initial Version and Version 1 of RprojectInterface


Ignore:
Timestamp:
2010-10-01T23:38:13+02:00 (11 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RprojectInterface

    v1 v1  
     1[[TOC()]]
     2= Using R for data analysis. =
     3This page shows how to connect R scripts to XGAP. See RqtlIntegration for integration with the RqtlPackage.
     4
     5== Connecting R to XGAP ==
     6This example shows how to connect to XGAP from within R:
     7
     8 1. In the XGAP user interface, go to "Programming interfaces".
     9
     10 2. Click the link at "Access from the R project: source the file at api/R".
     11
     12 3. Select and copy all the commands
     13
     14 4. Open R, and paste the commands. If you haven't installed RCurl, please do so now. (See "step 1" in the sourced code)
     15
     16 [[Image(R1.png)]]
     17
     18== Retrieving annotation data ==
     19This examples show how to retrieve annotation data from XGAP (except data matrices). We will use the example of 'marker'.
     20
     21 1. Retrieve annotations use find.*. For example:
     22{{{
     23allMarkers <- find.marker()
     24}}}
     25 Tip: if you only type 'find.' and then push 'TAB' you will see all find.* functions available.
     26
     27 2. One can use the R function dim() to see the dimensions of this object. In this example, there are 251 markers with 17 attributes each.
     28{{{
     29dim(allMarkers)
     30}}}
     31 Result:
     32 
     33 [[Image(R2.png)]]
     34
     35 3. By selecting the first column, you get to see the chromosome attribute for each marker:
     36 Use:
     37{{{
     38allMarkers[,1]
     39}}}
     40
     41 It is even easier to retrieve a particular column using the '$ plus column name' notation:
     42 Use:
     43{{{
     44allMarkers$chr
     45}}}
     46 Result:
     47
     48 [[Image(R3.png)]]
     49
     50 4. You can select the first marker by picking the first row:
     51 Use:
     52{{{
     53allMarkers[1,]
     54}}}
     55
     56 5. And only name of this marker by combining the syntaxes:
     57 Use:
     58{{{
     59allMarkers[1,]$name
     60}}}
     61
     62 6. The '$' notation does not work for multiple columns. So, if you wish to see specific attributes for all markers, you have to pass the column names as follows:
     63 Use:
     64 {{{
     65allMarkers[,c("id","chr","name")]
     66}}}
     67 Result:
     68 
     69 [[Image(R4.png)]]
     70
     71
     72== Retrieving data matrices ==
     73Data is stored in XGAP in the form of matrices. The following examples show how to retrieve these data sets into R.
     74
     75 1. First, get a list of all data matrices available in the database. Adding some more arguments will limit the output shown to only data.id, name and investigation name.
     76 Use:
     77{{{
     78find.data()[,c("id","name","investigation_name")]
     79}}}
     80 Result:
     81 [[Image(R5.png)]]
     82
     83 2. Select and retrieve the data for one data matrix. In this example let's pick the metabolite expression matrix which had id 6. Then you can download it like this:
     84{{{
     85data <- find.datamatrix(6)
     86}}}
     87 3. Like with annotation data above, one can also inspect the size of the downloaded matrix:
     88{{{
     89dim(data)
     90}}}
     91 Result:
     92 
     93 [[Image(R6.png)]]
     94
     95 3. Now make an 'overplotted' plot of the first column. (In this case: the first 1 trait, for all individuals)
     96{{{
     97plot(data[1,], type="o")
     98}}}
     99 Result:
     100 
     101 [[Image(R7.png)]]
     102
     103 4. In contrast with annotations, data matrices also have row headers, next to column headers. You can check this by using functions like colnames(), rownames(). Note that you can use these to select a subset of the matrix:
     104{{{
     105data[1:5, 1:5]
     106}}}
     107 Result:
     108 
     109 [[Image(R8.png)]]
     110
     111== Uploading annotations ==
     112
     113Get a list of the investigations with attributes 'id' and 'name'. In this case, we use the 'MetaNetwork' investigation which has id = 1.
     114
     115 Use:
     116{{{
     117find.investigation()[,c("id", "name")]
     118}}}
     119
     120Suppose we would like to add a pseudomarker during a QTL investigation. We can easily add a single marker by using add.marker:
     121
     122 Use:
     123{{{
     124add.marker(name="loc50.0", cm="50.0", chr="2", investigation_id=1)
     125}}}
     126
     127It's also possible to add a list of markers at once. This can be done by constructing a dataframe. Use 'colnames' to set the attributes. An example:
     128
     129 Use:
     130{{{
     131pseudo <- NULL
     132pseudo <- rbind(pseudo, c("loc0.0","0.0","14", 1))
     133pseudo <- rbind(pseudo, c("loc10.0","10.0","15", 1))
     134pseudo <- rbind(pseudo, c("loc22.0","22.0","16", 1))
     135colnames(pseudo) <- c("name", "cm", "chr", "investigation")
     136add.marker(pseudo)
     137}}}
     138
     139You can add the result to a variable so you can use its properties (eg. the assigned id in the database) later on.
     140
     141 Use:
     142{{{
     143myPseudo = add.marker(pseudo);
     144myPseudo #print the list of markers
     145}}}
     146
     147== Uploading data matrices ==
     148
     149=== Option A ===
     150
     151This is the easier way, using a custom function.
     152
     153Creata a matrix and add two rows with, for example, genotyping data:
     154
     155 Use:
     156{{{
     157data <- NULL
     158data <- rbind(data, c("A", "B"))
     159data <- rbind(data, c("B", "A"))
     160}}}
     161
     162If the individuals and markers are not present in the database, add them first.
     163
     164 Use:
     165{{{
     166marker1 = add.marker(name="myMarker1", cm="10.0", chr="2", investigation_id=1)
     167marker2 = add.marker(name="myMarker2", cm="20.0", chr="2", investigation_id=1)
     168ind1 = add.individual(name="myInd1", investigation_id=1)
     169ind2 = add.individual(name="myInd2", investigation_id=1)
     170}}}
     171
     172Now add reference to the individuals of which the genotypes are measured, and ofcourse the markers that have been genotyped. Be careful to not switch 'rows' with 'columns'.
     173
     174 Use:
     175{{{
     176colnames(data) <- c("myInd1", "myInd2")
     177rownames(data) <- c("myMarker1", "myMarker2")
     178}}}
     179
     180Now we add this matrix by using the custom 'add.datamatrix' function. Several attributes need to be entered, for example the name and row/column type of the matrix. We know the investigation to add this matrix to has id = 1.
     181
     182 Use:
     183{{{
     184add.datamatrix(data, name="myResults", investigation_id=1, rowtype="Marker", coltype="Individual", valuetype="Text")
     185}}}
     186
     187When successful, something like this will appear:
     188
     189 [[Image(R10.png)]]
     190
     191You can inspect the result in the user interface:
     192
     193 [[Image(R9.png)]]
     194
     195=== Option B ===
     196
     197This is the harder way, performing several manual steps by yourself.
     198
     199First, add a Data object. This is basically the description of a datamatrix. We add it under the investigation with id = 1. Say we add genotyping data. In this case, the rowtype will be 'marker', the columns 'individual. We add two rows and two columns. The values will be text. (eg. 'A' or 'B')
     200
     201 Use:
     202{{{
     203data <- add.data(name = "myResults", investigation_id=1, rowtype="Marker",coltype="Individual",totalrows=2,totalcols=2,valuetype="Text")
     204}}}
     205
     206Now lets add elements which we will refer to. This can also be existing elements ofcourse. We add two markers with some information, and two individuals with just names.
     207
     208 Use:
     209{{{
     210marker1 = add.marker(name="myMarker1", cm="10.0", chr="2", investigation_id=1)
     211marker2 = add.marker(name="myMarker2", cm="20.0", chr="2", investigation_id=1)
     212ind1 = add.individual(name="myInd1", investigation_id=1)
     213ind2 = add.individual(name="myInd2", investigation_id=1)
     214}}}
     215
     216We can now add the actual values. Here we use 'add.textdataelement' contrary to 'add.decimaldataelement' because the valuetype for the matrix is text. From the existing 'data' object, we get the id to add the element to. We do the same for the markers and individuals. Then we indicate the position of the elements in the matrix using indices and finally the value of the element.
     217
     218 Use:
     219{{{
     220add.textdataelement(data_id=data$id, row_id=marker1$id, col_id=ind1$id, rowindex=0, colindex=0, value="A")
     221add.textdataelement(data_id=data$id, row_id=marker1$id, col_id=ind2$id, rowindex=0, colindex=1, value="B")
     222add.textdataelement(data_id=data$id, row_id=marker2$id, col_id=ind1$id, rowindex=1, colindex=0, value="B")
     223add.textdataelement(data_id=data$id, row_id=marker2$id, col_id=ind2$id, rowindex=1, colindex=1, value="A")
     224}}}