PageRenderTime 81ms CodeModel.GetById 65ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/R/fb_genetic.R

http://fb-scraping-project.googlecode.com/
R | 62 lines | 61 code | 1 blank | 0 comment | 6 complexity | 2c160a6cea52a36714798c2c85b3f721 MD5 | raw file
 1fb_genetic <- function(idFB = NA, Genus = NA, Species = NA, server = 'http://www.fishbase.tw/')
 2  {
 3require("XML")
 4require("stringr")
 5require('RCurl')
 6  if (is.na(idFB)) 
 7		{
 8		ids <- fb_ids(Genus=Genus, Species=Species, server = server)
 9		idFB <- ids$idFB
10		StockCode <- ids$StockCode
11		}
12  if ((is.na(idFB)) & (is.na(Genus) | is.na(Species)))
13    {
14    warning("idFB or Genus and Specie are required!")
15    }
16	if(!is.na(idFB))
17		{
18		url <- paste(server, "Genetics/FishGeneticsList.php?ID=", idFB, sep = "")
19    s <- readHTMLTable(url, as.data.frame=TRUE, colnames = c('idFB', 'Genus', 
20                        'Species', 'Locality', 'Country', 'HaploidGam', 
21                        'Diploid_zyg','GenMarkers', 'Reference'))
22
23		if (dim(s[[4]])[1]==2)
24			{
25			list <- c(as.character(idFB), as.character(Genus), 
26                as.character(Species), t(rep(NA, 7)))
27			names(list) <-  c('idFB', 'Genus', 'Species', 'Locality', 'Country', 'HaploidGam', 
28                        'DiploidZygI','DiploidZygII','GenMarkers', 'Reference')
29			}
30		if (dim(s[[4]])[1]>=3)
31			{
32			diet <- cbind(as.character(idFB), as.character(Genus), as.character(Species), 
33                    s[[4]])
34			names(diet) <-   c('idFB', 'Genus', 'Species', 'Locality', 'Country', 'HaploidGam', 
35                        'Diploid_zyg','GenMarkers', 'Reference')
36			diet$idFB <- as.character(diet$idFB)
37			diet$Genus <- as.character(diet$Genus)
38			diet$Species <- as.character(diet$Species)
39			diet$Locality <- as.character(diet$Locality)
40			diet$Country <- as.character(diet$Country)
41			diet$HaploidGam <- as.character(diet$HaploidGam)
42			diet$Diploid_zyg <- str_split(as.character(diet$Diploid_zyg), '-')
43      diet$GenMarkers <- as.character(diet$GenMarkers)
44      diet$Reference <- as.character(diet$Reference)
45      diet <- diet[-(1:2),]
46      diet$DiploidZygI <- NA
47      diet$DiploidZygII <- NA
48      for (i in 1:length(diet$Diploid_zyg))
49        {
50        diet$DiploidZygI[i] <- diet$Diploid_zyg[[i]][1]
51        diet$DiploidZygII[i] <- diet$Diploid_zyg[[i]][2]
52        }
53      diet <- diet[,-7]
54      list <- as.data.frame(cbind(diet$idFB, diet$Genus, diet$Species, diet$Locality, diet$Country,
55                    diet$HaploidGam, diet$DiploidZygI, diet$DiploidZygII,
56                    diet$GenMarkers, diet$Reference))
57      names(list) <-   c('idFB', 'Genus', 'Species', 'Locality', 'Country', 'HaploidGam', 
58                        'DiploidZygI','DiploidZygII','GenMarkers', 'Reference')
59		}
60	}
61  return(list)
62}