├── .Rbuildignore ├── .gitignore ├── .travis.yml ├── DESCRIPTION ├── LICENSE.md ├── NAMESPACE ├── R ├── accuracy.MC.R ├── accuracy.kfold.R ├── accuracy.plot.R ├── assign.MC.R ├── assign.X.R ├── assign.kfold.R ├── assign.matrix.R ├── check.loci.R ├── compile.data.R ├── membership.plot.R ├── read.Genepop.R ├── read.Structure.R └── reduce.allele.R ├── README.md ├── assignPOP.Rproj ├── inst └── extdata │ ├── Rate.txt │ ├── TinyGenepop.txt │ ├── TinyVars.csv │ ├── morphData.csv │ ├── morphDataX.csv │ ├── simGenepop.txt │ └── simGenepopX.txt ├── man ├── accuracy.MC.Rd ├── accuracy.kfold.Rd ├── accuracy.plot.Rd ├── assign.MC.Rd ├── assign.X.Rd ├── assign.kfold.Rd ├── assign.matrix.Rd ├── check.loci.Rd ├── compile.data.Rd ├── membership.plot.Rd ├── read.Genepop.Rd ├── read.Structure.Rd └── reduce.allele.Rd └── tests ├── testthat.R └── testthat ├── ResKFtest ├── AnalysisInfo.txt ├── Loci_1_K3_1.txt ├── Loci_1_K3_2.txt ├── Loci_1_K3_3.txt ├── Out_1_K3_1.txt ├── Out_1_K3_2.txt └── Out_1_K3_3.txt ├── ResMCtest ├── AnalysisInfo.txt ├── Loci_0.5_1_1.txt ├── Loci_0.5_1_2.txt ├── Loci_0.5_1_3.txt ├── Out_0.5_1_1.txt ├── Out_0.5_1_2.txt └── Out_0.5_1_3.txt ├── ResMCtestFst ├── AnalysisInfo.txt ├── Loci_0.5_1_1.txt ├── Loci_0.5_1_2.txt ├── Loci_0.5_1_3.txt ├── Out_0.5_1_1.txt ├── Out_0.5_1_2.txt └── Out_0.5_1_3.txt ├── testData ├── .gitignore ├── GenepopEx1.txt ├── GenepopEx2.txt ├── GenepopUnk.txt ├── StructureEx1.txt ├── StructureEx2.txt ├── varDummy1.csv └── varDummy2.csv ├── test_accuracy.R ├── test_assignMC.R ├── test_assignX.R ├── test_assignkfold.R ├── test_assignmatrix.R ├── test_checkloci.R ├── test_compiledata.R ├── test_membership.R ├── test_readGenepopStructure.R └── test_reduceallele.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .travis.yml 4 | LICENSE.md 5 | README.md -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | .Rbuildignore 6 | .travis.yml 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | cache: packages -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: assignPOP 2 | Type: Package 3 | Title: Population Assignment using Genetic, Non-Genetic or Integrated Data in a 4 | Machine Learning Framework 5 | Version: 1.3.0 6 | Author: Kuan-Yu (Alex) Chen [aut, cre], Elizabeth A. Marschall [aut], Michael 7 | G. Sovic [aut], Anthony C. Fries [aut], H. Lisle Gibbs [aut], Stuart A. Ludsin 8 | [aut] 9 | Maintainer: Kuan-Yu (Alex) Chen 10 | Description: Use Monte-Carlo and K-fold cross-validation coupled with machine- 11 | learning classification algorithms to perform population assignment, with 12 | functionalities of evaluating discriminatory power of independent training 13 | samples, identifying informative loci, reducing data dimensionality for genomic 14 | data, integrating genetic and non-genetic data, and visualizing results. 15 | URL: https://github.com/alexkychen/assignPOP 16 | Depends: 17 | R (>= 2.3.2) 18 | Imports: 19 | caret, 20 | doParallel, 21 | e1071, 22 | foreach, 23 | ggplot2, 24 | MASS, 25 | parallel, 26 | randomForest, 27 | reshape2, 28 | stringr, 29 | tree, 30 | rlang, 31 | Suggests: 32 | gtable, 33 | iterators, 34 | klaR, 35 | stringi, 36 | knitr, 37 | rmarkdown, 38 | testthat 39 | License: GPL (>= 2) 40 | RoxygenNote: 7.3.1 41 | Encoding: UTF-8 42 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(accuracy.MC) 4 | export(accuracy.kfold) 5 | export(accuracy.plot) 6 | export(assign.MC) 7 | export(assign.X) 8 | export(assign.kfold) 9 | export(assign.matrix) 10 | export(check.loci) 11 | export(compile.data) 12 | export(membership.plot) 13 | export(read.Genepop) 14 | export(read.Structure) 15 | export(reduce.allele) 16 | import(foreach) 17 | import(ggplot2) 18 | import(stringr) 19 | importFrom(MASS,lda) 20 | importFrom(caret,createFolds) 21 | importFrom(doParallel,registerDoParallel) 22 | importFrom(e1071,naiveBayes) 23 | importFrom(e1071,svm) 24 | importFrom(parallel,detectCores) 25 | importFrom(parallel,makeCluster) 26 | importFrom(parallel,stopCluster) 27 | importFrom(randomForest,importance) 28 | importFrom(randomForest,randomForest) 29 | importFrom(reshape2,melt) 30 | importFrom(rlang,.data) 31 | importFrom(stats,model.matrix) 32 | importFrom(stats,prcomp) 33 | importFrom(stats,predict) 34 | importFrom(stats,reorder) 35 | importFrom(stats,sd) 36 | importFrom(stats,xtabs) 37 | importFrom(tree,tree) 38 | importFrom(utils,packageVersion) 39 | importFrom(utils,read.csv) 40 | importFrom(utils,read.table) 41 | importFrom(utils,setTxtProgressBar) 42 | importFrom(utils,txtProgressBar) 43 | importFrom(utils,write.table) 44 | -------------------------------------------------------------------------------- /R/accuracy.MC.R: -------------------------------------------------------------------------------- 1 | #' Estimate assignment accuracy of Monte-Carlo cross-validation results 2 | #' 3 | #' This function allows you to estimate assignment accuracies of Monte-Carlo cross-validation results. The output results can be used to make assignment accuracy plots (use function accuracy.plot). 4 | #' @param dir A character string to specify the folder that has your Monte-Carlo cross-validation results. A slash should be included at the end (e.g., dir="YourFolderName/"). 5 | #' @return This function outputs the results in a text file (a table). It can return a data frame when a returning object is specified. 6 | #' @export 7 | accuracy.MC <- function(dir=NULL){ 8 | #Read all "Out_*" file names in a specified directory 9 | fileName_vec <- list.files(path=dir, pattern="Out_*") 10 | fileName_vec <- sort(fileName_vec) 11 | noFiles <- length(fileName_vec)#count number of files 12 | #Read one of files and get pop names 13 | result01 <- read.table(paste0(dir,fileName_vec[1]), header=T, check.names=F, stringsAsFactors=T) 14 | pops <- names(result01)[4:length(names(result01))] #read column name and get the pop names between 4th to last column 15 | noPops <- length(pops)#Number of pops 16 | #create vectors for saving data 17 | Var1 <- NULL; Var2 <- NULL 18 | train.inds <- NULL 19 | train.loci <- NULL 20 | iters <- NULL 21 | assign.rate.all <- NULL 22 | assign.rate.each <- as.data.frame(matrix(nrow=0,ncol=noPops),stringsAsFactors=F) #this will be an N tests by M pops dataframe 23 | #Analyze each assignment test result 24 | for(i in 1:noFiles){ 25 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) #split file name to 4 elements (e.g.,"Out" "4" "0.1" "1.txt");"4"is train.level,"0.1"is fst.level,"1"is iter 26 | train.inds[i] <- oneFileName[2] 27 | train.loci[i] <- oneFileName[3] 28 | iters[i] <- unlist(strsplit(oneFileName[4],split=".txt")) 29 | df <- read.table(paste0(dir,fileName_vec[i]),header=T,stringsAsFactors=T) 30 | #Calculate overall correct assignment rate 31 | #set levels of df$pred.pop and df$origin.pop to pops; handle cases when test individuals are not from every pop. 32 | #df$pred.pop <- factor(df$pred.pop, levels=levels(df$origin.pop)) -- use in ver.1.1.4 and earlier; see issue about accurac.MC error 33 | #levels(df$origin.pop) <- pops -- use in ver1.1.5/6 this causes issues that level of pred pop is ordered by pops and mismatch pop when test individuals not assigned to every pop 34 | #levels(df$pred.pop) <- pops 35 | df$origin.pop <- factor(df$origin.pop, levels=levels(factor(pops))) 36 | df$pred.pop <- factor(df$pred.pop, levels=levels(factor(pops))) 37 | ctable <- table(df$origin.pop,df$pred.pop)#make contingency table 38 | ftable <- as.data.frame(ctable)#convert table to data frame with frequency column 39 | totalSample <- sum(ftable$Freq) 40 | AllcorrectNo <- sum(subset(ftable,Var1==Var2)$Freq) 41 | assign.rate.all[i] <- AllcorrectNo/totalSample 42 | #calculate correct assignment rate each pop 43 | popCorrectRate_vec <- NULL 44 | for(p in pops){ 45 | pop_size <- sum(subset(ftable,Var1==p)$Freq) 46 | if(pop_size==0){ 47 | #if no individual from a pop was assigned to test set, set correct rate to 0 48 | popCorrectRate = 0 49 | }else{ 50 | popCorrectNo <- subset(subset(ftable,Var1==Var2), Var1==p)$Freq 51 | popCorrectRate <- popCorrectNo / pop_size 52 | } 53 | popCorrectRate_vec <- c(popCorrectRate_vec, popCorrectRate) 54 | } 55 | #append correct assign rate of each pop as one row to data frame 56 | assign.rate.each[i,] <- popCorrectRate_vec 57 | } 58 | #concatenate all data 59 | assign_rate_df <- cbind(train.inds, train.loci, iters, assign.rate.all, assign.rate.each, stringsAsFactors=T) 60 | names(assign_rate_df)[5:ncol(assign_rate_df)] <- paste0("assign.rate.",pops) 61 | #output result 62 | write.table(assign_rate_df, file=paste0(dir,"Rate_of_",nrow(assign_rate_df),"_tests_",noPops,"_pops.txt"), quote=F, row.names=F ) 63 | #Print some message to console 64 | cat("\n Correct assignment rates were estimated!!") 65 | cat(paste0("\n A total of ",nrow(assign_rate_df)," assignment tests for ",noPops," pops.")) 66 | cat(paste0("\n Results were also saved in a 'Rate_of_",nrow(assign_rate_df),"_tests_",noPops,"_pops.txt' file in the directory.")) 67 | 68 | return(assign_rate_df) 69 | 70 | } -------------------------------------------------------------------------------- /R/accuracy.kfold.R: -------------------------------------------------------------------------------- 1 | #' Estimate assignment accuracies of K-fold cross-validation results 2 | #' 3 | #' This function allows you to estimate assignment accuracies of K-fold cross-validation results. The output results can be used to make assignment accuracy plots (use function accuracy.plot) and membership probability plot (use function membership.plot) 4 | #' @param dir A character string to specify the folder that has your K-fold cross-validation results. A slash should be included at the end (e.g., dir="YourFolderName/"). 5 | #' @return This function outputs the results in a text file (a table). It can return a data frame when a returning object is specified. 6 | #' @export 7 | accuracy.kfold <- function(dir=NULL){ 8 | #Read all "Out_*" file names in a specified directory 9 | fileName_vec <- list.files(path=dir, pattern="Out_*") 10 | fileName_vec <- sort(fileName_vec) 11 | noFiles <- length(fileName_vec)#count number of files 12 | #Read one of files and get pop names 13 | result01 <- read.table(paste0(dir,fileName_vec[1]), header=T, check.names=F, stringsAsFactors=T) 14 | pops <- names(result01)[4:length(names(result01))] #read column name and get the pop names between 4th to last column 15 | noPops <- length(pops)#Number of pops 16 | #create vectors for saving data 17 | Var1 <- NULL; Var2 <- NULL 18 | KF <- NULL 19 | fold <- NULL 20 | train.loci <- NULL 21 | assign.rate.all <- NULL 22 | assign.rate.each <- as.data.frame(matrix(nrow=0,ncol=noPops),stringsAsFactors=F) #this will be an N tests by M pops dataframe 23 | #Analyze each assignment test result 24 | for(i in 1:noFiles){ 25 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) #split file name to 4 elements (e.g.,"Out" "0.1" "K3" "1.txt");"0.1"is train.loci,"K3"is k fold,"1"is fold 26 | train.loci[i] <- oneFileName[2] 27 | KF[i] <- gsub("K","",oneFileName[3])#Remove "k" character in the "K3" string 28 | fold[i] <- unlist(strsplit(oneFileName[4],split=".txt")) 29 | df <- read.table(paste0(dir,fileName_vec[i]),header=T, stringsAsFactors=T) 30 | #Calculate overall correct assignment rate 31 | df$origin.pop <- factor(df$origin.pop, levels=levels(factor(pops)))#ensure origin/pred pop still keeps the same levels as input pops 32 | df$pred.pop <- factor(df$pred.pop, levels=levels(factor(pops)))#which allows to handle test individuals not assigned to every pop 33 | ctable <- table(df$origin.pop,df$pred.pop)#make contingency table 34 | ftable <- as.data.frame(ctable)#convert table to data frame with frequency column 35 | totalSample <- sum(ftable$Freq) 36 | AllcorrectNo <- sum(subset(ftable,Var1==Var2)$Freq) 37 | assign.rate.all[i] <- AllcorrectNo/totalSample 38 | #calculate correct assignment rate each pop 39 | popCorrectRate_vec <- NULL 40 | for(p in pops){ 41 | pop_size <- sum(subset(ftable,Var1==p)$Freq) 42 | popCorrectNo <- subset(subset(ftable,Var1==Var2), Var1==p)$Freq 43 | popCorrectRate <- popCorrectNo / pop_size 44 | popCorrectRate_vec <- c(popCorrectRate_vec, popCorrectRate) 45 | } 46 | #append correct assign rate of each pop as one row to data frame 47 | assign.rate.each[i,] <- popCorrectRate_vec 48 | }#for(i in 1:noFiles) 49 | #concatenate all data 50 | assign_rate_df <- cbind(KF, fold, train.loci, assign.rate.all, assign.rate.each, stringsAsFactors=T) 51 | names(assign_rate_df)[5:ncol(assign_rate_df)] <- paste0("assign.rate.",pops) 52 | #output result 53 | write.table(assign_rate_df, file=paste0(dir,"Rate_of_",nrow(assign_rate_df),"_tests_",noPops,"_pops.txt"), quote=F, row.names=F ) 54 | #Print some message to console 55 | cat("\n Correct assignment rates were estimated!!") 56 | cat(paste0("\n A total of ",nrow(assign_rate_df)," assignment tests for ",noPops," pops.")) 57 | cat(paste0("\n Results were also saved in a 'Rate_of_",nrow(assign_rate_df),"_tests_",noPops,"_pops.txt' file in the directory.")) 58 | 59 | return(assign_rate_df) 60 | 61 | } 62 | -------------------------------------------------------------------------------- /R/accuracy.plot.R: -------------------------------------------------------------------------------- 1 | #' Make a boxplot (ggplot2 style) of assignment accuracy from cross-validation results 2 | #' 3 | #' This functions allows you to make a boxplot of assignment accuracies estimated from Monte-Carlo or K-fold cross-validation results. 4 | #' @param df A dataframe of your assignment accuracy results. It could be the object returned from the function accuracy.MC() or accuracy.kfold() or a data frame imported to R via other functions (e.g., read.table(...)). 5 | #' @param pop Population names (one or multiple string characters) for making the plot. By default, it uses "all", meaning overall assignment accuracies. It creates faceted plot with one population per panel, if multiple population names are given. The specified population name should match what you entered in read.genpop() earlier. 6 | #' @return This function returns a boxplot plot using the ggplot2 library. Users can modified the plot (e.g., change color, text, etc.) using functions provided by ggplot2 library. 7 | #' @import ggplot2 8 | #' @importFrom reshape2 melt 9 | #' @importFrom rlang .data 10 | #' @examples 11 | #' Your_df <- read.table(system.file("extdata/Rate.txt", package="assignPOP"), header=TRUE) 12 | #' accuracy.plot(Your_df, pop="all") 13 | #' @export 14 | #' 15 | accuracy.plot <- function(df, pop="all"){ 16 | #claim variables 17 | train.inds <- NULL; train.loci <- NULL; value <- NULL; KF <- NULL 18 | #claim variables for aes use 19 | train_inds <- "train.inds"; train_loci <- "train.loci"; KF_ <- "KF" 20 | #validate specified pop names 21 | df_popName <- substring(colnames(df)[4:ncol(df)], 13, 1000L) 22 | if(!all(pop %in% df_popName)){ #if specified pop name not in df 23 | stop(paste0("Pop name not found. Please use one or more of the following names [ ",toString(df_popName)," ] in argument 'pop'. ")) 24 | } 25 | #check if assignment results of either Monte-Carlo or K-fold 26 | firstColname <- names(df)[1] 27 | if(firstColname=="train.inds"){ 28 | #check if training inds is in proportion or fixed number 29 | if(is.factor(df$train.inds)){ #this is used when df is returned object from accuracy.MC() or accuracy.kfold() 30 | checkTrainInds <- as.numeric(levels(df$train.inds))[df$train.inds] 31 | }else if(is.numeric(df$train.inds)){ #this is used when df is read from read.table() 32 | checkTrainInds <- as.numeric(unique(df$train.inds)) 33 | } 34 | #claim x label 35 | if(all(checkTrainInds > 1)){ 36 | x_label <- "Number of individuals used in training set" 37 | }else if(all(checkTrainInds < 1)){ 38 | x_label <- "Proportion of individuals used in training set" 39 | } 40 | #Convert training.inds & train.loci to factors 41 | df$train.inds <- factor(df$train.inds) 42 | df$train.loci <- factor(df$train.loci) 43 | # 44 | #check pop names (one or multiple) 45 | if(length(pop)==1){ 46 | col <- paste0("assign.rate.",pop) 47 | #see if multiple levels of train loci used.(e.g.,10%, 20%...of loci) 48 | if(length(unique(df$train.loci)) > 1 ){ 49 | ## boxplot <- ggplot(df, aes_string(y=col, x="train.inds", fill="train.loci"))+ # aes_string is deprecated in ggplot2 3.0 50 | boxplot <- ggplot(df, aes(y=.data[[col]], x=.data[[train_inds]], fill=.data[[train_loci]]))+ 51 | geom_boxplot()+ 52 | xlab(x_label) + ylab("Assignment accuracy")+ 53 | scale_fill_discrete(name="Prop. of\ntrain loci",guide=guide_legend(reverse=TRUE))+ 54 | theme_bw()+ 55 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 56 | #strip.text.y = element_text(size=16, face="bold"), 57 | #legend.title = element_text(size=17), 58 | #legend.text = element_text(size=16), 59 | #axis.text=element_text(size=16, colour="black"), 60 | #axis.title.y=element_text(size=20, vjust=1.5), 61 | #axis.title.x=element_text(size=20, vjust=0.1)) 62 | return(boxplot) 63 | #see if only one level of train loci used (e.g.,used all loci) 64 | }else if(length(unique(df$train.loci))==1){ 65 | boxplot <- ggplot(df, aes(y=.data[[col]], x=.data[[train_inds]]))+ 66 | geom_boxplot()+ 67 | xlab(x_label) + ylab("Assignment accuracy")+ 68 | theme_bw()+ 69 | theme(legend.position="none", 70 | panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 71 | #axis.text=element_text(size=16, colour="black"), 72 | #axis.title.y=element_text(size=20, vjust=1.5), 73 | #axis.title.x=element_text(size=20, vjust=0.1)) 74 | return(boxplot) 75 | }#else if(length(unique...)) 76 | #If more than one pop names are specified, make faceted plots 77 | }else if(length(pop)>1){ 78 | #Get the first three column data (train.inds, train.loci, iter) 79 | dfn <- df[, 1:3] 80 | #Get the pop assign.rate column by arg. 'pop' 81 | for(pname in pop){ 82 | coln <- paste0("assign.rate.",pname) 83 | colnrate <- df[,coln] 84 | dfn <- cbind(dfn,colnrate) 85 | colnames(dfn)[ncol(dfn)] <- pname 86 | } 87 | #reshape data frame 88 | dfre <- melt(dfn, id=c(1,2,3)) #keep the first three column 89 | dfre$train.inds <- as.factor(dfre$train.inds) 90 | dfre$train.loci <- as.factor(dfre$train.loci) 91 | if("all" %in% pop){ 92 | levels(dfre$variable) <- sub("all", "Overall", levels(dfre$variable)) #change "all" to "Overall" if exists 93 | } 94 | #check if train.loci has multiple levels 95 | if(length(unique(dfre$train.loci)) > 1){ 96 | boxplot <- ggplot(dfre, aes(x=train.inds, y=value, fill=train.loci))+ 97 | geom_boxplot()+ 98 | facet_grid(. ~ variable)+ 99 | xlab(x_label) + ylab("Assignment accuracy") + 100 | scale_fill_discrete(name="Prop. of\ntrain loci",guide=guide_legend(reverse=TRUE))+ 101 | theme_bw()+ 102 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 103 | #strip.text.x = element_text(size=16, face="bold"), 104 | #legend.title = element_text(size=18), 105 | #legend.text = element_text(size=16), 106 | #axis.text=element_text(size=16, colour="black"), 107 | #axis.title.y=element_text(size=20, vjust=1.5), 108 | #axis.title.x=element_text(size=20, vjust=0.1)) 109 | return(boxplot) 110 | 111 | #check if train.loci is one level 112 | }else if(length(unique(dfre$train.loci))==1){ 113 | boxplot <- ggplot(dfre, aes(x=train.inds, y=value))+ 114 | geom_boxplot()+ 115 | facet_grid(. ~ variable)+ 116 | xlab(x_label) + ylab("Assignment accuracy") + 117 | theme_bw()+ 118 | theme(legend.position="none", 119 | panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 120 | #strip.text.x = element_text(size=16, face="bold"), 121 | #axis.text=element_text(size=16, colour="black"), 122 | #axis.title.y=element_text(size=20, vjust=1.5), 123 | #axis.title.x=element_text(size=20, vjust=0.1)) 124 | return(boxplot) 125 | 126 | }#else if(length(unique(dfre$train.loci))==1) 127 | # 128 | }#else if(length(pop)>1) 129 | # 130 | }else if(firstColname=="KF"){ 131 | #check pop names 132 | if(length(pop)==1){ 133 | #Convert training.inds & train.loci to factors 134 | df$KF <- factor(df$KF) 135 | df$fold <- factor(df$fold) 136 | df$train.loci <- factor(df$train.loci) 137 | col <- paste0("assign.rate.",pop) 138 | # 139 | if(length(unique(df$train.loci)) > 1 ){ #see if multiple levels of train loci used.(e.g.,10%, 20%...of loci) 140 | boxplot <- ggplot(df, aes(y=.data[[col]], x=.data[[KF_]] ,fill=.data[[train_loci]]))+ 141 | geom_boxplot()+ 142 | #geom_point(size=5, position=dodge)+ 143 | xlab("K") + ylab("Assignment accuracy")+ 144 | scale_fill_discrete(name="Prop. of\ntrain loci",guide=guide_legend(reverse=TRUE))+ 145 | theme_bw()+ 146 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 147 | #strip.text.y = element_text(size=16, face="bold"), 148 | #legend.title = element_text(size=17), 149 | #legend.text = element_text(size=16), 150 | #axis.text=element_text(size=16, colour="black"), 151 | #axis.title.y=element_text(size=20, vjust=1.5), 152 | #axis.title.x=element_text(size=20, vjust=0.1)) 153 | return(boxplot) 154 | 155 | }else if(length(unique(df$train.loci))==1){ #see if only one level of train loci used (e.g.,used all loci) 156 | boxplot <- ggplot(df, aes(y=.data[[col]], x=.data[[KF_]]))+ 157 | geom_boxplot()+ 158 | xlab("K") + ylab("Assignment accuracy")+ 159 | theme_bw()+ 160 | theme(legend.position="none", 161 | panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 162 | #axis.text=element_text(size=16, colour="black"), 163 | #axis.title.y=element_text(size=20, vjust=1.5), 164 | #axis.title.x=element_text(size=20, vjust=0.1)) 165 | return(boxplot) 166 | } #else if(length(... 167 | 168 | }else if(length(pop)>1){ 169 | #Get the first three column data (train.inds, train.loci, iter) 170 | dfn <- df[, 1:3] 171 | #Get the pop assign.rate column by arg. 'pop' 172 | for(pname in pop){ 173 | coln <- paste0("assign.rate.",pname) 174 | colnrate <- df[,coln] 175 | dfn <- cbind(dfn,colnrate) 176 | colnames(dfn)[ncol(dfn)] <- pname 177 | } 178 | #reshape data frame 179 | dfre <- melt(dfn, id=c(1,2,3)) #keep the first three column 180 | dfre$KF <- as.factor(dfre$KF) 181 | dfre$fold <- as.factor(dfre$fold) 182 | dfre$train.loci <- as.factor(dfre$train.loci) 183 | if("all" %in% pop){ 184 | levels(dfre$variable) <- sub("all", "Overall", levels(dfre$variable)) #change "all" to "Overall" if exists 185 | } 186 | #check if train.loci have multiple levels 187 | if(length(unique(dfre$train.loci)) > 1 ){ 188 | boxplot <- ggplot(dfre, aes(x=KF, y=value, fill=train.loci))+ 189 | geom_boxplot()+ 190 | facet_grid(. ~ variable)+ 191 | xlab("K") + ylab("Assignment accuracy") + 192 | scale_fill_discrete(name="Prop. of\ntrain loci",guide=guide_legend(reverse=TRUE))+ #Reverse box order in legend 193 | theme_bw()+ 194 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 195 | #strip.text.x = element_text(size=16, face="bold"), 196 | #legend.title = element_text(size=18), 197 | #legend.text = element_text(size=16), 198 | #axis.text=element_text(size=16, colour="black"), 199 | #axis.title.y=element_text(size=20, vjust=1.5), 200 | #axis.title.x=element_text(size=20, vjust=0.1)) 201 | return(boxplot) 202 | #check if train.loci is one level 203 | }else if(length(unique(dfre$train.loci))==1 ){ 204 | boxplot <- ggplot(dfre, aes(x=KF, y=value))+ 205 | geom_boxplot()+ 206 | facet_grid(. ~ variable)+ 207 | xlab("K") + ylab("Assignment accuracy") + 208 | theme_bw()+ 209 | theme(legend.position="none", 210 | panel.grid.major = element_blank(), panel.grid.minor=element_blank()) 211 | #strip.text.x = element_text(size=16, face="bold"), 212 | #axis.text=element_text(size=16, colour="black"), 213 | #axis.title.y=element_text(size=20, vjust=1.5), 214 | #axis.title.x=element_text(size=20, vjust=0.1)) 215 | return(boxplot) 216 | } 217 | }#else if(length(pop)>1) 218 | 219 | }#else if(firstColname=="KF") 220 | 221 | }#End 222 | -------------------------------------------------------------------------------- /R/assign.matrix.R: -------------------------------------------------------------------------------- 1 | #' Make an assignment maxtrix from cross-validation results 2 | #' 3 | #' This function generates a pairwise assignment matrix with mean and variation of assignment accuracies estimated across all assignment tests. 4 | #' @param dir A character string to specify the folder that has your cross-validation assignment results. 5 | #' @param train.loci Choose your proportions of training loci used in Monte-Carlo or K-fold cross-validation. Default is "all". 6 | #' @param train.inds Choose your numbers or proportions of training individuals used in Monte-Carlo cross-validation. Default is "all". 7 | #' @param k.fold Choose the k fold values used in K-fold cross-validation. Default is "all". 8 | #' @return The function returns a matrix in R console as well as a file named "assignment_matrix.txt" in the folder. 9 | #' @importFrom stats sd xtabs 10 | #' @export 11 | #' 12 | assign.matrix <- function(dir=NULL, train.loci="all", train.inds="all", k.fold="all"){ 13 | #read files in the folder 14 | fileName_vec <- list.files(path=dir, pattern="Out_*") 15 | fileName_vec <- sort(fileName_vec) 16 | noFiles <- length(fileName_vec)#count number of files 17 | #Read one of files and get pop names 18 | result01 <- read.table(paste0(dir,fileName_vec[1]), header=T, check.names=F, stringsAsFactors=T) 19 | pops <- names(result01)[4:length(names(result01))] #read column name and get the pop names between 4th to last column 20 | noPops <- length(pops)#Number of pops 21 | 22 | #claim variables 23 | fileName_select <- NULL 24 | 25 | #check whether results are from MC or kfold 26 | if(grepl("K", fileName_vec[1])){ #check if it is from kfold results 27 | cvmethod <- "K fold" 28 | #Grap selected train.loci & kfold files 29 | if(!train.loci=="all"){ 30 | if(!k.fold=="all"){ 31 | #when train.loci!="all" & k.fold!="all" 32 | for(i in 1:noFiles){ 33 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 34 | if(oneFileName[2] %in% train.loci){ 35 | if(gsub("K","",oneFileName[3]) %in% k.fold ){ 36 | fileName_select <- c(fileName_select, fileName_vec[i]) 37 | } 38 | } 39 | } 40 | }else{ #when k.fold="all" 41 | #when train.loci!="all" & k.fold=="all" 42 | for(i in 1:noFiles){ 43 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 44 | if(oneFileName[2] %in% train.loci){ 45 | fileName_select <- c(fileName_select, fileName_vec[i]) 46 | } 47 | } 48 | } 49 | }else{ #when train.loci="all" 50 | if(!k.fold=="all"){ 51 | #when train.loci=="all" & k.fold!="all" 52 | for(i in 1:noFiles){ 53 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 54 | if(gsub("K","",oneFileName[3]) %in% k.fold){ 55 | fileName_select <- c(fileName_select, fileName_vec[i]) 56 | } 57 | } 58 | }else{ 59 | #when train.loci & k.fold=="all" 60 | fileName_select <- fileName_vec 61 | } 62 | } 63 | 64 | 65 | }else{ #else it is from MC results 66 | cvmethod <- "Monte-Carlo" 67 | #Grap selected train.loci & train.inds files 68 | if(!train.inds=="all"){ 69 | if(!train.loci=="all"){ 70 | #when train.inds & train.loci != "all" 71 | for(i in 1:noFiles){ 72 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 73 | if(oneFileName[2] %in% train.inds){ 74 | if(oneFileName[3] %in% train.loci){ 75 | fileName_select <- c(fileName_select, fileName_vec[i]) 76 | } 77 | } 78 | } 79 | }else{ 80 | #when train.inds!="all" & train.loci=="all" 81 | for(i in 1:noFiles){ 82 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 83 | if(oneFileName[2] %in% train.inds){ 84 | fileName_select <- c(fileName_select, fileName_vec[i]) 85 | } 86 | } 87 | } 88 | }else{ #train.inds == "all" 89 | if(!train.loci=="all"){ 90 | #when train.inds=="all"&train.loci!="all" 91 | for(i in 1:noFiles){ 92 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 93 | if(oneFileName[3] %in% train.loci){ 94 | fileName_select <- c(fileName_select, fileName_vec[i]) 95 | } 96 | } 97 | }else{ 98 | #when train.inds & train.loci =="all" 99 | fileName_select <- fileName_vec 100 | } 101 | } 102 | } 103 | #check number of selected files 104 | noFiles_select <- length(fileName_select) 105 | 106 | freq_df <- as.data.frame(matrix(nrow=noPops*noPops,ncol=0),stringsAsFactors=F) 107 | #Read each file and process data 108 | for(j in 1:noFiles_select){ 109 | df <- read.table(paste0(dir,fileName_select[j]),header=T, stringsAsFactors=T) 110 | #df$pred.pop <- factor(df$pred.pop, levels=levels(df$origin.pop)) -- used in ver1.1.4 111 | #set levels of df$origin.pop and df$pred.pop to pops 112 | df$origin.pop <- factor(df$origin.pop, levels=levels(factor(pops))) 113 | df$pred.pop <- factor(df$pred.pop, levels=levels(factor(pops))) 114 | ctable <- table(df$origin.pop,df$pred.pop) 115 | #calcuate assignment rate;convert number to rate 116 | for(k in 1:noPops){ 117 | ctable[k,] <- round(ctable[k,]/sum(ctable[k,]), digits=2) 118 | } 119 | ftable <- as.data.frame(ctable)#convert table to data frame with frequency column 120 | freq_df <- cbind(freq_df, ftable$Freq) 121 | } 122 | #estimate mean and sd of assignment rate 123 | assign_mean <- round(apply(freq_df,1,mean,na.rm=T),digits=2) 124 | assign_sd <- round(apply(freq_df,1,sd,na.rm=T),digits=2) 125 | #create dataframe for saving mean and sd 126 | assign_df <- ftable[c(1,2)];colnames(assign_df) <- c("origin","assignment") 127 | #create dataframe of assignment mean 128 | assign_df <- cbind(assign_df,assign_mean, assign_sd) 129 | 130 | #print information 131 | cat(paste0("Assignment across ",noFiles_select," tests from ",cvmethod," cross-validation.\n")) 132 | #print assignment mean with contingency table 133 | cat(" Mean \n") 134 | assignment_mean <- xtabs(assign_mean ~ origin + assignment, data=assign_df) 135 | print(assignment_mean) 136 | #print assignment sd with contingency table 137 | cat("\n Standard Deviation \n") 138 | assignment_SD <- xtabs(assign_sd ~ origin + assignment, data=assign_df) 139 | print(assignment_SD) 140 | 141 | }#end -------------------------------------------------------------------------------- /R/check.loci.R: -------------------------------------------------------------------------------- 1 | #' Check which loci frequently have high Fst across training sets 2 | #' 3 | #' This function reads through training locus file for each assignment test and counts the frequency of those loci and outputs the results in a text file. 4 | #' @param dir A character string to specify the folder with your cross-validation results. A slash should be entered at the end. 5 | #' @param top.loci An integer to specify how many top informative loci to output. 6 | #' @return This function output the results in a text file. It includes the top N informative loci in N rows, and each row has a list of loci sorted by its occurrence. 7 | #' @import stringr 8 | #' @export 9 | #' 10 | check.loci <- function(dir=NULL, top.loci=20){ 11 | #Read all "Out_*" file names in a specified directory 12 | fileName_vec <- list.files(path=dir, pattern="Loci_*") 13 | fileName_vec <- sort(fileName_vec) 14 | noFiles <- length(fileName_vec)#count number of files 15 | 16 | ana_info <- readLines(paste0(dir,"AnalysisInfo.txt")) 17 | locusSampleMethod <- ana_info[8] 18 | if(grepl(pattern="fst", locusSampleMethod)){ #check if results from prior fst sampling method 19 | train_level <- NULL 20 | for(i in 1:noFiles){ 21 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) #split file name to 4 elements (e.g.,"Loci" "4" "0.1" "1.txt");"4"is train.level,"0.1"is fst.level,"1"is iter 22 | train_level[i] <- oneFileName[2] 23 | #fst.level[i] <- oneFileName[3] 24 | #iters[i] <- unlist(strsplit(oneFileName[4],split=".txt")) 25 | } 26 | trainLevels <- unique(train_level)#check out unique training individual levels 27 | #see if only one level of training individuals, if so, skip question 28 | if(length(trainLevels)==1){ 29 | ans_0 <- "all" 30 | }else{ # if levels of training individuals are more than one 31 | cat(paste0("\n ",length(trainLevels)," levels of training individuals are found.")) 32 | cat("\n Which levels would you like to check? (separate levels by a whitespace if multiple)") 33 | cat("\n Options: ");cat(trainLevels, sep=", ");cat(", or all") 34 | ans_0 <- readline("\n enter here: ") 35 | } 36 | 37 | if(grepl(pattern="ALL",toupper(ans_0))){ #if answer is "all" 38 | cat(paste0("Loci occur in top ",top.loci," high Fst across all training data\n"), file=paste0(dir,"High_Fst_Locus_Freq.txt")) #Output the following result to text file 39 | lociMatrix <- NULL 40 | #Read through each Loci_ file. 41 | for(i in 1:noFiles){ 42 | oneFileLoci <- readLines(paste0(dir,fileName_vec[i]))[1:top.loci] #Read one Loci_ file for [top.loci] 43 | lociMatrix <- cbind(lociMatrix, oneFileLoci) 44 | } 45 | #colnames(lociMatrix) <- fileName_vec #rename column for lociMatrix (minor) 46 | #lociMatrix_freq <- as.data.frame(matrix(nrow=0, ncol=3), stringsAsFactors=F) 47 | #Check out top # high Fst loci 48 | for(n in 1:top.loci){ 49 | locName_sort <- names(sort(table(lociMatrix[n,]), decreasing=T)) #Read lociMatrix each row (n-th row) from the top row (highest Fst), and get locus name sorted by freq 50 | locFreq_sort <- as.character(sort(table(lociMatrix[n,]), decreasing=T)) #Get freq of locus, sorted 51 | cat(paste0("top.",n,"(",length(locName_sort),"): "), file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 52 | for(j in 1:length(locName_sort)){ 53 | cat(locName_sort[j], file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 54 | cat(paste0("(",locFreq_sort[j],"), ") , file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 55 | } 56 | cat("\n",file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T ) 57 | } 58 | #print some message to console when done 59 | cat("\n Results were saved in a 'High_Fst_Locus_Freq.txt' file in the directory.") 60 | 61 | #Process data if only a portion is selected 62 | }else if(!grepl(pattern="ALL",toupper(ans_0))){ #else if answer contains no "all" 63 | #parse user input 64 | ans_0 <- str_trim(ans_0, side="both") 65 | ans_0 <- unlist(strsplit(ans_0," ")) 66 | newFileName_vec <- NULL 67 | #Check out selected train level files 68 | for(k in ans_0){ 69 | #Look through Loci_ files 70 | for(i in 1:noFiles){ 71 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) 72 | if(oneFileName[2]==k){ # if train level matches, append the file name to newFileName_vec 73 | newFileName_vec <- c(newFileName_vec, fileName_vec[i]) 74 | } 75 | } 76 | } 77 | noNewFiles <- length(newFileName_vec)#count number of selected files 78 | #Output results to text file and print title 79 | cat(paste0("Loci occur in top ",top.loci," high Fst across selected ( train inds level: "), file=paste0(dir,"High_Fst_Locus_Freq.txt")) 80 | for(a in ans_0){ 81 | cat(paste0(a," "), file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 82 | } 83 | cat(") training data\n", file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T ) 84 | lociMatrix <- NULL 85 | #Read through selected Loci_ file. 86 | for(s in 1:noNewFiles){ 87 | oneFileLoci <- readLines(paste0(dir,newFileName_vec[s]))[1:top.loci] #Read one Loci_ file for [top.loci] 88 | lociMatrix <- cbind(lociMatrix, oneFileLoci) 89 | } 90 | #Check out top # high Fst loci 91 | for(n in 1:top.loci){ 92 | locName_sort <- names(sort(table(lociMatrix[n,]), decreasing=T)) #Read lociMatrix each row (n-th row) from the top row (highest Fst), and get locus name sorted by freq 93 | locFreq_sort <- as.character(sort(table(lociMatrix[n,]), decreasing=T)) #Get freq of locus, sorted 94 | cat(paste0("top.",n,"(",length(locName_sort),"): "), file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 95 | for(j in 1:length(locName_sort)){ 96 | cat(locName_sort[j], file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 97 | cat(paste0("(",locFreq_sort[j],"), ") , file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T) 98 | } 99 | cat("\n",file=paste0(dir,"High_Fst_Locus_Freq.txt"), append=T ) 100 | } 101 | #Print some message in console when done 102 | cat("\n Results were saved in a 'High_Fst_Locus_Freq.txt' file in the directory.") 103 | } 104 | 105 | }else if(grepl(pattern="random", locusSampleMethod)){ 106 | #save for future development 107 | message('Checking loci of results generated from random sampling is not useful.\nUse loci.sample = "Fst" in your analyses.') 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /R/compile.data.R: -------------------------------------------------------------------------------- 1 | #' Compile genetic and other non-genetic data 2 | #' 3 | #' This function allows you to combine genetic and other non-genetic data, such as morphometrics, of the observations for assignment tests. 4 | #' @param x A returned object (list) from the function read.genpop() or reduce.allele(). 5 | #' @param add.x A file containing non-genetic data that has sample ID in the first column. The sample ID must be the same as your GENEPOP file. 6 | #' @param method A method to match sample ID between genetic and non-genetic data. The "common" method only concatenate the data that has sample ID in both files. If an individual only exists in one of the files, this individual will be discarded. 7 | #' @param skipQ A logical variable to determine whether prompting interactive dialogue. If set TRUE, input data type will be recognized as default type and not be verified by the user. 8 | #' @return This function returns a new object (list) that comprises 5 items. [[1]] data matrix including genetic and non-genetic data, [[2]] a sample ID vector, [[3]] a locus name vector, [[4]] a vector of non-genetic variable names, and [[5]] the number of non-genetic variables. 9 | #' @import stringr 10 | #' @importFrom reshape2 melt 11 | #' @importFrom utils read.csv read.table 12 | #' @export 13 | #' 14 | compile.data <- function(x, add.x, method="common", skipQ = F){ 15 | #Read genetic and non-genetic data 16 | genoMatrix <- x[[1]] 17 | if(grepl(pattern=".csv", add.x)){ 18 | cat(" Import a .CSV file.") 19 | add.df <- read.csv(add.x, header=T, stringsAsFactors=T) 20 | 21 | }else { 22 | cat(" Import a table which elements separated by space.") 23 | add.df <- read.table(add.x, header=T, stringsAsFactors=T) 24 | } 25 | #Analyze non-genetic data 26 | varNames <- names(add.df[2:ncol(add.df)])#get variable names (exclude ind ID column) 27 | noVars <- length(varNames)#count number of variables 28 | cat(paste0("\n ",noVars," additional variables detected.")) 29 | cat("\n Checking variable data type...\n") 30 | for(i in 1:noVars){ 31 | var_name <- names(add.df)[i+1] 32 | var_type <- class(add.df[,i+1]) 33 | cat(paste0(" ",var_name,"(",var_type,")")) 34 | } 35 | if(skipQ){ 36 | ans_0 <- "Y" 37 | }else{ 38 | ans_0 <- readline(" Are they correct? (enter Y/N): ") 39 | } 40 | 41 | if(grepl(pattern="N",toupper(ans_0))){ 42 | cat(" please enter variable names for changing data type (separate names by a whitespace if multiple)\n") 43 | ans_1 <- readline(" enter here: ") 44 | ans_1 <- str_trim(ans_1, side="both") 45 | ans_1 <- unlist(strsplit(ans_1,split=" "))#check out variable name to be processed 46 | noChangeVar <- length(ans_1) 47 | #Check if entry is correct 48 | if(!all(ans_1 %in% varNames)){ #if any of entry not in varNames is true 49 | stop("Please enter correct feature names.") 50 | } 51 | #Process variables and convert factor data to dummy variable (binary data) 52 | for(name in ans_1){ 53 | ans_2 <- readline(paste0(" Which data type should '",name,"' be? (enter numeric or factor): ")) 54 | if(grepl(pattern="N",toupper(ans_2))){ 55 | add.df[,name] <- as.numeric(as.character(add.df[,name])) 56 | }else if(grepl(pattern="F",toupper(ans_2))){ 57 | add.df[,name] <- as.factor(add.df[,name]) 58 | } 59 | } 60 | #Look through non-genetic variables and convert to dummy if is factor 61 | for(name in varNames){ 62 | if(is.factor(add.df[,name])){ 63 | #Convert factor variable to numeric binary variable (dummy variable) 64 | dummyData <- as.data.frame(model.matrix( ~ add.df[,name]-1, data=add.df))#get dummy variable data frame 65 | names(dummyData) <- substring(names(dummyData), 15, 1000L)#extract meaningful wording, or remove some funny wording 66 | names(dummyData) <- sub("\\b", paste0(name,"."), names(dummyData))#appedn original variabel name at the beginning 67 | add.df[,name] <- NULL #remove original factor data column 68 | add.df <- cbind(add.df, dummyData) #append new dummy variable column 69 | } 70 | } 71 | }else if(grepl(pattern="Y",toupper(ans_0))){ 72 | #Look through non-genetic variables and convert to dummy if it is factor 73 | for(name in varNames){ 74 | if(is.factor(add.df[,name])){ 75 | #Convert factor variable to numeric binary variable (dummy variable) 76 | dummyData <- as.data.frame(model.matrix( ~ add.df[,name]-1, data=add.df))#get dummy variable data frame 77 | names(dummyData) <- substring(names(dummyData), 15, 1000L)#extract meaningful wording, or remove some funny wording 78 | names(dummyData) <- sub("\\b", paste0(name,"."), names(dummyData))#appedn original variabel name at the beginning 79 | add.df[,name] <- NULL #remove original factor data column 80 | add.df <- cbind(add.df, dummyData) #append new dummy variable column 81 | } 82 | } 83 | } 84 | #Concatenate genetic and non-geneitc data 85 | if(method=="common"){ 86 | #Identify individual IDs 87 | geneData_indID <- x[[2]] #individual ID from genetic data 88 | addData_indID <- as.character(add.df[,1]) #individual ID from non-genetic data 89 | common_indID <- intersect(geneData_indID, addData_indID) #get the common ind ID of two datasets 90 | #Subset data by ind ID 91 | genoMatrix_wID <- cbind(geneData_indID, genoMatrix)#Concatenate ind ID back to genoMatrix 92 | genoMatrix_com <- genoMatrix_wID[(genoMatrix_wID$geneData_indID %in% common_indID),]#subset genoMatrix data based on common ind ID 93 | add.df_com <- add.df[(add.df[,1] %in% common_indID),]#subset non-genetic data based on common ind ID 94 | #Reorder non-genetic data rows by genoMatrix_com ID column 95 | add.df_com <- add.df_com[match(genoMatrix_com[,1], add.df_com[,1]), ] 96 | #Insert non-genetic data to genoMatrix, if two data sets have exact same ind ID 97 | if(identical(as.character(add.df_com[,1]),as.character(genoMatrix_com[,1]))){ 98 | #Concatenate three items: new genoMatrix data, new non-geneitc data, and pop name column 99 | comMatrix <- cbind(genoMatrix_com[,2:(ncol(genoMatrix_com)-1)], add.df_com[,2:ncol(add.df_com)], genoMatrix_com$popNames_vector, stringsAsFactors=T) 100 | colnames(comMatrix)[ncol(comMatrix)] <- "popNames_vector"#rename the last column 101 | rownames(comMatrix) <- NULL 102 | }else { 103 | stop("Individual ID are not identical between two data sets") 104 | } 105 | #Count number of non-genetic columns (new variables) 106 | noVarCols <- ncol(add.df_com)-1 #total number of columns minus first ID column 107 | #Print some message 108 | cat("\n New data set created!!") 109 | cat(paste0("\n It has ",nrow(comMatrix)," observations by ",ncol(comMatrix)," variables")) 110 | cat(paste0("\n including ",length(x[[3]])," loci(",ncol(genoMatrix)-1," alleles) plus ",noVars," additional variables(",ncol(add.df)-1," columns)")) 111 | 112 | return(list(comMatrix, common_indID, x[[3]], varNames, noVarCols )) 113 | 114 | }else if(method=="all"){ 115 | ###(reserve for future development) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /R/membership.plot.R: -------------------------------------------------------------------------------- 1 | #' Make a membership probability plot using results from K-fold cross-validation (ggplot2 style) 2 | #' 3 | #' This function allows you to make a membership probability plot (stacked-bar plot) using results estimated from K-fold cross-validation. 4 | #' @param dir A character string to specify the folder that has your K-fold cross-validation assignment results. A slash should be entered at the end. 5 | #' @param style An option for output style. If style=1, it creates the plot which individuals on the x-axis are in random order. If style=2, individuals are sorted by probabilities within each population. If style=3, individuals of different folds are in seperate plots. If style=4, individuals are separated by fold and sorted by probability. 6 | #' @param non.genetic A logical variable to specify if data are non-genetic. Set it TRUE if you're analyzing non-genetic alone. 7 | #' @param plot.k A number to specify which K of the data set should be plotted. If not given, it will prompt the question. 8 | #' @param plot.loci The proportion of training loci used in your K-fold cross-validation analysis. Specify one of the numbers here to skip question prompt. 9 | #' @return This function returns a stacked-bar plot using the ggplot2 library. Users can modified the plot (e.g., change color, text, etc.) using functions provided by ggplot2 library. 10 | #' @import ggplot2 11 | #' @importFrom reshape2 melt 12 | #' @importFrom stats reorder 13 | #' @export 14 | membership.plot <- function(dir=NULL, style=NULL, non.genetic=FALSE, plot.k=NULL, plot.loci=NULL){ 15 | Ind.ID <- NULL; value <- NULL; variable <- NULL #some NULL variable to handle R CMD check 16 | #Read all "Out_*" file names in a specified directory 17 | fileName_vec <- list.files(path=dir, pattern="Out_*") 18 | fileName_vec <- sort(fileName_vec) 19 | noFiles <- length(fileName_vec)#count number of files 20 | #Read one of files and get pop names 21 | result01 <- read.table(paste0(dir,fileName_vec[1]), header=T, stringsAsFactors=T) 22 | pops <- names(result01)[4:length(names(result01))] #read column name and get the pop names between 4th to last column 23 | noPops <- length(pops)#Number of pops 24 | #create vectors for saving data 25 | k_fold_vec <- NULL 26 | train_loci_vec <- NULL 27 | #Read through file name and collect k.fold and train.loci 28 | for(i in 1:noFiles){ 29 | oneFileName <- unlist(strsplit(fileName_vec[i], split="_")) #split file name to 4 elements (e.g.,"Out" "0.1" "K3" "1.txt");"0.1"is train.loci,"K3"is k fold,"1"is fold 30 | train_loci_vec[i] <- oneFileName[2] 31 | k_fold_vec[i] <- gsub("K","",oneFileName[3])#Remove "k" character in the "K3" string 32 | } 33 | k.fold <- unique(k_fold_vec)#identify unique levels of K 34 | train.loci <- unique(train_loci_vec)#identify unique levels of training loci 35 | 36 | #Check if k.fold only has one level or multiple levels 37 | if(length(k.fold) == 1){ 38 | ans_k <- k.fold 39 | #when plot.k argument is given 40 | }else if(is.numeric(plot.k) & length(plot.k) == 1){ 41 | plot.k <- as.integer(plot.k) 42 | ans_k <- plot.k 43 | if(!ans_k %in% k.fold){ 44 | stop("Your plot.k entry does not exist.") 45 | } 46 | }else{ 47 | cat("\n K = ");cat(paste0(k.fold," "));cat(" are found.") #print out detected "K" on console 48 | ans_k <- readline(" Please enter one of the K numbers: ") #ask user to enter one of the K numbers 49 | ans_k <- str_trim(ans_k, side="both") #clean any space 50 | if(!ans_k %in% k.fold){ 51 | stop("Your entry is not correct.") 52 | } 53 | } 54 | 55 | #Check training loci proportions (levels) or if it is non-genetic data only 56 | if(length(train.loci) > 1){ 57 | if(is.numeric(plot.loci) & length(plot.loci) == 1){ 58 | ans_t <- plot.loci 59 | if(!ans_t %in% train.loci){ 60 | stop("Your plot.loci entry is not correct.") 61 | } 62 | }else{ 63 | cat(paste0("\n ", length(train.loci)," proportions of training loci are found.")) 64 | cat("\n Levels[train.loci]: ");cat(paste0(train.loci," ")) 65 | ans_t <- readline(" Please enter one of the proportions: ") 66 | ans_t <- str_trim(ans_t, side="both") 67 | if(!ans_t %in% train.loci){ 68 | stop("Your entry is not correct.") 69 | } 70 | } 71 | 72 | pltext <- paste0(" , training locus proportion = ",ans_t) 73 | 74 | #If there is only one proportion of training loci or it is non-genetic data only 75 | }else if(length(train.loci)==1){ 76 | if(non.genetic){ 77 | ans_t <- "N" 78 | }else{ 79 | cat(paste0("\n Only one proportion of training loci is found.")) 80 | ans_t <- readline(" Do data include genetic loci? (enter Y/N): ") 81 | ans_t <- str_trim(ans_t, side="both") 82 | if(!toupper(ans_t) %in% c("N","Y","NO","YES")){ 83 | stop("Your entry is not correct.") 84 | } 85 | } 86 | if(grepl(pattern="Y",toupper(ans_t))){ 87 | pltext <- paste0(" , training locus proportion = ",train.loci) 88 | }else if(grepl(pattern="N",toupper(ans_t))){ 89 | pltext <- " " #If it is non-genetic dataset, assign nothing to plot title 90 | } 91 | } 92 | 93 | #Read selected files 94 | df_mas <- data.frame(matrix(ncol=0,nrow=0)) 95 | for(i in 1:ans_k){ 96 | if(length(train.loci) > 1){ 97 | oneFile <- read.table(paste0(dir,"Out_",ans_t,"_K",ans_k,"_",i,".txt"), header=T, stringsAsFactors=T) 98 | }else if(length(train.loci)==1){ 99 | oneFile <- read.table(paste0(dir,"Out_",train.loci,"_K",ans_k,"_",i,".txt"), header=T, stringsAsFactors=T) 100 | } 101 | sampleSize <- nrow(oneFile) 102 | fold_n <- rep(paste0("fold_",i),sampleSize) 103 | oneFile <- cbind(oneFile, fold_n) 104 | df_mas <- rbind(df_mas, oneFile) 105 | } 106 | 107 | if(is.null(style)){ 108 | cat("\n Finally, select one of the output styles.") 109 | cat("\n [1] Random order (Individuals on x-axis are in random order)") 110 | cat("\n [2] Sorted by probability (Individuals are sorted by probabilities within each group)") 111 | cat("\n [3] Separated by fold (Individuals of different folds are in separate plots)") 112 | cat("\n [4] Separated and Sorted (Individuals are separated by fold and sorted by probability)") 113 | style <- readline(" Please enter 1, 2, 3, or 4: ") 114 | style <- str_trim(style, side="both") 115 | if(!style %in% c(1,2,3,4)){ 116 | stop("Your entry is not correct.") 117 | } 118 | } 119 | 120 | if(style==2){ # Individuals are sorted based on the probability of their own populations 121 | #Separate inds among pops and sort 122 | df_mas_2 <- data.frame(matrix(ncol=0,nrow=0)) 123 | for(p in pops){ 124 | df_pop <- df_mas[which(df_mas$origin.pop==p),]#Subset samples for each pop based on the origin.pop variable 125 | df_pop$Ind.ID <- as.factor(df_pop$Ind.ID) #convert ind id charater to factor data 126 | df_pop$Ind.ID <- droplevels(df_pop$Ind.ID) #Drop unexisting ind id levels 127 | df_pop$Ind.ID <- reorder(df_pop$Ind.ID, -df_pop[ ,p])#Reorder ind id levels based on the prob. of its pop 128 | df_mas_2 <- rbind(df_mas_2, df_pop) 129 | } 130 | ndf <- melt(df_mas_2, id.vars=c("Ind.ID","origin.pop","pred.pop","fold_n"))#Reshape the data, making probabilities in one single column (var name="value") 131 | stackplot <- ggplot(ndf, aes(x=Ind.ID, y=value, fill=variable))+ 132 | geom_bar(stat="identity", width=1)+ # width=1 allows no space between bars 133 | #scale_fill_grey()+ # Make the bar color in grey scale 134 | facet_grid( . ~ origin.pop, scales="free_x", space="free_x")+ #scales="free" allows each facet includes the data that exist; space="free" allows facet size being proportionally adjusted 135 | ylab("Probability")+ 136 | labs(title=paste0("K = ",ans_k," ",pltext))+ 137 | coord_cartesian(ylim=c(0, 1.005))+ #add 0.005 on y to give tiny space between panel and facet strip 138 | guides(fill=guide_legend(title=NULL))+ #Hiding title of legend 139 | theme_bw()+ 140 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(),#hiding grid of the panel 141 | strip.background = element_rect(colour="black", fill="white", linetype="solid"),#change facet title background color 142 | plot.title = element_text(hjust=0.5), 143 | axis.title.x = element_blank(), axis.text.x = element_text(angle = 90)) 144 | #plot.title = element_text(size=16, vjust=0.8), 145 | #legend.text = element_text(size=14), 146 | #strip.text.x = element_text(size=16), 147 | #axis.title.y = element_text(size=16), axis.text.y = element_text(size=14, colour="black"), 148 | #axis.title.x = element_blank(), axis.text.x = element_text(angle=90, size=7) ) 149 | return(stackplot) 150 | 151 | }else if(style==3){ #Individuals are separated by each fold 152 | ndf <- melt(df_mas, id.vars=c("Ind.ID","origin.pop","pred.pop","fold_n"))#Reshape the data, making probabilities in one single column (var name="value") 153 | stackplot <- ggplot(ndf, aes(x=Ind.ID, y=value, fill=variable))+ 154 | geom_bar(stat="identity", width=1)+ # width=1 allows no space between bars 155 | #scale_fill_grey()+ # Make the bar color in grey scale 156 | facet_grid( fold_n ~ origin.pop, scales="free_x", space="free_x")+ #scales="free" allows each facet includes the data that exist; space="free" allows facet size being proportionally adjusted 157 | ylab("Probability")+ 158 | labs(title=paste0("K = ",ans_k," ",pltext))+ 159 | coord_cartesian(ylim=c(0, 1.005))+ #add 0.005 on y to give tiny space between panel and facet strip 160 | guides(fill=guide_legend(title=NULL))+ #Hiding title of legend 161 | theme_bw()+ 162 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(),#hiding grid of the panel 163 | strip.background = element_rect(colour="black", fill="white", linetype="solid"),#change facet title background color 164 | plot.title = element_text(hjust=0.5), 165 | axis.title.x = element_blank(), axis.text.x = element_text(angle = 90)) 166 | #plot.title = element_text(size=16, vjust=0.8), 167 | #legend.text = element_text(size=14), 168 | #strip.text.x = element_text(size=16), 169 | #axis.title.y = element_text(size=16), axis.text.y = element_text(size=14, colour="black"), 170 | #axis.title.x = element_blank(), axis.text.x = element_text(angle=90, size=7) ) 171 | return(stackplot) 172 | 173 | }else if(style==4){ #Individuals are separated by fold and sorted by probability 174 | df_mas_4 <- data.frame(matrix(ncol=0,nrow=0)) 175 | for(p in pops){ 176 | df_pop <- df_mas[which(df_mas$origin.pop==p),]#Subset samples for each pop based on the origin.pop variable 177 | df_pop$Ind.ID <- as.factor(df_pop$Ind.ID) #convert ind id charater to factor data 178 | df_pop$Ind.ID <- droplevels(df_pop$Ind.ID) #Drop unexisting ind id levels 179 | df_pop$Ind.ID <- factor(df_pop$Ind.ID, levels=df_pop$Ind.ID[order(df_pop$fold_n, -df_pop[,p])], ordered=T)##Reorder samples by fold.k and then probability values of that pop 180 | df_mas_4 <- rbind(df_mas_4, df_pop) 181 | } 182 | ndf <- melt(df_mas_4, id.vars=c("Ind.ID","origin.pop","pred.pop","fold_n"))#Reshape the data, making probabilities in one single column (var name="value") 183 | stackplot <- ggplot(ndf, aes(x=Ind.ID, y=value, fill=variable))+ 184 | geom_bar(stat="identity", width=1)+ # width=1 allows no space between bars 185 | #scale_fill_grey()+ # Make the bar color in grey scale 186 | facet_grid( fold_n ~ origin.pop, scales="free_x", space="free_x")+ #scales="free" allows each facet includes the data that exist; space="free" allows facet size being proportionally adjusted 187 | ylab("Probability")+ 188 | labs(title=paste0("K = ",ans_k," ",pltext))+ 189 | coord_cartesian(ylim=c(0, 1.005))+ #add 0.005 on y to give tiny space between panel and facet strip 190 | guides(fill=guide_legend(title=NULL))+ #Hiding title of legend 191 | theme_bw()+ 192 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(),#hiding grid of the panel 193 | strip.background = element_rect(colour="black", fill="white", linetype="solid"),#change facet title background color 194 | plot.title = element_text(hjust=0.5), 195 | axis.title.x = element_blank(), axis.text.x = element_text(angle = 90)) 196 | #plot.title = element_text(size=16, vjust=0.8), 197 | #legend.text = element_text(size=14), 198 | #strip.text.x = element_text(size=16), 199 | #axis.title.y = element_text(size=16), axis.text.y = element_text(size=14, colour="black"), 200 | #axis.title.x = element_blank(), axis.text.x = element_text(angle=90, size=7) ) 201 | return(stackplot) 202 | 203 | }else { 204 | ndf <- melt(df_mas, id.vars=c("Ind.ID","origin.pop","pred.pop","fold_n"))#Reshape the data, making probabilities in one single column (var name="value") 205 | stackplot <- ggplot(ndf, aes(x=Ind.ID, y=value, fill=variable))+ 206 | geom_bar(stat="identity", width=1)+ # width=1 allows no space between bars 207 | #scale_fill_grey()+ # Make the bar color in grey scale 208 | facet_grid( . ~ origin.pop, scales="free_x", space="free_x")+ #scales="free" allows each facet includes the data that exist; space="free" allows facet size being proportionally adjusted 209 | ylab("Probability")+ 210 | labs(title=paste0("K = ",ans_k," ",pltext))+ 211 | coord_cartesian(ylim=c(0, 1.005))+ #add 0.005 on y to give tiny space between panel and facet strip 212 | guides(fill=guide_legend(title=NULL))+ #Hiding title of legend 213 | theme_bw()+ 214 | theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(),#hiding grid of the panel 215 | strip.background = element_rect(colour="black", fill="white", linetype="solid"),#change facet title background color 216 | plot.title = element_text(hjust=0.5), 217 | axis.title.x = element_blank(), axis.text.x = element_text(angle = 90)) 218 | #legend.text = element_text(size=14), 219 | #strip.text.x = element_text(size=16), 220 | #axis.title.y = element_text(size=16), axis.text.y = element_text(size=14, colour="black"), 221 | #axis.title.x = element_blank(), axis.text.x = element_text(angle=90, size=7) ) 222 | return(stackplot) 223 | } 224 | 225 | } #End 226 | -------------------------------------------------------------------------------- /R/read.Genepop.R: -------------------------------------------------------------------------------- 1 | #' Read GENEPOP format file 2 | #' 3 | #' This function allows you to import a GENEPOP format file into R. Population names can be specified in the argument. See http://genepop.curtin.edu.au/help_input.html for details about GENEPOP format. 4 | #' @param x GENEPOP file or path to the file. The filename extension (e.g., .txt) should be included. 5 | #' @param pop.names A character string vector for population names. The order of the name should be the same with the order (top to down) in your GENEPOP file. 6 | #' @param haploid A logical variable (TRUE or FALSE) to specify whether your dataset is haploid data. Default is FALSE. 7 | #' @param pos A parameter for program development use; users can ignore it. 8 | #' @return This function returns a list comprising three elements. 1. YOU_NAME_IT$DataMatrix: A matrix of genetic data with a population name label ($popNameVector) in the last column. 2. YOU_NAME_IT$SampleID: A vector of sample ID. 3. YOU_NAME_IT$LocusName: A vector of locus name. 9 | #' @examples # infile <- read.Genepop("Your_Genepop_File.txt", pop.names=c("pop_A", "pop_B", "pop_C")) 10 | #' @references Rousset, F. 2008. Genepop'007: a complete reimplementation of the Genepop software for Windows and Linux. Mol. Ecol. Resources 8: 103-106 11 | #' @import stringr 12 | #' @importFrom reshape2 melt 13 | #' @importFrom utils packageVersion 14 | #' @export 15 | #' 16 | read.Genepop <- function(x, pop.names=NULL, haploid = FALSE, pos=1){ 17 | dataType <- NULL 18 | df <- readLines(x) 19 | df <- df[-1] #remove first line of file description 20 | popIndex <- grep("pop", df, ignore.case=T)#get "pop" tag index 21 | noPops <- length(popIndex) 22 | 23 | #check out pop name and verify number of pops 24 | if(length(pop.names)==0){ 25 | pop.names <- paste0("pop.", seq_along(1:noPops)) 26 | }else if(length(pop.names)>0){ 27 | if(!length(pop.names) == noPops){ 28 | stop("Lengths of 'pop.names' and 'pop' in file not match...") 29 | } 30 | } 31 | 32 | #Extract locus names and save them in locusNames 33 | if(popIndex[1]==2){ #for one-row locus format 34 | locusNames <- str_trim(df[1],side="both") 35 | locusNames <- strsplit(locusNames, ",")[[1]] 36 | }else if(popIndex[1] > 2){ #for multi-row locus format 37 | index <- popIndex[1] - 1 38 | locusNames <- df[1:index] 39 | } 40 | locusNames <- str_trim(locusNames, side="both") 41 | noLocus <- length(locusNames) 42 | 43 | #Get index for individuals 44 | for(i in 1:noPops){ 45 | start <- popIndex[i] + 1 46 | if(i < noPops){ 47 | end <- popIndex[i+1] - 1 48 | 49 | }else if(i == noPops){ 50 | end <- length(df) 51 | } 52 | assign(paste0("pop_", i, "_index"), start:end, envir=as.environment(pos)) 53 | } 54 | pop_all <- lapply(paste0("pop_",seq_along(1:noPops),"_index"), FUN=get) 55 | 56 | #save individal index in one vector 57 | ind_all_index <- unlist(pop_all) 58 | #count total number of individuals 59 | noInds <- length(ind_all_index) 60 | 61 | #extract individual genetic data 62 | ind_df <- df[ind_all_index] 63 | #separate individual ID and genetic data 64 | ind_df <- strsplit(ind_df, split = ",") 65 | #get individual ID 66 | id_vector <- unlist(lapply(ind_df,`[[`,1)) 67 | #clear extra space on both side of id, if exist 68 | id_vector <- str_trim(id_vector, side="both") 69 | 70 | #get genotype data 71 | cat("\n Converting data format...\n") 72 | geno_list <- unlist(lapply(ind_df,`[[`,2)) 73 | geno_list <- str_trim(geno_list, side="both") 74 | #separate each locus by spaces or tabs 75 | geno_list <- strsplit(geno_list, split="[ \t]+", perl=T) 76 | #convert nested list to matrix; high computing step 77 | geno_mx <- matrix(unlist(geno_list), nrow=noInds, byrow=T) 78 | 79 | #check number of digits in one locus (if fewer or equal to 3, set haploid=T) 80 | locusCharSize <- nchar(geno_mx[1,1]) 81 | if(locusCharSize <= 3){ 82 | haploid = T 83 | } 84 | 85 | #apply one-hot encoding for genetic data; high computing step 86 | cat("\n Encoding genetic data...\n") 87 | if(haploid){ 88 | dataType <- "haploid" 89 | onehot_list <- apply(geno_mx,2 ,genepop_onehot, ploidy=1, noChar=locusCharSize) 90 | }else{ 91 | dataType <- "diploid" 92 | onehot_list <- apply(geno_mx,2 ,genepop_onehot, ploidy=2, noChar=locusCharSize) 93 | } 94 | #check if entire list is NA 95 | if(all(is.na(onehot_list))){ 96 | stop("Entire NA data due to identical genotype across samples.") 97 | } 98 | #check and remove locus that is NA 99 | LocusNA_idx <- which(is.na(onehot_list)) 100 | #remove NA locus if exists 101 | if(length(LocusNA_idx)>0){ 102 | onehot_list <- onehot_list[-LocusNA_idx] 103 | #get locus name 104 | locusNames <- locusNames[-LocusNA_idx] 105 | } 106 | #change dataframe's colnames 107 | if(length(onehot_list) == length(locusNames)){ 108 | for(i in 1:length(locusNames)){ 109 | names(onehot_list[[i]]) <- paste0(locusNames[i],"_",names(onehot_list[[i]])) 110 | } 111 | }else{ 112 | stop("Oops, lengths of onehot_list and locusNames differ.") 113 | } 114 | #concatenate dataframe in onehot_list 115 | genoMatrix <- do.call(cbind, onehot_list) 116 | #count number of columns (alleles) in genetic data matrix 117 | noLociVar <- ncol(genoMatrix) 118 | 119 | #Create pop name vector and concatenate to the genoMatrix 120 | popNames_vector <- NULL 121 | for (i in 1:noPops){ 122 | popsize <- length(pop_all[[i]]) 123 | popNameVector <- rep(pop.names[i], popsize) 124 | popNames_vector <- c(popNames_vector, popNameVector) 125 | } 126 | genoMatrix <- cbind(genoMatrix, popNames_vector) 127 | 128 | #Print some message to console 129 | cat(paste0("\n ################ assignPOP v",packageVersion("assignPOP")," ################\n")) 130 | cat("\n A GENEPOP format file was successfully imported!\n") 131 | cat(paste0("\n Imported Data Info: ",noInds," obs. by ",noLocus," loci (",dataType,")")) 132 | cat(paste0("\n Number of pop: ",noPops)) 133 | for(i in 1:noPops){ 134 | popSize <- length(get(paste0("pop_",i,"_index"))) 135 | cat(paste0("\n Number of inds (",pop.names[i],"): ",popSize ) ) 136 | } 137 | cat(paste0("\n DataMatrix: ",nrow(genoMatrix)," rows by ",ncol(genoMatrix), " columns, with ",noLociVar," allele variables")) 138 | 139 | cat("\n") 140 | cat("\n Data output in a list comprising the following three elements:") 141 | cat("\n YOUR_LIST_NAME$DataMatrix") 142 | cat("\n YOUR_LIST_NAME$SampleID") 143 | cat("\n YOUR_LIST_NAME$LocusName") 144 | cat("\n\n") 145 | 146 | #Remove variables from GlobalEnv. 147 | rm(list = ls(pattern="^pop_.*_index$", envir = .GlobalEnv), envir = .GlobalEnv) 148 | 149 | finalList <- list(genoMatrix, id_vector, locusNames) 150 | names(finalList) <- c("DataMatrix", "SampleID" , "LocusName") 151 | return(finalList) 152 | 153 | } 154 | 155 | ######################################## 156 | # Genepop genetic data one-hot encoding 157 | ######################################## 158 | genepop_onehot <- function(oneLoc, ploidy=NULL, noChar=NULL){ 159 | #x is character string vector of a locus 160 | #sample test 161 | #oneLoc <- geno_mx[,3] #multi-alleles 162 | #oneLoc <- geno_mx[,4] #all NA 163 | #oneLoc <- geno_mx[,5] #single allele 164 | 165 | #check if only one allele 166 | oneAllele <- FALSE 167 | if(length(unique(oneLoc))==1){ 168 | oneAllele <- TRUE 169 | } 170 | 171 | #if dataset is haploid 172 | if(ploidy==1){ 173 | #if only one allele 174 | if(oneAllele){ 175 | #if all NA 176 | if(any(c("0","00","000","0000","000000") %in% oneLoc)){ 177 | onehotDF <- NA 178 | }else{ 179 | onehotDF <- as.data.frame(rep(1, length(oneLoc))) 180 | names(onehotDF) <- oneLoc[1] 181 | } 182 | #multi-alleles 183 | }else{ 184 | #convert one locus vector to dataframe 185 | oneLocDF <- data.frame(oneLoc, stringsAsFactors = T) 186 | #get one-hot encoding dataframe 187 | onehotDF <- as.data.frame(model.matrix(~0+oneLocDF[,1])) 188 | names(onehotDF) <- levels(oneLocDF$oneLoc) 189 | #remove missing data 190 | if(any(c("0","00","000","0000","000000") %in% names(onehotDF))){ 191 | onehotDF <- onehotDF[ , -which(names(onehotDF) %in% c("0","00","000","0000","000000")), drop=FALSE] 192 | } 193 | } 194 | #if dataset is diploid 195 | }else if(ploidy==2){ 196 | #if only one allele 197 | if(oneAllele){ 198 | #if all NA 199 | if(any(c("0","00","000","0000","000000") %in% oneLoc)){ 200 | onehotDF <- NA 201 | }else{ 202 | onehotDF <- as.data.frame(rep(1, length(oneLoc))) 203 | names(onehotDF) <- substr(oneLoc[1],1,as.integer(noChar/2)) 204 | } 205 | #if multi-alleles 206 | }else{ 207 | #separate alleles 208 | alleles <- strsplit(oneLoc, split=paste0("(?<=.{",noChar/2,"})"), perl=T) 209 | alleles <- unlist(alleles) 210 | #convert one locus to dataframe 211 | alleles_DF <- data.frame(alleles, stringsAsFactors = T) 212 | onehotMX <- model.matrix(~0+alleles_DF[,1]) 213 | onehotDF <- as.data.frame((onehotMX[c(T,F),] + onehotMX[c(F,T),])/2) 214 | rownames(onehotDF) <- NULL 215 | names(onehotDF) <- levels(alleles_DF$alleles) 216 | #remove missing data 217 | if(any(c("0","00","000","0000","000000") %in% names(onehotDF))){ 218 | onehotDF <- onehotDF[ , -which(names(onehotDF) %in% c("0","00","000","0000","000000")), drop=FALSE] 219 | #drop=FALSE allows to keep it as data frame. Otherwise, only one column will become a vector 220 | } 221 | } 222 | } 223 | return(onehotDF) 224 | } 225 | -------------------------------------------------------------------------------- /R/read.Structure.R: -------------------------------------------------------------------------------- 1 | #' Read Structure format file 2 | #' 3 | #' This function allows you to import a STRUCTURE format file into R. The first row should be locus name (either with or withour column names for sample ID and population label); the first column should be sample ID; the second column should be population label; the rest are genotype. Use "-9" for missing alleles. 4 | #' @param x STRUCTURE file or path to the file. The filename extension (e.g., .txt) should be included. 5 | #' @param ploidy An integer of 1, 2, 3, or 4, to indicate haploid, diploid, triploid, or tetraploid data. Default is 2 (diploid). 6 | #' @return This function returns a list comprising three elements. 1. YOU_NAME_IT$DataMatrix: A matrix of genetic data with a population name label ($popNameVector) in the last column. 2. YOU_NAME_IT$SampleID: A vector of sample ID. 3. YOU_NAME_IT$LocusName: A vector of locus name. 7 | #' @examples # infile <- read.Structure("Your_Structure_File.txt") 8 | #' @references Pritchard, J.K., Stephens, M. and Donnelly, P., 2000. Inference of population structure using multilocus genotype data. Genetics, 155(2), pp.945-959. 9 | #' @import stringr 10 | #' @importFrom utils setTxtProgressBar txtProgressBar packageVersion 11 | #' @export 12 | #' 13 | read.Structure <- function(x, ploidy = 2){ 14 | dataType <- NULL 15 | #check ploidy entry 16 | if(!ploidy %in% c(1,2,3,4)){ 17 | stop("Ploidy should be an integer of 1, 2, 3, or 4.") 18 | } 19 | df <- readLines(x) 20 | #check if there're sample ID and pop label in col names 21 | firstrow <- str_trim(df[1], side="both") 22 | firstrow <- strsplit(firstrow, split="[ \t]+", perl=T)[[1]] 23 | 24 | secondrow <- str_trim(df[2], side="both") 25 | secondrow <- strsplit(secondrow, split="[ \t]+", perl=T)[[1]] 26 | 27 | #Check length of first and second row and then extract locus names 28 | if(length(secondrow) - length(firstrow) == 0){ #there're ID and Pop label 29 | locusNames <- firstrow[3:length(firstrow)] 30 | }else if(length(secondrow) - length(firstrow) == 1){ #there's ID or Pop label 31 | locusNames <- firstrow[2:length(firstrow)] 32 | }else if(length(secondrow) - length(firstrow) == 2){ #there's no ID or Pop label 33 | locusNames <- firstrow 34 | }else { 35 | stop("The numbers of locus and locus name may not be the same.") 36 | } 37 | noLocus <- length(locusNames) 38 | 39 | #remove first row (col names) 40 | df <- df[-1] 41 | #separate all elements 42 | df_split <- strsplit(df, split="[ \t]+", perl=T) 43 | #convert to a matrix 44 | master_mx <- matrix(unlist(df_split), nrow=length(df), byrow=T) 45 | #get sample ID 46 | id_vector <- master_mx[,1] 47 | #get pop label 48 | pop_vector <- master_mx[,2] 49 | #get genetic data 50 | genoMatrix <- master_mx[,3:ncol(master_mx)] 51 | 52 | #one-hot encoding genetic data 53 | genoMatrix <- apply(genoMatrix, 2, structure_onehot, ploidy=ploidy) 54 | 55 | #check if entire NA data 56 | if(all(is.na(genoMatrix))){ 57 | stop("Entire NA data due to identical genotype across samples.") 58 | } 59 | #check and remove locus that is NA 60 | LocusNA_idx <- which(is.na(genoMatrix)) 61 | #remove NA locus if exists 62 | if(length(LocusNA_idx)>0){ 63 | genoMatrix <- genoMatrix[-LocusNA_idx] 64 | #get locus name 65 | locusNames <- locusNames[-LocusNA_idx] 66 | } 67 | 68 | #change dataframe's colnames 69 | if(length(genoMatrix) == length(locusNames)){ 70 | for(i in 1:length(locusNames)){ 71 | names(genoMatrix[[i]]) <- paste0(locusNames[i],"_",names(genoMatrix[[i]])) 72 | } 73 | }else{ 74 | stop("Oops, lengths of genoMatrix and locusNames differ.") 75 | } 76 | #concatenate dataframe in onehot_list 77 | genoMatrix <- do.call(cbind, genoMatrix) 78 | #count number of columns (alleles) in genetic data matrix 79 | noLociVar <- ncol(genoMatrix) 80 | 81 | #get population label 82 | popNames_vector <- NULL 83 | noPops <- length(unique(pop_vector)) 84 | 85 | #for haploid data 86 | if(ploidy == 1){ 87 | dataType <- "haploid" 88 | popNames_vector <- pop_vector 89 | sampleID_vec <- id_vector 90 | }else if(ploidy == 2){ 91 | dataType <- "diploid" 92 | popNames_vector <- pop_vector[c(T,F)] 93 | sampleID_vec <- id_vector[c(T,F)] 94 | }else if(ploidy == 3){ 95 | dataType <- "triploid" 96 | popNames_vector <- pop_vector[c(T,F,F)] 97 | sampleID_vec <- id_vector[c(T,F,F)] 98 | }else if(ploidy == 4){ 99 | dataType <- "tetraploid" 100 | popNames_vector <- pop_vector[c(T,F,F,F)] 101 | sampleID_vec <- id_vector[c(T,F,F,F)] 102 | } 103 | #combine genetic data and pop label 104 | genoMatrix <- cbind(genoMatrix, popNames_vector) 105 | #get number of individuals 106 | noInds <- nrow(genoMatrix) 107 | #get each pop size 108 | popDF <- data.frame(table(popNames_vector)) 109 | 110 | #Print some message to console 111 | cat(paste0("\n ################ assignPOP v",packageVersion("assignPOP")," ################\n")) 112 | cat("\n A STRUCTURE format file was successfully imported!\n") 113 | cat(paste0("\n Imported Data Info: ",noInds," obs. by ",noLocus," loci (",dataType,")")) 114 | cat(paste0("\n Number of pop: ",noPops)) 115 | for(i in 1:noPops){ 116 | cat(paste0("\n Number of inds (",popDF$popNames_vector[i],"): ",popDF$Freq[i] ) ) 117 | } 118 | cat(paste0("\n DataMatrix: ",nrow(genoMatrix)," rows by ",ncol(genoMatrix), " columns, with ",noLociVar," allele variables")) 119 | 120 | cat("\n") 121 | cat("\n Data output in a list comprising the following three elements:") 122 | cat("\n YOUR_LIST_NAME$DataMatrix") 123 | cat("\n YOUR_LIST_NAME$SampleID") 124 | cat("\n YOUR_LIST_NAME$LocusName") 125 | cat("\n\n") 126 | 127 | finalList <- list(genoMatrix, sampleID_vec, locusNames) 128 | names(finalList) <- c("DataMatrix", "SampleID" , "LocusName") 129 | return(finalList) 130 | } 131 | 132 | ######################################## 133 | #Structure genetic data one-hot encoding 134 | ####################################### 135 | structure_onehot <- function(oneLoc, ploidy=NULL){ 136 | #oneLoc <- genoMatrix[,1] 137 | 138 | #if a locus across samples has one allele 139 | if(length(unique(oneLoc))==1){ 140 | #if all NA 141 | if("-9" %in% oneLoc){ 142 | onehotMX <- NA 143 | #if only one allele 144 | }else{ 145 | onehotMX <- as.data.frame(rep(1,length(oneLoc))) 146 | names(onehotMX) <- oneLoc[1] 147 | } 148 | 149 | }else{ 150 | #below also process for haploid 151 | oneLocDF <- data.frame(oneLoc, stringsAsFactors=T) 152 | onehotMX <- as.data.frame(model.matrix(~0+oneLoc, data=oneLocDF)) 153 | #for diploid 154 | if(ploidy==2){ 155 | onehotMX <- as.data.frame(onehotMX[c(T,F),] + onehotMX[c(F,T),])/2 156 | }else if(ploidy==3){ 157 | onehotMX <- (onehotMX[c(T,F,F),] + onehotMX[c(F,T,F),] + onehotMX[c(F,F,T),])/3 158 | }else if(ploidy==4){ 159 | onehotMX <- (onehotMX[c(T,F,F,F),]+onehotMX[c(F,T,F,F),]+onehotMX[c(F,F,T,F),]+onehotMX[c(F,F,F,T),])/4 160 | } 161 | #reset row names 162 | rownames(onehotMX) <- NULL 163 | #rename column names 164 | names(onehotMX) <- levels(oneLocDF$oneLoc) 165 | #remove missing alleles (-9) 166 | if("-9" %in% names(onehotMX)){ 167 | onehotMX <- onehotMX[, -which(names(onehotMX)=="-9"), drop=FALSE] 168 | } 169 | } 170 | return(onehotMX) 171 | } 172 | -------------------------------------------------------------------------------- /R/reduce.allele.R: -------------------------------------------------------------------------------- 1 | #' Remove low variance alleles (dimensionality reduction) 2 | #' 3 | #' This function helps remove alleles that have low variance in the data set such that it can speed up further analyses for a large data set (e.g., > 10K SNPs). 4 | #' @param x A returned object (a list) from the function read.genpop(). 5 | #' @param p A threshold of variance for the alleles to be removed. For example, if p = 0.95 (default setting), an allele occupied more than 95 percents across all the samples will be removed. 6 | #' @return This function return the same object as the function read.genpop() except that the number of columns in the matrix [[1]] is reduced and so is the locus name [[3]]. 7 | #' @export 8 | #' 9 | reduce.allele <- function(x, p = 0.95){ 10 | genoMatrix <- x[[1]] 11 | locusNames <- x[[3]] 12 | noAlleles <- ncol(genoMatrix)-1 13 | noInds <- nrow(genoMatrix) 14 | colToRemove <- NULL 15 | for(i in 1:noAlleles){ #for loop each allele column 16 | alleleVector <- genoMatrix[,i] #extract allele column to a vector 17 | if(length(unique(alleleVector))==1){ #if that allele column has only one kind, remove it 18 | colToRemove <- c(colToRemove, i) #save column #th 19 | } else if(length(unique(alleleVector)) > 1){ #if allele column has more than one kind, 20 | mostAllele <- max(table(alleleVector)) #count number of an allele that has the largest number across individuals 21 | if( (mostAllele/noInds) > p){ #if the most allele has more than p% across individuals 22 | colToRemove <- c(colToRemove, i) #save column #th 23 | } 24 | } 25 | } 26 | #check if all loci variance less than p 27 | if(length(colToRemove)==0){ 28 | stop("All loci have most abudnant alleles less than p threshold. Nothing to be removed.") 29 | } 30 | #Remove columns (alleles) that have low variance 31 | genoMatrix <- genoMatrix[-colToRemove] 32 | #Count new number of columns 33 | newNoAlleles <- ncol(genoMatrix) - 1 34 | #Count how many columns are removed 35 | noColumnsRemoved <- noAlleles - newNoAlleles 36 | 37 | #check if any locus is gone (all alleles gone) and edit the locus name vector 38 | newLocusNames <- NULL 39 | newAlleleLeft <- names(genoMatrix) #get remaining allele names 40 | for(j in 1:length(locusNames)){ 41 | checking <- grep(paste0("^",locusNames[j],"_"), newAlleleLeft) 42 | if(!length(checking)==0){ 43 | newLocusNames <- c(newLocusNames,locusNames[j]) 44 | } 45 | } 46 | newNoLocus <- length(newLocusNames) 47 | #Print some message 48 | cat("\n New data matrix has created! :)") 49 | cat(paste0("\n New DataMatrix size: ",noInds," rows by ",ncol(genoMatrix)," columns")) 50 | cat(paste0("\n ",noColumnsRemoved," columns (alleles) have been removed")) 51 | cat(paste0("\n ",newNoLocus," loci remaining" )) 52 | cat("\n\n") 53 | return(list(genoMatrix, x[[2]], newLocusNames))#x[[2]]is individual ID vector, x[[3]] is locus name 54 | 55 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CRAN status](http://www.r-pkg.org/badges/version/assignPOP)](https://cran.r-project.org/package=assignPOP) 2 | [![GitHub release](https://img.shields.io/github/release/alexkychen/assignPOP.svg)](https://github.com/alexkychen/assignPOP/releases) 3 | [![license](https://img.shields.io/github/license/alexkychen/assignPOP.svg)](https://github.com/alexkychen/assignPOP/blob/master/LICENSE.md) 4 | 5 | # assignPOP 6 | 7 | Population Assignment using Genetic, Non-Genetic or Integrated Data in a Machine-learning Framework 8 | 9 | ## Description 10 | This R package helps perform population assignment and infer population structure using a machine-learning framework. It employs supervised machine-learning methods to evaluate the discriminatory power of your data collected from source populations, and is able to analyze **large genetic, non-genetic, or integrated (genetic plus non-genetic) data** sets. This framework is designed for solving the upward bias issue discussed in previous studies. Main features are listed as follows. 11 | 12 | - Use principle component analysis (PCA) for dimensionality reduction (or data transformation) 13 | - Use Monte-Carlo cross-validation to estimate mean and variance of assignment accuracy 14 | - Use *K*-fold cross-validation to estimate membership probability 15 | - Allow to resample various sizes of training datasets (proportions or fixed numbers of individuals and proportions of loci) 16 | - Allow to choose from various proportions of training loci either randomly or based on locus *Fst* values 17 | - Provide several machine-learning classification algorithms, including LDA, SVM, naive Bayes, decision tree, and random forest, to build tunable predictive models. 18 | - Output results in publication-quality plots that can be modified using ggplot2 functions 19 | 20 | ## Install assignPOP 21 | You can install the released version from CRAN or the up-to-date version from this Github respository. 22 | 23 | - To install from CRAN 24 | * Simply enter `install.packages("assignPOP")` in your R console 25 | 26 | - To install from Github 27 | * step 1. Install devtools package by entering `install.packages("devtools")` 28 | * step 2. Import the library, `library(devtools)` 29 | * step 3. Then enter `install_github("alexkychen/assignPOP")` 30 | 31 | Note: When you install the package from Github, you may need to install additional packages before the assignPOP can be successfully installed. Follow the hints that R provided and then re-run `install_github("alexkychen/assignPOP")`. 32 | 33 | ## Package tutorial 34 | Please visit our tutorial website for more infomration 35 | * [http://alexkychen.github.io/assignPOP/](http://alexkychen.github.io/assignPOP/) 36 | 37 | ## What's new 38 | Changes in ver. 1.3.0 (2024.3.13) 39 | - Update accuracy.plot - adjust ggplot's aes_string() due to its deprecation. 40 | - Update testthat test_accuracy and test_membership to meet ggplot2 3.5.0 requirements 41 | 42 |
43 | History 44 | 45 | Changes in ver. 1.2.4 (2021.10.27) 46 | - Update membership.plot - add argument 'plot.k' and 'plot.loci' to skip related question prompt. 47 | 48 | Changes in ver. 1.2.3 (2021.8.17) 49 | - Update assign.X - (1)Add argument 'common' to specify whether stopping the analysis when inconsistent features between data sets were found. (2)Add argument 'skipQ' to skip data type checking on non-genetic data. (3)Modify argument 'mplot' to handle membership probability plot output. 50 | 51 | Changes in ver. 1.2.2 (2020.11.6) 52 | - Update read.Genepop and read.Structure - locus has only one allele across samples will be kept. Use reduce.allele to remove single-allele or low variance loci. 53 | - In ver. 1.2.1, errors might be generated when running assign.MC (and other assignment test functions) due to existence of single-allele loci. (fixed in ver. 1.2.2) 54 | 55 | Changes in ver. 1.2.1 (2020.8.24) 56 | - Update read.Genepop to increase file reading speed (~40 times faster) 57 | - Update read.Structure to increase file reading speed (~90 times faster) 58 | - read.Structure now also can handle triploid and tetraploid organisms (see arg. ploidy) 59 | - fix bug in allele.reduce to handle small p threshold across all loci 60 | 61 | Changes in ver. 1.2.0 (2020.7.24) 62 | - Add codes to check model name in assign.MC, assign.kfold, assign.X 63 | - Add text to SVM description 64 | - Fix cbind/stringsAsFactors issues in several places for R 4.0 65 | - Able to inject arugments used in models (e.g., gamma in SVM) 66 | 67 | Changes in ver. 1.1.9 (2020.3.16) 68 | - Fix input non-genetic data (x1) error in assign.X 69 | 70 | Changes in ver. 1.1.8 (2020.2.28) 71 | - update following functions to work with R 4.0.0 72 | - accuracy.MC, accuracy.kfold, assign.matrix, compile.data, membership.plot 73 | - add stringsAsFactor=T to read.table and read.csv 74 | - temporarily turn off testthat due to its current failure to pass test in Debian system 75 | 76 | Changes in ver. 1.1.7 (2019.8.26) 77 | - add broken-stick method for principal component selection in assign.MC, assign.kfold, and assign.X functions 78 | - update accuracy.MC, accuracy.kfold, assign.matrix to handle missing levels of predicted population in test results 79 | - update assign. and accuracy. functions to handle numeric population names 80 | 81 | Changes in ver. 1.1.6 (2019.6.8) 82 | - fix multiprocess issue in assign.kfold function 83 | 84 | Changes in ver. 1.1.5 (2018.3.23) 85 | - Update assign.MC & assign.kfold to detect pop size and train.inds/k.fold setting 86 | - Update accuracy.MC & assign.matrix to handle test individuals not from every pop 87 | - Slightly modify levels method in accuracy.kfold 88 | - fix bugs in accuracy.plot for K-fold results 89 | - fix membership.plot title positioning and set text size to default 90 | 91 | Changes in ver. 1.1.4 (2018.3.8) 92 | - Fix missing assign.matrix function 93 | 94 | Changes in ver. 1.1.3 (2017.6.15) 95 | - Add unit tests (using package testthat) 96 | 97 | Changes in ver. 1.1.2 (2017.5.13) 98 | - Change function name read.genpop to read.Genepop; Add function read.Structure. 99 | - Update read.genpop function, now can read haploid data 100 |
101 | 102 | ## Cite this package 103 | Chen, K. Y., Marschall, E. A., Sovic, M. G., Fries, A. C., Gibbs, H. L., & Ludsin, S. A. (2018). assign POP: An R package for population assignment using genetic, non-genetic, or integrated data in a machine-learning framework. *Methods in Ecology and Evolution*. 9(2)439-446. https://doi.org/10.1111/2041-210X.12897 104 | 105 | [Papers citing our package](https://scholar.google.com/scholar?oi=bibs&hl=en&cites=14878258167162189944&as_sdt=5) 106 | 107 | ## Previous version 108 | Previous packages can be found and downloaded at the [releases page](https://github.com/alexkychen/assignPOP/releases) 109 | 110 | ## Version compatibility (2020.7.24) 111 | assignPOP version 1.1.9 and earlier are not fully compatible with newly released R 4.0.0. 112 | If you're using R 4.0.0 (or newer), please update your assignPOP to 1.2.0. 113 | 114 | -------------------------------------------------------------------------------- /assignPOP.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: Ask 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: No 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: ASCII 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | PackageCheckArgs: --as-cran --no-manual 19 | PackageRoxygenize: rd,collate,namespace,vignette 20 | -------------------------------------------------------------------------------- /inst/extdata/Rate.txt: -------------------------------------------------------------------------------- 1 | train.inds train.loci iters assign.rate.all assign.rate.pop_A assign.rate.pop_B assign.rate.pop_C 2 | 0.5 0.1 1 0.933333333333333 1 0.866666666666667 0.933333333333333 3 | 0.5 0.1 10 0.911111111111111 0.933333333333333 1 0.8 4 | 0.5 0.1 11 0.866666666666667 0.866666666666667 0.866666666666667 0.866666666666667 5 | 0.5 0.1 12 0.911111111111111 0.933333333333333 0.866666666666667 0.933333333333333 6 | 0.5 0.1 13 0.844444444444444 0.866666666666667 0.8 0.866666666666667 7 | 0.5 0.1 14 0.911111111111111 1 0.733333333333333 1 8 | 0.5 0.1 15 0.866666666666667 0.933333333333333 0.933333333333333 0.733333333333333 9 | 0.5 0.1 16 0.844444444444444 0.866666666666667 0.666666666666667 1 10 | 0.5 0.1 17 0.866666666666667 0.933333333333333 0.8 0.866666666666667 11 | 0.5 0.1 18 0.933333333333333 1 0.866666666666667 0.933333333333333 12 | 0.5 0.1 19 0.866666666666667 0.933333333333333 1 0.666666666666667 13 | 0.5 0.1 2 0.888888888888889 0.933333333333333 0.733333333333333 1 14 | 0.5 0.1 20 0.844444444444444 1 0.8 0.733333333333333 15 | 0.5 0.1 21 0.822222222222222 0.8 0.733333333333333 0.933333333333333 16 | 0.5 0.1 22 0.822222222222222 0.933333333333333 0.733333333333333 0.8 17 | 0.5 0.1 23 0.844444444444444 0.933333333333333 0.733333333333333 0.866666666666667 18 | 0.5 0.1 24 0.866666666666667 0.933333333333333 0.733333333333333 0.933333333333333 19 | 0.5 0.1 25 0.911111111111111 1 0.8 0.933333333333333 20 | 0.5 0.1 26 0.844444444444444 1 0.733333333333333 0.8 21 | 0.5 0.1 27 0.888888888888889 0.933333333333333 0.8 0.933333333333333 22 | 0.5 0.1 28 0.844444444444444 0.8 0.933333333333333 0.8 23 | 0.5 0.1 29 0.866666666666667 1 0.666666666666667 0.933333333333333 24 | 0.5 0.1 3 0.955555555555556 0.933333333333333 1 0.933333333333333 25 | 0.5 0.1 30 0.866666666666667 0.933333333333333 0.866666666666667 0.8 26 | 0.5 0.1 4 0.888888888888889 0.933333333333333 0.866666666666667 0.866666666666667 27 | 0.5 0.1 5 0.844444444444444 0.866666666666667 0.8 0.866666666666667 28 | 0.5 0.1 6 0.933333333333333 0.866666666666667 0.933333333333333 1 29 | 0.5 0.1 7 0.911111111111111 0.933333333333333 0.866666666666667 0.933333333333333 30 | 0.5 0.1 8 0.777777777777778 0.866666666666667 0.6 0.866666666666667 31 | 0.5 0.1 9 0.844444444444444 0.866666666666667 0.8 0.866666666666667 32 | 0.5 0.25 1 0.888888888888889 1 0.933333333333333 0.733333333333333 33 | 0.5 0.25 10 0.911111111111111 0.933333333333333 1 0.8 34 | 0.5 0.25 11 0.911111111111111 0.866666666666667 0.933333333333333 0.933333333333333 35 | 0.5 0.25 12 0.888888888888889 0.933333333333333 0.866666666666667 0.866666666666667 36 | 0.5 0.25 13 0.866666666666667 0.866666666666667 0.8 0.933333333333333 37 | 0.5 0.25 14 0.888888888888889 1 0.666666666666667 1 38 | 0.5 0.25 15 0.888888888888889 0.933333333333333 0.866666666666667 0.866666666666667 39 | 0.5 0.25 16 0.755555555555556 0.8 0.533333333333333 0.933333333333333 40 | 0.5 0.25 17 0.844444444444444 0.933333333333333 0.733333333333333 0.866666666666667 41 | 0.5 0.25 18 0.933333333333333 1 0.933333333333333 0.866666666666667 42 | 0.5 0.25 19 0.888888888888889 0.933333333333333 0.8 0.933333333333333 43 | 0.5 0.25 2 0.866666666666667 0.866666666666667 0.733333333333333 1 44 | 0.5 0.25 20 0.866666666666667 1 0.866666666666667 0.733333333333333 45 | 0.5 0.25 21 0.822222222222222 0.866666666666667 0.733333333333333 0.866666666666667 46 | 0.5 0.25 22 0.822222222222222 0.866666666666667 0.733333333333333 0.866666666666667 47 | 0.5 0.25 23 0.844444444444444 0.933333333333333 0.666666666666667 0.933333333333333 48 | 0.5 0.25 24 0.866666666666667 0.933333333333333 0.8 0.866666666666667 49 | 0.5 0.25 25 0.933333333333333 1 0.8 1 50 | 0.5 0.25 26 0.822222222222222 1 0.733333333333333 0.733333333333333 51 | 0.5 0.25 27 0.866666666666667 0.866666666666667 0.733333333333333 1 52 | 0.5 0.25 28 0.866666666666667 0.8 0.933333333333333 0.866666666666667 53 | 0.5 0.25 29 0.866666666666667 1 0.733333333333333 0.866666666666667 54 | 0.5 0.25 3 0.977777777777778 0.933333333333333 1 1 55 | 0.5 0.25 30 0.866666666666667 0.866666666666667 0.866666666666667 0.866666666666667 56 | 0.5 0.25 4 0.844444444444444 0.933333333333333 0.8 0.8 57 | 0.5 0.25 5 0.866666666666667 0.866666666666667 0.866666666666667 0.866666666666667 58 | 0.5 0.25 6 0.866666666666667 0.933333333333333 0.666666666666667 1 59 | 0.5 0.25 7 0.866666666666667 0.933333333333333 0.866666666666667 0.8 60 | 0.5 0.25 8 0.777777777777778 0.866666666666667 0.6 0.866666666666667 61 | 0.5 0.25 9 0.8 0.8 0.666666666666667 0.933333333333333 62 | 0.5 0.5 1 0.866666666666667 1 0.866666666666667 0.733333333333333 63 | 0.5 0.5 10 0.888888888888889 0.933333333333333 1 0.733333333333333 64 | 0.5 0.5 11 0.8 0.8 0.866666666666667 0.733333333333333 65 | 0.5 0.5 12 0.888888888888889 1 0.8 0.866666666666667 66 | 0.5 0.5 13 0.866666666666667 0.866666666666667 0.866666666666667 0.866666666666667 67 | 0.5 0.5 14 0.888888888888889 0.933333333333333 0.733333333333333 1 68 | 0.5 0.5 15 0.844444444444444 0.866666666666667 0.933333333333333 0.733333333333333 69 | 0.5 0.5 16 0.911111111111111 0.933333333333333 0.866666666666667 0.933333333333333 70 | 0.5 0.5 17 0.866666666666667 0.933333333333333 0.8 0.866666666666667 71 | 0.5 0.5 18 0.933333333333333 1 0.933333333333333 0.866666666666667 72 | 0.5 0.5 19 0.866666666666667 0.933333333333333 0.733333333333333 0.933333333333333 73 | 0.5 0.5 2 0.866666666666667 0.866666666666667 0.733333333333333 1 74 | 0.5 0.5 20 0.866666666666667 1 0.866666666666667 0.733333333333333 75 | 0.5 0.5 21 0.844444444444444 0.866666666666667 0.8 0.866666666666667 76 | 0.5 0.5 22 0.822222222222222 0.866666666666667 0.666666666666667 0.933333333333333 77 | 0.5 0.5 23 0.888888888888889 0.933333333333333 0.733333333333333 1 78 | 0.5 0.5 24 0.888888888888889 0.933333333333333 0.8 0.933333333333333 79 | 0.5 0.5 25 0.933333333333333 0.933333333333333 0.866666666666667 1 80 | 0.5 0.5 26 0.844444444444444 1 0.733333333333333 0.8 81 | 0.5 0.5 27 0.866666666666667 0.866666666666667 0.733333333333333 1 82 | 0.5 0.5 28 0.822222222222222 0.733333333333333 0.933333333333333 0.8 83 | 0.5 0.5 29 0.911111111111111 1 0.733333333333333 1 84 | 0.5 0.5 3 0.911111111111111 0.933333333333333 1 0.8 85 | 0.5 0.5 30 0.866666666666667 0.866666666666667 0.8 0.933333333333333 86 | 0.5 0.5 4 0.888888888888889 0.866666666666667 0.866666666666667 0.933333333333333 87 | 0.5 0.5 5 0.822222222222222 0.866666666666667 0.666666666666667 0.933333333333333 88 | 0.5 0.5 6 0.822222222222222 0.8 0.866666666666667 0.8 89 | 0.5 0.5 7 0.822222222222222 0.866666666666667 0.933333333333333 0.666666666666667 90 | 0.5 0.5 8 0.8 0.866666666666667 0.666666666666667 0.866666666666667 91 | 0.5 0.5 9 0.755555555555556 0.866666666666667 0.533333333333333 0.866666666666667 92 | 0.5 1 1 0.844444444444444 0.933333333333333 0.866666666666667 0.733333333333333 93 | 0.5 1 10 0.888888888888889 0.933333333333333 1 0.733333333333333 94 | 0.5 1 11 0.822222222222222 0.866666666666667 0.866666666666667 0.733333333333333 95 | 0.5 1 12 0.911111111111111 0.866666666666667 0.866666666666667 1 96 | 0.5 1 13 0.844444444444444 0.866666666666667 0.8 0.866666666666667 97 | 0.5 1 14 0.866666666666667 0.933333333333333 0.666666666666667 1 98 | 0.5 1 15 0.822222222222222 0.866666666666667 0.8 0.8 99 | 0.5 1 16 0.844444444444444 0.8 0.8 0.933333333333333 100 | 0.5 1 17 0.844444444444444 0.933333333333333 0.733333333333333 0.866666666666667 101 | 0.5 1 18 0.888888888888889 1 0.866666666666667 0.8 102 | 0.5 1 19 0.866666666666667 0.933333333333333 0.8 0.866666666666667 103 | 0.5 1 2 0.844444444444444 0.8 0.733333333333333 1 104 | 0.5 1 20 0.822222222222222 1 0.8 0.666666666666667 105 | 0.5 1 21 0.822222222222222 0.866666666666667 0.8 0.8 106 | 0.5 1 22 0.8 0.866666666666667 0.6 0.933333333333333 107 | 0.5 1 23 0.822222222222222 0.933333333333333 0.733333333333333 0.8 108 | 0.5 1 24 0.888888888888889 0.933333333333333 0.8 0.933333333333333 109 | 0.5 1 25 0.933333333333333 1 0.866666666666667 0.933333333333333 110 | 0.5 1 26 0.866666666666667 1 0.733333333333333 0.866666666666667 111 | 0.5 1 27 0.866666666666667 0.866666666666667 0.733333333333333 1 112 | 0.5 1 28 0.8 0.666666666666667 0.933333333333333 0.8 113 | 0.5 1 29 0.822222222222222 1 0.6 0.866666666666667 114 | 0.5 1 3 0.888888888888889 0.866666666666667 1 0.8 115 | 0.5 1 30 0.888888888888889 0.866666666666667 0.866666666666667 0.933333333333333 116 | 0.5 1 4 0.866666666666667 0.933333333333333 0.8 0.866666666666667 117 | 0.5 1 5 0.844444444444444 0.866666666666667 0.666666666666667 1 118 | 0.5 1 6 0.866666666666667 0.866666666666667 0.933333333333333 0.8 119 | 0.5 1 7 0.822222222222222 0.866666666666667 0.866666666666667 0.733333333333333 120 | 0.5 1 8 0.777777777777778 0.866666666666667 0.6 0.866666666666667 121 | 0.5 1 9 0.777777777777778 0.866666666666667 0.533333333333333 0.933333333333333 122 | 0.7 0.1 1 0.740740740740741 1 0.666666666666667 0.555555555555556 123 | 0.7 0.1 10 0.888888888888889 1 0.666666666666667 1 124 | 0.7 0.1 11 0.888888888888889 1 0.888888888888889 0.777777777777778 125 | 0.7 0.1 12 0.851851851851852 1 0.666666666666667 0.888888888888889 126 | 0.7 0.1 13 0.888888888888889 0.888888888888889 0.777777777777778 1 127 | 0.7 0.1 14 0.851851851851852 1 0.555555555555556 1 128 | 0.7 0.1 15 0.925925925925926 0.888888888888889 0.888888888888889 1 129 | 0.7 0.1 16 0.888888888888889 1 0.777777777777778 0.888888888888889 130 | 0.7 0.1 17 0.962962962962963 0.888888888888889 1 1 131 | 0.7 0.1 18 0.888888888888889 1 0.666666666666667 1 132 | 0.7 0.1 19 0.925925925925926 0.888888888888889 0.888888888888889 1 133 | 0.7 0.1 2 0.851851851851852 1 0.666666666666667 0.888888888888889 134 | 0.7 0.1 20 0.962962962962963 0.888888888888889 1 1 135 | 0.7 0.1 21 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 136 | 0.7 0.1 22 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 137 | 0.7 0.1 23 0.888888888888889 0.888888888888889 0.777777777777778 1 138 | 0.7 0.1 24 0.814814814814815 1 0.777777777777778 0.666666666666667 139 | 0.7 0.1 25 0.777777777777778 0.888888888888889 0.555555555555556 0.888888888888889 140 | 0.7 0.1 26 0.851851851851852 1 0.666666666666667 0.888888888888889 141 | 0.7 0.1 27 0.888888888888889 1 0.888888888888889 0.777777777777778 142 | 0.7 0.1 28 0.888888888888889 0.888888888888889 0.777777777777778 1 143 | 0.7 0.1 29 0.925925925925926 1 0.777777777777778 1 144 | 0.7 0.1 3 0.925925925925926 1 0.777777777777778 1 145 | 0.7 0.1 30 0.814814814814815 0.888888888888889 0.666666666666667 0.888888888888889 146 | 0.7 0.1 4 0.888888888888889 0.888888888888889 0.777777777777778 1 147 | 0.7 0.1 5 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 148 | 0.7 0.1 6 0.851851851851852 1 0.888888888888889 0.666666666666667 149 | 0.7 0.1 7 1 1 1 1 150 | 0.7 0.1 8 0.851851851851852 0.888888888888889 0.666666666666667 1 151 | 0.7 0.1 9 0.962962962962963 1 1 0.888888888888889 152 | 0.7 0.25 1 0.740740740740741 1 0.666666666666667 0.555555555555556 153 | 0.7 0.25 10 0.888888888888889 1 0.666666666666667 1 154 | 0.7 0.25 11 0.925925925925926 1 0.888888888888889 0.888888888888889 155 | 0.7 0.25 12 0.851851851851852 1 0.555555555555556 1 156 | 0.7 0.25 13 0.888888888888889 0.888888888888889 0.777777777777778 1 157 | 0.7 0.25 14 0.851851851851852 0.777777777777778 0.888888888888889 0.888888888888889 158 | 0.7 0.25 15 0.925925925925926 0.888888888888889 1 0.888888888888889 159 | 0.7 0.25 16 0.851851851851852 1 0.666666666666667 0.888888888888889 160 | 0.7 0.25 17 0.962962962962963 0.888888888888889 1 1 161 | 0.7 0.25 18 0.851851851851852 0.888888888888889 0.777777777777778 0.888888888888889 162 | 0.7 0.25 19 0.925925925925926 1 0.888888888888889 0.888888888888889 163 | 0.7 0.25 2 0.814814814814815 1 0.444444444444444 1 164 | 0.7 0.25 20 1 1 1 1 165 | 0.7 0.25 21 0.888888888888889 0.888888888888889 1 0.777777777777778 166 | 0.7 0.25 22 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 167 | 0.7 0.25 23 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 168 | 0.7 0.25 24 0.851851851851852 1 0.777777777777778 0.777777777777778 169 | 0.7 0.25 25 0.777777777777778 0.777777777777778 0.666666666666667 0.888888888888889 170 | 0.7 0.25 26 0.888888888888889 1 0.666666666666667 1 171 | 0.7 0.25 27 0.851851851851852 1 0.777777777777778 0.777777777777778 172 | 0.7 0.25 28 0.888888888888889 0.888888888888889 0.777777777777778 1 173 | 0.7 0.25 29 0.888888888888889 1 0.666666666666667 1 174 | 0.7 0.25 3 0.851851851851852 1 0.777777777777778 0.777777777777778 175 | 0.7 0.25 30 0.814814814814815 0.888888888888889 0.666666666666667 0.888888888888889 176 | 0.7 0.25 4 0.925925925925926 1 0.777777777777778 1 177 | 0.7 0.25 5 0.851851851851852 0.777777777777778 0.888888888888889 0.888888888888889 178 | 0.7 0.25 6 0.814814814814815 0.888888888888889 0.888888888888889 0.666666666666667 179 | 0.7 0.25 7 0.888888888888889 1 1 0.666666666666667 180 | 0.7 0.25 8 0.851851851851852 0.888888888888889 0.666666666666667 1 181 | 0.7 0.25 9 0.925925925925926 0.888888888888889 1 0.888888888888889 182 | 0.7 0.5 1 0.740740740740741 1 0.555555555555556 0.666666666666667 183 | 0.7 0.5 10 0.888888888888889 1 0.666666666666667 1 184 | 0.7 0.5 11 0.925925925925926 1 0.888888888888889 0.888888888888889 185 | 0.7 0.5 12 0.814814814814815 0.888888888888889 0.555555555555556 1 186 | 0.7 0.5 13 0.888888888888889 0.888888888888889 0.777777777777778 1 187 | 0.7 0.5 14 0.814814814814815 0.777777777777778 0.777777777777778 0.888888888888889 188 | 0.7 0.5 15 0.925925925925926 0.888888888888889 1 0.888888888888889 189 | 0.7 0.5 16 0.814814814814815 1 0.555555555555556 0.888888888888889 190 | 0.7 0.5 17 0.925925925925926 0.888888888888889 0.888888888888889 1 191 | 0.7 0.5 18 0.851851851851852 0.888888888888889 0.777777777777778 0.888888888888889 192 | 0.7 0.5 19 0.925925925925926 1 0.888888888888889 0.888888888888889 193 | 0.7 0.5 2 0.888888888888889 1 0.666666666666667 1 194 | 0.7 0.5 20 0.962962962962963 1 0.888888888888889 1 195 | 0.7 0.5 21 0.851851851851852 0.888888888888889 0.888888888888889 0.777777777777778 196 | 0.7 0.5 22 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 197 | 0.7 0.5 23 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 198 | 0.7 0.5 24 0.777777777777778 1 0.555555555555556 0.777777777777778 199 | 0.7 0.5 25 0.814814814814815 0.777777777777778 0.777777777777778 0.888888888888889 200 | 0.7 0.5 26 0.814814814814815 0.888888888888889 0.555555555555556 1 201 | 0.7 0.5 27 0.777777777777778 0.777777777777778 0.777777777777778 0.777777777777778 202 | 0.7 0.5 28 0.888888888888889 1 0.777777777777778 0.888888888888889 203 | 0.7 0.5 29 0.814814814814815 0.777777777777778 0.666666666666667 1 204 | 0.7 0.5 3 0.888888888888889 1 0.888888888888889 0.777777777777778 205 | 0.7 0.5 30 0.814814814814815 0.888888888888889 0.666666666666667 0.888888888888889 206 | 0.7 0.5 4 0.888888888888889 1 0.777777777777778 0.888888888888889 207 | 0.7 0.5 5 0.888888888888889 0.777777777777778 0.888888888888889 1 208 | 0.7 0.5 6 0.814814814814815 0.888888888888889 1 0.555555555555556 209 | 0.7 0.5 7 0.888888888888889 1 0.888888888888889 0.777777777777778 210 | 0.7 0.5 8 0.814814814814815 0.888888888888889 0.555555555555556 1 211 | 0.7 0.5 9 0.888888888888889 0.888888888888889 1 0.777777777777778 212 | 0.7 1 1 0.740740740740741 1 0.555555555555556 0.666666666666667 213 | 0.7 1 10 0.888888888888889 1 0.666666666666667 1 214 | 0.7 1 11 0.888888888888889 1 0.888888888888889 0.777777777777778 215 | 0.7 1 12 0.851851851851852 0.888888888888889 0.666666666666667 1 216 | 0.7 1 13 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 217 | 0.7 1 14 0.814814814814815 0.777777777777778 0.666666666666667 1 218 | 0.7 1 15 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 219 | 0.7 1 16 0.851851851851852 1 0.666666666666667 0.888888888888889 220 | 0.7 1 17 0.962962962962963 0.888888888888889 1 1 221 | 0.7 1 18 0.851851851851852 0.888888888888889 0.777777777777778 0.888888888888889 222 | 0.7 1 19 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 223 | 0.7 1 2 0.814814814814815 1 0.444444444444444 1 224 | 0.7 1 20 1 1 1 1 225 | 0.7 1 21 0.925925925925926 0.888888888888889 1 0.888888888888889 226 | 0.7 1 22 0.851851851851852 0.777777777777778 0.888888888888889 0.888888888888889 227 | 0.7 1 23 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 228 | 0.7 1 24 0.740740740740741 1 0.444444444444444 0.777777777777778 229 | 0.7 1 25 0.814814814814815 0.777777777777778 0.888888888888889 0.777777777777778 230 | 0.7 1 26 0.851851851851852 0.888888888888889 0.666666666666667 1 231 | 0.7 1 27 0.777777777777778 0.777777777777778 0.777777777777778 0.777777777777778 232 | 0.7 1 28 0.925925925925926 1 0.888888888888889 0.888888888888889 233 | 0.7 1 29 0.814814814814815 0.666666666666667 0.777777777777778 1 234 | 0.7 1 3 0.888888888888889 1 0.777777777777778 0.888888888888889 235 | 0.7 1 30 0.814814814814815 0.888888888888889 0.666666666666667 0.888888888888889 236 | 0.7 1 4 0.888888888888889 0.888888888888889 0.888888888888889 0.888888888888889 237 | 0.7 1 5 0.888888888888889 0.777777777777778 0.888888888888889 1 238 | 0.7 1 6 0.814814814814815 0.888888888888889 1 0.555555555555556 239 | 0.7 1 7 0.888888888888889 1 1 0.666666666666667 240 | 0.7 1 8 0.814814814814815 0.888888888888889 0.555555555555556 1 241 | 0.7 1 9 0.888888888888889 0.888888888888889 1 0.777777777777778 242 | 0.9 0.1 1 0.777777777777778 0.666666666666667 0.666666666666667 1 243 | 0.9 0.1 10 0.888888888888889 0.666666666666667 1 1 244 | 0.9 0.1 11 0.888888888888889 1 1 0.666666666666667 245 | 0.9 0.1 12 0.777777777777778 0.666666666666667 0.666666666666667 1 246 | 0.9 0.1 13 1 1 1 1 247 | 0.9 0.1 14 0.777777777777778 1 0.333333333333333 1 248 | 0.9 0.1 15 0.777777777777778 1 0.333333333333333 1 249 | 0.9 0.1 16 0.777777777777778 1 0.666666666666667 0.666666666666667 250 | 0.9 0.1 17 0.888888888888889 1 0.666666666666667 1 251 | 0.9 0.1 18 1 1 1 1 252 | 0.9 0.1 19 0.666666666666667 0.666666666666667 0.333333333333333 1 253 | 0.9 0.1 2 0.888888888888889 0.666666666666667 1 1 254 | 0.9 0.1 20 0.888888888888889 1 1 0.666666666666667 255 | 0.9 0.1 21 0.666666666666667 0.333333333333333 0.666666666666667 1 256 | 0.9 0.1 22 0.888888888888889 1 0.666666666666667 1 257 | 0.9 0.1 23 0.777777777777778 0.666666666666667 1 0.666666666666667 258 | 0.9 0.1 24 0.777777777777778 1 0.666666666666667 0.666666666666667 259 | 0.9 0.1 25 0.888888888888889 0.666666666666667 1 1 260 | 0.9 0.1 26 0.888888888888889 1 0.666666666666667 1 261 | 0.9 0.1 27 0.888888888888889 1 1 0.666666666666667 262 | 0.9 0.1 28 1 1 1 1 263 | 0.9 0.1 29 1 1 1 1 264 | 0.9 0.1 3 1 1 1 1 265 | 0.9 0.1 30 1 1 1 1 266 | 0.9 0.1 4 1 1 1 1 267 | 0.9 0.1 5 1 1 1 1 268 | 0.9 0.1 6 1 1 1 1 269 | 0.9 0.1 7 1 1 1 1 270 | 0.9 0.1 8 1 1 1 1 271 | 0.9 0.1 9 1 1 1 1 272 | 0.9 0.25 1 0.888888888888889 0.666666666666667 1 1 273 | 0.9 0.25 10 0.888888888888889 0.666666666666667 1 1 274 | 0.9 0.25 11 0.666666666666667 0.666666666666667 0.666666666666667 0.666666666666667 275 | 0.9 0.25 12 0.777777777777778 0.666666666666667 0.666666666666667 1 276 | 0.9 0.25 13 0.888888888888889 1 1 0.666666666666667 277 | 0.9 0.25 14 0.777777777777778 1 0.333333333333333 1 278 | 0.9 0.25 15 0.777777777777778 1 0.333333333333333 1 279 | 0.9 0.25 16 0.888888888888889 1 1 0.666666666666667 280 | 0.9 0.25 17 0.888888888888889 1 0.666666666666667 1 281 | 0.9 0.25 18 1 1 1 1 282 | 0.9 0.25 19 0.777777777777778 0.666666666666667 0.666666666666667 1 283 | 0.9 0.25 2 0.888888888888889 0.666666666666667 1 1 284 | 0.9 0.25 20 0.888888888888889 1 0.666666666666667 1 285 | 0.9 0.25 21 0.777777777777778 0.333333333333333 1 1 286 | 0.9 0.25 22 0.666666666666667 0.666666666666667 0.666666666666667 0.666666666666667 287 | 0.9 0.25 23 0.777777777777778 0.666666666666667 1 0.666666666666667 288 | 0.9 0.25 24 0.777777777777778 1 0.666666666666667 0.666666666666667 289 | 0.9 0.25 25 0.888888888888889 0.666666666666667 1 1 290 | 0.9 0.25 26 1 1 1 1 291 | 0.9 0.25 27 1 1 1 1 292 | 0.9 0.25 28 1 1 1 1 293 | 0.9 0.25 29 1 1 1 1 294 | 0.9 0.25 3 1 1 1 1 295 | 0.9 0.25 30 1 1 1 1 296 | 0.9 0.25 4 1 1 1 1 297 | 0.9 0.25 5 1 1 1 1 298 | 0.9 0.25 6 1 1 1 1 299 | 0.9 0.25 7 1 1 1 1 300 | 0.9 0.25 8 1 1 1 1 301 | 0.9 0.25 9 1 1 1 1 302 | 0.9 0.5 1 0.888888888888889 0.666666666666667 1 1 303 | 0.9 0.5 10 0.888888888888889 0.666666666666667 1 1 304 | 0.9 0.5 11 0.666666666666667 0.666666666666667 1 0.333333333333333 305 | 0.9 0.5 12 0.777777777777778 0.666666666666667 0.666666666666667 1 306 | 0.9 0.5 13 0.888888888888889 1 1 0.666666666666667 307 | 0.9 0.5 14 0.777777777777778 1 0.333333333333333 1 308 | 0.9 0.5 15 0.888888888888889 1 0.666666666666667 1 309 | 0.9 0.5 16 1 1 1 1 310 | 0.9 0.5 17 0.888888888888889 1 0.666666666666667 1 311 | 0.9 0.5 18 0.888888888888889 1 0.666666666666667 1 312 | 0.9 0.5 19 0.666666666666667 0.666666666666667 0.333333333333333 1 313 | 0.9 0.5 2 0.888888888888889 0.666666666666667 1 1 314 | 0.9 0.5 20 0.888888888888889 1 0.666666666666667 1 315 | 0.9 0.5 21 0.777777777777778 0.333333333333333 1 1 316 | 0.9 0.5 22 0.777777777777778 0.666666666666667 0.666666666666667 1 317 | 0.9 0.5 23 0.888888888888889 1 1 0.666666666666667 318 | 0.9 0.5 24 0.888888888888889 1 0.666666666666667 1 319 | 0.9 0.5 25 0.888888888888889 0.666666666666667 1 1 320 | 0.9 0.5 26 0.888888888888889 1 0.666666666666667 1 321 | 0.9 0.5 27 0.888888888888889 1 1 0.666666666666667 322 | 0.9 0.5 28 0.666666666666667 1 0.333333333333333 0.666666666666667 323 | 0.9 0.5 29 1 1 1 1 324 | 0.9 0.5 3 1 1 1 1 325 | 0.9 0.5 30 1 1 1 1 326 | 0.9 0.5 4 1 1 1 1 327 | 0.9 0.5 5 0.888888888888889 1 1 0.666666666666667 328 | 0.9 0.5 6 0.888888888888889 0.666666666666667 1 1 329 | 0.9 0.5 7 1 1 1 1 330 | 0.9 0.5 8 1 1 1 1 331 | 0.9 0.5 9 1 1 1 1 332 | 0.9 1 1 0.888888888888889 0.666666666666667 1 1 333 | 0.9 1 10 0.888888888888889 0.666666666666667 1 1 334 | 0.9 1 11 0.666666666666667 0.666666666666667 0.666666666666667 0.666666666666667 335 | 0.9 1 12 0.666666666666667 0.666666666666667 0.666666666666667 0.666666666666667 336 | 0.9 1 13 0.888888888888889 1 1 0.666666666666667 337 | 0.9 1 14 0.777777777777778 1 0.333333333333333 1 338 | 0.9 1 15 0.888888888888889 1 0.666666666666667 1 339 | 0.9 1 16 1 1 1 1 340 | 0.9 1 17 0.888888888888889 1 0.666666666666667 1 341 | 0.9 1 18 1 1 1 1 342 | 0.9 1 19 0.666666666666667 0.666666666666667 0.333333333333333 1 343 | 0.9 1 2 0.888888888888889 0.666666666666667 1 1 344 | 0.9 1 20 0.888888888888889 1 0.666666666666667 1 345 | 0.9 1 21 0.777777777777778 0.333333333333333 1 1 346 | 0.9 1 22 0.777777777777778 0.666666666666667 1 0.666666666666667 347 | 0.9 1 23 0.888888888888889 1 1 0.666666666666667 348 | 0.9 1 24 0.888888888888889 1 0.666666666666667 1 349 | 0.9 1 25 0.888888888888889 0.666666666666667 1 1 350 | 0.9 1 26 1 1 1 1 351 | 0.9 1 27 0.888888888888889 1 1 0.666666666666667 352 | 0.9 1 28 0.777777777777778 1 0.333333333333333 1 353 | 0.9 1 29 1 1 1 1 354 | 0.9 1 3 1 1 1 1 355 | 0.9 1 30 1 1 1 1 356 | 0.9 1 4 1 1 1 1 357 | 0.9 1 5 1 1 1 1 358 | 0.9 1 6 0.888888888888889 0.666666666666667 1 1 359 | 0.9 1 7 1 1 1 1 360 | 0.9 1 8 0.888888888888889 1 1 0.666666666666667 361 | 0.9 1 9 1 1 1 1 362 | -------------------------------------------------------------------------------- /inst/extdata/TinyGenepop.txt: -------------------------------------------------------------------------------- 1 | Genepop toy 2 | Locus_1 3 | Locus_2 4 | Locus_3 5 | pop 6 | 1_1, 0101 0102 0202 7 | 1_2, 0101 0102 0202 8 | pop 9 | 2_1, 0101 0102 0202 10 | 2_2, 0101 0102 0202 11 | pop 12 | 3_1, 0101 0102 0202 13 | 3_2, 0101 0102 0202 -------------------------------------------------------------------------------- /inst/extdata/TinyVars.csv: -------------------------------------------------------------------------------- 1 | ID,D1-2,D2-3 2 | 1_1,44,73 3 | 1_2,53,71 4 | 2_1,45,66 5 | 2_2,46,78 6 | 3_1,54,66 7 | 3_2,47,83 -------------------------------------------------------------------------------- /inst/extdata/morphData.csv: -------------------------------------------------------------------------------- 1 | "ID","D1-2","D2-3","D3-4","D1-4" 2 | "1_1",42,76,45,87 3 | "1_2",44,73,56,88 4 | "1_3",53,71,48,85 5 | "1_4",45,66,46,69 6 | "1_5",46,78,49,87 7 | "1_6",54,66,49,76 8 | "1_7",47,83,56,81 9 | "1_8",39,79,50,81 10 | "1_9",42,69,54,83 11 | "1_10",43,64,48,80 12 | "1_11",51,66,51,67 13 | "1_12",47,72,48,86 14 | "1_13",47,69,50,83 15 | "1_14",46,68,46,78 16 | "1_15",42,64,43,81 17 | "1_16",54,70,60,81 18 | "1_17",47,65,53,82 19 | "1_18",35,60,44,93 20 | "1_19",49,68,47,78 21 | "1_20",43,76,44,81 22 | "1_21",40,67,61,89 23 | "1_22",44,74,57,88 24 | "1_23",40,60,49,89 25 | "1_24",41,70,53,75 26 | "1_25",42,73,48,96 27 | "1_26",37,72,48,81 28 | "1_27",49,71,46,95 29 | "1_28",46,66,47,69 30 | "1_29",39,65,58,80 31 | "1_30",51,64,50,90 32 | "2_1",62,66,56,64 33 | "2_2",59,59,56,64 34 | "2_3",64,62,61,62 35 | "2_4",64,63,52,62 36 | "2_5",64,76,50,67 37 | "2_6",63,61,63,73 38 | "2_7",63,66,53,54 39 | "2_8",60,65,51,72 40 | "2_9",58,59,49,80 41 | "2_10",58,65,49,86 42 | "2_11",57,74,52,80 43 | "2_12",59,68,58,76 44 | "2_13",54,65,61,56 45 | "2_14",71,62,59,65 46 | "2_15",66,53,53,67 47 | "2_16",54,72,55,76 48 | "2_17",58,56,51,69 49 | "2_18",58,69,51,60 50 | "2_19",64,76,59,83 51 | "2_20",60,56,50,77 52 | "2_21",61,69,65,72 53 | "2_22",60,63,55,80 54 | "2_23",60,56,56,59 55 | "2_24",67,56,51,75 56 | "2_25",59,55,52,66 57 | "2_26",68,62,48,75 58 | "2_27",52,56,54,70 59 | "2_28",63,69,57,75 60 | "2_29",61,78,57,81 61 | "2_30",61,57,51,70 62 | "3_1",62,71,56,78 63 | "3_2",57,71,57,60 64 | "3_3",58,68,67,64 65 | "3_4",55,60,54,82 66 | "3_5",55,65,59,73 67 | "3_6",62,64,70,54 68 | "3_7",62,69,59,59 69 | "3_8",60,64,53,68 70 | "3_9",65,72,57,77 71 | "3_10",70,64,62,69 72 | "3_11",58,72,58,75 73 | "3_12",48,60,57,78 74 | "3_13",65,58,58,83 75 | "3_14",56,85,60,70 76 | "3_15",57,63,68,70 77 | "3_16",65,68,60,56 78 | "3_17",59,70,65,71 79 | "3_18",54,63,63,65 80 | "3_19",61,69,59,62 81 | "3_20",59,68,52,69 82 | "3_21",60,65,57,78 83 | "3_22",62,66,58,54 84 | "3_23",58,66,60,67 85 | "3_24",63,79,67,71 86 | "3_25",59,62,71,63 87 | "3_26",62,59,68,73 88 | "3_27",65,66,59,73 89 | "3_28",62,68,51,70 90 | "3_29",58,69,58,50 91 | "3_30",66,63,60,91 92 | -------------------------------------------------------------------------------- /inst/extdata/morphDataX.csv: -------------------------------------------------------------------------------- 1 | ID,D1-2,D2-3,D3-4,D1-4 2 | X.01,51,64,52,78 3 | X.02,47,69,49,82 4 | X.03,47,64,54,74 5 | X.04,46,66,54,98 6 | X.05,42,66,54,91 7 | X.06,54,60,53,75 8 | X.07,47,75,53,80 9 | X.08,35,71,50,80 10 | X.09,49,63,48,88 11 | X.10,43,78,48,82 12 | X.11,62,67,53,70 13 | X.12,61,60,46,73 14 | X.13,61,62,59,67 15 | X.14,66,57,52,75 16 | X.15,60,56,52,68 17 | X.16,67,66,59,73 18 | X.17,55,67,54,79 19 | X.18,63,64,50,73 20 | X.19,61,70,56,67 21 | X.20,62,78,54,79 22 | X.21,64,63,57,71 23 | X.22,62,69,63,61 24 | X.23,61,66,52,66 25 | X.24,57,65,60,68 26 | X.25,65,61,63,87 27 | X.26,58,67,62,64 28 | X.27,69,62,61,72 29 | X.28,66,57,57,71 30 | X.29,59,65,56,61 31 | X.30,56,73,55,69 -------------------------------------------------------------------------------- /man/accuracy.MC.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accuracy.MC.R 3 | \name{accuracy.MC} 4 | \alias{accuracy.MC} 5 | \title{Estimate assignment accuracy of Monte-Carlo cross-validation results} 6 | \usage{ 7 | accuracy.MC(dir = NULL) 8 | } 9 | \arguments{ 10 | \item{dir}{A character string to specify the folder that has your Monte-Carlo cross-validation results. A slash should be included at the end (e.g., dir="YourFolderName/").} 11 | } 12 | \value{ 13 | This function outputs the results in a text file (a table). It can return a data frame when a returning object is specified. 14 | } 15 | \description{ 16 | This function allows you to estimate assignment accuracies of Monte-Carlo cross-validation results. The output results can be used to make assignment accuracy plots (use function accuracy.plot). 17 | } 18 | -------------------------------------------------------------------------------- /man/accuracy.kfold.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accuracy.kfold.R 3 | \name{accuracy.kfold} 4 | \alias{accuracy.kfold} 5 | \title{Estimate assignment accuracies of K-fold cross-validation results} 6 | \usage{ 7 | accuracy.kfold(dir = NULL) 8 | } 9 | \arguments{ 10 | \item{dir}{A character string to specify the folder that has your K-fold cross-validation results. A slash should be included at the end (e.g., dir="YourFolderName/").} 11 | } 12 | \value{ 13 | This function outputs the results in a text file (a table). It can return a data frame when a returning object is specified. 14 | } 15 | \description{ 16 | This function allows you to estimate assignment accuracies of K-fold cross-validation results. The output results can be used to make assignment accuracy plots (use function accuracy.plot) and membership probability plot (use function membership.plot) 17 | } 18 | -------------------------------------------------------------------------------- /man/accuracy.plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/accuracy.plot.R 3 | \name{accuracy.plot} 4 | \alias{accuracy.plot} 5 | \title{Make a boxplot (ggplot2 style) of assignment accuracy from cross-validation results} 6 | \usage{ 7 | accuracy.plot(df, pop = "all") 8 | } 9 | \arguments{ 10 | \item{df}{A dataframe of your assignment accuracy results. It could be the object returned from the function accuracy.MC() or accuracy.kfold() or a data frame imported to R via other functions (e.g., read.table(...)).} 11 | 12 | \item{pop}{Population names (one or multiple string characters) for making the plot. By default, it uses "all", meaning overall assignment accuracies. It creates faceted plot with one population per panel, if multiple population names are given. The specified population name should match what you entered in read.genpop() earlier.} 13 | } 14 | \value{ 15 | This function returns a boxplot plot using the ggplot2 library. Users can modified the plot (e.g., change color, text, etc.) using functions provided by ggplot2 library. 16 | } 17 | \description{ 18 | This functions allows you to make a boxplot of assignment accuracies estimated from Monte-Carlo or K-fold cross-validation results. 19 | } 20 | \examples{ 21 | Your_df <- read.table(system.file("extdata/Rate.txt", package="assignPOP"), header=TRUE) 22 | accuracy.plot(Your_df, pop="all") 23 | } 24 | -------------------------------------------------------------------------------- /man/assign.MC.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/assign.MC.R 3 | \name{assign.MC} 4 | \alias{assign.MC} 5 | \title{Population assignment test using Monte-Carlo cross-validation} 6 | \usage{ 7 | assign.MC( 8 | x, 9 | train.inds = c(0.5, 0.7, 0.9), 10 | train.loci = c(0.1, 0.25, 0.5, 1), 11 | loci.sample = "fst", 12 | iterations = 20, 13 | dir = NULL, 14 | scaled = FALSE, 15 | pca.method = "mixed", 16 | pca.PCs = "kaiser-guttman", 17 | pca.loadings = F, 18 | model = "svm", 19 | svm.kernel = "linear", 20 | svm.cost = 1, 21 | ntree = 50, 22 | multiprocess = TRUE, 23 | processors = 999, 24 | skipQ = FALSE, 25 | ... 26 | ) 27 | } 28 | \arguments{ 29 | \item{x}{An input object which should be the object (list) returned from the function read.genpop(), reduce.allele(), or compile.data(). It could also be a data frame (with column name) returned from read.csv() or read.table() if you're analyzing non-genetic data, such as morphormetrics, chemistry data. The non-genetic data frame should have sample ID in the first column and population label in the last column.} 30 | 31 | \item{train.inds}{The number (integer greater than 1) or proportion (float between 0 and 1) of individuals (observations) from each population to be used as training data. Use a numeric vector to specify multiple sets of training individuals. No mixture of integer and float in a vector.} 32 | 33 | \item{train.loci}{The proportion (float between 0 and 1) of loci to be used as training data. Use a numeric vector to specify multiple sets of training loci. This argument will be ignored if you're analyzing non-genetic data.} 34 | 35 | \item{loci.sample}{Locus sampling method, "fst" or "random". If loci.sample="fst" (default) and train.loci=0.1, it means that top 10 percent of high Fst loci will be sampled as training loci. On the other hand, if loci.sample="random", then random 10 percent of loci will be sampled as training loci. This argument will be ignored if you're analyzing non-genetic data.} 36 | 37 | \item{iterations}{Resampling times (an integer) for each combination of training individuals and loci.} 38 | 39 | \item{dir}{A character string to specify the folder name for saving output files. A slash at the end must be included (e.g., dir="YourFolderName/"). Otherwise, the files will be saved under your working directory.} 40 | 41 | \item{scaled}{A logical variable (TRUE or FALSE) to specify whether to center (make mean of each feature to 0) and scale (make standard deviation of each feature to 1) the entire dataset before performing PCA and cross-validation. Default is FALSE. As genetic data has converted to numeric data between 0 and 1, to scale or not to scale the genetic data should not be critical. However, it is recommended to set scaled=TRUE when integrated data contains various scales of features.} 42 | 43 | \item{pca.method}{Either a character string ("mixed", "independent", or "original") or logical variable (TRUE or FALSE) to specify how to perform PCA on non-genetic data (PCA is always performed on genetic data). The character strings are used when analyzing integrated (genetic plus non-genetic) data. If using "mixed" (default), PCA is perfromed across the genetic and non-genetic data, resulting in each PC summarizing mixed variations of genetic and non-genetic data. If using "independent", PCA is independently performed on non-genetic data. Genetic PCs and non-genetic PCs are then used as new features. If using "original", original non-genetic data and genetic PCs are used as features. The logical variable is used when analyzing non-genetic data.If TRUE, it performs PCA on the training data and applys the loadings to the test data. Scores of training and test data will be used as new features.} 44 | 45 | \item{pca.PCs}{A criterion ("Kaiser-Guttman","broken-stick", or numeric) to retain number of PCs. By default, it uses Kaiser-Guttman criterion that any PC has the eigenvalue greater than 1 will be retained as the new variable/feature. Users can set an integer to specify the number of PCs to be retained.} 46 | 47 | \item{pca.loadings}{A logical variable (TRUE or FALSE) to determine whether to output the loadings of training data to text files. Default is FALSE. Just a heads-up, the output files could take some storage space, if set TRUE.} 48 | 49 | \item{model}{A character string to specify which classifier to use for creating predictive models. The current options include "lda", "svm", "naiveBayes", "tree", and "randomForest". Default is "svm"(support vector machine).} 50 | 51 | \item{svm.kernel}{A character string to specify which kernel to be used when using "svm" classifier. Default is "linear". Other options include "polynomial", "radial", and "sigmoid". Look up R pacakge e1071 for more details about SVM, or see a guidance at https://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf} 52 | 53 | \item{svm.cost}{A number to specify the cost for "svm" method.} 54 | 55 | \item{ntree}{A integer to specify how many trees to build when using "randomForest" method.} 56 | 57 | \item{multiprocess}{A logical variable to determine whether using multiprocess. Default is TRUE. If set FALSE, it will only use single core to run the program.} 58 | 59 | \item{processors}{The number of processors to be used for parallel running. By default, it uses N-1 processors in your computer.} 60 | 61 | \item{skipQ}{A logical variable to determine whether prompting interactive dialogue when analyzing non-genetic data. If set TRUE, default data type and original values of non-genetic data will be used.} 62 | 63 | \item{...}{Other arguments that could be potentially used for various models} 64 | } 65 | \value{ 66 | You don't need to specify a name for the returned object when using this function. It automatically outputs results in text files to your designated folder. 67 | } 68 | \description{ 69 | This function employs Monte-Carlo cross-validation for assignment tests. The results help evaluate if known data set has sufficient discriminatory power. It accepts genetic-only [object returned from read.genpop() or reducel.allele()], integrated [object returned from compile.data()], or non-genetic [R data frame with header] data as input, and outputs results to text files. Several built-in options are provided. See below for more details. 70 | } 71 | -------------------------------------------------------------------------------- /man/assign.X.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/assign.X.R 3 | \name{assign.X} 4 | \alias{assign.X} 5 | \title{Perform a population assignment test on unknown individuals using known data} 6 | \usage{ 7 | assign.X( 8 | x1, 9 | x2, 10 | dir = NULL, 11 | common = T, 12 | scaled = F, 13 | pca.method = "mixed", 14 | pca.PCs = "kaiser-guttman", 15 | pca.loadings = F, 16 | model = "svm", 17 | svm.kernel = "linear", 18 | svm.cost = 1, 19 | ntree = 50, 20 | mplot = T, 21 | skipQ = F, 22 | ... 23 | ) 24 | } 25 | \arguments{ 26 | \item{x1}{An input object containing data from known individuals for building predictive models. It could be a list object returned from the function read.genpop(), reduce.allele() or compile.data(). Or, it could be a data frame containing non-genetic data returned from read.csv() or read.table().} 27 | 28 | \item{x2}{An input object containing data from unknown individuals to be predicted. It could be a list object returned from read.genpop(), reduce.allele(), or compile.data(). Or, it could be a data frame containing non-genetic data returned from read.csv() or read.table(). The x1 and x2 should be the same type (both are either lists or data frames).} 29 | 30 | \item{dir}{A character string to specify the folder name for saving output files. A slash at the end must be included (e.g., dir="YourFolderName/"). Otherwise, the files will be saved under your working directory.} 31 | 32 | \item{common}{A logical variable (TRUE or FALSE) to specify whether exclusively using features, the name of which is in common, between known and unknown data sets. Default is TRUE. If it is FALSE, it will stop performing analysis when inconsistent feature names were found.} 33 | 34 | \item{scaled}{A logical variable (TRUE or FALSE) to specify whether to center (make mean of each feature to 0) and scale (make standard deviation of each feature to 1) the dataset before performing PCA and cross-validation. Default is FALSE. As genetic data has converted to numeric data between 0 and 1, to scale or not to scale the genetic data should not be critical. However, it is recommended to set scaled=TRUE when integrated data contains various scales of features.} 35 | 36 | \item{pca.method}{Either a character string ("mixed", "independent", or "original") or logical variable (TRUE or FALSE) to specify how to perform PCA on non-genetic data (PCA is always performed on genetic data). The character strings are used when analyzing integrated (genetic plus non-genetic) data. If using "mixed" (default), PCA is perfromed across the genetic and non-genetic data, resulting in each PC summarizing mixed variations of genetic and non-genetic data. If using "independent", PCA is independently performed on non-genetic data. Genetic PCs and non-genetic PCs are then used as new features. If using "original", original non-genetic data and genetic PCs are used as features. The logical variable is used when analyzing non-genetic data.If TRUE, it performs PCA on the training data and applys the loadings to the test data. Scores of training and test data will be used as new features.} 37 | 38 | \item{pca.PCs}{A criterion ("Kaiser-Guttman","broken-stick", or numeric) to retain number of PCs. By default, it uses Kaiser-Guttman criterion that any PC has the eigenvalue greater than 1 will be retained as the new variable/feature. Users can set an integer to specify the number of PCs to be retained.} 39 | 40 | \item{pca.loadings}{A logical variable (TRUE or FALSE) to determine whether to output the loadings of training data to text files. Default is FALSE. Just a heads-up, the output files could take some storage space, if set TRUE.} 41 | 42 | \item{model}{A character string to specify which classifier to use for creating predictive models. The current options include "lda", "svm", "naiveBayes", "tree", and "randomForest". Default is "svm"(support vector machine).} 43 | 44 | \item{svm.kernel}{A character string to specify which kernel to be used when using "svm" classifier. Default is "linear". Other options include "polynomial", "radial", and "sigmoid". Look up R pacakge e1071 for more details about SVM, or see a guidance at https://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf} 45 | 46 | \item{svm.cost}{A number to specify the cost for "svm" method.} 47 | 48 | \item{ntree}{A integer to specify how many trees to build when using "randomForest" method.} 49 | 50 | \item{mplot}{A logical variable (TRUE or FALSE) to specify whether making a membership probability plot right after the assignment test is done. Default is TRUE.} 51 | 52 | \item{skipQ}{A logical variable (TRUE or FALSE) to skip data type checking on non-genetic data. Default is FALSE and will prompt questions to confirm data type. If it is TRUE, it will skip the confirmation and use data type by default (integer and float will be numeric data).} 53 | 54 | \item{...}{Other arguments that could be potentially used for various models} 55 | } 56 | \value{ 57 | This function outputs assignment results and other analytical information in text files that will be saved under your designated folder. It also outputs a membership probability plot, if permitted. 58 | } 59 | \description{ 60 | This function assigns unknown individuals to possible source populations based on known individuals and genetic or non-genetic or integrated data. 61 | } 62 | -------------------------------------------------------------------------------- /man/assign.kfold.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/assign.kfold.R 3 | \name{assign.kfold} 4 | \alias{assign.kfold} 5 | \title{Population assignment test using K-fold cross-validation} 6 | \usage{ 7 | assign.kfold( 8 | x, 9 | k.fold = c(3, 4, 5), 10 | train.loci = c(0.1, 0.25, 0.5, 1), 11 | loci.sample = "fst", 12 | dir = NULL, 13 | scaled = FALSE, 14 | pca.method = "mixed", 15 | pca.PCs = "kaiser-guttman", 16 | pca.loadings = F, 17 | model = "svm", 18 | svm.kernel = "linear", 19 | svm.cost = 1, 20 | ntree = 50, 21 | processors = 999, 22 | multiprocess = TRUE, 23 | skipQ = FALSE, 24 | ... 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An input object which should be the object (list) returned from the function read.genpop(), reduce.allele(), or compile.data(). It could also be a data frame (with column name) returned from read.csv() or read.table() if you're analyzing non-genetic data, such as morphormetrics, chemistry data. The non-genetic data frame should have sample ID in the first column and population label in the last column.} 29 | 30 | \item{k.fold}{The number of groups to be divided for each population. Use a numeric vector to specify multiple sets of k-folds.} 31 | 32 | \item{train.loci}{The proportion (float between 0 and 1) of loci to be used as training data. Use a numeric vector to specify multiple sets of training loci. This argument will be ignored if you're analyzing non-genetic data.} 33 | 34 | \item{loci.sample}{Locus sampling method, "fst" or "random". If loci.sample="fst" (default) and train.loci=0.1, it means that top 10 percent of high Fst loci will be sampled as training loci. On the other hand, if loci.sample="random", then random 10 percent of loci will be sampled as training loci. This argument will be ignored if you're analyzing non-genetic data.} 35 | 36 | \item{dir}{A character string to specify the folder name for saving output files. A slash at the end must be included (e.g., dir="YourFolderName/"). Otherwise, the files will be saved under your working directory.} 37 | 38 | \item{scaled}{A logical variable (TRUE or FALSE) to specify whether to center (make mean of each feature to 0) and scale (make standard deviation of each feature to 1) the entire dataset before performing PCA and cross-validation. Default is FALSE. As genetic data has converted to numeric data between 0 and 1, to scale or not to scale the genetic data should not be critical. However, it is recommended to set scaled=TRUE when integrated data contains various scales of features.} 39 | 40 | \item{pca.method}{Either a character string ("mixed", "independent", or "original") or logical variable (TRUE or FALSE) to specify how to perform PCA on non-genetic data (PCA is always performed on genetic data). The character strings are used when analyzing integrated (genetic plus non-genetic) data. If using "mixed" (default), PCA is perfromed across the genetic and non-genetic data, resulting in each PC summarizing mixed variations of genetic and non-genetic data. If using "independent", PCA is independently performed on non-genetic data. Genetic PCs and non-genetic PCs are then used as new features. If using "original", original non-genetic data and genetic PCs are used as features. The logical variable is used when analyzing non-genetic data alone. If TRUE, it performs PCA on the training data and applys the loadings to the test data. Scores of training and test data will be used as new features.} 41 | 42 | \item{pca.PCs}{A criterion ("Kaiser-Guttman","broken-stick", or numeric) to retain number of PCs. By default, it uses Kaiser-Guttman criterion that any PC has the eigenvalue greater than 1 will be retained as the new variable/feature. Users can set an integer to specify the number of PCs to be retained.} 43 | 44 | \item{pca.loadings}{A logical variable (False or True) to determine whether it prints the loadings of training data to output text files. Default is False, if set True, the overall output files could be large.} 45 | 46 | \item{model}{A character string to specify which classifier to use for creating predictive models. The current options include "lda", "svm", "naiveBayes", "tree", and "randomForest".} 47 | 48 | \item{svm.kernel}{A character string to specify which kernel to be used when using "svm" classifier. Default is "linear". Other options include "polynomial", "radial", and "sigmoid". Look up R pacakge e1071 for more details about SVM, or see a guidance at https://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf} 49 | 50 | \item{svm.cost}{A number to specify the cost for "svm" method.} 51 | 52 | \item{ntree}{A integer to specify how many trees to build when using "randomForest" method.} 53 | 54 | \item{processors}{The number of processors to be used for parallel running. By default, it uses N-1 processors in your computer.} 55 | 56 | \item{multiprocess}{A logical variable to determine whether using multiprocess. Default is TRUE. If set FALSE, it will only use single core to run the program.} 57 | 58 | \item{skipQ}{A logical variable to determine whether prompting interactive dialogue when analyzing non-genetic data. If set TRUE, default data type and original values of non-genetic data will be used.} 59 | 60 | \item{...}{Other arguments that could be potentially used for various models} 61 | } 62 | \value{ 63 | You don't need to specify a name for the returned object when using this function. It automatically outputs results in text files to your designated folder. 64 | } 65 | \description{ 66 | This function employs K-fold cross-validation for assignment tests. The results help estimate membership probabilities of every individual. It accepts genetic-only [object returned from read.genpop() or reducel.allele()], integrated [object returned from compile.data()], or non-genetic [R data frame with header] data as input, and outputs results to text files. Several built-in options are provided. See below for more details. 67 | } 68 | -------------------------------------------------------------------------------- /man/assign.matrix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/assign.matrix.R 3 | \name{assign.matrix} 4 | \alias{assign.matrix} 5 | \title{Make an assignment maxtrix from cross-validation results} 6 | \usage{ 7 | assign.matrix( 8 | dir = NULL, 9 | train.loci = "all", 10 | train.inds = "all", 11 | k.fold = "all" 12 | ) 13 | } 14 | \arguments{ 15 | \item{dir}{A character string to specify the folder that has your cross-validation assignment results.} 16 | 17 | \item{train.loci}{Choose your proportions of training loci used in Monte-Carlo or K-fold cross-validation. Default is "all".} 18 | 19 | \item{train.inds}{Choose your numbers or proportions of training individuals used in Monte-Carlo cross-validation. Default is "all".} 20 | 21 | \item{k.fold}{Choose the k fold values used in K-fold cross-validation. Default is "all".} 22 | } 23 | \value{ 24 | The function returns a matrix in R console as well as a file named "assignment_matrix.txt" in the folder. 25 | } 26 | \description{ 27 | This function generates a pairwise assignment matrix with mean and variation of assignment accuracies estimated across all assignment tests. 28 | } 29 | -------------------------------------------------------------------------------- /man/check.loci.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/check.loci.R 3 | \name{check.loci} 4 | \alias{check.loci} 5 | \title{Check which loci frequently have high Fst across training sets} 6 | \usage{ 7 | check.loci(dir = NULL, top.loci = 20) 8 | } 9 | \arguments{ 10 | \item{dir}{A character string to specify the folder with your cross-validation results. A slash should be entered at the end.} 11 | 12 | \item{top.loci}{An integer to specify how many top informative loci to output.} 13 | } 14 | \value{ 15 | This function output the results in a text file. It includes the top N informative loci in N rows, and each row has a list of loci sorted by its occurrence. 16 | } 17 | \description{ 18 | This function reads through training locus file for each assignment test and counts the frequency of those loci and outputs the results in a text file. 19 | } 20 | -------------------------------------------------------------------------------- /man/compile.data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/compile.data.R 3 | \name{compile.data} 4 | \alias{compile.data} 5 | \title{Compile genetic and other non-genetic data} 6 | \usage{ 7 | compile.data(x, add.x, method = "common", skipQ = F) 8 | } 9 | \arguments{ 10 | \item{x}{A returned object (list) from the function read.genpop() or reduce.allele().} 11 | 12 | \item{add.x}{A file containing non-genetic data that has sample ID in the first column. The sample ID must be the same as your GENEPOP file.} 13 | 14 | \item{method}{A method to match sample ID between genetic and non-genetic data. The "common" method only concatenate the data that has sample ID in both files. If an individual only exists in one of the files, this individual will be discarded.} 15 | 16 | \item{skipQ}{A logical variable to determine whether prompting interactive dialogue. If set TRUE, input data type will be recognized as default type and not be verified by the user.} 17 | } 18 | \value{ 19 | This function returns a new object (list) that comprises 5 items. [[1]] data matrix including genetic and non-genetic data, [[2]] a sample ID vector, [[3]] a locus name vector, [[4]] a vector of non-genetic variable names, and [[5]] the number of non-genetic variables. 20 | } 21 | \description{ 22 | This function allows you to combine genetic and other non-genetic data, such as morphometrics, of the observations for assignment tests. 23 | } 24 | -------------------------------------------------------------------------------- /man/membership.plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/membership.plot.R 3 | \name{membership.plot} 4 | \alias{membership.plot} 5 | \title{Make a membership probability plot using results from K-fold cross-validation (ggplot2 style)} 6 | \usage{ 7 | membership.plot( 8 | dir = NULL, 9 | style = NULL, 10 | non.genetic = FALSE, 11 | plot.k = NULL, 12 | plot.loci = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{dir}{A character string to specify the folder that has your K-fold cross-validation assignment results. A slash should be entered at the end.} 17 | 18 | \item{style}{An option for output style. If style=1, it creates the plot which individuals on the x-axis are in random order. If style=2, individuals are sorted by probabilities within each population. If style=3, individuals of different folds are in seperate plots. If style=4, individuals are separated by fold and sorted by probability.} 19 | 20 | \item{non.genetic}{A logical variable to specify if data are non-genetic. Set it TRUE if you're analyzing non-genetic alone.} 21 | 22 | \item{plot.k}{A number to specify which K of the data set should be plotted. If not given, it will prompt the question.} 23 | 24 | \item{plot.loci}{The proportion of training loci used in your K-fold cross-validation analysis. Specify one of the numbers here to skip question prompt.} 25 | } 26 | \value{ 27 | This function returns a stacked-bar plot using the ggplot2 library. Users can modified the plot (e.g., change color, text, etc.) using functions provided by ggplot2 library. 28 | } 29 | \description{ 30 | This function allows you to make a membership probability plot (stacked-bar plot) using results estimated from K-fold cross-validation. 31 | } 32 | -------------------------------------------------------------------------------- /man/read.Genepop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read.Genepop.R 3 | \name{read.Genepop} 4 | \alias{read.Genepop} 5 | \title{Read GENEPOP format file} 6 | \usage{ 7 | read.Genepop(x, pop.names = NULL, haploid = FALSE, pos = 1) 8 | } 9 | \arguments{ 10 | \item{x}{GENEPOP file or path to the file. The filename extension (e.g., .txt) should be included.} 11 | 12 | \item{pop.names}{A character string vector for population names. The order of the name should be the same with the order (top to down) in your GENEPOP file.} 13 | 14 | \item{haploid}{A logical variable (TRUE or FALSE) to specify whether your dataset is haploid data. Default is FALSE.} 15 | 16 | \item{pos}{A parameter for program development use; users can ignore it.} 17 | } 18 | \value{ 19 | This function returns a list comprising three elements. 1. YOU_NAME_IT$DataMatrix: A matrix of genetic data with a population name label ($popNameVector) in the last column. 2. YOU_NAME_IT$SampleID: A vector of sample ID. 3. YOU_NAME_IT$LocusName: A vector of locus name. 20 | } 21 | \description{ 22 | This function allows you to import a GENEPOP format file into R. Population names can be specified in the argument. See http://genepop.curtin.edu.au/help_input.html for details about GENEPOP format. 23 | } 24 | \examples{ 25 | # infile <- read.Genepop("Your_Genepop_File.txt", pop.names=c("pop_A", "pop_B", "pop_C")) 26 | } 27 | \references{ 28 | Rousset, F. 2008. Genepop'007: a complete reimplementation of the Genepop software for Windows and Linux. Mol. Ecol. Resources 8: 103-106 29 | } 30 | -------------------------------------------------------------------------------- /man/read.Structure.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/read.Structure.R 3 | \name{read.Structure} 4 | \alias{read.Structure} 5 | \title{Read Structure format file} 6 | \usage{ 7 | read.Structure(x, ploidy = 2) 8 | } 9 | \arguments{ 10 | \item{x}{STRUCTURE file or path to the file. The filename extension (e.g., .txt) should be included.} 11 | 12 | \item{ploidy}{An integer of 1, 2, 3, or 4, to indicate haploid, diploid, triploid, or tetraploid data. Default is 2 (diploid).} 13 | } 14 | \value{ 15 | This function returns a list comprising three elements. 1. YOU_NAME_IT$DataMatrix: A matrix of genetic data with a population name label ($popNameVector) in the last column. 2. YOU_NAME_IT$SampleID: A vector of sample ID. 3. YOU_NAME_IT$LocusName: A vector of locus name. 16 | } 17 | \description{ 18 | This function allows you to import a STRUCTURE format file into R. The first row should be locus name (either with or withour column names for sample ID and population label); the first column should be sample ID; the second column should be population label; the rest are genotype. Use "-9" for missing alleles. 19 | } 20 | \examples{ 21 | # infile <- read.Structure("Your_Structure_File.txt") 22 | } 23 | \references{ 24 | Pritchard, J.K., Stephens, M. and Donnelly, P., 2000. Inference of population structure using multilocus genotype data. Genetics, 155(2), pp.945-959. 25 | } 26 | -------------------------------------------------------------------------------- /man/reduce.allele.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/reduce.allele.R 3 | \name{reduce.allele} 4 | \alias{reduce.allele} 5 | \title{Remove low variance alleles (dimensionality reduction)} 6 | \usage{ 7 | reduce.allele(x, p = 0.95) 8 | } 9 | \arguments{ 10 | \item{x}{A returned object (a list) from the function read.genpop().} 11 | 12 | \item{p}{A threshold of variance for the alleles to be removed. For example, if p = 0.95 (default setting), an allele occupied more than 95 percents across all the samples will be removed.} 13 | } 14 | \value{ 15 | This function return the same object as the function read.genpop() except that the number of columns in the matrix [[1]] is reduced and so is the locus name [[3]]. 16 | } 17 | \description{ 18 | This function helps remove alleles that have low variance in the data set such that it can speed up further analyses for a large data set (e.g., > 10K SNPs). 19 | } 20 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(assignPOP) 3 | 4 | test_check("assignPOP") 5 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/AnalysisInfo.txt: -------------------------------------------------------------------------------- 1 | Analysis Description ( R - assignPOP ver. 1.1.2 ) 2 | Perform assign.kfold() @ 2017-06-07 16:07:06 3 | 4 | k.fold = 3 5 | train.loci = 1 6 | Total assignment tests = 3 7 | Fst locus sample method: random 8 | Data scaled and centerd: FALSE 9 | PC retaining criteria: kaiser-guttman 10 | PCA for non-genetic data: mixed 11 | Machine learning model: svm 12 | 13 | Input Data ( genetics ) 14 | Number of individuals: 24 15 | Number of loci: 4 16 | Number of non-genetic variables: 0 17 | Number of populations: 3 18 | pop.1 pop.2 pop.3 19 | 8 10 6 20 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Loci_1_K3_1.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Loci_1_K3_2.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Loci_1_K3_3.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Out_1_K3_1.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA9 pop.1 pop.1 0.399851339392022 0.313535069466902 0.286613591141075 3 | A10 pop.1 pop.3 0.326290094975471 0.309687882685856 0.364022022338673 4 | A12 pop.1 pop.3 0.2596649898228 0.304858213530777 0.435476796646423 5 | AF4 pop.2 pop.1 0.382773809644966 0.311236085544211 0.305990104810822 6 | AF5 pop.2 pop.1 0.439867835988121 0.3133791006948 0.246753063317079 7 | AF10 pop.2 pop.1 0.485868686620624 0.316325368832722 0.197805944546654 8 | C451 pop.3 pop.1 0.485868686620624 0.316325368832722 0.197805944546654 9 | C456 pop.3 pop.1 0.395041233738674 0.312875782364087 0.29208298389724 10 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Out_1_K3_2.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA8 pop.1 pop.3 0.258770178941063 0.267230713453834 0.473999107605103 3 | A11 pop.1 pop.3 0.301355059290659 0.258742183230728 0.439902757478614 4 | A15 pop.1 pop.3 0.352883079316936 0.248708791081661 0.398408129601403 5 | AF1 pop.2 pop.3 0.201077232374683 0.279319095840962 0.519603671784355 6 | AF2 pop.2 pop.3 0.367674765794403 0.245841901251224 0.386483332954373 7 | AF3 pop.2 pop.1 0.409314068175894 0.237725131326161 0.352960800497945 8 | AF9 pop.2 pop.3 0.300586153322997 0.258893307488357 0.440520539188646 9 | C452 pop.3 pop.1 0.50604412960481 0.217964444417606 0.275991425977585 10 | C453 pop.3 pop.1 0.467492762287111 0.226064445464586 0.306442792248303 11 | -------------------------------------------------------------------------------- /tests/testthat/ResKFtest/Out_1_K3_3.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | A13 pop.1 pop.1 0.355021678595518 0.352638937314 0.292339384090482 3 | A14 pop.1 pop.1 0.443415338730705 0.360972538527257 0.195612122742038 4 | AF6 pop.2 pop.2 0.338889092712518 0.355834064109745 0.305276843177738 5 | AF7 pop.2 pop.1 0.446253095988667 0.362678776090548 0.191068127920785 6 | AF8 pop.2 pop.1 0.455874328396553 0.371142041492085 0.172983630111362 7 | C454 pop.3 pop.1 0.383217511338703 0.349782177855951 0.267000310805346 8 | C455 pop.3 pop.2 0.327086322586557 0.358836800747551 0.314076876665893 9 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/AnalysisInfo.txt: -------------------------------------------------------------------------------- 1 | Analysis Description ( R - assignPOP ver. 1.1.2 ) 2 | Perform assign.MC() @ 2017-06-07 16:05:05 3 | 4 | train.inds = 0.5 5 | train.loci = 1 6 | iterations = 3 7 | Total assignment tests = 3 8 | Fst locus sample method: random 9 | Data scaled and centerd: FALSE 10 | PC retaining criteria: kaiser-guttman 11 | PCA for non-genetic data: NA 12 | Machine learning model: svm 13 | 14 | Input Data ( genetics ) 15 | Number of individuals: 24 16 | Number of loci: 4 17 | Number of non-genetic variables: 0 18 | Number of populations: 3 19 | pop.1 pop.2 pop.3 20 | 8 10 6 21 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Loci_0.5_1_1.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Loci_0.5_1_2.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Loci_0.5_1_3.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Out_0.5_1_1.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA8 pop.1 pop.2 0.252839050654772 0.384928834424123 0.362232114921105 3 | A10 pop.1 pop.2 0.270500065006027 0.375844784109529 0.353655150884444 4 | A13 pop.1 pop.1 0.339897913367486 0.336570270344843 0.323531816287671 5 | A14 pop.1 pop.2 0.248530309214807 0.388136804893119 0.363332885892074 6 | AF1 pop.2 pop.2 0.196174234422259 0.416863857528113 0.386961908049628 7 | AF3 pop.2 pop.1 0.358475013605599 0.32548714042865 0.316037845965751 8 | AF4 pop.2 pop.2 0.290428407310527 0.36504090188855 0.344530690800923 9 | AF8 pop.2 pop.2 0.244905471807553 0.390166050947755 0.364928477244692 10 | AF9 pop.2 pop.2 0.272608125682993 0.374726829725794 0.352665044591213 11 | C451 pop.3 pop.1 0.42102995490744 0.288886687446128 0.290083357646432 12 | C452 pop.3 pop.1 0.418458404772686 0.290373568532821 0.291168026694493 13 | C453 pop.3 pop.1 0.421439892476381 0.288649703629076 0.289910403894543 14 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Out_0.5_1_2.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA8 pop.1 pop.2 0.29846826704144 0.480114829118736 0.221416903839823 3 | A12 pop.1 pop.2 0.294489507077973 0.525421634201004 0.180088858721023 4 | A13 pop.1 pop.2 0.315462731453075 0.357386853909094 0.327150414637831 5 | A14 pop.1 pop.2 0.298623642875066 0.495943754173495 0.205432602951438 6 | AF3 pop.2 pop.3 0.330159757474244 0.311502071295425 0.358338171230331 7 | AF4 pop.2 pop.2 0.300862453471829 0.447999617988892 0.251137928539278 8 | AF6 pop.2 pop.3 0.364037149103826 0.252936480144069 0.383026370752104 9 | AF7 pop.2 pop.2 0.299128271246855 0.471005241687641 0.229866487065504 10 | AF10 pop.2 pop.1 0.425073687186777 0.199886030292383 0.37504028252084 11 | C452 pop.3 pop.3 0.374577474724787 0.240413523294758 0.385009001980455 12 | C454 pop.3 pop.2 0.308071075927674 0.397833885475471 0.294095038596854 13 | C456 pop.3 pop.2 0.306513696889194 0.410307621049504 0.283178682061302 14 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtest/Out_0.5_1_3.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | A10 pop.1 pop.1 0.469721137761267 0.276184721503393 0.25409414073534 3 | A11 pop.1 pop.1 0.474823091551244 0.276951279030727 0.248225629418029 4 | A13 pop.1 pop.1 0.413655656939362 0.274640332751939 0.3117040103087 5 | A15 pop.1 pop.1 0.448492078646093 0.274266908243373 0.277241013110533 6 | AF2 pop.2 pop.1 0.415848937078823 0.274505632763433 0.309645430157743 7 | AF6 pop.2 pop.1 0.417192122844175 0.274430078169742 0.308377798986082 8 | AF8 pop.2 pop.1 0.483140743367608 0.276611925116438 0.240247331515954 9 | AF9 pop.2 pop.1 0.45472562083122 0.274637394453959 0.270636984714821 10 | AF10 pop.2 pop.1 0.382227229562144 0.278045079504744 0.339727690933112 11 | C451 pop.3 pop.1 0.382227229562144 0.278045079504744 0.339727690933112 12 | C453 pop.3 pop.1 0.38317596379447 0.277903348369028 0.338920687836502 13 | C454 pop.3 pop.1 0.428870264375724 0.274001089576795 0.297128646047481 14 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/AnalysisInfo.txt: -------------------------------------------------------------------------------- 1 | Analysis Description ( R - assignPOP ver. 1.1.2 ) 2 | Perform assign.MC() @ 2017-06-07 16:05:05 3 | 4 | train.inds = 0.5 5 | train.loci = 1 6 | iterations = 3 7 | Total assignment tests = 3 8 | Fst locus sample method: fst 9 | Data scaled and centerd: FALSE 10 | PC retaining criteria: kaiser-guttman 11 | PCA for non-genetic data: NA 12 | Machine learning model: svm 13 | 14 | Input Data ( genetics ) 15 | Number of individuals: 24 16 | Number of loci: 4 17 | Number of non-genetic variables: 0 18 | Number of populations: 3 19 | pop.1 pop.2 pop.3 20 | 8 10 6 21 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Loci_0.5_1_1.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Loci_0.5_1_2.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Loci_0.5_1_3.txt: -------------------------------------------------------------------------------- 1 | Loc1 2 | Loc2 3 | Loc3 4 | Loc4 5 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Out_0.5_1_1.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA8 pop.1 pop.2 0.252839050654772 0.384928834424123 0.362232114921105 3 | A10 pop.1 pop.2 0.270500065006027 0.375844784109529 0.353655150884444 4 | A13 pop.1 pop.1 0.339897913367486 0.336570270344843 0.323531816287671 5 | A14 pop.1 pop.2 0.248530309214807 0.388136804893119 0.363332885892074 6 | AF1 pop.2 pop.2 0.196174234422259 0.416863857528113 0.386961908049628 7 | AF3 pop.2 pop.1 0.358475013605599 0.32548714042865 0.316037845965751 8 | AF4 pop.2 pop.2 0.290428407310527 0.36504090188855 0.344530690800923 9 | AF8 pop.2 pop.2 0.244905471807553 0.390166050947755 0.364928477244692 10 | AF9 pop.2 pop.2 0.272608125682993 0.374726829725794 0.352665044591213 11 | C451 pop.3 pop.1 0.42102995490744 0.288886687446128 0.290083357646432 12 | C452 pop.3 pop.1 0.418458404772686 0.290373568532821 0.291168026694493 13 | C453 pop.3 pop.1 0.421439892476381 0.288649703629076 0.289910403894543 14 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Out_0.5_1_2.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | AA8 pop.1 pop.2 0.29846826704144 0.480114829118736 0.221416903839823 3 | A12 pop.1 pop.2 0.294489507077973 0.525421634201004 0.180088858721023 4 | A13 pop.1 pop.2 0.315462731453075 0.357386853909094 0.327150414637831 5 | A14 pop.1 pop.2 0.298623642875066 0.495943754173495 0.205432602951438 6 | AF3 pop.2 pop.3 0.330159757474244 0.311502071295425 0.358338171230331 7 | AF4 pop.2 pop.2 0.300862453471829 0.447999617988892 0.251137928539278 8 | AF6 pop.2 pop.3 0.364037149103826 0.252936480144069 0.383026370752104 9 | AF7 pop.2 pop.2 0.299128271246855 0.471005241687641 0.229866487065504 10 | AF10 pop.2 pop.1 0.425073687186777 0.199886030292383 0.37504028252084 11 | C452 pop.3 pop.3 0.374577474724787 0.240413523294758 0.385009001980455 12 | C454 pop.3 pop.2 0.308071075927674 0.397833885475471 0.294095038596854 13 | C456 pop.3 pop.2 0.306513696889194 0.410307621049504 0.283178682061302 14 | -------------------------------------------------------------------------------- /tests/testthat/ResMCtestFst/Out_0.5_1_3.txt: -------------------------------------------------------------------------------- 1 | Ind.ID origin.pop pred.pop pop.1 pop.2 pop.3 2 | A10 pop.1 pop.1 0.469721137761267 0.276184721503393 0.25409414073534 3 | A11 pop.1 pop.1 0.474823091551244 0.276951279030727 0.248225629418029 4 | A13 pop.1 pop.1 0.413655656939362 0.274640332751939 0.3117040103087 5 | A15 pop.1 pop.1 0.448492078646093 0.274266908243373 0.277241013110533 6 | AF2 pop.2 pop.1 0.415848937078823 0.274505632763433 0.309645430157743 7 | AF6 pop.2 pop.1 0.417192122844175 0.274430078169742 0.308377798986082 8 | AF8 pop.2 pop.1 0.483140743367608 0.276611925116438 0.240247331515954 9 | AF9 pop.2 pop.1 0.45472562083122 0.274637394453959 0.270636984714821 10 | AF10 pop.2 pop.1 0.382227229562144 0.278045079504744 0.339727690933112 11 | C451 pop.3 pop.1 0.382227229562144 0.278045079504744 0.339727690933112 12 | C453 pop.3 pop.1 0.38317596379447 0.277903348369028 0.338920687836502 13 | C454 pop.3 pop.1 0.428870264375724 0.274001089576795 0.297128646047481 14 | -------------------------------------------------------------------------------- /tests/testthat/testData/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /tests/testthat/testData/GenepopEx1.txt: -------------------------------------------------------------------------------- 1 | Microsat on Chiracus radioactivus, a pest species 2 | Loc1, Loc2, Loc3, Y-linked, Loc4 3 | POP 4 | AA8, 0405 0711 0304 0000 0505 5 | AA9, 0405 0609 0208 0000 0505 6 | A10, 0205 0609 0101 0000 0305 7 | A11, 0405 0606 0102 0000 0504 8 | A12, 0202 0609 0105 0000 0507 9 | A13, 0505 0909 0107 0000 0505 10 | A14, 0202 0609 0207 0000 0503 11 | A15, 0405 0609 0101 0000 0505 12 | Pop 13 | AF1, 0000 0000 0000 0000 0505 14 | AF2, 0205 0307 0102 0000 0505 15 | AF3, 0202 0609 0202 0000 0505 16 | AF4, 0205 0909 0000 0000 0505 17 | AF5, 0205 0307 0202 0000 0505 18 | AF6, 0505 0303 0102 0000 0505 19 | AF7, 0205 0700 0000 0000 0505 20 | AF8, 0505 0900 0000 0000 0405 21 | AF9, 0205 0600 0000 0000 0505 22 | AF10, 0505 0606 0202 0000 0505 23 | pop 24 | C451, 0505 0606 0202 0000 0505 25 | C452, 0505 0909 0202 0000 0505 26 | C453, 0505 0306 0202 0000 0505 27 | C454, 0505 0909 0102 0000 0405 28 | C455, 0205 0303 0202 0000 0505 29 | C456, 0205 0909 0202 0000 0405 30 | -------------------------------------------------------------------------------- /tests/testthat/testData/GenepopEx2.txt: -------------------------------------------------------------------------------- 1 | Title line: "Mosquito populations in southern France" 2 | MicroSat 1 3 | Microsat 2 4 | Est-3 5 | Pop 6 | RueDuQuai , 250230 564568 110100 7 | RueDuQuai , 252238 568558 100120 8 | RueDuQuai , 254230 564558 090100 9 | RueDuQuai , 250230 564568 110100 10 | RueDuQuai , 252240 568558 100120 11 | RueDuQuai , 254230 564558 090090 12 | Pop 13 | Benitier , 254230 564558 080100 14 | Benitier , 000230 564558 090080 15 | Benitier , 254230 000000 090100 16 | Benitier , 254230 564000 090120 17 | -------------------------------------------------------------------------------- /tests/testthat/testData/GenepopUnk.txt: -------------------------------------------------------------------------------- 1 | Microsat on Chiracus radioactivus, a pest species 2 | Loc1, Loc2, Loc3, Y-linked, Loc4 3 | POP 4 | AA8, 0405 0711 0304 0000 0505 5 | AA9, 0405 0609 0208 0000 0505 6 | A10, 0205 0609 0101 0000 0305 7 | A11, 0405 0606 0102 0000 0504 8 | A12, 0202 0609 0105 0000 0507 9 | A13, 0505 0909 0107 0000 0505 10 | A14, 0202 0609 0207 0000 0503 11 | A15, 0405 0609 0101 0000 0505 12 | AF1, 0000 0000 0000 0000 0505 13 | AF2, 0205 0307 0102 0000 0505 14 | AF3, 0202 0609 0202 0000 0505 15 | AF4, 0205 0909 0000 0000 0505 16 | AF5, 0205 0307 0202 0000 0505 17 | AF6, 0505 0303 0102 0000 0505 18 | AF7, 0205 0700 0000 0000 0505 19 | AF8, 0505 0900 0000 0000 0405 20 | AF9, 0205 0600 0000 0000 0505 21 | AF10, 0505 0606 0202 0000 0505 22 | C451, 0505 0606 0202 0000 0505 23 | C452, 0505 0909 0202 0000 0505 24 | C453, 0505 0306 0202 0000 0505 25 | C454, 0505 0909 0102 0000 0405 26 | C455, 0205 0303 0202 0000 0505 27 | C456, 0205 0909 0202 0000 0405 28 | -------------------------------------------------------------------------------- /tests/testthat/testData/StructureEx1.txt: -------------------------------------------------------------------------------- 1 | loc_a loc_b loc_c loc_d loc_e 2 | George 1 -9 145 66 0 92 3 | George 1 -9 -9 64 0 94 4 | Paula 1 106 142 68 1 92 5 | Paula 1 106 148 64 0 94 6 | Matthew 2 110 145 -9 0 92 7 | Matthew 2 110 148 66 1 -9 8 | Bob 2 108 142 64 1 94 9 | Bob 2 -9 142 -9 0 94 10 | Anja 1 112 142 -9 1 -9 11 | Anja 1 114 142 66 1 94 12 | Peter 1 -9 145 66 0 -9 13 | Peter 1 110 145 -9 1 -9 14 | Carsten 2 108 145 62 0 -9 15 | Carsten 2 110 145 64 1 92 16 | -------------------------------------------------------------------------------- /tests/testthat/testData/StructureEx2.txt: -------------------------------------------------------------------------------- 1 | id pop loc_a loc_b loc_c loc_d loc_e 2 | George 1 -9 145 66 0 92 3 | George 1 -9 -9 64 0 94 4 | Paula 1 106 142 68 1 92 5 | Paula 1 106 148 64 0 94 6 | Matthew 2 110 145 -9 0 92 7 | Matthew 2 110 148 66 1 -9 8 | Bob 2 108 142 64 1 94 9 | Bob 2 -9 142 -9 0 94 10 | Anja 1 112 142 -9 1 -9 11 | Anja 1 114 142 66 1 94 12 | Peter 1 -9 145 66 0 -9 13 | Peter 1 110 145 -9 1 -9 14 | Carsten 2 108 145 62 0 -9 15 | Carsten 2 110 145 64 1 92 16 | -------------------------------------------------------------------------------- /tests/testthat/testData/varDummy1.csv: -------------------------------------------------------------------------------- 1 | Id, ng1,ng2,ng3,ng4 2 | AA8,42,76,45,87 3 | AA9,44,73,56,88 4 | A10,53,71,48,85 5 | A11,45,66,46,69 6 | A12,46,78,49,87 7 | A13,54,66,49,76 8 | A14,47,83,56,81 9 | A15,39,79,50,81 10 | AF1,42,69,54,83 11 | AF2,43,64,48,80 12 | AF3,51,66,51,67 13 | AF4,47,72,48,86 14 | AF5,47,69,50,83 15 | AF6,46,68,46,78 16 | AF7,42,64,43,81 17 | AF8,54,70,60,81 18 | AF9,47,65,53,82 19 | AF10,35,60,44,93 20 | C451,49,68,47,78 21 | C452,43,76,44,81 22 | C453,40,67,61,89 23 | C454,44,74,57,88 24 | C455,40,60,49,89 25 | C456,41,70,53,75 -------------------------------------------------------------------------------- /tests/testthat/testData/varDummy2.csv: -------------------------------------------------------------------------------- 1 | Id, ng1,ng2,ng3,ng4 2 | George,42,76,45,87 3 | Paula,44,73,56,88 4 | Matthew,53,71,48,85 5 | Bob,45,66,46,69 6 | Anja,46,78,49,87 7 | Peter,54,66,49,76 8 | Carsten,47,83,56,81 -------------------------------------------------------------------------------- /tests/testthat/test_accuracy.R: -------------------------------------------------------------------------------- 1 | context("Test accuracy.xxx") 2 | 3 | test_that("Calculate assignment accuracy for Monte-Carlo results",{ 4 | AccuMC <- accuracy.MC(dir="ResMCtest/") 5 | expect_output(str(AccuMC),"data.frame") 6 | expect_true(file.exists("ResMCtest/Rate_of_3_tests_3_pops.txt")) 7 | plot <- accuracy.plot(AccuMC) 8 | expect_s3_class(plot, "ggplot") 9 | }) 10 | 11 | unlink("ResMCtest/Rate_of_3_tests_3_pops.txt") 12 | 13 | test_that("Calculate assignment accuracy for K-fold results",{ 14 | AccuKF <- accuracy.kfold(dir="ResKFtest/") 15 | expect_output(str(AccuKF),"data.frame") 16 | expect_true(file.exists("ResKFtest/Rate_of_3_tests_3_pops.txt")) 17 | plot <- accuracy.plot(AccuKF) 18 | expect_s3_class(plot, "ggplot") 19 | }) 20 | 21 | unlink("ResKFtest/Rate_of_3_tests_3_pops.txt") 22 | 23 | -------------------------------------------------------------------------------- /tests/testthat/test_assignMC.R: -------------------------------------------------------------------------------- 1 | context("Test assign.MC") 2 | 3 | genin <- read.Genepop("testData/GenepopEx1.txt") 4 | 5 | test_that("Perfrom Monte-Calro cross-validation - loci.sample=random",{ 6 | assign.MC(genin, dir="ResMC/", train.inds=0.5, train.loci=1, loci.sample="random", iterations=3, multiprocess=F) 7 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 8 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 9 | }) 10 | unlink("ResMC", re = T) 11 | 12 | test_that("Perfrom Monte-Calro cross-validation - loci.sample=Fst",{ 13 | assign.MC(genin, dir="ResMC/", train.inds=0.5, train.loci=1, loci.sample="Fst", iterations=3, multiprocess=F) 14 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 15 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 16 | }) 17 | unlink("ResMC", re = T) 18 | 19 | test_that("Perfrom Monte-Calro cross-validation - model=lda",{ 20 | assign.MC(genin, dir="ResMC/", model="lda", train.inds=3, train.loci=c(0.5,1), loci.sample="random", iterations=3, multiprocess=F) 21 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 22 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 23 | }) 24 | unlink("ResMC", re = T) 25 | 26 | test_that("Perfrom Monte-Calro cross-validation - model=naiveBayes",{ 27 | assign.MC(genin, dir="ResMC/", model="naiveBayes", train.inds=c(0.5,0.6), train.loci=c(0.5,1), loci.sample="random", iterations=3, multiprocess=F) 28 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 29 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 30 | }) 31 | unlink("ResMC", re = T) 32 | 33 | test_that("Perfrom Monte-Calro cross-validation - model=tree",{ 34 | assign.MC(genin, dir="ResMC/", model="tree", train.inds=c(3,4), train.loci=c(0.5,1), loci.sample="random", iterations=3, multiprocess=F) 35 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 36 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 37 | }) 38 | unlink("ResMC", re = T) 39 | 40 | test_that("Perfrom Monte-Calro cross-validation - model=randomForest",{ 41 | assign.MC(genin, dir="ResMC/", model="randomForest", train.inds=0.5, train.loci=1, loci.sample="random", iterations=3, multiprocess=F) 42 | expect_true(file.exists("ResMC/AnalysisInfo.txt")) 43 | expect_equal(length(readLines("ResMC/AnalysisInfo.txt")), 20) 44 | }) 45 | unlink("ResMC", re = T) 46 | -------------------------------------------------------------------------------- /tests/testthat/test_assignX.R: -------------------------------------------------------------------------------- 1 | context("Test assign.X") 2 | 3 | genin <- read.Genepop("testData/GenepopEx1.txt") 4 | genin_unknown <- read.Genepop("testData/GenepopUnk.txt") 5 | 6 | test_that("Perform one-time assignment test on unknown individuals",{ 7 | assign.X(x1=genin, x2=genin_unknown, dir="AssignRes/", mplot=T) 8 | expect_true(file.exists("AssignRes/AnalysisInfo.txt")) 9 | expect_true(file.exists("AssignRes/AssignmentResult.txt")) 10 | }) 11 | unlink("AssignRes", re = T) -------------------------------------------------------------------------------- /tests/testthat/test_assignkfold.R: -------------------------------------------------------------------------------- 1 | context("Test assign.kfold") 2 | 3 | genin <- read.Genepop("testData/GenepopEx1.txt") 4 | 5 | test_that("Perform K-fold cross-validation, genetic data",{ 6 | assign.kfold(genin, k.fold=3, train.loci=1, loci.sample="random", dir="ResKF/", multiprocess=F, skipQ=T) 7 | expect_true(file.exists("ResKF/AnalysisInfo.txt")) 8 | expect_equal(length(readLines("ResKF/AnalysisInfo.txt")), 19) 9 | }) 10 | unlink("ResKF", re = T) 11 | 12 | comin <- compile.data(genin, "testData/varDummy1.csv", skipQ = T) 13 | 14 | test_that("Perform K-fold cross-validation, integrated data",{ 15 | assign.kfold(comin, k.fold=3, train.loci=1, loci.sample="random", dir="ResKF/", multiprocess=F, skipQ=T) 16 | expect_true(file.exists("ResKF/AnalysisInfo.txt")) 17 | expect_equal(length(readLines("ResKF/AnalysisInfo.txt")), 19) 18 | }) 19 | unlink("ResKF", re = T) 20 | 21 | nongen <- read.csv("testData/varDummy1.csv", header=T, stringsAsFactors=T) 22 | pop_label <- c(rep("A",8), rep("B",10), rep("C",6)) 23 | nongen <- cbind(nongen, pop_label) 24 | 25 | test_that("Perform K-fold cross-validation, non-genetic data",{ 26 | assign.kfold(nongen, k.fold=3, dir="ResKF/", multiprocess=F, skipQ=T) 27 | expect_true(file.exists("ResKF/AnalysisInfo.txt")) 28 | expect_equal(length(readLines("ResKF/AnalysisInfo.txt")), 18) 29 | }) 30 | unlink("ResKF", re = T) 31 | -------------------------------------------------------------------------------- /tests/testthat/test_assignmatrix.R: -------------------------------------------------------------------------------- 1 | context("Test assign.matrix") 2 | 3 | test_that("run assign.matrix",{ 4 | expect_output(str(assign.matrix(dir = "ResMCtest/")),"xtabs") 5 | }) -------------------------------------------------------------------------------- /tests/testthat/test_checkloci.R: -------------------------------------------------------------------------------- 1 | context("Test check.loci") 2 | 3 | test_that("check loci from results generated by loci.sample=random",{ 4 | expect_message(check.loci(dir="ResMCtest/"), "") 5 | }) 6 | 7 | test_that("check loci from results generated by loci.sample=fst",{ 8 | check.loci(dir="ResMCtestFst/") 9 | expect_true(file.exists("ResMCtestFst/High_Fst_Locus_Freq.txt")) 10 | }) 11 | unlink("ResMCtestFst/High_Fst_Locus_Freq.txt") -------------------------------------------------------------------------------- /tests/testthat/test_compiledata.R: -------------------------------------------------------------------------------- 1 | context("Test compile.data") 2 | 3 | test_that("Compile genetic and non-genetic data",{ 4 | expect_output(str(compile.data(read.Genepop("testData/GenepopEx1.txt"), "testData/varDummy1.csv", skipQ = T)),"List of 5") 5 | expect_output(str(compile.data(read.Structure("testData/StructureEx1.txt"), "testData/varDummy2.csv", skipQ = T)),"List of 5") 6 | }) -------------------------------------------------------------------------------- /tests/testthat/test_membership.R: -------------------------------------------------------------------------------- 1 | context("Test membership.prob.plot") 2 | 3 | test_that("Plot membership probability",{ 4 | plot <- membership.plot(dir="ResKFtest/", style=1, non.genetic=T) 5 | expect_s3_class(plot, "ggplot") 6 | }) -------------------------------------------------------------------------------- /tests/testthat/test_readGenepopStructure.R: -------------------------------------------------------------------------------- 1 | context("Test read.Genepop and read.Structure") 2 | 3 | test_that("read.Genepop imports Genepop file as a list", { 4 | expect_output(str(read.Genepop("testData/GenepopEx1.txt")), "List of 3") 5 | expect_output(str(read.Genepop("testData/GenepopEx1.txt", pop.names = c("A","B","C"))), "List of 3") 6 | expect_output(str(read.Genepop("testData/GenepopEx1.txt", pop.names = c("A","B","C"), haploid = T)), "List of 3") 7 | expect_output(str(read.Genepop("testData/GenepopEx2.txt")), "List of 3") 8 | }) 9 | 10 | test_that("read.Structure imports Structure file as a list",{ 11 | expect_output(str(read.Structure("testData/StructureEx1.txt")),"List of 3") 12 | expect_output(str(read.Structure("testData/StructureEx1.txt", ploidy = 2)),"List of 3") 13 | expect_output(str(read.Structure("testData/StructureEx2.txt")),"List of 3") 14 | expect_output(str(read.Structure("testData/StructureEx2.txt", ploidy = 2)),"List of 3") 15 | }) -------------------------------------------------------------------------------- /tests/testthat/test_reduceallele.R: -------------------------------------------------------------------------------- 1 | context("Test reduce.allele") 2 | 3 | test_that("reduce.allele() removes loci with minor alleles",{ 4 | #data <- read.Genepop("GenepopEx1.txt") 5 | expect_output(str(reduce.allele(read.Genepop("testData/GenepopEx1.txt"))),"List of 3") 6 | #data <- read.Structure("StructureEx1.txt") 7 | expect_output(str(reduce.allele(read.Structure("testData/StructureEx1.txt"), p = 0.8)),"List of 3") 8 | }) --------------------------------------------------------------------------------