| 1 | # GenPerl module | ||
| 2 | # | ||
| 3 | |||
| 4 | # POD documentation - main docs before the code | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Genetics::API::Analysis::Linkage | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | # The following code runs a full Genehunter genome scan using two different | ||
| 13 | # disease models: | ||
| 14 | use Genetics::API ; | ||
| 15 | |||
| 16 | $api = new Genetics::API(DSN => {driver => "mysql", | ||
| 17 | host => $Host, | ||
| 18 | database => $Database}, | ||
| 19 | user => $UserName, | ||
| 20 | password => $Password) ; | ||
| 21 | |||
| 22 | # Kindreds | ||
| 23 | $kindredCluster = $api->getObject({TYPE => "Cluster", | ||
| 24 | NAME => "Bpall two generations"}) ; | ||
| 25 | @kindreds = $api->getClusterContents($kindredCluster, 1) ; | ||
| 26 | # Allele FrequencySource: | ||
| 27 | $bpall = $api->getObject({TYPE => "Cluster", NAME => "Bpall"}) ; | ||
| 28 | # StudyVariables: | ||
| 29 | $bp = $api->getObject({TYPE => "StudyVariable", NAME => "BP"}) ; | ||
| 30 | $bpup = $api->getObject({TYPE => "StudyVariable", NAME => "BPUP"}) ; | ||
| 31 | @studyVars = ($bp, $bpup) ; | ||
| 32 | $lcd = $api->getObject({TYPE => "StudyVariable", NAME => "AoO Dom LC"}) ; | ||
| 33 | |||
| 34 | @chromosomes = qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 XY) ; | ||
| 35 | foreach $chr (@chromosomes) { | ||
| 36 | $markerCluster = $api->getObject({TYPE => "Cluster", | ||
| 37 | NAME => "Chr$chr Markers"}) ; | ||
| 38 | @markers = $api->getClusterContents($markerCluster, 1) ; | ||
| 39 | $map = $api->getObject({TYPE => "Map", | ||
| 40 | NAME => "Marshfield Chr$chr Map"}) ; | ||
| 41 | |||
| 42 | foreach $sv (@studyVars) { | ||
| 43 | $svName = $sv->name() ; | ||
| 44 | $runName = $chr . $svName ; | ||
| 45 | $api->runGenehunter( | ||
| 46 | KINDREDS => \@kindreds, | ||
| 47 | MARKERS => \@markers, | ||
| 48 | MAP => $map, | ||
| 49 | ALLELETYPE => "Size", | ||
| 50 | AFS => $bpall, | ||
| 51 | TRAIT => $sv, | ||
| 52 | LC => $lcd, | ||
| 53 | SETUPFILENAME => "$runName.setup", | ||
| 54 | PHOTOFILENAME => "$runName.out", | ||
| 55 | DATFILENAME => "$runName.dat", | ||
| 56 | PEDFILENAME => "$runName.pre", | ||
| 57 | ANALYSIS => "BOTH", | ||
| 58 | SINGLEPOINT => "off", | ||
| 59 | OFFEND => "5.0", | ||
| 60 | INCREMENT => "step 3", | ||
| 61 | MAXBITS => 18, | ||
| 62 | ) ; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | This package contains methods relating to the use of data contained in GenPerl | ||
| 69 | objects in genetic linkage analyses. Generally speaking, this means reading | ||
| 70 | and writing linkage format pedigree and locus files, and/or running programs | ||
| 71 | such as genehunter, etc. | ||
| 72 | |||
| 73 | =head1 LICENSE | ||
| 74 | |||
| 75 | This program is free software; you can redistribute it and/or modify | ||
| 76 | it under the terms of the GNU General Public License as published by | ||
| 77 | the Free Software Foundation; either version 2 of the License, or | ||
| 78 | (at your option) any later version. | ||
| 79 | |||
| 80 | This program is distributed in the hope that it will be useful, | ||
| 81 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 82 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 83 | GNU General Public License for more details. | ||
| 84 | |||
| 85 | You should have received a copy of the GNU General Public License | ||
| 86 | along with this program; if not, write to the Free Software | ||
| 87 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 88 | |||
| 89 | =head1 FEEDBACK | ||
| 90 | |||
| 91 | Currently, all feedback should be sent directly to the author. | ||
| 92 | |||
| 93 | =head1 AUTHOR - Steve Mathias | ||
| 94 | |||
| 95 | Email: mathias@genomica.com | ||
| 96 | |||
| 97 | Phone: (720) 565-4029 | ||
| 98 | |||
| 99 | Address: Genomica Corporation | ||
| 100 | 1745 38th Street | ||
| 101 | Boulder, CO 80301 | ||
| 102 | |||
| 103 | =head1 DETAILS | ||
| 104 | |||
| 105 | The rest of the documentation describes each of the methods. The names of | ||
| 106 | internal variables and methods are preceded with an underscore (_). | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | ################## | ||
| 111 | # # | ||
| 112 | # Begin the code # | ||
| 113 | # # | ||
| 114 | ################## | ||
| 115 | |||
| 116 | package Genetics::API::Analysis::Linkage ; | ||
| 117 | |||
| 118 | BEGIN { | ||
| 119 | $ID = "Genetics::API::Analysis::Linkage" ; | ||
| 120 | #$DEBUG = $main::DEBUG ; | ||
| 121 | $DEBUG = 0 ; | ||
| 122 | $DEBUG and warn "Debugging in $ID (v$VERSION) is on" ; | ||
| 123 | } | ||
| 124 | |||
| 125 | =head1 Imported Packages | ||
| 126 | |||
| 127 | strict Just to be anal | ||
| 128 | vars Global variables | ||
| 129 | Carp Error reporting | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | use strict ; | ||
| 134 | use vars qw(@ISA @EXPORT @EXPORT_OK $ID $DEBUG) ; | ||
| 135 | use Carp ; | ||
| 136 | use Exporter ; | ||
| 137 | |||
| 138 | =head1 Inheritance | ||
| 139 | |||
| 140 | Exporter Make methods available to importing packages | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | @ISA = qw(Exporter) ; | ||
| 145 | |||
| 146 | @EXPORT = qw(writeLinkageFiles runGenehunter | ||
| 147 | writeKEMFiles runKEM processKemResults | ||
| 148 | printProcessedKemResults htmlizeKemResults | ||
| 149 | orderMarkersWithMap _renumberAlleles) ; | ||
| 150 | @EXPORT_OK = qw(); | ||
| 151 | |||
| 152 | |||
| 153 | =head1 Public Methods | ||
| 154 | |||
| 155 | =head2 writeLinkageFiles | ||
| 156 | |||
| 157 | Function : Write LINKAGE format pedigree and locus files. | ||
| 158 | Arguments : A hash of parameters as follows | ||
| 159 | KINDREDS => Array pointer to a list of Kindred objects. | ||
| 160 | These define the Subjects being analyzed. | ||
| 161 | Required, for obvious reasons. | ||
| 162 | MARKERS => Array pointer to a list of Marker objects. | ||
| 163 | These define the Markers being analyzed. | ||
| 164 | Required, for obvious reasons. | ||
| 165 | ALLELETYPE => String containing the allele type of the alleles | ||
| 166 | to be used. | ||
| 167 | Optional. Default value is Code. | ||
| 168 | AFS => Array pointer to a list of Subject or Kindred objects, or | ||
| 169 | a single Cluster (Kindred or Subject) object. | ||
| 170 | The source for allele frequencies. | ||
| 171 | Optional. If not provided, the input Kindreds will be used | ||
| 172 | EXTAFS => A FrequencySource object. | ||
| 173 | The source for allele frequencies. | ||
| 174 | Optional. If not provided, the input Kindreds will be used | ||
| 175 | TRAIT => A StudyVariable object. | ||
| 176 | The trait locus being analyzed. The category of this | ||
| 177 | StudyVariable must be DynamicAffectionStatus or | ||
| 178 | StaticAffectionStatus. | ||
| 179 | Optional. | ||
| 180 | LC => A StudyVariable object. | ||
| 181 | This StudyVariable defines the liability classes to be used | ||
| 182 | with the trait locus being analyzed. The category of this | ||
| 183 | StudyVariable must be StaticLiabilityClass. | ||
| 184 | Optional. | ||
| 185 | QTLS => Array pointer to a list of StudyVariable objects. | ||
| 186 | QTLs to be analyzed. | ||
| 187 | Optional. | ||
| 188 | MAP => A Map object. | ||
| 189 | This Map will be used to order the markers. Also, the distances | ||
| 190 | will be used. Right now, every input Marker MUST be on the Map | ||
| 191 | and there MUST NOT be any markers on the map that are not being | ||
| 192 | analyzed. | ||
| 193 | Optional. | ||
| 194 | DATFILE => Filehandle reference. | ||
| 195 | The filehandle to which the locus file will be written. | ||
| 196 | Optional. If not provided, STDOUT will be used. | ||
| 197 | PEDFILE => Filehandle reference. | ||
| 198 | The filehandle to which the pedigree file will be | ||
| 199 | written. | ||
| 200 | Optional. If not provided, STDOUT will be used. | ||
| 201 | MARKERDELIM => Scalar containint the string to be used to | ||
| 202 | delimit marker names in the locus file. | ||
| 203 | Optional. If not provided, # is used. | ||
| 204 | RENUMBER => Boolean. | ||
| 205 | Optional. If not provided, alleles are NOT re- | ||
| 206 | numbered. | ||
| 207 | Returns : N/A | ||
| 208 | Scope : Public | ||
| 209 | Called by : | ||
| 210 | Comments : To Do: support for X-linked data | ||
| 211 | support for dynamic trait loci | ||
| 212 | support for dynamic liability classes | ||
| 213 | support for QTLs | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub writeLinkageFiles { | ||
| 218 | my($self, %param) = @_ ; | ||
| 219 | my($kListPtr, $mListPtr, $alleleType, $afsPtr, $fs, $extAFSFlag, $traitSV, | ||
| 220 | $traitFlag, $lcSV, $lcFlag, $qtlListPtr, $qtlFlag, $datFh, $pedFh, $mdStr, | ||
| 221 | $rnFlag, $map, $mapFlag, @orderedMarkers, @unorderedMarkers, @distances, | ||
| 222 | $locusCount, $locusStr, $mapStr, $i, $asdPtr, $allelesStr, $codesListPtr, | ||
| 223 | $lcCount, $codePtr, $marker, $markerName, %renum, $freqsPtr, $alleleCount, | ||
| 224 | $freq, $freqsStr, $allele, $kindred, $kindredName, @subjects, $subject, | ||
| 225 | $subjectName, @subjNames, $momName, $dadName, $sex, $code, @alleles, $a1, | ||
| 226 | $a2, $c1, $c2, | ||
| 227 | ) ; | ||
| 228 | |||
| 229 | defined($kListPtr = $param{KINDREDS}) or | ||
| 230 | croak "ERROR [writeLinkageFiles]: No Kindreds!" ; | ||
| 231 | defined($mListPtr = $param{MARKERS}) or | ||
| 232 | croak "ERROR [writeLinkageFiles]: No Markers!" ; | ||
| 233 | defined($alleleType = $param{ALLELETYPE}) or $alleleType = "Code" ; | ||
| 234 | if ( defined($afsPtr = $param{AFS}) ) { | ||
| 235 | $extAFSFlag = 0 ; | ||
| 236 | } elsif ( defined($fs = $param{EXTAFS}) ) { | ||
| 237 | $extAFSFlag = 1 ; | ||
| 238 | } else { | ||
| 239 | $afsPtr = $kListPtr ; | ||
| 240 | $extAFSFlag = 0 ; | ||
| 241 | } | ||
| 242 | defined($traitSV = $param{TRAIT}) or $traitSV = undef ; | ||
| 243 | _validateTraitInput($traitSV, \$traitFlag, \$lcFlag) or | ||
| 244 | croak "ERROR [writeLinkageFiles]: Invalid trait locus StudyVariable!" ; | ||
| 245 | defined($lcSV = $param{LC}) or $lcSV = undef ; | ||
| 246 | _validateLCInput($lcSV, \$lcFlag) or | ||
| 247 | croak "ERROR [writeLinkageFiles]: Invalid liability class StudyVariable!" ; | ||
| 248 | $qtlListPtr = undef ; # QTLs not supported | ||
| 249 | # defined($qtlListPtr = $param{QTLS}) or $qtlListPtr = undef ; | ||
| 250 | # _validateQTLInput($qtlListPtr, \$qtlFlag) or | ||
| 251 | # croak "ERROR [writeLinkageFiles]: Invalid QTL StudyVariable(s)!" ; | ||
| 252 | defined($datFh = $param{DATFILE}) or $datFh = \*STDOUT ; | ||
| 253 | defined($pedFh = $param{PEDFILE}) or $pedFh = \*STDOUT ; | ||
| 254 | defined($mdStr = $param{MARKERDELIM}) or $mdStr = "#" ; | ||
| 255 | defined($rnFlag = $param{RENUMBER}) or $rnFlag = 0 ; | ||
| 256 | |||
| 257 | defined($map = $param{MAP}) or $map = undef ; | ||
| 258 | _processMarkersAndMap($mListPtr, $map, \$mapFlag, \@orderedMarkers, | ||
| 259 | \@unorderedMarkers, \@distances) or croak "ERROR [writeLinkageFiles]: Input marker set and Map are not compatible!" ; | ||
| 260 | |||
| 261 | # Locus file | ||
| 262 | $locusCount = scalar(@$mListPtr) ; | ||
| 263 | $traitFlag and $locusCount++ ; | ||
| 264 | $qtlFlag and $locusCount += scalar(@$qtlListPtr) ; | ||
| 265 | $mapStr = " " ; | ||
| 266 | print $datFh "$locusCount 0 0 5\n" ; | ||
| 267 | print $datFh "0 0.0 0.0 0\n" ; | ||
| 268 | $locusStr = " " ; | ||
| 269 | $i = 1 ; | ||
| 270 | while ($i <= $locusCount) { | ||
| 271 | $locusStr .= "$i " ; | ||
| 272 | $i++ ; | ||
| 273 | } | ||
| 274 | print $datFh "$locusStr\n" ; | ||
| 275 | if ($traitFlag) { | ||
| 276 | $asdPtr = $traitSV->field("AffStatDef") ; | ||
| 277 | print $datFh "1 2 << affection status\n" ; | ||
| 278 | $allelesStr = (1 - $$asdPtr{diseaseAlleleFreq}) ; | ||
| 279 | $allelesStr .= " $$asdPtr{diseaseAlleleFreq} << allele frequencies" ; | ||
| 280 | print $datFh "$allelesStr\n" ; | ||
| 281 | if ($lcFlag == 2) { | ||
| 282 | # using dynamic liability classes | ||
| 283 | |||
| 284 | } elsif ($lcFlag == 1) { | ||
| 285 | # using static liability classes | ||
| 286 | $codesListPtr = $lcSV->field("Codes") ; | ||
| 287 | $lcCount = scalar(@$codesListPtr) ; | ||
| 288 | print $datFh "$lcCount << number of liability classes\n" ; | ||
| 289 | foreach $codePtr (@$codesListPtr) { | ||
| 290 | print $datFh "$$codePtr{pen11} $$codePtr{pen12} $$codePtr{pen22} << penetrance values\n" ; | ||
| 291 | } | ||
| 292 | } else { | ||
| 293 | # use the default liability classes | ||
| 294 | print $datFh "1 << number of liability classes\n" ; | ||
| 295 | print $datFh "$$asdPtr{pen11} $$asdPtr{pen12} $$asdPtr{pen22} << penetrance values\n" ; | ||
| 296 | } | ||
| 297 | $mapStr .= "0.100 " ; | ||
| 298 | } | ||
| 299 | |||
| 300 | foreach $marker (@orderedMarkers, @unorderedMarkers) { | ||
| 301 | $markerName = $marker->field("name") ; | ||
| 302 | if ($rnFlag) { | ||
| 303 | $renum{$markerName} = $self->_renumberAlleles($marker, $alleleType) ; | ||
| 304 | } | ||
| 305 | if ($extAFSFlag) { | ||
| 306 | $freqsPtr = $fs->getAlleleFreqsByMarkerName($markerName, $alleleType) ; | ||
| 307 | } else { | ||
| 308 | $freqsPtr = $self->getAlleleFreqs($marker, $alleleType, $afsPtr) ; | ||
| 309 | } | ||
| 310 | $alleleCount = scalar(keys %$freqsPtr) ; | ||
| 311 | print $datFh "3 $alleleCount $mdStr $markerName\n" ; | ||
| 312 | $freqsStr = "" ; | ||
| 313 | foreach $allele ($self->getAllelesByType($marker, $alleleType)) { | ||
| 314 | # alleles are sorted numerically, then alphabetically | ||
| 315 | $freq = $$freqsPtr{$allele} ; | ||
| 316 | $freq = 0.0001 if $freq eq "0" ; | ||
| 317 | $freq = 0.0001 if $freq == 0 ; | ||
| 318 | $freqsStr .= "$freq " ; | ||
| 319 | } | ||
| 320 | $mapStr .= shift @distances ; | ||
| 321 | print $datFh "$freqsStr << allele frequencies\n" ; | ||
| 322 | } | ||
| 323 | #$mapStr =~ s/0.1 $// ; # markers - 1 distances | ||
| 324 | print $datFh "0 0\n" ; | ||
| 325 | print $datFh "$mapStr << map\n" ; | ||
| 326 | print $datFh "1 0.10000 0.45000\n" ; | ||
| 327 | |||
| 328 | # Pedigree file | ||
| 329 | foreach $kindred (@$kListPtr) { | ||
| 330 | $kindredName = $kindred->field("name") ; | ||
| 331 | @subjects = $self->getSubjectsByKindred($kindred) ; | ||
| 332 | @subjNames = map { $_->name } @subjects ; | ||
| 333 | foreach $subject (@subjects) { | ||
| 334 | $allelesStr = $code = "" ; | ||
| 335 | $subjectName = $subject->field("name") ; | ||
| 336 | # Particularly when using derived Kindreds, mom and dad may be defined | ||
| 337 | # from a Subject's point of view, but not be in the Kindred that is | ||
| 338 | # being analyzed. | ||
| 339 | if ( defined($momName = $subject->getMotherName()) ) { | ||
| 340 | grep /$momName/, @subjNames or $momName = "0" ; | ||
| 341 | } else { | ||
| 342 | $momName = "0" ; | ||
| 343 | } | ||
| 344 | if ( defined($dadName = $subject->getFatherName()) ) { | ||
| 345 | grep /$dadName/, @subjNames or $dadName = "0" ; | ||
| 346 | } else { | ||
| 347 | $dadName = "0" ; | ||
| 348 | } | ||
| 349 | $sex = $subject->field("gender") ; | ||
| 350 | $sex eq "Male" and $sex = 1 ; | ||
| 351 | $sex eq "Female" and $sex = 2 ; | ||
| 352 | if ($traitFlag) { | ||
| 353 | $code = $self->getPtValue($subject, $traitSV) ; | ||
| 354 | $allelesStr .= "$code " ; | ||
| 355 | if ($lcFlag == 1) { | ||
| 356 | # static liability class assignments | ||
| 357 | $code = $self->getPtValue($subject, $lcSV) ; | ||
| 358 | $allelesStr .= "$code " ; | ||
| 359 | } elsif ($lcFlag == 2) { | ||
| 360 | # dynamic liability class assignments | ||
| 361 | } else { | ||
| 362 | # no liability class assignments | ||
| 363 | $allelesStr .= " " ; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | foreach $marker (@orderedMarkers, @unorderedMarkers) { | ||
| 367 | $markerName = $marker->field("name") ; | ||
| 368 | @alleles = $self->getGtAlleles($subject, $marker) ; | ||
| 369 | if ( defined($alleles[0]) ) { | ||
| 370 | $alleleCount = scalar(@alleles) ; | ||
| 371 | if ($alleleCount == 2) { | ||
| 372 | if ($rnFlag) { | ||
| 373 | $a1 = $alleles[0] ; | ||
| 374 | $a2 = $alleles[1] ; | ||
| 375 | $c1 = ${renum{$markerName}}{$a1} ; # renumbered code corresponding to allele1 | ||
| 376 | $c2 = ${renum{$markerName}}{$a2} ; # renumbered code corresponding to allele2 | ||
| 377 | unless (defined($c1) and defined($c2)) { | ||
| 378 | print "\nWARNING: Allele renumbering problem for marker $markerName:\n" ; | ||
| 379 | print "\tAlleles: $a1, $a2 => " ; | ||
| 380 | print "\tCodes: $c1, $c2\n" ; | ||
| 381 | } | ||
| 382 | $allelesStr .= "$c1 $c2 " ; | ||
| 383 | } else { | ||
| 384 | $allelesStr .= "$alleles[0] $alleles[1] " ; | ||
| 385 | } | ||
| 386 | } elsif ($alleleCount == 1) { | ||
| 387 | $allelesStr .= "$alleles[0] $alleles[0] " ; | ||
| 388 | } else { | ||
| 389 | $allelesStr .= "$alleles[0] $alleles[1] " ; | ||
| 390 | print STDERR "WARNING: More then two alleles found for $subjectName/$markerName" ; | ||
| 391 | } | ||
| 392 | } else { | ||
| 393 | $allelesStr .= "0 0 " ; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | if ($qtlFlag) { | ||
| 397 | # Get QTL Pts | ||
| 398 | } | ||
| 399 | print $pedFh "$kindredName $subjectName $dadName $momName $sex $allelesStr\n" ; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | #close PRE ; | ||
| 403 | |||
| 404 | return(1) ; | ||
| 405 | } | ||
| 406 | |||
| 407 | =head2 runGenehunter | ||
| 408 | |||
| 409 | Function : Run a Genehunter analysis | ||
| 410 | Arguments : A hash of parameters as follows | ||
| 411 | KINDREDS => Array pointer to a list of Kindred objects. | ||
| 412 | These define the Subjects being analyzed. | ||
| 413 | Required, for obvious reasons. | ||
| 414 | MARKERS => Array pointer to a list of Marker objects. | ||
| 415 | These define the Markers being analyzed. | ||
| 416 | Required, for obvious reasons. | ||
| 417 | AFS => Array pointer to a list of Subject or Kindred objects, or | ||
| 418 | a single Cluster (Kindred or Subject) object. | ||
| 419 | The source for allele frequencies. | ||
| 420 | Optional. If not provided, the input Kindreds will be used | ||
| 421 | EXTAFS => A FrequencySource object. | ||
| 422 | The source for allele frequencies. | ||
| 423 | Optional. If not provided, the input Kindreds will be used | ||
| 424 | TRAIT => A StudyVariable object. | ||
| 425 | The trait locus being analyzed. The category of this | ||
| 426 | StudyVariable must be AffectionStatus or StaticAffectionStatus. | ||
| 427 | Optional. | ||
| 428 | LC => A StudyVariable object. | ||
| 429 | This StudyVariable defines the liability classes to be used | ||
| 430 | with the trait locus being analyzed. The category of this | ||
| 431 | StudyVariable must be StaticLiabilityClass. | ||
| 432 | Optional. | ||
| 433 | DATFILENAME => A name to be used for the locus file. | ||
| 434 | Required. | ||
| 435 | PEDFILENAME => A name to be used for the pedigree file. | ||
| 436 | Required. | ||
| 437 | PHOTOFILENAME => A name to be used for the photo file. | ||
| 438 | Optional. | ||
| 439 | FILESONLY => Boolean. If true, only write files. | ||
| 440 | Optional, defaults to false. | ||
| 441 | Returns : N/A | ||
| 442 | Scope : Public | ||
| 443 | Comments : | ||
| 444 | |||
| 445 | =cut | ||
| 446 | |||
| 447 | sub runGenehunter { | ||
| 448 | my($self, %param) = @_ ; | ||
| 449 | my($setup, $analysis, $score, $singlePt, $ht, $countRecs, $offEnd, $incr, | ||
| 450 | $mapFunc, $units, $maxBits, $discard, $skipLarge, $totalStat, | ||
| 451 | $setupFileName, $gh) ; | ||
| 452 | |||
| 453 | open(DAT, "> $param{DATFILENAME}") or | ||
| 454 | croak "ERROR [runGenehunter]: Can't write locus file: $!" ; | ||
| 455 | open(PRE, "> $param{PEDFILENAME}") or | ||
| 456 | croak "ERROR [runGenehunter]: Can't write pedigree file: $!" ; | ||
| 457 | |||
| 458 | $self->writeLinkageFiles(KINDREDS => $param{KINDREDS}, | ||
| 459 | MARKERS => $param{MARKERS}, | ||
| 460 | ALLELETYPE => $param{ALLELETYPE}, | ||
| 461 | AFS => $param{AFS}, | ||
| 462 | TRAIT => $param{TRAIT}, | ||
| 463 | LC => $param{LC}, | ||
| 464 | MAP => $param{MAP}, | ||
| 465 | RENUMBER => 1, | ||
| 466 | DATFILE => \*DAT, | ||
| 467 | PEDFILE => \*PRE) ; | ||
| 468 | close DAT ; | ||
| 469 | close PRE ; | ||
| 470 | |||
| 471 | if (defined $param{PHOTOFILENAME}) { | ||
| 472 | $setup = "photo $param{PHOTOFILENAME}\n" ; | ||
| 473 | $setup .= "load markers $param{DATFILENAME}\n" ; | ||
| 474 | } else { | ||
| 475 | $setup = "load markers $param{DATFILENAME}\n" ; | ||
| 476 | } | ||
| 477 | defined($analysis = $param{ANALYSIS}) or $analysis = "BOTH" ; | ||
| 478 | $setup .= "analysis $analysis\n" ; | ||
| 479 | defined($score = $param{SCORE}) or $score = "ALL" ; | ||
| 480 | $setup .= "score $score\n" ; | ||
| 481 | defined($singlePt = $param{SINGLEPOINT}) or $singlePt = "off" ; | ||
| 482 | $setup .= "single point $singlePt\n" ; | ||
| 483 | if ($singlePt eq "off") { | ||
| 484 | defined($offEnd = $param{OFFEND}) or $offEnd = "0.0" ; | ||
| 485 | $setup .= "off end $offEnd\n" ; | ||
| 486 | defined($incr = $param{INCREMENT}) or $incr = "step 5" ; | ||
| 487 | $setup .= "increment $incr\n" ; | ||
| 488 | } | ||
| 489 | defined($ht = $param{HAPLOTYPE}) or $ht = "off" ; | ||
| 490 | $setup .= "haplotype $ht\n" ; | ||
| 491 | defined($countRecs = $param{COUNTRECS}) or $countRecs = "off" ; | ||
| 492 | $setup .= "count recs $countRecs\n" ; | ||
| 493 | defined($mapFunc = $param{MAPFUNCTION}) or $mapFunc = "kosambi" ; | ||
| 494 | $setup .= "map function $mapFunc\n" ; | ||
| 495 | defined($units = $param{UNITS}) or $units = "cM" ; | ||
| 496 | $setup .= "units $units\n" ; | ||
| 497 | defined($maxBits = $param{MAXBITS}) or $maxBits = "20" ; | ||
| 498 | $setup .= "max bits $maxBits\n" ; | ||
| 499 | defined($discard = $param{DISCARD}) or $discard = "on" ; | ||
| 500 | $setup .= "discard $discard\n" ; | ||
| 501 | defined($skipLarge = $param{SKIPLARGE}) or $skipLarge = "off" ; | ||
| 502 | $setup .= "skip large $skipLarge\n" ; | ||
| 503 | $setup .= "scan pedigrees $param{PEDFILENAME}\n" ; | ||
| 504 | defined $param{PS} and $setup .= "ps $param{PS}\n" ; | ||
| 505 | defined $param{DS} and $setup .= "ds $param{DS}\n" ; | ||
| 506 | if (defined($totalStat = $param{TOTALSTAT})) { | ||
| 507 | $totalStat = "total stat $totalStat" ; | ||
| 508 | } else { | ||
| 509 | $totalStat = "total stat" ; | ||
| 510 | } | ||
| 511 | $setup .= "$totalStat\n" ; | ||
| 512 | |||
| 513 | defined($setupFileName = $param{SETUPFILENAME}) or | ||
| 514 | $setupFileName = "/tmp/gh.$$.setup" ; | ||
| 515 | open(SETUP, "> $setupFileName") or | ||
| 516 | croak "ERROR [runGenehunter]: Can't write setup file: $!" ; | ||
| 517 | print SETUP $setup ; | ||
| 518 | close SETUP ; | ||
| 519 | |||
| 520 | unless ($param{FILESONLY}) { | ||
| 521 | defined $param{PATHTOGH} ? $gh = $param{PATHTOGH} | ||
| 522 | : $gh = "gh" ; | ||
| 523 | system("cat $setupFileName | $gh") == 0 or | ||
| 524 | croak "ERROR [runGenehunter]: Problem running genehunter: $?" ; | ||
| 525 | # system("cat $setupFileName") == 0 or | ||
| 526 | # croak "ERROR [runGenehunter]: Problem: $?" ; | ||
| 527 | } | ||
| 528 | |||
| 529 | return(1) ; | ||
| 530 | } | ||
| 531 | |||
| 532 | =head2 writeKEMFiles | ||
| 533 | |||
| 534 | Function : Write "LINKAGE format" pedigree and locus files for use by KEM. | ||
| 535 | Arguments : A hash of parameters as follows | ||
| 536 | SUBJECTS => Array pointer to a list of Kindred objects. | ||
| 537 | These define the Subjects being analyzed. | ||
| 538 | Required, for obvious reasons. | ||
| 539 | MARKERS => Array pointer to a list of Marker objects. | ||
| 540 | These define the Markers being analyzed. | ||
| 541 | Required, for obvious reasons. | ||
| 542 | DISTANCES => Array pointer to a list of distances bewtween the | ||
| 543 | Markers. | ||
| 544 | ALLELETYPE => String containing the allele type of the alleles | ||
| 545 | to be used. | ||
| 546 | Optional. Default value is Code. | ||
| 547 | DATFILE => Filehandle reference. | ||
| 548 | The filehandle to which the locus file will be written. | ||
| 549 | Optional. If not provided, STDOUT will be used. | ||
| 550 | PEDFILE => Filehandle reference. | ||
| 551 | The filehandle to which the pedigree file will be | ||
| 552 | written. | ||
| 553 | Optional. If not provided, STDOUT will be used. | ||
| 554 | Returns : A hash, keyed on marker names that maps the codes (used in the | ||
| 555 | output files) to the allele names of the markers. The hash has | ||
| 556 | the following structure: | ||
| 557 | markerNames => code2alleleName hash references | ||
| 558 | Scope : Public | ||
| 559 | Called by : | ||
| 560 | Comments : Alleles have to be re-numbered for KEM. | ||
| 561 | Markers and distances are used in their input order. | ||
| 562 | |||
| 563 | =cut | ||
| 564 | |||
| 565 | sub writeKEMFiles { | ||
| 566 | my($self, %param) = @_ ; | ||
| 567 | my($sListPtr, $mListPtr, $dListPtr, $alleleType, $datFh, $pedFh, | ||
| 568 | $locusCount, $mapStr, $marker, $markerName, $alleleCount, %renum, | ||
| 569 | @subjects, $subject, $subjectName, $sex, @fullyGtdSubjects, | ||
| 570 | $allelesStr, @alleles, $a1, $a2, $c1, $c2, | ||
| 571 | ) ; | ||
| 572 | |||
| 573 | defined($sListPtr = $param{SUBJECTS}) or | ||
| 574 | croak "ERROR [writeKEMFiles]: No Subjects!" ; | ||
| 575 | defined($mListPtr = $param{MARKERS}) or | ||
| 576 | croak "ERROR [writeKEMFiles]: No Markers!" ; | ||
| 577 | defined($dListPtr = $param{DISTANCES}) or | ||
| 578 | croak "ERROR [writeKEMFiles]: No Distances!" ; | ||
| 579 | defined($alleleType = $param{ALLELETYPE}) or $alleleType = "Code" ; | ||
| 580 | defined($datFh = $param{DATFILE}) or $datFh = \*STDOUT ; | ||
| 581 | defined($pedFh = $param{PEDFILE}) or $pedFh = \*STDOUT ; | ||
| 582 | |||
| 583 | # Marker file | ||
| 584 | $locusCount = scalar(@$mListPtr) ; | ||
| 585 | print $datFh "$locusCount\n\n\n" ; | ||
| 586 | $mapStr = " " ; | ||
| 587 | |||
| 588 | foreach $marker (@$mListPtr) { | ||
| 589 | $markerName = $marker->name() ; | ||
| 590 | $renum{$markerName} = $self->_renumberAlleles($marker, $alleleType) ; | ||
| 591 | $alleleCount = scalar(keys %{$renum{$markerName}}) ; | ||
| 592 | print $datFh "3 $alleleCount # $markerName\n" ; | ||
| 593 | if (@$dListPtr) { | ||
| 594 | $mapStr .= shift(@$dListPtr) ; | ||
| 595 | $mapStr .= " " ; | ||
| 596 | } | ||
| 597 | print $datFh "\n" ; | ||
| 598 | } | ||
| 599 | print $datFh "\n$mapStr << map\n\n" ; | ||
| 600 | |||
| 601 | # Pedigree file | ||
| 602 | foreach $subject (@$sListPtr) { | ||
| 603 | $subjectName = $subject->name() ; | ||
| 604 | $sex = $subject->gender() ; | ||
| 605 | $sex eq "Male" and $sex = "M" ; | ||
| 606 | $sex eq "Female" and $sex = "F" ; | ||
| 607 | |||
| 608 | $allelesStr = "" ; | ||
| 609 | foreach $marker (@$mListPtr) { | ||
| 610 | $markerName = $marker->name() ; | ||
| 611 | @alleles = $self->getGtAlleles($subject, $marker) ; | ||
| 612 | if ( defined($alleles[0]) ) { | ||
| 613 | $alleleCount = scalar(@alleles) ; | ||
| 614 | if ($alleleCount == 2) { | ||
| 615 | $a1 = $alleles[0] ; | ||
| 616 | $a2 = $alleles[1] ; | ||
| 617 | $c1 = ${renum{$markerName}}{$a1} ; # renumbered code corresponding to allele1 | ||
| 618 | $c2 = ${renum{$markerName}}{$a2} ; # renumbered code corresponding to allele2 | ||
| 619 | unless (defined($c1) and defined($c2)) { | ||
| 620 | print "\nWARNING: Allele renumbering problem for marker $markerName:\n" ; | ||
| 621 | print "\tAlleles: $a1, $a2 => " ; | ||
| 622 | print "\tCodes: $c1, $c2\n" ; | ||
| 623 | } | ||
| 624 | $allelesStr .= "$c1 $c2 " ; | ||
| 625 | } elsif ($alleleCount == 1) { | ||
| 626 | $a1 = $alleles[0] ; | ||
| 627 | $c1 = ${renum{$markerName}}{$a1} ; # renumbered code corresponding to allele1 | ||
| 628 | $allelesStr .= "$c1 $c1 " ; | ||
| 629 | } else { | ||
| 630 | $a1 = $alleles[0] ; | ||
| 631 | $a2 = $alleles[1] ; | ||
| 632 | $c1 = ${renum{$markerName}}{$a1} ; # renumbered code corresponding to allele1 | ||
| 633 | $c2 = ${renum{$markerName}}{$a2} ; # renumbered code corresponding to allele2 | ||
| 634 | $allelesStr .= "$c1 $c2 " ; | ||
| 635 | print STDERR "WARNING: More then two alleles found for $subjectName/$markerName" ; | ||
| 636 | } | ||
| 637 | } else { | ||
| 638 | $allelesStr .= "0 0 " ; | ||
| 639 | } | ||
| 640 | } | ||
| 641 | print $pedFh "X $subjectName X X $sex $allelesStr\n" ; | ||
| 642 | } | ||
| 643 | |||
| 644 | return( _reverseRenum(%renum) ) ; | ||
| 645 | } | ||
| 646 | |||
| 647 | =head2 processKemResults | ||
| 648 | |||
| 649 | Function : Process K-EM output, as descibed below. | ||
| 650 | Arguments : A file handle reference to a K-EM output file, an array reference | ||
| 651 | to the list of markers used in the analysis, and a hash reference | ||
| 652 | to the hash mapping allele codes to names (as returned by | ||
| 653 | writeKEMFiles()). | ||
| 654 | Returns : An array. the first element is a scalar containing the best log | ||
| 655 | liklihood of the K-EM run. The rest of the elements are hash | ||
| 656 | pointers containing processed results. The structure of the | ||
| 657 | referenced hashes is: | ||
| 658 | $htResult{name} = Ht number | ||
| 659 | $htResult{freq} = float | ||
| 660 | $htResult{ht} = array reference to a list of allele names | ||
| 661 | Example : @kemResults = processKemResults(\@snps, \%poNameCode2Allele) ; | ||
| 662 | Scope : Public Class Method | ||
| 663 | Comments : Converts codes in K-EM output file back into allele names | ||
| 664 | |||
| 665 | =cut | ||
| 666 | |||
| 667 | sub processKemResults { | ||
| 668 | my($fh, $poListPtr, $renumPtr) = @_ ; | ||
| 669 | my($getHtFlag, $line, $htAlleleStr, $freq, @htAlleles, %kemResults, | ||
| 670 | @poNames, $i, $htNum, @htCodes, $htStr, $c2aPtr, %htResult, | ||
| 671 | @processedKemResults) ; | ||
| 672 | |||
| 673 | $getHtFlag = 0 ; | ||
| 674 | while (defined($line = <$fh>)) { | ||
| 675 | if ($line =~ /^Best log\(likelihood\) = (\S+)/) { | ||
| 676 | push @processedKemResults, $1 ; # First element of processed results | ||
| 677 | } | ||
| 678 | if ($line =~ /^Haplotype m1/) { | ||
| 679 | $getHtFlag = 1 ; | ||
| 680 | next ; | ||
| 681 | } | ||
| 682 | next unless $getHtFlag == 1 ; | ||
| 683 | #last if $line =~ /^Total CPU/ ; # C++ | ||
| 684 | last if $line =~ /^[0-9:\.]+ KEM complete/ ; # Java | ||
| 685 | |||
| 686 | ($htNum, $htAlleleStr, $freq) = $line =~ /^\s+(\d+)\s+(.+)\s+([0-9\.eE-]+)$/ ; | ||
| 687 | $htAlleleStr =~ s/^\s*// ; | ||
| 688 | $htAlleleStr =~ s/\s*$// ; | ||
| 689 | @htAlleles = split(/\s+/, $htAlleleStr) ; | ||
| 690 | $kemResults{$htNum}{freq} = $freq ; | ||
| 691 | $kemResults{$htNum}{ht} = [ @htAlleles ] ; | ||
| 692 | } | ||
| 693 | |||
| 694 | # Generate a list of marker names corresponding to the marker order | ||
| 695 | # in the haplotypes | ||
| 696 | for (@$poListPtr) { | ||
| 697 | push @poNames, $_->name() ; | ||
| 698 | } | ||
| 699 | |||
| 700 | foreach $htNum (sort { $kemResults{$b}->{freq} <=> $kemResults{$a}->{freq} } keys %kemResults) { | ||
| 701 | $htStr = "" ; | ||
| 702 | @htCodes = @{$kemResults{$htNum}{ht}} ; | ||
| 703 | for ($i=0 ; $i<=$#htCodes ; $i++) { | ||
| 704 | $c2aPtr = $$renumPtr{$poNames[$i]} ; | ||
| 705 | $htStr .= "$$c2aPtr{$htCodes[$i]} " ; | ||
| 706 | } | ||
| 707 | $htResult{name} = $htNum ; | ||
| 708 | $htResult{freq} = $kemResults{$htNum}{freq} ; | ||
| 709 | $htResult{ht} = $htStr ; | ||
| 710 | push @processedKemResults, { %htResult } ; | ||
| 711 | } | ||
| 712 | |||
| 713 | return(@processedKemResults) ; | ||
| 714 | } | ||
| 715 | |||
| 716 | =head2 printProcessedKemResults | ||
| 717 | |||
| 718 | Function : | ||
| 719 | Arguments : | ||
| 720 | Returns : | ||
| 721 | Example : printProcessedKemResults(%kemResults) | ||
| 722 | Scope : Public Class Method | ||
| 723 | Comments : | ||
| 724 | |||
| 725 | =cut | ||
| 726 | |||
| 727 | sub printProcessedKemResults { | ||
| 728 | my(@kemResults) = @_ ; | ||
| 729 | my($logLik, $htResultPtr) ; | ||
| 730 | |||
| 731 | $logLik = shift @kemResults ; | ||
| 732 | print "\nBest Log Liklihood: $logLik\nHaplotypes:\n" ; | ||
| 733 | foreach $htResultPtr (@kemResults) { | ||
| 734 | printf "%10s %1.6f ", $$htResultPtr{name}, $$htResultPtr{freq} ; | ||
| 735 | print "$$htResultPtr{ht}\n" ; | ||
| 736 | } | ||
| 737 | |||
| 738 | return(1) ; | ||
| 739 | } | ||
| 740 | |||
| 741 | |||
| 742 | =head2 htmlizeKemResults | ||
| 743 | |||
| 744 | Function : Format processed K-EM results as HTML | ||
| 745 | Arguments : An array of hash references as returned by processKemResults() | ||
| 746 | Returns : Scalar text string containing HTML | ||
| 747 | Example : $html = htmlizeKemResults(@processedKemResults) | ||
| 748 | Scope : Public Class Method | ||
| 749 | Comments : | ||
| 750 | |||
| 751 | =cut | ||
| 752 | |||
| 753 | sub htmlizeKemResults { | ||
| 754 | my(@kemResults) = @_ ; | ||
| 755 | my($html, $logLik, $htResultPtr) ; | ||
| 756 | |||
| 757 | $logLik = shift @kemResults ; | ||
| 758 | $html = "" ; | ||
| 759 | $html .= "<table border=\"1\" cellspacing=\"2\" cellpadding=\"5\">\n" ; | ||
| 760 | $html .= "<tr><th colspan=\"3\">Best Log Liklihood: $logLik</th></tr>\n" ; | ||
| 761 | $html .= "<tr><th>#</th><th>Freq.</th><th>Haplotype</th></tr>\n" ; | ||
| 762 | foreach $htResultPtr (@kemResults) { | ||
| 763 | $html .= "<tr><td><b>$$htResultPtr{name}</b></td>" ; | ||
| 764 | $html .= "<td>$$htResultPtr{freq}</td>" ; | ||
| 765 | $html .= "<td>$$htResultPtr{ht}</td></tr>\n" ; | ||
| 766 | } | ||
| 767 | $html .= "</table>\n" ; | ||
| 768 | |||
| 769 | return($html) ; | ||
| 770 | } | ||
| 771 | |||
| 772 | =head2 orderMarkersWithMap | ||
| 773 | |||
| 774 | Function : Order markers and generate distances based on a Map object. | ||
| 775 | Arguments : An array reference to a list of input Markers/SNPs, a Map, and two | ||
| 776 | array reference to be populated with the ordered markers and | ||
| 777 | distances, respecitvely. | ||
| 778 | Returns : N/A | ||
| 779 | Example : $api->orderMarkersWithMap(\@snps, undef, \@orderedSnps, \@distances) ; | ||
| 780 | Scope : Public Instance Method | ||
| 781 | Comments : This is used inconjunction with runKEM(). | ||
| 782 | This is basically a stub method right now. It populates to | ||
| 783 | ordered list with a copy of the input list and populates the | ||
| 784 | distance list with 0.1s. | ||
| 785 | |||
| 786 | =cut | ||
| 787 | |||
| 788 | sub orderMarkersWithMap { | ||
| 789 | my($self, $mListPtr, $map, $omListPtr, $dListPtr) = @_ ; | ||
| 790 | my($i) ; | ||
| 791 | |||
| 792 | @$omListPtr = @$mListPtr ; | ||
| 793 | $i = 1 ; | ||
| 794 | while ($i < scalar(@$mListPtr)) { | ||
| 795 | push @$dListPtr, 0.1 ; | ||
| 796 | $i++ ; | ||
| 797 | } | ||
| 798 | |||
| 799 | return(1) ; | ||
| 800 | } | ||
| 801 | |||
| 802 | =head1 Private Methods | ||
| 803 | |||
| 804 | =head2 _renumberAlleles | ||
| 805 | |||
| 806 | Function : Renumber alleles into consecutive integers | ||
| 807 | Arguments : A Marker or SNP object and a string containing an allele type. | ||
| 808 | Returns : A reference to a hash with the following structure: | ||
| 809 | %hash = (alleleName => code) | ||
| 810 | Scope : Private instance method | ||
| 811 | Called by : writeLinkageFiles(). | ||
| 812 | Comments : | ||
| 813 | |||
| 814 | =cut | ||
| 815 | |||
| 816 | sub _renumberAlleles { | ||
| 817 | my($self, $marker, $alleleType) = @_ ; | ||
| 818 | my($markerName, @alleleNames, %alleleName2Code, $i) ; | ||
| 819 | my $dbh = $self->{dbh} ; | ||
| 820 | |||
| 821 | $markerName = $marker->field("name") ; | ||
| 822 | print STDERR "MESSAGE [_renumberAlleles]: $markerName " ; | ||
| 823 | |||
| 824 | @alleleNames = $self->getAllelesByType($marker, $alleleType) ; | ||
| 825 | |||
| 826 | for ($i=0 ; $i<=$#alleleNames ; $i++) { | ||
| 827 | $alleleName2Code{$alleleNames[$i]} = $i+1 ; | ||
| 828 | print STDERR "$alleleNames[$i] -> $alleleName2Code{$alleleNames[$i]}, " ; | ||
| 829 | } | ||
| 830 | print STDERR "\n" ; | ||
| 831 | |||
| 832 | return(\%alleleName2Code) ; | ||
| 833 | } | ||
| 834 | |||
| 835 | =head2 _reverseRenum | ||
| 836 | |||
| 837 | Function : See below | ||
| 838 | Arguments : A hash of with the following structure: | ||
| 839 | markerNames => alleleName2code hash references | ||
| 840 | Returns : A hash of with the following structure: | ||
| 841 | markerNames => code2alleleName hash references | ||
| 842 | Example : %poName_c2a = _reverseRenum(%poName_a2c) | ||
| 843 | Scope : Private Class Method | ||
| 844 | Comments : This is needed because the linkage file writing routines | ||
| 845 | need to keep track of allele->code mapping, but they need | ||
| 846 | to return code->allele mappings. | ||
| 847 | |||
| 848 | =cut | ||
| 849 | |||
| 850 | sub _reverseRenum { | ||
| 851 | my(%renum) = @_ ; | ||
| 852 | my($poName, $a2cPtr, %c2a, %poName_c2a) ; | ||
| 853 | |||
| 854 | while ( ($poName, $a2cPtr) = each %renum ) { | ||
| 855 | %c2a = reverse %$a2cPtr ; | ||
| 856 | $poName_c2a{$poName} = { %c2a } ; | ||
| 857 | } | ||
| 858 | |||
| 859 | return(%poName_c2a) ; | ||
| 860 | } | ||
| 861 | |||
| 862 | =head2 _validateTraitInput | ||
| 863 | |||
| 864 | Function : Verify that a StudyVariable may be used as a trait locus. | ||
| 865 | Arguments : A StudyVariable object and two pointers to flag variables used | ||
| 866 | in writeLinkageFiles(). | ||
| 867 | Returns : N/A | ||
| 868 | Scope : Private class method | ||
| 869 | Called by : writeLinkageFiles() | ||
| 870 | Comments : The fisrt flag pointer is to the trait flag; it is set to 1 if | ||
| 871 | the input StudyVariable may be used for static liability class | ||
| 872 | definition and assignments. The second flag pointer is to | ||
| 873 | the liability class flag. It is set to 2 if the input | ||
| 874 | StudyVariable may be used for dynamic liability class definition | ||
| 875 | and assignments. | ||
| 876 | |||
| 877 | =cut | ||
| 878 | |||
| 879 | sub _validateTraitInput { | ||
| 880 | my($sv, $traitFlagPtr, $lcFlagPtr) = @_ ; | ||
| 881 | |||
| 882 | if (defined $sv) { | ||
| 883 | if ($sv->field("category") eq "StaticAffectionStatus") { | ||
| 884 | $$traitFlagPtr = 1 ; | ||
| 885 | } elsif ($sv->field("category") eq "DynamicAffectionStatus") { | ||
| 886 | $$traitFlagPtr = 1 ; | ||
| 887 | $$lcFlagPtr = 2 ; | ||
| 888 | } else { | ||
| 889 | $$traitFlagPtr = 0 ; | ||
| 890 | carp "WARNING [_validateTraitInput]: Can't use StudyVariable $sv as a trait locus!" ; | ||
| 891 | } | ||
| 892 | } else { | ||
| 893 | $$traitFlagPtr = 0 ; | ||
| 894 | } | ||
| 895 | |||
| 896 | return(1) ; | ||
| 897 | } | ||
| 898 | |||
| 899 | =head2 _validateLCInput | ||
| 900 | |||
| 901 | Function : Verify that a StudyVariable may be used for static liability | ||
| 902 | class definition and assignments. | ||
| 903 | Arguments : A StudyVariable object a pointer to a flag variable used | ||
| 904 | in writeLinkageFiles(). | ||
| 905 | Returns : | ||
| 906 | Scope : Private class method | ||
| 907 | Called by : writeLinkageFiles() | ||
| 908 | Comments : The flag pointer is to the the liability class flag. It is set | ||
| 909 | to 1 if the input StudyVariable may be used for static liability | ||
| 910 | class definition and assignments. | ||
| 911 | |||
| 912 | =cut | ||
| 913 | |||
| 914 | sub _validateLCInput { | ||
| 915 | my($sv, $flagPtr) = @_ ; | ||
| 916 | |||
| 917 | # NB. There is no check for dynamic liability classes here, as that is done | ||
| 918 | # by _validateTraitInput(). | ||
| 919 | if (defined $sv) { | ||
| 920 | if ($sv->field("category") eq "StaticLiabilityClass") { | ||
| 921 | $$flagPtr = 1 ; | ||
| 922 | } else { | ||
| 923 | carp "WARNING [_validateLCInput]: Can't use StudyVariable $sv for liability classes!" ; | ||
| 924 | return(undef) ; | ||
| 925 | } | ||
| 926 | } else { | ||
| 927 | $$flagPtr = 0 ; | ||
| 928 | } | ||
| 929 | |||
| 930 | return(1) ; | ||
| 931 | } | ||
| 932 | |||
| 933 | =head2 _validateQTLInput | ||
| 934 | |||
| 935 | Function : Verify that StudyVariable(s) may be used as QTL varibales in a | ||
| 936 | linkage analysis. | ||
| 937 | Arguments : N/A | ||
| 938 | Returns : N/A | ||
| 939 | Scope : Private class method | ||
| 940 | Called by : writeLinkageFiles() | ||
| 941 | Comments : Not implemented. | ||
| 942 | |||
| 943 | |||
| 944 | =cut | ||
| 945 | |||
| 946 | sub _validateQTLInput { | ||
| 947 | my($inListPtr, $qtlListPtr, $flagPtr) = @_ ; | ||
| 948 | my($sv) ; | ||
| 949 | |||
| 950 | if (defined $inListPtr) { | ||
| 951 | foreach $sv (@$inListPtr) { | ||
| 952 | $sv->field("format") eq "Number" and push(@$qtlListPtr, $sv) ; | ||
| 953 | } | ||
| 954 | defined($$qtlListPtr[0]) ? $$flagPtr = 1 : $$flagPtr = 0 ; | ||
| 955 | } else { | ||
| 956 | $$flagPtr = 0 ; | ||
| 957 | } | ||
| 958 | |||
| 959 | return(1) ; | ||
| 960 | } | ||
| 961 | |||
| 962 | =head2 _processMarkersAndMap | ||
| 963 | |||
| 964 | Function : Order markers and generate distances for linkage analysis. | ||
| 965 | Arguments : An array reference to a list of input Markers/SNPs, a Map | ||
| 966 | Returns : N/A | ||
| 967 | Scope : Private class method | ||
| 968 | Called by : writeLinkageFiles() | ||
| 969 | Comments : There does not have to be a Map object. If there is not the | ||
| 970 | marker order used is that of the input list and all distances are | ||
| 971 | set to 0.1. If there are input Markers/SNPs that are not on the | ||
| 972 | Map, they are included in the analysis after the ordered | ||
| 973 | Markers/SNPs with all distances are set to 0.1. | ||
| 974 | |||
| 975 | NB. This does not yet deal with the following: | ||
| 976 | - markers on the map that are not in the input set | ||
| 977 | - global map order (it may never do this) | ||
| 978 | - map distance units other than Theta or cM (it may never do this) | ||
| 979 | |||
| 980 | =cut | ||
| 981 | |||
| 982 | sub _processMarkersAndMap { | ||
| 983 | my($mListPtr, $map, $flagPtr, $omListPtr, $uomListPtr, $dListPtr) = @_ ; | ||
| 984 | my($markerCount, $units, $omeListPtr, @soNames, $omePtr, $marker, $markerName, | ||
| 985 | @toBeOrdered, $mID, $soID, $i, $j, %order, $cM2ThetaFlag, $dist) ; | ||
| 986 | |||
| 987 | $markerCount = scalar(@$mListPtr) ; | ||
| 988 | if (defined $map) { | ||
| 989 | # Use a map to order the markers | ||
| 990 | unless ($map->field("orderingMethod") eq "Relative") { | ||
| 991 | carp "WARNING [_processMarkersAndMap]: Inappropriate Map ordering method!" ; | ||
| 992 | return(undef) ; | ||
| 993 | } | ||
| 994 | $units = $map->distanceUnits() ; | ||
| 995 | if ($units eq "cM") { | ||
| 996 | $cM2ThetaFlag = 1 ; | ||
| 997 | } elsif ($units eq "Theta") { | ||
| 998 | $cM2ThetaFlag = 0 ; | ||
| 999 | } else { | ||
| 1000 | carp "WARNING [_processMarkersAndMap]: Invalid Map.distanceUnits: $units!" ; | ||
| 1001 | return(undef) ; | ||
| 1002 | } | ||
| 1003 | $omeListPtr = $map->field("OrderedMapElements") ; | ||
| 1004 | if (scalar(@$omeListPtr) > scalar(@$mListPtr)) { | ||
| 1005 | carp "WARNING [_processMarkersAndMap]: There are Markers/SNPs on the map that are not in the input set!" ; | ||
| 1006 | return(undef) ; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | # NB. input markers and markers on the map are verified by name - probably | ||
| 1010 | # not the most bomb-proof way to do this. | ||
| 1011 | # First, generate a list of all element names on the map... | ||
| 1012 | foreach $omePtr (@$omeListPtr) { | ||
| 1013 | push @soNames, $$omePtr{SeqObj}->{name} ; | ||
| 1014 | } | ||
| 1015 | # ...then, check to make sure each input marker's name is in that list... | ||
| 1016 | foreach $marker (@$mListPtr) { | ||
| 1017 | $markerName = $marker->field("name") ; | ||
| 1018 | if (grep {$markerName eq $_} @soNames) { | ||
| 1019 | # ...if so, add it to list of markers to be ordered... | ||
| 1020 | push @toBeOrdered, $marker ; | ||
| 1021 | } else { | ||
| 1022 | # ...if not, add it to unordered markers list and give a warning | ||
| 1023 | push @$uomListPtr, $marker ; | ||
| 1024 | carp "WARNING [_processMarkersAndMap]: Marker $markerName is not on the Map!" ; | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | # If we get to here everything is ok, so set the flag... | ||
| 1028 | $$flagPtr = 1 ; | ||
| 1029 | # ...and order the markers | ||
| 1030 | # There HAS to be a better way to do this, but this works | ||
| 1031 | foreach $marker (@toBeOrdered) { | ||
| 1032 | $mID = $marker->field("id") ; | ||
| 1033 | $i = 1 ; | ||
| 1034 | foreach $omePtr (@$omeListPtr) { | ||
| 1035 | $soID = $$omePtr{SeqObj}->{id} ; | ||
| 1036 | if ($mID == $soID) { | ||
| 1037 | $order{$i} = $marker ; | ||
| 1038 | next ; | ||
| 1039 | } | ||
| 1040 | $i++ ; | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | $j = 0 ; | ||
| 1044 | # The sort on the following line is where the markers are actually ordered | ||
| 1045 | foreach $i ( sort {$a <=> $b} keys(%order) ) { | ||
| 1046 | push @$omListPtr, $order{$i} ; | ||
| 1047 | $omePtr = $$omeListPtr[$j] ; | ||
| 1048 | # If the ordered markers include the last marker on the map, the distance | ||
| 1049 | # will be undefined for that marker | ||
| 1050 | if (defined $$omePtr{distance}) { | ||
| 1051 | if ($cM2ThetaFlag) { | ||
| 1052 | $dist = _cM2theta($$omePtr{distance}) ; | ||
| 1053 | } else { | ||
| 1054 | $dist = $$omePtr{distance} ; | ||
| 1055 | } | ||
| 1056 | push @$dListPtr, "$dist " ; | ||
| 1057 | } else { | ||
| 1058 | push @$dListPtr, "" ; | ||
| 1059 | } | ||
| 1060 | $j++ ; | ||
| 1061 | } | ||
| 1062 | # Add distances for the unordered markers | ||
| 1063 | $i = 0 ; | ||
| 1064 | while ($i < scalar(@$uomListPtr)) { | ||
| 1065 | push @$dListPtr, "0.1 " ; | ||
| 1066 | $i++ ; | ||
| 1067 | } | ||
| 1068 | } else { | ||
| 1069 | # There is no input map. Markers are ordered as they were input. All | ||
| 1070 | # distances are set to 0.1 | ||
| 1071 | $$flagPtr = 0 ; | ||
| 1072 | while (scalar(@$dListPtr) < $markerCount) { | ||
| 1073 | push @$dListPtr, "0.1 " ; | ||
| 1074 | } | ||
| 1075 | @$omListPtr = @$mListPtr ; | ||
| 1076 | @$uomListPtr = () ; | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | return(1) ; | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | sub _theta2cM { | ||
| 1083 | my $theta = shift ; | ||
| 1084 | my $M = 0.25 * log( ((1 + $theta*2) / (1 - $theta*2)) ) ; | ||
| 1085 | my $cM = $M * 100 ; | ||
| 1086 | return sprintf("%.2f", $cM); | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | sub _cM2theta { | ||
| 1090 | my $cM = shift ; | ||
| 1091 | my $M = $cM / 100 ; | ||
| 1092 | my $e4x = 2.7182818285 ** (4*$M) ; | ||
| 1093 | my $theta = 0.5 * (($e4x -1) / ($e4x + 1)) ; | ||
| 1094 | return sprintf("%.4f", $theta); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | 1; |