cluster.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600
  1. /* The C clustering library.
  2. * Copyright (C) 2002 Michiel Jan Laurens de Hoon.
  3. *
  4. * This library was written at the Laboratory of DNA Information Analysis,
  5. * Human Genome Center, Institute of Medical Science, University of Tokyo,
  6. * 4-6-1 Shirokanedai, Minato-ku, Tokyo 108-8639, Japan.
  7. * Contact: mdehoon 'AT' gsc.riken.jp
  8. *
  9. * Permission to use, copy, modify, and distribute this software and its
  10. * documentation with or without modifications and for any purpose and
  11. * without fee is hereby granted, provided that any copyright notices
  12. * appear in all copies and that both those copyright notices and this
  13. * permission notice appear in supporting documentation, and that the
  14. * names of the contributors or copyright holders not be used in
  15. * advertising or publicity pertaining to distribution of the software
  16. * without specific prior permission.
  17. *
  18. * THE CONTRIBUTORS AND COPYRIGHT HOLDERS OF THIS SOFTWARE DISCLAIM ALL
  19. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
  21. * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT
  22. * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  23. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  24. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  25. * OR PERFORMANCE OF THIS SOFTWARE.
  26. *
  27. */
  28. #include <time.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31. #include <float.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. #include "cluster.h"
  35. #ifdef WINDOWS
  36. # include <windows.h>
  37. #endif
  38. /* ************************************************************************ */
  39. #ifdef WINDOWS
  40. /* Then we make a Windows DLL */
  41. int WINAPI
  42. clusterdll_init (HANDLE h, DWORD reason, void* foo)
  43. {
  44. return 1;
  45. }
  46. #endif
  47. /* ************************************************************************ */
  48. double mean(int n, double x[])
  49. { double result = 0.;
  50. int i;
  51. for (i = 0; i < n; i++) result += x[i];
  52. result /= n;
  53. return result;
  54. }
  55. /* ************************************************************************ */
  56. double median (int n, double x[])
  57. /*
  58. Find the median of X(1), ... , X(N), using as much of the quicksort
  59. algorithm as is needed to isolate it.
  60. N.B. On exit, the array X is partially ordered.
  61. Based on Alan J. Miller's median.f90 routine.
  62. */
  63. { int i, j;
  64. int nr = n / 2;
  65. int nl = nr - 1;
  66. int even = 0;
  67. /* hi & lo are position limits encompassing the median. */
  68. int lo = 0;
  69. int hi = n-1;
  70. if (n==2*nr) even = 1;
  71. if (n<3)
  72. { if (n<1) return 0.;
  73. if (n == 1) return x[0];
  74. return 0.5*(x[0]+x[1]);
  75. }
  76. /* Find median of 1st, middle & last values. */
  77. do
  78. { int loop;
  79. int mid = (lo + hi)/2;
  80. double result = x[mid];
  81. double xlo = x[lo];
  82. double xhi = x[hi];
  83. if (xhi<xlo)
  84. { double temp = xlo;
  85. xlo = xhi;
  86. xhi = temp;
  87. }
  88. if (result>xhi) result = xhi;
  89. else if (result<xlo) result = xlo;
  90. /* The basic quicksort algorithm to move all values <= the sort key (XMED)
  91. * to the left-hand end, and all higher values to the other end.
  92. */
  93. i = lo;
  94. j = hi;
  95. do
  96. { while (x[i]<result) i++;
  97. while (x[j]>result) j--;
  98. loop = 0;
  99. if (i<j)
  100. { double temp = x[i];
  101. x[i] = x[j];
  102. x[j] = temp;
  103. i++;
  104. j--;
  105. if (i<=j) loop = 1;
  106. }
  107. } while (loop); /* Decide which half the median is in. */
  108. if (even)
  109. { if (j==nl && i==nr)
  110. /* Special case, n even, j = n/2 & i = j + 1, so the median is
  111. * between the two halves of the series. Find max. of the first
  112. * half & min. of the second half, then average.
  113. */
  114. { int k;
  115. double xmax = x[0];
  116. double xmin = x[n-1];
  117. for (k = lo; k <= j; k++) xmax = max(xmax,x[k]);
  118. for (k = i; k <= hi; k++) xmin = min(xmin,x[k]);
  119. return 0.5*(xmin + xmax);
  120. }
  121. if (j<nl) lo = i;
  122. if (i>nr) hi = j;
  123. if (i==j)
  124. { if (i==nl) lo = nl;
  125. if (j==nr) hi = nr;
  126. }
  127. }
  128. else
  129. { if (j<nr) lo = i;
  130. if (i>nr) hi = j;
  131. /* Test whether median has been isolated. */
  132. if (i==j && i==nr) return result;
  133. }
  134. }
  135. while (lo<hi-1);
  136. if (even) return (0.5*(x[nl]+x[nr]));
  137. if (x[lo]>x[hi])
  138. { double temp = x[lo];
  139. x[lo] = x[hi];
  140. x[hi] = temp;
  141. }
  142. return x[nr];
  143. }
  144. /* ********************************************************************** */
  145. static const double* sortdata = NULL; /* used in the quicksort algorithm */
  146. /* ---------------------------------------------------------------------- */
  147. static
  148. int compare(const void* a, const void* b)
  149. /* Helper function for sort. Previously, this was a nested function under
  150. * sort, which is not allowed under ANSI C.
  151. */
  152. { const int i1 = *(const int*)a;
  153. const int i2 = *(const int*)b;
  154. const double term1 = sortdata[i1];
  155. const double term2 = sortdata[i2];
  156. if (term1 < term2) return -1;
  157. if (term1 > term2) return +1;
  158. return 0;
  159. }
  160. /* ---------------------------------------------------------------------- */
  161. void sort(int n, const double data[], int index[])
  162. /* Sets up an index table given the data, such that data[index[]] is in
  163. * increasing order. Sorting is done on the indices; the array data
  164. * is unchanged.
  165. */
  166. { int i;
  167. sortdata = data;
  168. for (i = 0; i < n; i++) index[i] = i;
  169. qsort(index, n, sizeof(int), compare);
  170. }
  171. /* ********************************************************************** */
  172. static double* getrank (int n, double data[])
  173. /* Calculates the ranks of the elements in the array data. Two elements with
  174. * the same value get the same rank, equal to the average of the ranks had the
  175. * elements different values. The ranks are returned as a newly allocated
  176. * array that should be freed by the calling routine. If getrank fails due to
  177. * a memory allocation error, it returns NULL.
  178. */
  179. { int i;
  180. double* rank;
  181. int* index;
  182. rank = malloc(n*sizeof(double));
  183. if (!rank) return NULL;
  184. index = malloc(n*sizeof(int));
  185. if (!index)
  186. { free(rank);
  187. return NULL;
  188. }
  189. /* Call sort to get an index table */
  190. sort (n, data, index);
  191. /* Build a rank table */
  192. for (i = 0; i < n; i++) rank[index[i]] = i;
  193. /* Fix for equal ranks */
  194. i = 0;
  195. while (i < n)
  196. { int m;
  197. double value = data[index[i]];
  198. int j = i + 1;
  199. while (j < n && data[index[j]] == value) j++;
  200. m = j - i; /* number of equal ranks found */
  201. value = rank[index[i]] + (m-1)/2.;
  202. for (j = i; j < i + m; j++) rank[index[j]] = value;
  203. i += m;
  204. }
  205. free (index);
  206. return rank;
  207. }
  208. /* ---------------------------------------------------------------------- */
  209. static int
  210. makedatamask(int nrows, int ncols, double*** pdata, int*** pmask)
  211. { int i;
  212. double** data;
  213. int** mask;
  214. data = malloc(nrows*sizeof(double*));
  215. if(!data) return 0;
  216. mask = malloc(nrows*sizeof(int*));
  217. if(!mask)
  218. { free(data);
  219. return 0;
  220. }
  221. for (i = 0; i < nrows; i++)
  222. { data[i] = malloc(ncols*sizeof(double));
  223. if(!data[i]) break;
  224. mask[i] = malloc(ncols*sizeof(int));
  225. if(!mask[i])
  226. { free(data[i]);
  227. break;
  228. }
  229. }
  230. if (i==nrows) /* break not encountered */
  231. { *pdata = data;
  232. *pmask = mask;
  233. return 1;
  234. }
  235. *pdata = NULL;
  236. *pmask = NULL;
  237. nrows = i;
  238. for (i = 0; i < nrows; i++)
  239. { free(data[i]);
  240. free(mask[i]);
  241. }
  242. free(data);
  243. free(mask);
  244. return 0;
  245. }
  246. /* ---------------------------------------------------------------------- */
  247. static void
  248. freedatamask(int n, double** data, int** mask)
  249. { int i;
  250. for (i = 0; i < n; i++)
  251. { free(mask[i]);
  252. free(data[i]);
  253. }
  254. free(mask);
  255. free(data);
  256. }
  257. /* ---------------------------------------------------------------------- */
  258. static
  259. double find_closest_pair(int n, double** distmatrix, int* ip, int* jp)
  260. /*
  261. This function searches the distance matrix to find the pair with the shortest
  262. distance between them. The indices of the pair are returned in ip and jp; the
  263. distance itself is returned by the function.
  264. n (input) int
  265. The number of elements in the distance matrix.
  266. distmatrix (input) double**
  267. A ragged array containing the distance matrix. The number of columns in each
  268. row is one less than the row index.
  269. ip (output) int*
  270. A pointer to the integer that is to receive the first index of the pair with
  271. the shortest distance.
  272. jp (output) int*
  273. A pointer to the integer that is to receive the second index of the pair with
  274. the shortest distance.
  275. */
  276. { int i, j;
  277. double temp;
  278. double distance = distmatrix[1][0];
  279. *ip = 1;
  280. *jp = 0;
  281. for (i = 1; i < n; i++)
  282. { for (j = 0; j < i; j++)
  283. { temp = distmatrix[i][j];
  284. if (temp<distance)
  285. { distance = temp;
  286. *ip = i;
  287. *jp = j;
  288. }
  289. }
  290. }
  291. return distance;
  292. }
  293. /* ********************************************************************* */
  294. static int svd(int m, int n, double** u, double w[], double** vt)
  295. /*
  296. * This subroutine is a translation of the Algol procedure svd,
  297. * Num. Math. 14, 403-420(1970) by Golub and Reinsch.
  298. * Handbook for Auto. Comp., Vol II-Linear Algebra, 134-151(1971).
  299. *
  300. * This subroutine determines the singular value decomposition
  301. * t
  302. * A=usv of a real m by n rectangular matrix, where m is greater
  303. * than or equal to n. Householder bidiagonalization and a variant
  304. * of the QR algorithm are used.
  305. *
  306. *
  307. * On input.
  308. *
  309. * m is the number of rows of A (and u).
  310. *
  311. * n is the number of columns of A (and u) and the order of v.
  312. *
  313. * u contains the rectangular input matrix A to be decomposed.
  314. *
  315. * On output.
  316. *
  317. * the routine returns an integer ierr equal to
  318. * 0 to indicate a normal return,
  319. * k if the k-th singular value has not been
  320. * determined after 30 iterations,
  321. * -1 if memory allocation fails.
  322. *
  323. *
  324. * w contains the n (non-negative) singular values of a (the
  325. * diagonal elements of s). they are unordered. if an
  326. * error exit is made, the singular values should be correct
  327. * for indices ierr+1,ierr+2,...,n.
  328. *
  329. *
  330. * u contains the matrix u (orthogonal column vectors) of the
  331. * decomposition.
  332. * if an error exit is made, the columns of u corresponding
  333. * to indices of correct singular values should be correct.
  334. *
  335. * t
  336. * vt contains the matrix v (orthogonal) of the decomposition.
  337. * if an error exit is made, the columns of v corresponding
  338. * to indices of correct singular values should be correct.
  339. *
  340. *
  341. * Questions and comments should be directed to B. S. Garbow,
  342. * Applied Mathematics division, Argonne National Laboratory
  343. *
  344. * Modified to eliminate machep
  345. *
  346. * Translated to C by Michiel de Hoon, Human Genome Center,
  347. * University of Tokyo, for inclusion in the C Clustering Library.
  348. * This routine is less general than the original svd routine, as
  349. * it focuses on the singular value decomposition as needed for
  350. * clustering. In particular,
  351. * - We calculate both u and v in all cases
  352. * - We pass the input array A via u; this array is subsequently
  353. * overwritten.
  354. * - We allocate for the array rv1, used as a working space,
  355. * internally in this routine, instead of passing it as an
  356. * argument. If the allocation fails, svd returns -1.
  357. * 2003.06.05
  358. */
  359. { int i, j, k, i1, k1, l1, its;
  360. double c,f,h,s,x,y,z;
  361. int l = 0;
  362. int ierr = 0;
  363. double g = 0.0;
  364. double scale = 0.0;
  365. double anorm = 0.0;
  366. double* rv1 = malloc(n*sizeof(double));
  367. if (!rv1) return -1;
  368. if (m >= n)
  369. { /* Householder reduction to bidiagonal form */
  370. for (i = 0; i < n; i++)
  371. { l = i + 1;
  372. rv1[i] = scale * g;
  373. g = 0.0;
  374. s = 0.0;
  375. scale = 0.0;
  376. for (k = i; k < m; k++) scale += fabs(u[k][i]);
  377. if (scale != 0.0)
  378. { for (k = i; k < m; k++)
  379. { u[k][i] /= scale;
  380. s += u[k][i]*u[k][i];
  381. }
  382. f = u[i][i];
  383. g = (f >= 0) ? -sqrt(s) : sqrt(s);
  384. h = f * g - s;
  385. u[i][i] = f - g;
  386. if (i < n-1)
  387. { for (j = l; j < n; j++)
  388. { s = 0.0;
  389. for (k = i; k < m; k++) s += u[k][i] * u[k][j];
  390. f = s / h;
  391. for (k = i; k < m; k++) u[k][j] += f * u[k][i];
  392. }
  393. }
  394. for (k = i; k < m; k++) u[k][i] *= scale;
  395. }
  396. w[i] = scale * g;
  397. g = 0.0;
  398. s = 0.0;
  399. scale = 0.0;
  400. if (i<n-1)
  401. { for (k = l; k < n; k++) scale += fabs(u[i][k]);
  402. if (scale != 0.0)
  403. { for (k = l; k < n; k++)
  404. { u[i][k] /= scale;
  405. s += u[i][k] * u[i][k];
  406. }
  407. f = u[i][l];
  408. g = (f >= 0) ? -sqrt(s) : sqrt(s);
  409. h = f * g - s;
  410. u[i][l] = f - g;
  411. for (k = l; k < n; k++) rv1[k] = u[i][k] / h;
  412. for (j = l; j < m; j++)
  413. { s = 0.0;
  414. for (k = l; k < n; k++) s += u[j][k] * u[i][k];
  415. for (k = l; k < n; k++) u[j][k] += s * rv1[k];
  416. }
  417. for (k = l; k < n; k++) u[i][k] *= scale;
  418. }
  419. }
  420. anorm = max(anorm,fabs(w[i])+fabs(rv1[i]));
  421. }
  422. /* accumulation of right-hand transformations */
  423. for (i = n-1; i>=0; i--)
  424. { if (i < n-1)
  425. { if (g != 0.0)
  426. { for (j = l; j < n; j++) vt[i][j] = (u[i][j] / u[i][l]) / g;
  427. /* double division avoids possible underflow */
  428. for (j = l; j < n; j++)
  429. { s = 0.0;
  430. for (k = l; k < n; k++) s += u[i][k] * vt[j][k];
  431. for (k = l; k < n; k++) vt[j][k] += s * vt[i][k];
  432. }
  433. }
  434. }
  435. for (j = l; j < n; j++)
  436. { vt[j][i] = 0.0;
  437. vt[i][j] = 0.0;
  438. }
  439. vt[i][i] = 1.0;
  440. g = rv1[i];
  441. l = i;
  442. }
  443. /* accumulation of left-hand transformations */
  444. for (i = n-1; i >= 0; i--)
  445. { l = i + 1;
  446. g = w[i];
  447. if (i!=n-1)
  448. for (j = l; j < n; j++) u[i][j] = 0.0;
  449. if (g!=0.0)
  450. { if (i!=n-1)
  451. { for (j = l; j < n; j++)
  452. { s = 0.0;
  453. for (k = l; k < m; k++) s += u[k][i] * u[k][j];
  454. /* double division avoids possible underflow */
  455. f = (s / u[i][i]) / g;
  456. for (k = i; k < m; k++) u[k][j] += f * u[k][i];
  457. }
  458. }
  459. for (j = i; j < m; j++) u[j][i] /= g;
  460. }
  461. else
  462. for (j = i; j < m; j++) u[j][i] = 0.0;
  463. u[i][i] += 1.0;
  464. }
  465. /* diagonalization of the bidiagonal form */
  466. for (k = n-1; k >= 0; k--)
  467. { k1 = k-1;
  468. its = 0;
  469. while(1)
  470. /* test for splitting */
  471. { for (l = k; l >= 0; l--)
  472. { l1 = l-1;
  473. if (fabs(rv1[l]) + anorm == anorm) break;
  474. /* rv1[0] is always zero, so there is no exit
  475. * through the bottom of the loop */
  476. if (fabs(w[l1]) + anorm == anorm)
  477. /* cancellation of rv1[l] if l greater than 0 */
  478. { c = 0.0;
  479. s = 1.0;
  480. for (i = l; i <= k; i++)
  481. { f = s * rv1[i];
  482. rv1[i] *= c;
  483. if (fabs(f) + anorm == anorm) break;
  484. g = w[i];
  485. h = sqrt(f*f+g*g);
  486. w[i] = h;
  487. c = g / h;
  488. s = -f / h;
  489. for (j = 0; j < m; j++)
  490. { y = u[j][l1];
  491. z = u[j][i];
  492. u[j][l1] = y * c + z * s;
  493. u[j][i] = -y * s + z * c;
  494. }
  495. }
  496. break;
  497. }
  498. }
  499. /* test for convergence */
  500. z = w[k];
  501. if (l==k) /* convergence */
  502. { if (z < 0.0)
  503. /* w[k] is made non-negative */
  504. { w[k] = -z;
  505. for (j = 0; j < n; j++) vt[k][j] = -vt[k][j];
  506. }
  507. break;
  508. }
  509. else if (its==30)
  510. { ierr = k;
  511. break;
  512. }
  513. else
  514. /* shift from bottom 2 by 2 minor */
  515. { its++;
  516. x = w[l];
  517. y = w[k1];
  518. g = rv1[k1];
  519. h = rv1[k];
  520. f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
  521. g = sqrt(f*f+1.0);
  522. f = ((x - z) * (x + z) + h * (y / (f + (f >= 0 ? g : -g)) - h)) / x;
  523. /* next qr transformation */
  524. c = 1.0;
  525. s = 1.0;
  526. for (i1 = l; i1 <= k1; i1++)
  527. { i = i1 + 1;
  528. g = rv1[i];
  529. y = w[i];
  530. h = s * g;
  531. g = c * g;
  532. z = sqrt(f*f+h*h);
  533. rv1[i1] = z;
  534. c = f / z;
  535. s = h / z;
  536. f = x * c + g * s;
  537. g = -x * s + g * c;
  538. h = y * s;
  539. y = y * c;
  540. for (j = 0; j < n; j++)
  541. { x = vt[i1][j];
  542. z = vt[i][j];
  543. vt[i1][j] = x * c + z * s;
  544. vt[i][j] = -x * s + z * c;
  545. }
  546. z = sqrt(f*f+h*h);
  547. w[i1] = z;
  548. /* rotation can be arbitrary if z is zero */
  549. if (z!=0.0)
  550. { c = f / z;
  551. s = h / z;
  552. }
  553. f = c * g + s * y;
  554. x = -s * g + c * y;
  555. for (j = 0; j < m; j++)
  556. { y = u[j][i1];
  557. z = u[j][i];
  558. u[j][i1] = y * c + z * s;
  559. u[j][i] = -y * s + z * c;
  560. }
  561. }
  562. rv1[l] = 0.0;
  563. rv1[k] = f;
  564. w[k] = x;
  565. }
  566. }
  567. }
  568. }
  569. else /* m < n */
  570. { /* Householder reduction to bidiagonal form */
  571. for (i = 0; i < m; i++)
  572. { l = i + 1;
  573. rv1[i] = scale * g;
  574. g = 0.0;
  575. s = 0.0;
  576. scale = 0.0;
  577. for (k = i; k < n; k++) scale += fabs(u[i][k]);
  578. if (scale != 0.0)
  579. { for (k = i; k < n; k++)
  580. { u[i][k] /= scale;
  581. s += u[i][k]*u[i][k];
  582. }
  583. f = u[i][i];
  584. g = (f >= 0) ? -sqrt(s) : sqrt(s);
  585. h = f * g - s;
  586. u[i][i] = f - g;
  587. if (i < m-1)
  588. { for (j = l; j < m; j++)
  589. { s = 0.0;
  590. for (k = i; k < n; k++) s += u[i][k] * u[j][k];
  591. f = s / h;
  592. for (k = i; k < n; k++) u[j][k] += f * u[i][k];
  593. }
  594. }
  595. for (k = i; k < n; k++) u[i][k] *= scale;
  596. }
  597. w[i] = scale * g;
  598. g = 0.0;
  599. s = 0.0;
  600. scale = 0.0;
  601. if (i<m-1)
  602. { for (k = l; k < m; k++) scale += fabs(u[k][i]);
  603. if (scale != 0.0)
  604. { for (k = l; k < m; k++)
  605. { u[k][i] /= scale;
  606. s += u[k][i] * u[k][i];
  607. }
  608. f = u[l][i];
  609. g = (f >= 0) ? -sqrt(s) : sqrt(s);
  610. h = f * g - s;
  611. u[l][i] = f - g;
  612. for (k = l; k < m; k++) rv1[k] = u[k][i] / h;
  613. for (j = l; j < n; j++)
  614. { s = 0.0;
  615. for (k = l; k < m; k++) s += u[k][j] * u[k][i];
  616. for (k = l; k < m; k++) u[k][j] += s * rv1[k];
  617. }
  618. for (k = l; k < m; k++) u[k][i] *= scale;
  619. }
  620. }
  621. anorm = max(anorm,fabs(w[i])+fabs(rv1[i]));
  622. }
  623. /* accumulation of right-hand transformations */
  624. for (i = m-1; i>=0; i--)
  625. { if (i < m-1)
  626. { if (g != 0.0)
  627. { for (j = l; j < m; j++) vt[j][i] = (u[j][i] / u[l][i]) / g;
  628. /* double division avoids possible underflow */
  629. for (j = l; j < m; j++)
  630. { s = 0.0;
  631. for (k = l; k < m; k++) s += u[k][i] * vt[k][j];
  632. for (k = l; k < m; k++) vt[k][j] += s * vt[k][i];
  633. }
  634. }
  635. }
  636. for (j = l; j < m; j++)
  637. { vt[i][j] = 0.0;
  638. vt[j][i] = 0.0;
  639. }
  640. vt[i][i] = 1.0;
  641. g = rv1[i];
  642. l = i;
  643. }
  644. /* accumulation of left-hand transformations */
  645. for (i = m-1; i >= 0; i--)
  646. { l = i + 1;
  647. g = w[i];
  648. if (i!=m-1)
  649. for (j = l; j < m; j++) u[j][i] = 0.0;
  650. if (g!=0.0)
  651. { if (i!=m-1)
  652. { for (j = l; j < m; j++)
  653. { s = 0.0;
  654. for (k = l; k < n; k++) s += u[i][k] * u[j][k];
  655. /* double division avoids possible underflow */
  656. f = (s / u[i][i]) / g;
  657. for (k = i; k < n; k++) u[j][k] += f * u[i][k];
  658. }
  659. }
  660. for (j = i; j < n; j++) u[i][j] /= g;
  661. }
  662. else
  663. for (j = i; j < n; j++) u[i][j] = 0.0;
  664. u[i][i] += 1.0;
  665. }
  666. /* diagonalization of the bidiagonal form */
  667. for (k = m-1; k >= 0; k--)
  668. { k1 = k-1;
  669. its = 0;
  670. while(1)
  671. /* test for splitting */
  672. { for (l = k; l >= 0; l--)
  673. { l1 = l-1;
  674. if (fabs(rv1[l]) + anorm == anorm) break;
  675. /* rv1[0] is always zero, so there is no exit
  676. * through the bottom of the loop */
  677. if (fabs(w[l1]) + anorm == anorm)
  678. /* cancellation of rv1[l] if l greater than 0 */
  679. { c = 0.0;
  680. s = 1.0;
  681. for (i = l; i <= k; i++)
  682. { f = s * rv1[i];
  683. rv1[i] *= c;
  684. if (fabs(f) + anorm == anorm) break;
  685. g = w[i];
  686. h = sqrt(f*f+g*g);
  687. w[i] = h;
  688. c = g / h;
  689. s = -f / h;
  690. for (j = 0; j < n; j++)
  691. { y = u[l1][j];
  692. z = u[i][j];
  693. u[l1][j] = y * c + z * s;
  694. u[i][j] = -y * s + z * c;
  695. }
  696. }
  697. break;
  698. }
  699. }
  700. /* test for convergence */
  701. z = w[k];
  702. if (l==k) /* convergence */
  703. { if (z < 0.0)
  704. /* w[k] is made non-negative */
  705. { w[k] = -z;
  706. for (j = 0; j < m; j++) vt[j][k] = -vt[j][k];
  707. }
  708. break;
  709. }
  710. else if (its==30)
  711. { ierr = k;
  712. break;
  713. }
  714. else
  715. /* shift from bottom 2 by 2 minor */
  716. { its++;
  717. x = w[l];
  718. y = w[k1];
  719. g = rv1[k1];
  720. h = rv1[k];
  721. f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
  722. g = sqrt(f*f+1.0);
  723. f = ((x - z) * (x + z) + h * (y / (f + (f >= 0 ? g : -g)) - h)) / x;
  724. /* next qr transformation */
  725. c = 1.0;
  726. s = 1.0;
  727. for (i1 = l; i1 <= k1; i1++)
  728. { i = i1 + 1;
  729. g = rv1[i];
  730. y = w[i];
  731. h = s * g;
  732. g = c * g;
  733. z = sqrt(f*f+h*h);
  734. rv1[i1] = z;
  735. c = f / z;
  736. s = h / z;
  737. f = x * c + g * s;
  738. g = -x * s + g * c;
  739. h = y * s;
  740. y = y * c;
  741. for (j = 0; j < m; j++)
  742. { x = vt[j][i1];
  743. z = vt[j][i];
  744. vt[j][i1] = x * c + z * s;
  745. vt[j][i] = -x * s + z * c;
  746. }
  747. z = sqrt(f*f+h*h);
  748. w[i1] = z;
  749. /* rotation can be arbitrary if z is zero */
  750. if (z!=0.0)
  751. { c = f / z;
  752. s = h / z;
  753. }
  754. f = c * g + s * y;
  755. x = -s * g + c * y;
  756. for (j = 0; j < n; j++)
  757. { y = u[i1][j];
  758. z = u[i][j];
  759. u[i1][j] = y * c + z * s;
  760. u[i][j] = -y * s + z * c;
  761. }
  762. }
  763. rv1[l] = 0.0;
  764. rv1[k] = f;
  765. w[k] = x;
  766. }
  767. }
  768. }
  769. }
  770. free(rv1);
  771. return ierr;
  772. }
  773. /* ********************************************************************* */
  774. int pca(int nrows, int ncolumns, double** u, double** v, double* w)
  775. /*
  776. Purpose
  777. =======
  778. This subroutine uses the singular value decomposition to perform principal
  779. components analysis of a real nrows by ncolumns rectangular matrix.
  780. Arguments
  781. =========
  782. nrows (input) int
  783. The number of rows in the matrix u.
  784. ncolumns (input) int
  785. The number of columns in the matrix v.
  786. u (input) double[nrows][ncolumns]
  787. On input, the array containing the data to which the principal component
  788. analysis should be applied. The function assumes that the mean has already been
  789. subtracted of each column, and hence that the mean of each column is zero.
  790. On output, see below.
  791. v (input) double[n][n], where n = min(nrows, ncolumns)
  792. Not used on input.
  793. w (input) double[n], where n = min(nrows, ncolumns)
  794. Not used on input.
  795. Return value
  796. ============
  797. On output:
  798. If nrows >= ncolumns, then
  799. u contains the coordinates with respect to the principal components;
  800. v contains the principal component vectors.
  801. The dot product u . v reproduces the data that were passed in u.
  802. If nrows < ncolumns, then
  803. u contains the principal component vectors;
  804. v contains the coordinates with respect to the principal components.
  805. The dot product v . u reproduces the data that were passed in u.
  806. The eigenvalues of the covariance matrix are returned in w.
  807. The arrays u, v, and w are sorted according to eigenvalue, with the largest
  808. eigenvalues appearing first.
  809. The function returns 0 if successful, -1 if memory allocation fails, and a
  810. positive integer if the singular value decomposition fails to converge.
  811. */
  812. {
  813. int i;
  814. int j;
  815. int error;
  816. int* index = malloc(ncolumns*sizeof(int));
  817. double* temp = malloc(ncolumns*sizeof(double));
  818. if (!index || !temp)
  819. { if (index) free(index);
  820. if (temp) free(temp);
  821. return -1;
  822. }
  823. error = svd(nrows, ncolumns, u, w, v);
  824. if (error==0)
  825. {
  826. if (nrows >= ncolumns)
  827. { for (j = 0; j < ncolumns; j++)
  828. { const double s = w[j];
  829. for (i = 0; i < nrows; i++) u[i][j] *= s;
  830. }
  831. sort(ncolumns, w, index);
  832. for (i = 0; i < ncolumns/2; i++)
  833. { j = index[i];
  834. index[i] = index[ncolumns-1-i];
  835. index[ncolumns-1-i] = j;
  836. }
  837. for (i = 0; i < nrows; i++)
  838. { for (j = 0; j < ncolumns; j++) temp[j] = u[i][index[j]];
  839. for (j = 0; j < ncolumns; j++) u[i][j] = temp[j];
  840. }
  841. for (i = 0; i < ncolumns; i++)
  842. { for (j = 0; j < ncolumns; j++) temp[j] = v[index[j]][i];
  843. for (j = 0; j < ncolumns; j++) v[j][i] = temp[j];
  844. }
  845. for (i = 0; i < ncolumns; i++) temp[i] = w[index[i]];
  846. for (i = 0; i < ncolumns; i++) w[i] = temp[i];
  847. }
  848. else /* nrows < ncolumns */
  849. { for (j = 0; j < nrows; j++)
  850. { const double s = w[j];
  851. for (i = 0; i < nrows; i++) v[i][j] *= s;
  852. }
  853. sort(nrows, w, index);
  854. for (i = 0; i < nrows/2; i++)
  855. { j = index[i];
  856. index[i] = index[nrows-1-i];
  857. index[nrows-1-i] = j;
  858. }
  859. for (j = 0; j < ncolumns; j++)
  860. { for (i = 0; i < nrows; i++) temp[i] = u[index[i]][j];
  861. for (i = 0; i < nrows; i++) u[i][j] = temp[i];
  862. }
  863. for (j = 0; j < nrows; j++)
  864. { for (i = 0; i < nrows; i++) temp[i] = v[j][index[i]];
  865. for (i = 0; i < nrows; i++) v[j][i] = temp[i];
  866. }
  867. for (i = 0; i < nrows; i++) temp[i] = w[index[i]];
  868. for (i = 0; i < nrows; i++) w[i] = temp[i];
  869. }
  870. }
  871. free(index);
  872. free(temp);
  873. return error;
  874. }
  875. /* ********************************************************************* */
  876. static
  877. double euclid (int n, double** data1, double** data2, int** mask1, int** mask2,
  878. const double weight[], int index1, int index2, int transpose)
  879. /*
  880. Purpose
  881. =======
  882. The euclid routine calculates the weighted Euclidean distance between two
  883. rows or columns in a matrix.
  884. Arguments
  885. =========
  886. n (input) int
  887. The number of elements in a row or column. If transpose==0, then n is the number
  888. of columns; otherwise, n is the number of rows.
  889. data1 (input) double array
  890. The data array containing the first vector.
  891. data2 (input) double array
  892. The data array containing the second vector.
  893. mask1 (input) int array
  894. This array which elements in data1 are missing. If mask1[i][j]==0, then
  895. data1[i][j] is missing.
  896. mask2 (input) int array
  897. This array which elements in data2 are missing. If mask2[i][j]==0, then
  898. data2[i][j] is missing.
  899. weight (input) double[n]
  900. The weights that are used to calculate the distance.
  901. index1 (input) int
  902. Index of the first row or column.
  903. index2 (input) int
  904. Index of the second row or column.
  905. transpose (input) int
  906. If transpose==0, the distance between two rows in the matrix is calculated.
  907. Otherwise, the distance between two columns in the matrix is calculated.
  908. ============================================================================
  909. */
  910. { double result = 0.;
  911. double tweight = 0;
  912. int i;
  913. if (transpose==0) /* Calculate the distance between two rows */
  914. { for (i = 0; i < n; i++)
  915. { if (mask1[index1][i] && mask2[index2][i])
  916. { double term = data1[index1][i] - data2[index2][i];
  917. result += weight[i]*term*term;
  918. tweight += weight[i];
  919. }
  920. }
  921. }
  922. else
  923. { for (i = 0; i < n; i++)
  924. { if (mask1[i][index1] && mask2[i][index2])
  925. { double term = data1[i][index1] - data2[i][index2];
  926. result += weight[i]*term*term;
  927. tweight += weight[i];
  928. }
  929. }
  930. }
  931. if (!tweight) return 0; /* usually due to empty clusters */
  932. result /= tweight;
  933. return result;
  934. }
  935. /* ********************************************************************* */
  936. static
  937. double cityblock (int n, double** data1, double** data2, int** mask1,
  938. int** mask2, const double weight[], int index1, int index2, int transpose)
  939. /*
  940. Purpose
  941. =======
  942. The cityblock routine calculates the weighted "City Block" distance between
  943. two rows or columns in a matrix. City Block distance is defined as the
  944. absolute value of X1-X2 plus the absolute value of Y1-Y2 plus..., which is
  945. equivalent to taking an "up and over" path.
  946. Arguments
  947. =========
  948. n (input) int
  949. The number of elements in a row or column. If transpose==0, then n is the number
  950. of columns; otherwise, n is the number of rows.
  951. data1 (input) double array
  952. The data array containing the first vector.
  953. data2 (input) double array
  954. The data array containing the second vector.
  955. mask1 (input) int array
  956. This array which elements in data1 are missing. If mask1[i][j]==0, then
  957. data1[i][j] is missing.
  958. mask2 (input) int array
  959. This array which elements in data2 are missing. If mask2[i][j]==0, then
  960. data2[i][j] is missing.
  961. weight (input) double[n]
  962. The weights that are used to calculate the distance.
  963. index1 (input) int
  964. Index of the first row or column.
  965. index2 (input) int
  966. Index of the second row or column.
  967. transpose (input) int
  968. If transpose==0, the distance between two rows in the matrix is calculated.
  969. Otherwise, the distance between two columns in the matrix is calculated.
  970. ============================================================================ */
  971. { double result = 0.;
  972. double tweight = 0;
  973. int i;
  974. if (transpose==0) /* Calculate the distance between two rows */
  975. { for (i = 0; i < n; i++)
  976. { if (mask1[index1][i] && mask2[index2][i])
  977. { double term = data1[index1][i] - data2[index2][i];
  978. result = result + weight[i]*fabs(term);
  979. tweight += weight[i];
  980. }
  981. }
  982. }
  983. else
  984. { for (i = 0; i < n; i++)
  985. { if (mask1[i][index1] && mask2[i][index2])
  986. { double term = data1[i][index1] - data2[i][index2];
  987. result = result + weight[i]*fabs(term);
  988. tweight += weight[i];
  989. }
  990. }
  991. }
  992. if (!tweight) return 0; /* usually due to empty clusters */
  993. result /= tweight;
  994. return result;
  995. }
  996. /* ********************************************************************* */
  997. static
  998. double correlation (int n, double** data1, double** data2, int** mask1,
  999. int** mask2, const double weight[], int index1, int index2, int transpose)
  1000. /*
  1001. Purpose
  1002. =======
  1003. The correlation routine calculates the weighted Pearson distance between two
  1004. rows or columns in a matrix. We define the Pearson distance as one minus the
  1005. Pearson correlation.
  1006. This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.
  1007. but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold
  1008. (e.g., choose b = a + c).
  1009. Arguments
  1010. =========
  1011. n (input) int
  1012. The number of elements in a row or column. If transpose==0, then n is the number
  1013. of columns; otherwise, n is the number of rows.
  1014. data1 (input) double array
  1015. The data array containing the first vector.
  1016. data2 (input) double array
  1017. The data array containing the second vector.
  1018. mask1 (input) int array
  1019. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1020. data1[i][j] is missing.
  1021. mask2 (input) int array
  1022. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1023. data2[i][j] is missing.
  1024. weight (input) double[n]
  1025. The weights that are used to calculate the distance.
  1026. index1 (input) int
  1027. Index of the first row or column.
  1028. index2 (input) int
  1029. Index of the second row or column.
  1030. transpose (input) int
  1031. If transpose==0, the distance between two rows in the matrix is calculated.
  1032. Otherwise, the distance between two columns in the matrix is calculated.
  1033. ============================================================================
  1034. */
  1035. { double result = 0.;
  1036. double sum1 = 0.;
  1037. double sum2 = 0.;
  1038. double denom1 = 0.;
  1039. double denom2 = 0.;
  1040. double tweight = 0.;
  1041. if (transpose==0) /* Calculate the distance between two rows */
  1042. { int i;
  1043. for (i = 0; i < n; i++)
  1044. { if (mask1[index1][i] && mask2[index2][i])
  1045. { double term1 = data1[index1][i];
  1046. double term2 = data2[index2][i];
  1047. double w = weight[i];
  1048. sum1 += w*term1;
  1049. sum2 += w*term2;
  1050. result += w*term1*term2;
  1051. denom1 += w*term1*term1;
  1052. denom2 += w*term2*term2;
  1053. tweight += w;
  1054. }
  1055. }
  1056. }
  1057. else
  1058. { int i;
  1059. for (i = 0; i < n; i++)
  1060. { if (mask1[i][index1] && mask2[i][index2])
  1061. { double term1 = data1[i][index1];
  1062. double term2 = data2[i][index2];
  1063. double w = weight[i];
  1064. sum1 += w*term1;
  1065. sum2 += w*term2;
  1066. result += w*term1*term2;
  1067. denom1 += w*term1*term1;
  1068. denom2 += w*term2*term2;
  1069. tweight += w;
  1070. }
  1071. }
  1072. }
  1073. if (!tweight) return 0; /* usually due to empty clusters */
  1074. result -= sum1 * sum2 / tweight;
  1075. denom1 -= sum1 * sum1 / tweight;
  1076. denom2 -= sum2 * sum2 / tweight;
  1077. if (denom1 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1078. if (denom2 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1079. result = result / sqrt(denom1*denom2);
  1080. result = 1. - result;
  1081. return result;
  1082. }
  1083. /* ********************************************************************* */
  1084. static
  1085. double acorrelation (int n, double** data1, double** data2, int** mask1,
  1086. int** mask2, const double weight[], int index1, int index2, int transpose)
  1087. /*
  1088. Purpose
  1089. =======
  1090. The acorrelation routine calculates the weighted Pearson distance between two
  1091. rows or columns, using the absolute value of the correlation.
  1092. This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.
  1093. but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold
  1094. (e.g., choose b = a + c).
  1095. Arguments
  1096. =========
  1097. n (input) int
  1098. The number of elements in a row or column. If transpose==0, then n is the number
  1099. of columns; otherwise, n is the number of rows.
  1100. data1 (input) double array
  1101. The data array containing the first vector.
  1102. data2 (input) double array
  1103. The data array containing the second vector.
  1104. mask1 (input) int array
  1105. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1106. data1[i][j] is missing.
  1107. mask2 (input) int array
  1108. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1109. data2[i][j] is missing.
  1110. weight (input) double[n]
  1111. The weights that are used to calculate the distance.
  1112. index1 (input) int
  1113. Index of the first row or column.
  1114. index2 (input) int
  1115. Index of the second row or column.
  1116. transpose (input) int
  1117. If transpose==0, the distance between two rows in the matrix is calculated.
  1118. Otherwise, the distance between two columns in the matrix is calculated.
  1119. ============================================================================
  1120. */
  1121. { double result = 0.;
  1122. double sum1 = 0.;
  1123. double sum2 = 0.;
  1124. double denom1 = 0.;
  1125. double denom2 = 0.;
  1126. double tweight = 0.;
  1127. if (transpose==0) /* Calculate the distance between two rows */
  1128. { int i;
  1129. for (i = 0; i < n; i++)
  1130. { if (mask1[index1][i] && mask2[index2][i])
  1131. { double term1 = data1[index1][i];
  1132. double term2 = data2[index2][i];
  1133. double w = weight[i];
  1134. sum1 += w*term1;
  1135. sum2 += w*term2;
  1136. result += w*term1*term2;
  1137. denom1 += w*term1*term1;
  1138. denom2 += w*term2*term2;
  1139. tweight += w;
  1140. }
  1141. }
  1142. }
  1143. else
  1144. { int i;
  1145. for (i = 0; i < n; i++)
  1146. { if (mask1[i][index1] && mask2[i][index2])
  1147. { double term1 = data1[i][index1];
  1148. double term2 = data2[i][index2];
  1149. double w = weight[i];
  1150. sum1 += w*term1;
  1151. sum2 += w*term2;
  1152. result += w*term1*term2;
  1153. denom1 += w*term1*term1;
  1154. denom2 += w*term2*term2;
  1155. tweight += w;
  1156. }
  1157. }
  1158. }
  1159. if (!tweight) return 0; /* usually due to empty clusters */
  1160. result -= sum1 * sum2 / tweight;
  1161. denom1 -= sum1 * sum1 / tweight;
  1162. denom2 -= sum2 * sum2 / tweight;
  1163. if (denom1 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1164. if (denom2 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1165. result = fabs(result) / sqrt(denom1*denom2);
  1166. result = 1. - result;
  1167. return result;
  1168. }
  1169. /* ********************************************************************* */
  1170. static
  1171. double ucorrelation (int n, double** data1, double** data2, int** mask1,
  1172. int** mask2, const double weight[], int index1, int index2, int transpose)
  1173. /*
  1174. Purpose
  1175. =======
  1176. The ucorrelation routine calculates the weighted Pearson distance between two
  1177. rows or columns, using the uncentered version of the Pearson correlation. In the
  1178. uncentered Pearson correlation, a zero mean is used for both vectors even if
  1179. the actual mean is nonzero.
  1180. This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.
  1181. but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold
  1182. (e.g., choose b = a + c).
  1183. Arguments
  1184. =========
  1185. n (input) int
  1186. The number of elements in a row or column. If transpose==0, then n is the number
  1187. of columns; otherwise, n is the number of rows.
  1188. data1 (input) double array
  1189. The data array containing the first vector.
  1190. data2 (input) double array
  1191. The data array containing the second vector.
  1192. mask1 (input) int array
  1193. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1194. data1[i][j] is missing.
  1195. mask2 (input) int array
  1196. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1197. data2[i][j] is missing.
  1198. weight (input) double[n]
  1199. The weights that are used to calculate the distance.
  1200. index1 (input) int
  1201. Index of the first row or column.
  1202. index2 (input) int
  1203. Index of the second row or column.
  1204. transpose (input) int
  1205. If transpose==0, the distance between two rows in the matrix is calculated.
  1206. Otherwise, the distance between two columns in the matrix is calculated.
  1207. ============================================================================
  1208. */
  1209. { double result = 0.;
  1210. double denom1 = 0.;
  1211. double denom2 = 0.;
  1212. int flag = 0;
  1213. /* flag will remain zero if no nonzero combinations of mask1 and mask2 are
  1214. * found.
  1215. */
  1216. if (transpose==0) /* Calculate the distance between two rows */
  1217. { int i;
  1218. for (i = 0; i < n; i++)
  1219. { if (mask1[index1][i] && mask2[index2][i])
  1220. { double term1 = data1[index1][i];
  1221. double term2 = data2[index2][i];
  1222. double w = weight[i];
  1223. result += w*term1*term2;
  1224. denom1 += w*term1*term1;
  1225. denom2 += w*term2*term2;
  1226. flag = 1;
  1227. }
  1228. }
  1229. }
  1230. else
  1231. { int i;
  1232. for (i = 0; i < n; i++)
  1233. { if (mask1[i][index1] && mask2[i][index2])
  1234. { double term1 = data1[i][index1];
  1235. double term2 = data2[i][index2];
  1236. double w = weight[i];
  1237. result += w*term1*term2;
  1238. denom1 += w*term1*term1;
  1239. denom2 += w*term2*term2;
  1240. flag = 1;
  1241. }
  1242. }
  1243. }
  1244. if (!flag) return 0.;
  1245. if (denom1==0.) return 1.;
  1246. if (denom2==0.) return 1.;
  1247. result = result / sqrt(denom1*denom2);
  1248. result = 1. - result;
  1249. return result;
  1250. }
  1251. /* ********************************************************************* */
  1252. static
  1253. double uacorrelation (int n, double** data1, double** data2, int** mask1,
  1254. int** mask2, const double weight[], int index1, int index2, int transpose)
  1255. /*
  1256. Purpose
  1257. =======
  1258. The uacorrelation routine calculates the weighted Pearson distance between two
  1259. rows or columns, using the absolute value of the uncentered version of the
  1260. Pearson correlation. In the uncentered Pearson correlation, a zero mean is used
  1261. for both vectors even if the actual mean is nonzero.
  1262. This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.
  1263. but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold
  1264. (e.g., choose b = a + c).
  1265. Arguments
  1266. =========
  1267. n (input) int
  1268. The number of elements in a row or column. If transpose==0, then n is the number
  1269. of columns; otherwise, n is the number of rows.
  1270. data1 (input) double array
  1271. The data array containing the first vector.
  1272. data2 (input) double array
  1273. The data array containing the second vector.
  1274. mask1 (input) int array
  1275. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1276. data1[i][j] is missing.
  1277. mask2 (input) int array
  1278. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1279. data2[i][j] is missing.
  1280. weight (input) double[n]
  1281. The weights that are used to calculate the distance.
  1282. index1 (input) int
  1283. Index of the first row or column.
  1284. index2 (input) int
  1285. Index of the second row or column.
  1286. transpose (input) int
  1287. If transpose==0, the distance between two rows in the matrix is calculated.
  1288. Otherwise, the distance between two columns in the matrix is calculated.
  1289. ============================================================================
  1290. */
  1291. { double result = 0.;
  1292. double denom1 = 0.;
  1293. double denom2 = 0.;
  1294. int flag = 0;
  1295. /* flag will remain zero if no nonzero combinations of mask1 and mask2 are
  1296. * found.
  1297. */
  1298. if (transpose==0) /* Calculate the distance between two rows */
  1299. { int i;
  1300. for (i = 0; i < n; i++)
  1301. { if (mask1[index1][i] && mask2[index2][i])
  1302. { double term1 = data1[index1][i];
  1303. double term2 = data2[index2][i];
  1304. double w = weight[i];
  1305. result += w*term1*term2;
  1306. denom1 += w*term1*term1;
  1307. denom2 += w*term2*term2;
  1308. flag = 1;
  1309. }
  1310. }
  1311. }
  1312. else
  1313. { int i;
  1314. for (i = 0; i < n; i++)
  1315. { if (mask1[i][index1] && mask2[i][index2])
  1316. { double term1 = data1[i][index1];
  1317. double term2 = data2[i][index2];
  1318. double w = weight[i];
  1319. result += w*term1*term2;
  1320. denom1 += w*term1*term1;
  1321. denom2 += w*term2*term2;
  1322. flag = 1;
  1323. }
  1324. }
  1325. }
  1326. if (!flag) return 0.;
  1327. if (denom1==0.) return 1.;
  1328. if (denom2==0.) return 1.;
  1329. result = fabs(result) / sqrt(denom1*denom2);
  1330. result = 1. - result;
  1331. return result;
  1332. }
  1333. /* ********************************************************************* */
  1334. static
  1335. double spearman (int n, double** data1, double** data2, int** mask1,
  1336. int** mask2, const double weight[], int index1, int index2, int transpose)
  1337. /*
  1338. Purpose
  1339. =======
  1340. The spearman routine calculates the Spearman distance between two rows or
  1341. columns. The Spearman distance is defined as one minus the Spearman rank
  1342. correlation.
  1343. Arguments
  1344. =========
  1345. n (input) int
  1346. The number of elements in a row or column. If transpose==0, then n is the number
  1347. of columns; otherwise, n is the number of rows.
  1348. data1 (input) double array
  1349. The data array containing the first vector.
  1350. data2 (input) double array
  1351. The data array containing the second vector.
  1352. mask1 (input) int array
  1353. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1354. data1[i][j] is missing.
  1355. mask2 (input) int array
  1356. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1357. data2[i][j] is missing.
  1358. weight (input) double[n]
  1359. These weights are ignored, but included for consistency with other distance
  1360. measures.
  1361. index1 (input) int
  1362. Index of the first row or column.
  1363. index2 (input) int
  1364. Index of the second row or column.
  1365. transpose (input) int
  1366. If transpose==0, the distance between two rows in the matrix is calculated.
  1367. Otherwise, the distance between two columns in the matrix is calculated.
  1368. ============================================================================
  1369. */
  1370. { int i;
  1371. int m = 0;
  1372. double* rank1;
  1373. double* rank2;
  1374. double result = 0.;
  1375. double denom1 = 0.;
  1376. double denom2 = 0.;
  1377. double avgrank;
  1378. double* tdata1;
  1379. double* tdata2;
  1380. tdata1 = malloc(n*sizeof(double));
  1381. if(!tdata1) return 0.0; /* Memory allocation error */
  1382. tdata2 = malloc(n*sizeof(double));
  1383. if(!tdata2) /* Memory allocation error */
  1384. { free(tdata1);
  1385. return 0.0;
  1386. }
  1387. if (transpose==0)
  1388. { for (i = 0; i < n; i++)
  1389. { if (mask1[index1][i] && mask2[index2][i])
  1390. { tdata1[m] = data1[index1][i];
  1391. tdata2[m] = data2[index2][i];
  1392. m++;
  1393. }
  1394. }
  1395. }
  1396. else
  1397. { for (i = 0; i < n; i++)
  1398. { if (mask1[i][index1] && mask2[i][index2])
  1399. { tdata1[m] = data1[i][index1];
  1400. tdata2[m] = data2[i][index2];
  1401. m++;
  1402. }
  1403. }
  1404. }
  1405. if (m==0)
  1406. { free(tdata1);
  1407. free(tdata2);
  1408. return 0;
  1409. }
  1410. rank1 = getrank(m, tdata1);
  1411. free(tdata1);
  1412. if(!rank1)
  1413. { free(tdata2);
  1414. return 0.0; /* Memory allocation error */
  1415. }
  1416. rank2 = getrank(m, tdata2);
  1417. free(tdata2);
  1418. if(!rank2) /* Memory allocation error */
  1419. { free(rank1);
  1420. return 0.0;
  1421. }
  1422. avgrank = 0.5*(m-1); /* Average rank */
  1423. for (i = 0; i < m; i++)
  1424. { const double value1 = rank1[i];
  1425. const double value2 = rank2[i];
  1426. result += value1 * value2;
  1427. denom1 += value1 * value1;
  1428. denom2 += value2 * value2;
  1429. }
  1430. /* Note: denom1 and denom2 cannot be calculated directly from the number
  1431. * of elements. If two elements have the same rank, the squared sum of
  1432. * their ranks will change.
  1433. */
  1434. free(rank1);
  1435. free(rank2);
  1436. result /= m;
  1437. denom1 /= m;
  1438. denom2 /= m;
  1439. result -= avgrank * avgrank;
  1440. denom1 -= avgrank * avgrank;
  1441. denom2 -= avgrank * avgrank;
  1442. if (denom1 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1443. if (denom2 <= 0) return 1; /* include '<' to deal with roundoff errors */
  1444. result = result / sqrt(denom1*denom2);
  1445. result = 1. - result;
  1446. return result;
  1447. }
  1448. /* ********************************************************************* */
  1449. static
  1450. double kendall (int n, double** data1, double** data2, int** mask1, int** mask2,
  1451. const double weight[], int index1, int index2, int transpose)
  1452. /*
  1453. Purpose
  1454. =======
  1455. The kendall routine calculates the Kendall distance between two
  1456. rows or columns. The Kendall distance is defined as one minus Kendall's tau.
  1457. Arguments
  1458. =========
  1459. n (input) int
  1460. The number of elements in a row or column. If transpose==0, then n is the number
  1461. of columns; otherwise, n is the number of rows.
  1462. data1 (input) double array
  1463. The data array containing the first vector.
  1464. data2 (input) double array
  1465. The data array containing the second vector.
  1466. mask1 (input) int array
  1467. This array which elements in data1 are missing. If mask1[i][j]==0, then
  1468. data1[i][j] is missing.
  1469. mask2 (input) int array
  1470. This array which elements in data2 are missing. If mask2[i][j]==0, then
  1471. data2[i][j] is missing.
  1472. weight (input) double[n]
  1473. These weights are ignored, but included for consistency with other distance
  1474. measures.
  1475. index1 (input) int
  1476. Index of the first row or column.
  1477. index2 (input) int
  1478. Index of the second row or column.
  1479. transpose (input) int
  1480. If transpose==0, the distance between two rows in the matrix is calculated.
  1481. Otherwise, the distance between two columns in the matrix is calculated.
  1482. ============================================================================
  1483. */
  1484. { int con = 0;
  1485. int dis = 0;
  1486. int exx = 0;
  1487. int exy = 0;
  1488. int flag = 0;
  1489. /* flag will remain zero if no nonzero combinations of mask1 and mask2 are
  1490. * found.
  1491. */
  1492. double denomx;
  1493. double denomy;
  1494. double tau;
  1495. int i, j;
  1496. if (transpose==0)
  1497. { for (i = 0; i < n; i++)
  1498. { if (mask1[index1][i] && mask2[index2][i])
  1499. { for (j = 0; j < i; j++)
  1500. { if (mask1[index1][j] && mask2[index2][j])
  1501. { double x1 = data1[index1][i];
  1502. double x2 = data1[index1][j];
  1503. double y1 = data2[index2][i];
  1504. double y2 = data2[index2][j];
  1505. if (x1 < x2 && y1 < y2) con++;
  1506. if (x1 > x2 && y1 > y2) con++;
  1507. if (x1 < x2 && y1 > y2) dis++;
  1508. if (x1 > x2 && y1 < y2) dis++;
  1509. if (x1 == x2 && y1 != y2) exx++;
  1510. if (x1 != x2 && y1 == y2) exy++;
  1511. flag = 1;
  1512. }
  1513. }
  1514. }
  1515. }
  1516. }
  1517. else
  1518. { for (i = 0; i < n; i++)
  1519. { if (mask1[i][index1] && mask2[i][index2])
  1520. { for (j = 0; j < i; j++)
  1521. { if (mask1[j][index1] && mask2[j][index2])
  1522. { double x1 = data1[i][index1];
  1523. double x2 = data1[j][index1];
  1524. double y1 = data2[i][index2];
  1525. double y2 = data2[j][index2];
  1526. if (x1 < x2 && y1 < y2) con++;
  1527. if (x1 > x2 && y1 > y2) con++;
  1528. if (x1 < x2 && y1 > y2) dis++;
  1529. if (x1 > x2 && y1 < y2) dis++;
  1530. if (x1 == x2 && y1 != y2) exx++;
  1531. if (x1 != x2 && y1 == y2) exy++;
  1532. flag = 1;
  1533. }
  1534. }
  1535. }
  1536. }
  1537. }
  1538. if (!flag) return 0.;
  1539. denomx = con + dis + exx;
  1540. denomy = con + dis + exy;
  1541. if (denomx==0) return 1;
  1542. if (denomy==0) return 1;
  1543. tau = (con-dis)/sqrt(denomx*denomy);
  1544. return 1.-tau;
  1545. }
  1546. /* ********************************************************************* */
  1547. static double(*setmetric(char dist))
  1548. (int, double**, double**, int**, int**, const double[], int, int, int)
  1549. { switch(dist)
  1550. { case 'e': return &euclid;
  1551. case 'b': return &cityblock;
  1552. case 'c': return &correlation;
  1553. case 'a': return &acorrelation;
  1554. case 'u': return &ucorrelation;
  1555. case 'x': return &uacorrelation;
  1556. case 's': return &spearman;
  1557. case 'k': return &kendall;
  1558. default: return &euclid;
  1559. }
  1560. return NULL; /* Never get here */
  1561. }
  1562. /* ********************************************************************* */
  1563. static double uniform(void)
  1564. /*
  1565. Purpose
  1566. =======
  1567. This routine returns a uniform random number between 0.0 and 1.0. Both 0.0
  1568. and 1.0 are excluded. This random number generator is described in:
  1569. Pierre l'Ecuyer
  1570. Efficient and Portable Combined Random Number Generators
  1571. Communications of the ACM, Volume 31, Number 6, June 1988, pages 742-749,774.
  1572. The first time this routine is called, it initializes the random number
  1573. generator using the current time. First, the current epoch time in seconds is
  1574. used as a seed for the random number generator in the C library. The first two
  1575. random numbers generated by this generator are used to initialize the random
  1576. number generator implemented in this routine.
  1577. Arguments
  1578. =========
  1579. None.
  1580. Return value
  1581. ============
  1582. A double-precison number between 0.0 and 1.0.
  1583. ============================================================================
  1584. */
  1585. { int z;
  1586. static const int m1 = 2147483563;
  1587. static const int m2 = 2147483399;
  1588. const double scale = 1.0/m1;
  1589. static int s1 = 0;
  1590. static int s2 = 0;
  1591. if (s1==0 || s2==0) /* initialize */
  1592. { unsigned int initseed = (unsigned int) time(0);
  1593. srand(initseed);
  1594. s1 = rand();
  1595. s2 = rand();
  1596. }
  1597. do
  1598. { int k;
  1599. k = s1/53668;
  1600. s1 = 40014*(s1-k*53668)-k*12211;
  1601. if (s1 < 0) s1+=m1;
  1602. k = s2/52774;
  1603. s2 = 40692*(s2-k*52774)-k*3791;
  1604. if(s2 < 0) s2+=m2;
  1605. z = s1-s2;
  1606. if(z < 1) z+=(m1-1);
  1607. } while (z==m1); /* To avoid returning 1.0 */
  1608. return z*scale;
  1609. }
  1610. /* ************************************************************************ */
  1611. static int binomial(int n, double p)
  1612. /*
  1613. Purpose
  1614. =======
  1615. This routine generates a random number between 0 and n inclusive, following
  1616. the binomial distribution with probability p and n trials. The routine is
  1617. based on the BTPE algorithm, described in:
  1618. Voratas Kachitvichyanukul and Bruce W. Schmeiser:
  1619. Binomial Random Variate Generation
  1620. Communications of the ACM, Volume 31, Number 2, February 1988, pages 216-222.
  1621. Arguments
  1622. =========
  1623. p (input) double
  1624. The probability of a single event. This probability should be less than or
  1625. equal to 0.5.
  1626. n (input) int
  1627. The number of trials.
  1628. Return value
  1629. ============
  1630. An integer drawn from a binomial distribution with parameters (p, n).
  1631. ============================================================================
  1632. */
  1633. { const double q = 1 - p;
  1634. if (n*p < 30.0) /* Algorithm BINV */
  1635. { const double s = p/q;
  1636. const double a = (n+1)*s;
  1637. double r = exp(n*log(q)); /* pow() causes a crash on AIX */
  1638. int x = 0;
  1639. double u = uniform();
  1640. while(1)
  1641. { if (u < r) return x;
  1642. u-=r;
  1643. x++;
  1644. r *= (a/x)-s;
  1645. }
  1646. }
  1647. else /* Algorithm BTPE */
  1648. { /* Step 0 */
  1649. const double fm = n*p + p;
  1650. const int m = (int) fm;
  1651. const double p1 = floor(2.195*sqrt(n*p*q) -4.6*q) + 0.5;
  1652. const double xm = m + 0.5;
  1653. const double xl = xm - p1;
  1654. const double xr = xm + p1;
  1655. const double c = 0.134 + 20.5/(15.3+m);
  1656. const double a = (fm-xl)/(fm-xl*p);
  1657. const double b = (xr-fm)/(xr*q);
  1658. const double lambdal = a*(1.0+0.5*a);
  1659. const double lambdar = b*(1.0+0.5*b);
  1660. const double p2 = p1*(1+2*c);
  1661. const double p3 = p2 + c/lambdal;
  1662. const double p4 = p3 + c/lambdar;
  1663. while (1)
  1664. { /* Step 1 */
  1665. int y;
  1666. int k;
  1667. double u = uniform();
  1668. double v = uniform();
  1669. u *= p4;
  1670. if (u <= p1) return (int)(xm-p1*v+u);
  1671. /* Step 2 */
  1672. if (u > p2)
  1673. { /* Step 3 */
  1674. if (u > p3)
  1675. { /* Step 4 */
  1676. y = (int)(xr-log(v)/lambdar);
  1677. if (y > n) continue;
  1678. /* Go to step 5 */
  1679. v = v*(u-p3)*lambdar;
  1680. }
  1681. else
  1682. { y = (int)(xl+log(v)/lambdal);
  1683. if (y < 0) continue;
  1684. /* Go to step 5 */
  1685. v = v*(u-p2)*lambdal;
  1686. }
  1687. }
  1688. else
  1689. { const double x = xl + (u-p1)/c;
  1690. v = v*c + 1.0 - fabs(m-x+0.5)/p1;
  1691. if (v > 1) continue;
  1692. /* Go to step 5 */
  1693. y = (int)x;
  1694. }
  1695. /* Step 5 */
  1696. /* Step 5.0 */
  1697. k = abs(y-m);
  1698. if (k > 20 && k < 0.5*n*p*q-1.0)
  1699. { /* Step 5.2 */
  1700. double rho = (k/(n*p*q))*((k*(k/3.0 + 0.625) + 0.1666666666666)/(n*p*q)+0.5);
  1701. double t = -k*k/(2*n*p*q);
  1702. double A = log(v);
  1703. if (A < t-rho) return y;
  1704. else if (A > t+rho) continue;
  1705. else
  1706. { /* Step 5.3 */
  1707. double x1 = y+1;
  1708. double f1 = m+1;
  1709. double z = n+1-m;
  1710. double w = n-y+1;
  1711. double x2 = x1*x1;
  1712. double f2 = f1*f1;
  1713. double z2 = z*z;
  1714. double w2 = w*w;
  1715. if (A > xm * log(f1/x1) + (n-m+0.5)*log(z/w)
  1716. + (y-m)*log(w*p/(x1*q))
  1717. + (13860.-(462.-(132.-(99.-140./f2)/f2)/f2)/f2)/f1/166320.
  1718. + (13860.-(462.-(132.-(99.-140./z2)/z2)/z2)/z2)/z/166320.
  1719. + (13860.-(462.-(132.-(99.-140./x2)/x2)/x2)/x2)/x1/166320.
  1720. + (13860.-(462.-(132.-(99.-140./w2)/w2)/w2)/w2)/w/166320.)
  1721. continue;
  1722. return y;
  1723. }
  1724. }
  1725. else
  1726. { /* Step 5.1 */
  1727. int i;
  1728. const double s = p/q;
  1729. const double aa = s*(n+1);
  1730. double f = 1.0;
  1731. for (i = m; i < y; f *= (aa/(++i)-s));
  1732. for (i = y; i < m; f /= (aa/(++i)-s));
  1733. if (v > f) continue;
  1734. return y;
  1735. }
  1736. }
  1737. }
  1738. /* Never get here */
  1739. return -1;
  1740. }
  1741. /* ************************************************************************ */
  1742. static void randomassign (int nclusters, int nelements, int clusterid[])
  1743. /*
  1744. Purpose
  1745. =======
  1746. The randomassign routine performs an initial random clustering, needed for
  1747. k-means or k-median clustering. Elements (genes or microarrays) are randomly
  1748. assigned to clusters. The number of elements in each cluster is chosen
  1749. randomly, making sure that each cluster will receive at least one element.
  1750. Arguments
  1751. =========
  1752. nclusters (input) int
  1753. The number of clusters.
  1754. nelements (input) int
  1755. The number of elements to be clustered (i.e., the number of genes or microarrays
  1756. to be clustered).
  1757. clusterid (output) int[nelements]
  1758. The cluster number to which an element was assigned.
  1759. ============================================================================
  1760. */
  1761. { int i, j;
  1762. int k = 0;
  1763. double p;
  1764. int n = nelements-nclusters;
  1765. /* Draw the number of elements in each cluster from a multinomial
  1766. * distribution, reserving ncluster elements to set independently
  1767. * in order to guarantee that none of the clusters are empty.
  1768. */
  1769. for (i = 0; i < nclusters-1; i++)
  1770. { p = 1.0/(nclusters-i);
  1771. j = binomial(n, p);
  1772. n -= j;
  1773. j += k+1; /* Assign at least one element to cluster i */
  1774. for ( ; k < j; k++) clusterid[k] = i;
  1775. }
  1776. /* Assign the remaining elements to the last cluster */
  1777. for ( ; k < nelements; k++) clusterid[k] = i;
  1778. /* Create a random permutation of the cluster assignments */
  1779. for (i = 0; i < nelements; i++)
  1780. { j = (int) (i + (nelements-i)*uniform());
  1781. k = clusterid[j];
  1782. clusterid[j] = clusterid[i];
  1783. clusterid[i] = k;
  1784. }
  1785. return;
  1786. }
  1787. /* ********************************************************************* */
  1788. static void getclustermeans(int nclusters, int nrows, int ncolumns,
  1789. double** data, int** mask, int clusterid[], double** cdata, int** cmask,
  1790. int transpose)
  1791. /*
  1792. Purpose
  1793. =======
  1794. The getclustermeans routine calculates the cluster centroids, given to which
  1795. cluster each element belongs. The centroid is defined as the mean over all
  1796. elements for each dimension.
  1797. Arguments
  1798. =========
  1799. nclusters (input) int
  1800. The number of clusters.
  1801. nrows (input) int
  1802. The number of rows in the gene expression data matrix, equal to the number of
  1803. genes.
  1804. ncolumns (input) int
  1805. The number of columns in the gene expression data matrix, equal to the number of
  1806. microarrays.
  1807. data (input) double[nrows][ncolumns]
  1808. The array containing the gene expression data.
  1809. mask (input) int[nrows][ncolumns]
  1810. This array shows which data values are missing. If mask[i][j]==0, then
  1811. data[i][j] is missing.
  1812. clusterid (output) int[nrows] if transpose==0
  1813. int[ncolumns] if transpose==1
  1814. The cluster number to which each element belongs. If transpose==0, then the
  1815. dimension of clusterid is equal to nrows (the number of genes). Otherwise, it
  1816. is equal to ncolumns (the number of microarrays).
  1817. cdata (output) double[nclusters][ncolumns] if transpose==0
  1818. double[nrows][nclusters] if transpose==1
  1819. On exit of getclustermeans, this array contains the cluster centroids.
  1820. cmask (output) int[nclusters][ncolumns] if transpose==0
  1821. int[nrows][nclusters] if transpose==1
  1822. This array shows which data values of are missing for each centroid. If
  1823. cmask[i][j]==0, then cdata[i][j] is missing. A data value is missing for
  1824. a centroid if all corresponding data values of the cluster members are missing.
  1825. transpose (input) int
  1826. If transpose==0, clusters of rows (genes) are specified. Otherwise, clusters of
  1827. columns (microarrays) are specified.
  1828. ========================================================================
  1829. */
  1830. { int i, j, k;
  1831. if (transpose==0)
  1832. { for (i = 0; i < nclusters; i++)
  1833. { for (j = 0; j < ncolumns; j++)
  1834. { cmask[i][j] = 0;
  1835. cdata[i][j] = 0.;
  1836. }
  1837. }
  1838. for (k = 0; k < nrows; k++)
  1839. { i = clusterid[k];
  1840. for (j = 0; j < ncolumns; j++)
  1841. { if (mask[k][j] != 0)
  1842. { cdata[i][j]+=data[k][j];
  1843. cmask[i][j]++;
  1844. }
  1845. }
  1846. }
  1847. for (i = 0; i < nclusters; i++)
  1848. { for (j = 0; j < ncolumns; j++)
  1849. { if (cmask[i][j]>0)
  1850. { cdata[i][j] /= cmask[i][j];
  1851. cmask[i][j] = 1;
  1852. }
  1853. }
  1854. }
  1855. }
  1856. else
  1857. { for (i = 0; i < nrows; i++)
  1858. { for (j = 0; j < nclusters; j++)
  1859. { cdata[i][j] = 0.;
  1860. cmask[i][j] = 0;
  1861. }
  1862. }
  1863. for (k = 0; k < ncolumns; k++)
  1864. { i = clusterid[k];
  1865. for (j = 0; j < nrows; j++)
  1866. { if (mask[j][k] != 0)
  1867. { cdata[j][i]+=data[j][k];
  1868. cmask[j][i]++;
  1869. }
  1870. }
  1871. }
  1872. for (i = 0; i < nrows; i++)
  1873. { for (j = 0; j < nclusters; j++)
  1874. { if (cmask[i][j]>0)
  1875. { cdata[i][j] /= cmask[i][j];
  1876. cmask[i][j] = 1;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. /* ********************************************************************* */
  1883. static void
  1884. getclustermedians(int nclusters, int nrows, int ncolumns,
  1885. double** data, int** mask, int clusterid[], double** cdata, int** cmask,
  1886. int transpose, double cache[])
  1887. /*
  1888. Purpose
  1889. =======
  1890. The getclustermedians routine calculates the cluster centroids, given to which
  1891. cluster each element belongs. The centroid is defined as the median over all
  1892. elements for each dimension.
  1893. Arguments
  1894. =========
  1895. nclusters (input) int
  1896. The number of clusters.
  1897. nrows (input) int
  1898. The number of rows in the gene expression data matrix, equal to the number of
  1899. genes.
  1900. ncolumns (input) int
  1901. The number of columns in the gene expression data matrix, equal to the number of
  1902. microarrays.
  1903. data (input) double[nrows][ncolumns]
  1904. The array containing the gene expression data.
  1905. mask (input) int[nrows][ncolumns]
  1906. This array shows which data values are missing. If mask[i][j]==0, then
  1907. data[i][j] is missing.
  1908. clusterid (output) int[nrows] if transpose==0
  1909. int[ncolumns] if transpose==1
  1910. The cluster number to which each element belongs. If transpose==0, then the
  1911. dimension of clusterid is equal to nrows (the number of genes). Otherwise, it
  1912. is equal to ncolumns (the number of microarrays).
  1913. cdata (output) double[nclusters][ncolumns] if transpose==0
  1914. double[nrows][nclusters] if transpose==1
  1915. On exit of getclustermedians, this array contains the cluster centroids.
  1916. cmask (output) int[nclusters][ncolumns] if transpose==0
  1917. int[nrows][nclusters] if transpose==1
  1918. This array shows which data values of are missing for each centroid. If
  1919. cmask[i][j]==0, then cdata[i][j] is missing. A data value is missing for
  1920. a centroid if all corresponding data values of the cluster members are missing.
  1921. transpose (input) int
  1922. If transpose==0, clusters of rows (genes) are specified. Otherwise, clusters of
  1923. columns (microarrays) are specified.
  1924. cache (input) double[nrows] if transpose==0
  1925. double[ncolumns] if transpose==1
  1926. This array should be allocated before calling getclustermedians; its contents
  1927. on input is not relevant. This array is used as a temporary storage space when
  1928. calculating the medians.
  1929. ========================================================================
  1930. */
  1931. { int i, j, k;
  1932. if (transpose==0)
  1933. { for (i = 0; i < nclusters; i++)
  1934. { for (j = 0; j < ncolumns; j++)
  1935. { int count = 0;
  1936. for (k = 0; k < nrows; k++)
  1937. { if (i==clusterid[k] && mask[k][j])
  1938. { cache[count] = data[k][j];
  1939. count++;
  1940. }
  1941. }
  1942. if (count>0)
  1943. { cdata[i][j] = median(count,cache);
  1944. cmask[i][j] = 1;
  1945. }
  1946. else
  1947. { cdata[i][j] = 0.;
  1948. cmask[i][j] = 0;
  1949. }
  1950. }
  1951. }
  1952. }
  1953. else
  1954. { for (i = 0; i < nclusters; i++)
  1955. { for (j = 0; j < nrows; j++)
  1956. { int count = 0;
  1957. for (k = 0; k < ncolumns; k++)
  1958. { if (i==clusterid[k] && mask[j][k])
  1959. { cache[count] = data[j][k];
  1960. count++;
  1961. }
  1962. }
  1963. if (count>0)
  1964. { cdata[j][i] = median(count,cache);
  1965. cmask[j][i] = 1;
  1966. }
  1967. else
  1968. { cdata[j][i] = 0.;
  1969. cmask[j][i] = 0;
  1970. }
  1971. }
  1972. }
  1973. }
  1974. }
  1975. /* ********************************************************************* */
  1976. int getclustercentroids(int nclusters, int nrows, int ncolumns,
  1977. double** data, int** mask, int clusterid[], double** cdata, int** cmask,
  1978. int transpose, char method)
  1979. /*
  1980. Purpose
  1981. =======
  1982. The getclustercentroids routine calculates the cluster centroids, given to
  1983. which cluster each element belongs. Depending on the argument method, the
  1984. centroid is defined as either the mean or the median for each dimension over
  1985. all elements belonging to a cluster.
  1986. Arguments
  1987. =========
  1988. nclusters (input) int
  1989. The number of clusters.
  1990. nrows (input) int
  1991. The number of rows in the gene expression data matrix, equal to the number of
  1992. genes.
  1993. ncolumns (input) int
  1994. The number of columns in the gene expression data matrix, equal to the number of
  1995. microarrays.
  1996. data (input) double[nrows][ncolumns]
  1997. The array containing the gene expression data.
  1998. mask (input) int[nrows][ncolumns]
  1999. This array shows which data values are missing. If mask[i][j]==0, then
  2000. data[i][j] is missing.
  2001. clusterid (output) int[nrows] if transpose==0
  2002. int[ncolumns] if transpose==1
  2003. The cluster number to which each element belongs. If transpose==0, then the
  2004. dimension of clusterid is equal to nrows (the number of genes). Otherwise, it
  2005. is equal to ncolumns (the number of microarrays).
  2006. cdata (output) double[nclusters][ncolumns] if transpose==0
  2007. double[nrows][nclusters] if transpose==1
  2008. On exit of getclustercentroids, this array contains the cluster centroids.
  2009. cmask (output) int[nclusters][ncolumns] if transpose==0
  2010. int[nrows][nclusters] if transpose==1
  2011. This array shows which data values of are missing for each centroid. If
  2012. cmask[i][j]==0, then cdata[i][j] is missing. A data value is missing for
  2013. a centroid if all corresponding data values of the cluster members are missing.
  2014. transpose (input) int
  2015. If transpose==0, clusters of rows (genes) are specified. Otherwise, clusters of
  2016. columns (microarrays) are specified.
  2017. method (input) char
  2018. For method=='a', the centroid is defined as the mean over all elements
  2019. belonging to a cluster for each dimension.
  2020. For method=='m', the centroid is defined as the median over all elements
  2021. belonging to a cluster for each dimension.
  2022. Return value
  2023. ============
  2024. The function returns an integer to indicate success or failure. If a
  2025. memory error occurs, or if method is not 'm' or 'a', getclustercentroids
  2026. returns 0. If successful, getclustercentroids returns 1.
  2027. ========================================================================
  2028. */
  2029. { switch(method)
  2030. { case 'm':
  2031. { const int nelements = (transpose==0) ? nrows : ncolumns;
  2032. double* cache = malloc(nelements*sizeof(double));
  2033. if (!cache) return 0;
  2034. getclustermedians(nclusters, nrows, ncolumns, data, mask, clusterid,
  2035. cdata, cmask, transpose, cache);
  2036. free(cache);
  2037. return 1;
  2038. }
  2039. case 'a':
  2040. { getclustermeans(nclusters, nrows, ncolumns, data, mask, clusterid,
  2041. cdata, cmask, transpose);
  2042. return 1;
  2043. }
  2044. }
  2045. return 0;
  2046. }
  2047. /* ********************************************************************* */
  2048. void getclustermedoids(int nclusters, int nelements, double** distance,
  2049. int clusterid[], int centroids[], double errors[])
  2050. /*
  2051. Purpose
  2052. =======
  2053. The getclustermedoids routine calculates the cluster centroids, given to which
  2054. cluster each element belongs. The centroid is defined as the element with the
  2055. smallest sum of distances to the other elements.
  2056. Arguments
  2057. =========
  2058. nclusters (input) int
  2059. The number of clusters.
  2060. nelements (input) int
  2061. The total number of elements.
  2062. distmatrix (input) double array, ragged
  2063. (number of rows is nelements, number of columns is equal to the row number)
  2064. The distance matrix. To save space, the distance matrix is given in the
  2065. form of a ragged array. The distance matrix is symmetric and has zeros
  2066. on the diagonal. See distancematrix for a description of the content.
  2067. clusterid (output) int[nelements]
  2068. The cluster number to which each element belongs.
  2069. centroid (output) int[nclusters]
  2070. The index of the element that functions as the centroid for each cluster.
  2071. errors (output) double[nclusters]
  2072. The within-cluster sum of distances between the items and the cluster
  2073. centroid.
  2074. ========================================================================
  2075. */
  2076. { int i, j, k;
  2077. for (j = 0; j < nclusters; j++) errors[j] = DBL_MAX;
  2078. for (i = 0; i < nelements; i++)
  2079. { double d = 0.0;
  2080. j = clusterid[i];
  2081. for (k = 0; k < nelements; k++)
  2082. { if (i==k || clusterid[k]!=j) continue;
  2083. d += (i < k ? distance[k][i] : distance[i][k]);
  2084. if (d > errors[j]) break;
  2085. }
  2086. if (d < errors[j])
  2087. { errors[j] = d;
  2088. centroids[j] = i;
  2089. }
  2090. }
  2091. }
  2092. /* ********************************************************************* */
  2093. static int
  2094. kmeans(int nclusters, int nrows, int ncolumns, double** data, int** mask,
  2095. double weight[], int transpose, int npass, char dist,
  2096. double** cdata, int** cmask, int clusterid[], double* error,
  2097. int tclusterid[], int counts[], int mapping[])
  2098. { int i, j, k;
  2099. const int nelements = (transpose==0) ? nrows : ncolumns;
  2100. const int ndata = (transpose==0) ? ncolumns : nrows;
  2101. int ifound = 1;
  2102. int ipass = 0;
  2103. /* Set the metric function as indicated by dist */
  2104. double (*metric)
  2105. (int, double**, double**, int**, int**, const double[], int, int, int) =
  2106. setmetric(dist);
  2107. /* We save the clustering solution periodically and check if it reappears */
  2108. int* saved = malloc(nelements*sizeof(int));
  2109. if (saved==NULL) return -1;
  2110. *error = DBL_MAX;
  2111. do
  2112. { double total = DBL_MAX;
  2113. int counter = 0;
  2114. int period = 10;
  2115. /* Perform the EM algorithm. First, randomly assign elements to clusters. */
  2116. if (npass!=0) randomassign (nclusters, nelements, tclusterid);
  2117. for (i = 0; i < nclusters; i++) counts[i] = 0;
  2118. for (i = 0; i < nelements; i++) counts[tclusterid[i]]++;
  2119. /* Start the loop */
  2120. while(1)
  2121. { double previous = total;
  2122. total = 0.0;
  2123. if (counter % period == 0) /* Save the current cluster assignments */
  2124. { for (i = 0; i < nelements; i++) saved[i] = tclusterid[i];
  2125. if (period < INT_MAX / 2) period *= 2;
  2126. }
  2127. counter++;
  2128. /* Find the center */
  2129. getclustermeans(nclusters, nrows, ncolumns, data, mask, tclusterid,
  2130. cdata, cmask, transpose);
  2131. for (i = 0; i < nelements; i++)
  2132. /* Calculate the distances */
  2133. { double distance;
  2134. k = tclusterid[i];
  2135. if (counts[k]==1) continue;
  2136. /* No reassignment if that would lead to an empty cluster */
  2137. /* Treat the present cluster as a special case */
  2138. distance = metric(ndata,data,cdata,mask,cmask,weight,i,k,transpose);
  2139. for (j = 0; j < nclusters; j++)
  2140. { double tdistance;
  2141. if (j==k) continue;
  2142. tdistance = metric(ndata,data,cdata,mask,cmask,weight,i,j,transpose);
  2143. if (tdistance < distance)
  2144. { distance = tdistance;
  2145. counts[tclusterid[i]]--;
  2146. tclusterid[i] = j;
  2147. counts[j]++;
  2148. }
  2149. }
  2150. total += distance;
  2151. }
  2152. if (total>=previous) break;
  2153. /* total>=previous is FALSE on some machines even if total and previous
  2154. * are bitwise identical. */
  2155. for (i = 0; i < nelements; i++)
  2156. if (saved[i]!=tclusterid[i]) break;
  2157. if (i==nelements)
  2158. break; /* Identical solution found; break out of this loop */
  2159. }
  2160. if (npass<=1)
  2161. { *error = total;
  2162. break;
  2163. }
  2164. for (i = 0; i < nclusters; i++) mapping[i] = -1;
  2165. for (i = 0; i < nelements; i++)
  2166. { j = tclusterid[i];
  2167. k = clusterid[i];
  2168. if (mapping[k] == -1) mapping[k] = j;
  2169. else if (mapping[k] != j)
  2170. { if (total < *error)
  2171. { ifound = 1;
  2172. *error = total;
  2173. for (j = 0; j < nelements; j++) clusterid[j] = tclusterid[j];
  2174. }
  2175. break;
  2176. }
  2177. }
  2178. if (i==nelements) ifound++; /* break statement not encountered */
  2179. } while (++ipass < npass);
  2180. free(saved);
  2181. return ifound;
  2182. }
  2183. /* ---------------------------------------------------------------------- */
  2184. static int
  2185. kmedians(int nclusters, int nrows, int ncolumns, double** data, int** mask,
  2186. double weight[], int transpose, int npass, char dist,
  2187. double** cdata, int** cmask, int clusterid[], double* error,
  2188. int tclusterid[], int counts[], int mapping[], double cache[])
  2189. { int i, j, k;
  2190. const int nelements = (transpose==0) ? nrows : ncolumns;
  2191. const int ndata = (transpose==0) ? ncolumns : nrows;
  2192. int ifound = 1;
  2193. int ipass = 0;
  2194. /* Set the metric function as indicated by dist */
  2195. double (*metric)
  2196. (int, double**, double**, int**, int**, const double[], int, int, int) =
  2197. setmetric(dist);
  2198. /* We save the clustering solution periodically and check if it reappears */
  2199. int* saved = malloc(nelements*sizeof(int));
  2200. if (saved==NULL) return -1;
  2201. *error = DBL_MAX;
  2202. do
  2203. { double total = DBL_MAX;
  2204. int counter = 0;
  2205. int period = 10;
  2206. /* Perform the EM algorithm. First, randomly assign elements to clusters. */
  2207. if (npass!=0) randomassign (nclusters, nelements, tclusterid);
  2208. for (i = 0; i < nclusters; i++) counts[i]=0;
  2209. for (i = 0; i < nelements; i++) counts[tclusterid[i]]++;
  2210. /* Start the loop */
  2211. while(1)
  2212. { double previous = total;
  2213. total = 0.0;
  2214. if (counter % period == 0) /* Save the current cluster assignments */
  2215. { for (i = 0; i < nelements; i++) saved[i] = tclusterid[i];
  2216. if (period < INT_MAX / 2) period *= 2;
  2217. }
  2218. counter++;
  2219. /* Find the center */
  2220. getclustermedians(nclusters, nrows, ncolumns, data, mask, tclusterid,
  2221. cdata, cmask, transpose, cache);
  2222. for (i = 0; i < nelements; i++)
  2223. /* Calculate the distances */
  2224. { double distance;
  2225. k = tclusterid[i];
  2226. if (counts[k]==1) continue;
  2227. /* No reassignment if that would lead to an empty cluster */
  2228. /* Treat the present cluster as a special case */
  2229. distance = metric(ndata,data,cdata,mask,cmask,weight,i,k,transpose);
  2230. for (j = 0; j < nclusters; j++)
  2231. { double tdistance;
  2232. if (j==k) continue;
  2233. tdistance = metric(ndata,data,cdata,mask,cmask,weight,i,j,transpose);
  2234. if (tdistance < distance)
  2235. { distance = tdistance;
  2236. counts[tclusterid[i]]--;
  2237. tclusterid[i] = j;
  2238. counts[j]++;
  2239. }
  2240. }
  2241. total += distance;
  2242. }
  2243. if (total>=previous) break;
  2244. /* total>=previous is FALSE on some machines even if total and previous
  2245. * are bitwise identical. */
  2246. for (i = 0; i < nelements; i++)
  2247. if (saved[i]!=tclusterid[i]) break;
  2248. if (i==nelements)
  2249. break; /* Identical solution found; break out of this loop */
  2250. }
  2251. if (npass<=1)
  2252. { *error = total;
  2253. break;
  2254. }
  2255. for (i = 0; i < nclusters; i++) mapping[i] = -1;
  2256. for (i = 0; i < nelements; i++)
  2257. { j = tclusterid[i];
  2258. k = clusterid[i];
  2259. if (mapping[k] == -1) mapping[k] = j;
  2260. else if (mapping[k] != j)
  2261. { if (total < *error)
  2262. { ifound = 1;
  2263. *error = total;
  2264. for (j = 0; j < nelements; j++) clusterid[j] = tclusterid[j];
  2265. }
  2266. break;
  2267. }
  2268. }
  2269. if (i==nelements) ifound++; /* break statement not encountered */
  2270. } while (++ipass < npass);
  2271. free(saved);
  2272. return ifound;
  2273. }
  2274. /* ********************************************************************* */
  2275. void kcluster (int nclusters, int nrows, int ncolumns,
  2276. double** data, int** mask, double weight[], int transpose,
  2277. int npass, char method, char dist,
  2278. int clusterid[], double* error, int* ifound)
  2279. /*
  2280. Purpose
  2281. =======
  2282. The kcluster routine performs k-means or k-median clustering on a given set of
  2283. elements, using the specified distance measure. The number of clusters is given
  2284. by the user. Multiple passes are being made to find the optimal clustering
  2285. solution, each time starting from a different initial clustering.
  2286. Arguments
  2287. =========
  2288. nclusters (input) int
  2289. The number of clusters to be found.
  2290. data (input) double[nrows][ncolumns]
  2291. The array containing the data of the elements to be clustered (i.e., the gene
  2292. expression data).
  2293. mask (input) int[nrows][ncolumns]
  2294. This array shows which data values are missing. If
  2295. mask[i][j] == 0, then data[i][j] is missing.
  2296. nrows (input) int
  2297. The number of rows in the data matrix, equal to the number of genes.
  2298. ncolumns (input) int
  2299. The number of columns in the data matrix, equal to the number of microarrays.
  2300. weight (input) double[n]
  2301. The weights that are used to calculate the distance.
  2302. transpose (input) int
  2303. If transpose==0, the rows of the matrix are clustered. Otherwise, columns
  2304. of the matrix are clustered.
  2305. npass (input) int
  2306. The number of times clustering is performed. Clustering is performed npass
  2307. times, each time starting from a different (random) initial assignment of
  2308. genes to clusters. The clustering solution with the lowest within-cluster sum
  2309. of distances is chosen.
  2310. If npass==0, then the clustering algorithm will be run once, where the initial
  2311. assignment of elements to clusters is taken from the clusterid array.
  2312. method (input) char
  2313. Defines whether the arithmetic mean (method=='a') or the median
  2314. (method=='m') is used to calculate the cluster center.
  2315. dist (input) char
  2316. Defines which distance measure is used, as given by the table:
  2317. dist=='e': Euclidean distance
  2318. dist=='b': City-block distance
  2319. dist=='c': correlation
  2320. dist=='a': absolute value of the correlation
  2321. dist=='u': uncentered correlation
  2322. dist=='x': absolute uncentered correlation
  2323. dist=='s': Spearman's rank correlation
  2324. dist=='k': Kendall's tau
  2325. For other values of dist, the default (Euclidean distance) is used.
  2326. clusterid (output; input) int[nrows] if transpose==0
  2327. int[ncolumns] if transpose==1
  2328. The cluster number to which a gene or microarray was assigned. If npass==0,
  2329. then on input clusterid contains the initial clustering assignment from which
  2330. the clustering algorithm starts. On output, it contains the clustering solution
  2331. that was found.
  2332. error (output) double*
  2333. The sum of distances to the cluster center of each item in the optimal k-means
  2334. clustering solution that was found.
  2335. ifound (output) int*
  2336. The number of times the optimal clustering solution was
  2337. found. The value of ifound is at least 1; its maximum value is npass. If the
  2338. number of clusters is larger than the number of elements being clustered,
  2339. *ifound is set to 0 as an error code. If a memory allocation error occurs,
  2340. *ifound is set to -1.
  2341. ========================================================================
  2342. */
  2343. { const int nelements = (transpose==0) ? nrows : ncolumns;
  2344. const int ndata = (transpose==0) ? ncolumns : nrows;
  2345. int i;
  2346. int ok;
  2347. int* tclusterid;
  2348. int* mapping = NULL;
  2349. double** cdata;
  2350. int** cmask;
  2351. int* counts;
  2352. if (nelements < nclusters)
  2353. { *ifound = 0;
  2354. return;
  2355. }
  2356. /* More clusters asked for than elements available */
  2357. *ifound = -1;
  2358. /* This will contain the number of elements in each cluster, which is
  2359. * needed to check for empty clusters. */
  2360. counts = malloc(nclusters*sizeof(int));
  2361. if(!counts) return;
  2362. /* Find out if the user specified an initial clustering */
  2363. if (npass<=1) tclusterid = clusterid;
  2364. else
  2365. { tclusterid = malloc(nelements*sizeof(int));
  2366. if (!tclusterid)
  2367. { free(counts);
  2368. return;
  2369. }
  2370. mapping = malloc(nclusters*sizeof(int));
  2371. if (!mapping)
  2372. { free(counts);
  2373. free(tclusterid);
  2374. return;
  2375. }
  2376. for (i = 0; i < nelements; i++) clusterid[i] = 0;
  2377. }
  2378. /* Allocate space to store the centroid data */
  2379. if (transpose==0) ok = makedatamask(nclusters, ndata, &cdata, &cmask);
  2380. else ok = makedatamask(ndata, nclusters, &cdata, &cmask);
  2381. if(!ok)
  2382. { free(counts);
  2383. if(npass>1)
  2384. { free(tclusterid);
  2385. free(mapping);
  2386. return;
  2387. }
  2388. }
  2389. if (method=='m')
  2390. { double* cache = malloc(nelements*sizeof(double));
  2391. if(cache)
  2392. { *ifound = kmedians(nclusters, nrows, ncolumns, data, mask, weight,
  2393. transpose, npass, dist, cdata, cmask, clusterid, error,
  2394. tclusterid, counts, mapping, cache);
  2395. free(cache);
  2396. }
  2397. }
  2398. else
  2399. *ifound = kmeans(nclusters, nrows, ncolumns, data, mask, weight,
  2400. transpose, npass, dist, cdata, cmask, clusterid, error,
  2401. tclusterid, counts, mapping);
  2402. /* Deallocate temporarily used space */
  2403. if (npass > 1)
  2404. { free(mapping);
  2405. free(tclusterid);
  2406. }
  2407. if (transpose==0) freedatamask(nclusters, cdata, cmask);
  2408. else freedatamask(ndata, cdata, cmask);
  2409. free(counts);
  2410. }
  2411. /* *********************************************************************** */
  2412. void kmedoids (int nclusters, int nelements, double** distmatrix,
  2413. int npass, int clusterid[], double* error, int* ifound)
  2414. /*
  2415. Purpose
  2416. =======
  2417. The kmedoids routine performs k-medoids clustering on a given set of elements,
  2418. using the distance matrix and the number of clusters passed by the user.
  2419. Multiple passes are being made to find the optimal clustering solution, each
  2420. time starting from a different initial clustering.
  2421. Arguments
  2422. =========
  2423. nclusters (input) int
  2424. The number of clusters to be found.
  2425. nelements (input) int
  2426. The number of elements to be clustered.
  2427. distmatrix (input) double array, ragged
  2428. (number of rows is nelements, number of columns is equal to the row number)
  2429. The distance matrix. To save space, the distance matrix is given in the
  2430. form of a ragged array. The distance matrix is symmetric and has zeros
  2431. on the diagonal. See distancematrix for a description of the content.
  2432. npass (input) int
  2433. The number of times clustering is performed. Clustering is performed npass
  2434. times, each time starting from a different (random) initial assignment of genes
  2435. to clusters. The clustering solution with the lowest within-cluster sum of
  2436. distances is chosen.
  2437. If npass==0, then the clustering algorithm will be run once, where the initial
  2438. assignment of elements to clusters is taken from the clusterid array.
  2439. clusterid (output; input) int[nelements]
  2440. On input, if npass==0, then clusterid contains the initial clustering assignment
  2441. from which the clustering algorithm starts; all numbers in clusterid should be
  2442. between zero and nelements-1 inclusive. If npass!=0, clusterid is ignored on
  2443. input.
  2444. On output, clusterid contains the clustering solution that was found: clusterid
  2445. contains the number of the cluster to which each item was assigned. On output,
  2446. the number of a cluster is defined as the item number of the centroid of the
  2447. cluster.
  2448. error (output) double
  2449. The sum of distances to the cluster center of each item in the optimal k-medoids
  2450. clustering solution that was found.
  2451. ifound (output) int
  2452. If kmedoids is successful: the number of times the optimal clustering solution
  2453. was found. The value of ifound is at least 1; its maximum value is npass.
  2454. If the user requested more clusters than elements available, ifound is set
  2455. to 0. If kmedoids fails due to a memory allocation error, ifound is set to -1.
  2456. ========================================================================
  2457. */
  2458. { int i, j, icluster;
  2459. int* tclusterid;
  2460. int* saved;
  2461. int* centroids;
  2462. double* errors;
  2463. int ipass = 0;
  2464. if (nelements < nclusters)
  2465. { *ifound = 0;
  2466. return;
  2467. } /* More clusters asked for than elements available */
  2468. *ifound = -1;
  2469. /* We save the clustering solution periodically and check if it reappears */
  2470. saved = malloc(nelements*sizeof(int));
  2471. if (saved==NULL) return;
  2472. centroids = malloc(nclusters*sizeof(int));
  2473. if(!centroids)
  2474. { free(saved);
  2475. return;
  2476. }
  2477. errors = malloc(nclusters*sizeof(double));
  2478. if(!errors)
  2479. { free(saved);
  2480. free(centroids);
  2481. return;
  2482. }
  2483. /* Find out if the user specified an initial clustering */
  2484. if (npass<=1) tclusterid = clusterid;
  2485. else
  2486. { tclusterid = malloc(nelements*sizeof(int));
  2487. if(!tclusterid)
  2488. { free(saved);
  2489. free(centroids);
  2490. free(errors);
  2491. return;
  2492. }
  2493. }
  2494. *error = DBL_MAX;
  2495. do /* Start the loop */
  2496. { double total = DBL_MAX;
  2497. int counter = 0;
  2498. int period = 10;
  2499. if (npass!=0) randomassign (nclusters, nelements, tclusterid);
  2500. while(1)
  2501. { double previous = total;
  2502. total = 0.0;
  2503. if (counter % period == 0) /* Save the current cluster assignments */
  2504. { for (i = 0; i < nelements; i++) saved[i] = tclusterid[i];
  2505. if (period < INT_MAX / 2) period *= 2;
  2506. }
  2507. counter++;
  2508. /* Find the center */
  2509. getclustermedoids(nclusters, nelements, distmatrix, tclusterid,
  2510. centroids, errors);
  2511. for (i = 0; i < nelements; i++)
  2512. /* Find the closest cluster */
  2513. { double distance = DBL_MAX;
  2514. for (icluster = 0; icluster < nclusters; icluster++)
  2515. { double tdistance;
  2516. j = centroids[icluster];
  2517. if (i==j)
  2518. { distance = 0.0;
  2519. tclusterid[i] = icluster;
  2520. break;
  2521. }
  2522. tdistance = (i > j) ? distmatrix[i][j] : distmatrix[j][i];
  2523. if (tdistance < distance)
  2524. { distance = tdistance;
  2525. tclusterid[i] = icluster;
  2526. }
  2527. }
  2528. total += distance;
  2529. }
  2530. if (total>=previous) break;
  2531. /* total>=previous is FALSE on some machines even if total and previous
  2532. * are bitwise identical. */
  2533. for (i = 0; i < nelements; i++)
  2534. if (saved[i]!=tclusterid[i]) break;
  2535. if (i==nelements)
  2536. break; /* Identical solution found; break out of this loop */
  2537. }
  2538. for (i = 0; i < nelements; i++)
  2539. { if (clusterid[i]!=centroids[tclusterid[i]])
  2540. { if (total < *error)
  2541. { *ifound = 1;
  2542. *error = total;
  2543. /* Replace by the centroid in each cluster. */
  2544. for (j = 0; j < nelements; j++)
  2545. clusterid[j] = centroids[tclusterid[j]];
  2546. }
  2547. break;
  2548. }
  2549. }
  2550. if (i==nelements) (*ifound)++; /* break statement not encountered */
  2551. } while (++ipass < npass);
  2552. /* Deallocate temporarily used space */
  2553. if (npass > 1) free(tclusterid);
  2554. free(saved);
  2555. free(centroids);
  2556. free(errors);
  2557. return;
  2558. }
  2559. /* ******************************************************************** */
  2560. double** distancematrix (int nrows, int ncolumns, double** data,
  2561. int** mask, double weights[], char dist, int transpose)
  2562. /*
  2563. Purpose
  2564. =======
  2565. The distancematrix routine calculates the distance matrix between genes or
  2566. microarrays using their measured gene expression data. Several distance measures
  2567. can be used. The routine returns a pointer to a ragged array containing the
  2568. distances between the genes. As the distance matrix is symmetric, with zeros on
  2569. the diagonal, only the lower triangular half of the distance matrix is saved.
  2570. The distancematrix routine allocates space for the distance matrix. If the
  2571. parameter transpose is set to a nonzero value, the distances between the columns
  2572. (microarrays) are calculated, otherwise distances between the rows (genes) are
  2573. calculated.
  2574. If sufficient space in memory cannot be allocated to store the distance matrix,
  2575. the routine returns a NULL pointer, and all memory allocated so far for the
  2576. distance matrix is freed.
  2577. Arguments
  2578. =========
  2579. nrows (input) int
  2580. The number of rows in the gene expression data matrix (i.e., the number of
  2581. genes)
  2582. ncolumns (input) int
  2583. The number of columns in the gene expression data matrix (i.e., the number of
  2584. microarrays)
  2585. data (input) double[nrows][ncolumns]
  2586. The array containing the gene expression data.
  2587. mask (input) int[nrows][ncolumns]
  2588. This array shows which data values are missing. If mask[i][j]==0, then
  2589. data[i][j] is missing.
  2590. weight (input) double[n]
  2591. The weights that are used to calculate the distance. The length of this vector
  2592. is equal to the number of columns if the distances between genes are calculated,
  2593. or the number of rows if the distances between microarrays are calculated.
  2594. dist (input) char
  2595. Defines which distance measure is used, as given by the table:
  2596. dist=='e': Euclidean distance
  2597. dist=='b': City-block distance
  2598. dist=='c': correlation
  2599. dist=='a': absolute value of the correlation
  2600. dist=='u': uncentered correlation
  2601. dist=='x': absolute uncentered correlation
  2602. dist=='s': Spearman's rank correlation
  2603. dist=='k': Kendall's tau
  2604. For other values of dist, the default (Euclidean distance) is used.
  2605. transpose (input) int
  2606. If transpose is equal to zero, the distances between the rows is
  2607. calculated. Otherwise, the distances between the columns is calculated.
  2608. The former is needed when genes are being clustered; the latter is used
  2609. when microarrays are being clustered.
  2610. ========================================================================
  2611. */
  2612. { /* First determine the size of the distance matrix */
  2613. const int n = (transpose==0) ? nrows : ncolumns;
  2614. const int ndata = (transpose==0) ? ncolumns : nrows;
  2615. int i,j;
  2616. double** matrix;
  2617. /* Set the metric function as indicated by dist */
  2618. double (*metric)
  2619. (int, double**, double**, int**, int**, const double[], int, int, int) =
  2620. setmetric(dist);
  2621. if (n < 2) return NULL;
  2622. /* Set up the ragged array */
  2623. matrix = malloc(n*sizeof(double*));
  2624. if(matrix==NULL) return NULL; /* Not enough memory available */
  2625. matrix[0] = NULL;
  2626. /* The zeroth row has zero columns. We allocate it anyway for convenience.*/
  2627. for (i = 1; i < n; i++)
  2628. { matrix[i] = malloc(i*sizeof(double));
  2629. if (matrix[i]==NULL) break; /* Not enough memory available */
  2630. }
  2631. if (i < n) /* break condition encountered */
  2632. { j = i;
  2633. for (i = 1; i < j; i++) free(matrix[i]);
  2634. return NULL;
  2635. }
  2636. /* Calculate the distances and save them in the ragged array */
  2637. for (i = 1; i < n; i++)
  2638. for (j = 0; j < i; j++)
  2639. matrix[i][j]=metric(ndata,data,data,mask,mask,weights,i,j,transpose);
  2640. return matrix;
  2641. }
  2642. /* ******************************************************************** */
  2643. double* calculate_weights(int nrows, int ncolumns, double** data, int** mask,
  2644. double weights[], int transpose, char dist, double cutoff, double exponent)
  2645. /*
  2646. Purpose
  2647. =======
  2648. This function calculates the weights using the weighting scheme proposed by
  2649. Michael Eisen:
  2650. w[i] = 1.0 / sum_{j where d[i][j]<cutoff} (1 - d[i][j]/cutoff)^exponent
  2651. where the cutoff and the exponent are specified by the user.
  2652. Arguments
  2653. =========
  2654. nrows (input) int
  2655. The number of rows in the gene expression data matrix, equal to the number of
  2656. genes.
  2657. ncolumns (input) int
  2658. The number of columns in the gene expression data matrix, equal to the number of
  2659. microarrays.
  2660. data (input) double[nrows][ncolumns]
  2661. The array containing the gene expression data.
  2662. mask (input) int[nrows][ncolumns]
  2663. This array shows which data values are missing. If mask[i][j]==0, then
  2664. data[i][j] is missing.
  2665. weight (input) int[ncolumns] if transpose==0,
  2666. int[nrows] if transpose==1
  2667. The weights that are used to calculate the distance. The length of this vector
  2668. is ncolumns if gene weights are being clustered, and nrows if microarrays
  2669. weights are being clustered.
  2670. transpose (input) int
  2671. If transpose==0, the weights of the rows of the data matrix are calculated.
  2672. Otherwise, the weights of the columns of the data matrix are calculated.
  2673. dist (input) char
  2674. Defines which distance measure is used, as given by the table:
  2675. dist=='e': Euclidean distance
  2676. dist=='b': City-block distance
  2677. dist=='c': correlation
  2678. dist=='a': absolute value of the correlation
  2679. dist=='u': uncentered correlation
  2680. dist=='x': absolute uncentered correlation
  2681. dist=='s': Spearman's rank correlation
  2682. dist=='k': Kendall's tau
  2683. For other values of dist, the default (Euclidean distance) is used.
  2684. cutoff (input) double
  2685. The cutoff to be used to calculate the weights.
  2686. exponent (input) double
  2687. The exponent to be used to calculate the weights.
  2688. Return value
  2689. ============
  2690. The function returns a pointer to a newly allocated array containing the
  2691. calculated weights for the rows (if transpose==0) or columns (if
  2692. transpose==1). If not enough memory could be allocated to store the
  2693. weights array, the function returns NULL.
  2694. ========================================================================
  2695. */
  2696. { int i,j;
  2697. const int ndata = (transpose==0) ? ncolumns : nrows;
  2698. const int nelements = (transpose==0) ? nrows : ncolumns;
  2699. /* Set the metric function as indicated by dist */
  2700. double (*metric)
  2701. (int, double**, double**, int**, int**, const double[], int, int, int) =
  2702. setmetric(dist);
  2703. double* result = malloc(nelements*sizeof(double));
  2704. if (!result) return NULL;
  2705. memset(result, 0, nelements*sizeof(double));
  2706. for (i = 0; i < nelements; i++)
  2707. { result[i] += 1.0;
  2708. for (j = 0; j < i; j++)
  2709. { const double distance = metric(ndata, data, data, mask, mask, weights,
  2710. i, j, transpose);
  2711. if (distance < cutoff)
  2712. { const double dweight = exp(exponent*log(1-distance/cutoff));
  2713. /* pow() causes a crash on AIX */
  2714. result[i] += dweight;
  2715. result[j] += dweight;
  2716. }
  2717. }
  2718. }
  2719. for (i = 0; i < nelements; i++) result[i] = 1.0/result[i];
  2720. return result;
  2721. }
  2722. /* ******************************************************************** */
  2723. void cuttree (int nelements, Node* tree, int nclusters, int clusterid[])
  2724. /*
  2725. Purpose
  2726. =======
  2727. The cuttree routine takes the output of a hierarchical clustering routine, and
  2728. divides the elements in the tree structure into clusters based on the
  2729. hierarchical clustering result. The number of clusters is specified by the user.
  2730. Arguments
  2731. =========
  2732. nelements (input) int
  2733. The number of elements that were clustered.
  2734. tree (input) Node[nelements-1]
  2735. The clustering solution. Each node in the array describes one linking event,
  2736. with tree[i].left and tree[i].right representig the elements that were joined.
  2737. The original elements are numbered 0..nelements-1, nodes are numbered
  2738. -1..-(nelements-1).
  2739. nclusters (input) int
  2740. The number of clusters to be formed.
  2741. clusterid (output) int[nelements]
  2742. The number of the cluster to which each element was assigned. Space for this
  2743. array should be allocated before calling the cuttree routine. If a memory
  2744. error occured, all elements in clusterid are set to -1.
  2745. ========================================================================
  2746. */
  2747. { int i, j, k;
  2748. int icluster = 0;
  2749. const int n = nelements-nclusters; /* number of nodes to join */
  2750. int* nodeid;
  2751. for (i = nelements-2; i >= n; i--)
  2752. { k = tree[i].left;
  2753. if (k>=0)
  2754. { clusterid[k] = icluster;
  2755. icluster++;
  2756. }
  2757. k = tree[i].right;
  2758. if (k>=0)
  2759. { clusterid[k] = icluster;
  2760. icluster++;
  2761. }
  2762. }
  2763. nodeid = malloc(n*sizeof(int));
  2764. if(!nodeid)
  2765. { for (i = 0; i < nelements; i++) clusterid[i] = -1;
  2766. return;
  2767. }
  2768. for (i = 0; i < n; i++) nodeid[i] = -1;
  2769. for (i = n-1; i >= 0; i--)
  2770. { if(nodeid[i]<0)
  2771. { j = icluster;
  2772. nodeid[i] = j;
  2773. icluster++;
  2774. }
  2775. else j = nodeid[i];
  2776. k = tree[i].left;
  2777. if (k<0) nodeid[-k-1] = j; else clusterid[k] = j;
  2778. k = tree[i].right;
  2779. if (k<0) nodeid[-k-1] = j; else clusterid[k] = j;
  2780. }
  2781. free(nodeid);
  2782. return;
  2783. }
  2784. /* ******************************************************************** */
  2785. static
  2786. Node* pclcluster (int nrows, int ncolumns, double** data, int** mask,
  2787. double weight[], double** distmatrix, char dist, int transpose)
  2788. /*
  2789. Purpose
  2790. =======
  2791. The pclcluster routine performs clustering using pairwise centroid-linking
  2792. on a given set of gene expression data, using the distance metric given by dist.
  2793. Arguments
  2794. =========
  2795. nrows (input) int
  2796. The number of rows in the gene expression data matrix, equal to the number of
  2797. genes.
  2798. ncolumns (input) int
  2799. The number of columns in the gene expression data matrix, equal to the number of
  2800. microarrays.
  2801. data (input) double[nrows][ncolumns]
  2802. The array containing the gene expression data.
  2803. mask (input) int[nrows][ncolumns]
  2804. This array shows which data values are missing. If
  2805. mask[i][j] == 0, then data[i][j] is missing.
  2806. weight (input) double[ncolumns] if transpose==0;
  2807. double[nrows] if transpose==1
  2808. The weights that are used to calculate the distance. The length of this vector
  2809. is ncolumns if genes are being clustered, and nrows if microarrays are being
  2810. clustered.
  2811. transpose (input) int
  2812. If transpose==0, the rows of the matrix are clustered. Otherwise, columns
  2813. of the matrix are clustered.
  2814. dist (input) char
  2815. Defines which distance measure is used, as given by the table:
  2816. dist=='e': Euclidean distance
  2817. dist=='b': City-block distance
  2818. dist=='c': correlation
  2819. dist=='a': absolute value of the correlation
  2820. dist=='u': uncentered correlation
  2821. dist=='x': absolute uncentered correlation
  2822. dist=='s': Spearman's rank correlation
  2823. dist=='k': Kendall's tau
  2824. For other values of dist, the default (Euclidean distance) is used.
  2825. distmatrix (input) double**
  2826. The distance matrix. This matrix is precalculated by the calling routine
  2827. treecluster. The pclcluster routine modifies the contents of distmatrix, but
  2828. does not deallocate it.
  2829. Return value
  2830. ============
  2831. A pointer to a newly allocated array of Node structs, describing the
  2832. hierarchical clustering solution consisting of nelements-1 nodes. Depending on
  2833. whether genes (rows) or microarrays (columns) were clustered, nelements is
  2834. equal to nrows or ncolumns. See src/cluster.h for a description of the Node
  2835. structure.
  2836. If a memory error occurs, pclcluster returns NULL.
  2837. ========================================================================
  2838. */
  2839. { int i, j;
  2840. const int nelements = (transpose==0) ? nrows : ncolumns;
  2841. int inode;
  2842. const int ndata = transpose ? nrows : ncolumns;
  2843. const int nnodes = nelements - 1;
  2844. /* Set the metric function as indicated by dist */
  2845. double (*metric)
  2846. (int, double**, double**, int**, int**, const double[], int, int, int) =
  2847. setmetric(dist);
  2848. Node* result;
  2849. double** newdata;
  2850. int** newmask;
  2851. int* distid = malloc(nelements*sizeof(int));
  2852. if(!distid) return NULL;
  2853. result = malloc(nnodes*sizeof(Node));
  2854. if(!result)
  2855. { free(distid);
  2856. return NULL;
  2857. }
  2858. if(!makedatamask(nelements, ndata, &newdata, &newmask))
  2859. { free(result);
  2860. free(distid);
  2861. return NULL;
  2862. }
  2863. for (i = 0; i < nelements; i++) distid[i] = i;
  2864. /* To remember which row/column in the distance matrix contains what */
  2865. /* Storage for node data */
  2866. if (transpose)
  2867. { for (i = 0; i < nelements; i++)
  2868. { for (j = 0; j < ndata; j++)
  2869. { newdata[i][j] = data[j][i];
  2870. newmask[i][j] = mask[j][i];
  2871. }
  2872. }
  2873. data = newdata;
  2874. mask = newmask;
  2875. }
  2876. else
  2877. { for (i = 0; i < nelements; i++)
  2878. { memcpy(newdata[i], data[i], ndata*sizeof(double));
  2879. memcpy(newmask[i], mask[i], ndata*sizeof(int));
  2880. }
  2881. data = newdata;
  2882. mask = newmask;
  2883. }
  2884. for (inode = 0; inode < nnodes; inode++)
  2885. { /* Find the pair with the shortest distance */
  2886. int is = 1;
  2887. int js = 0;
  2888. result[inode].distance = find_closest_pair(nelements-inode, distmatrix, &is, &js);
  2889. result[inode].left = distid[js];
  2890. result[inode].right = distid[is];
  2891. /* Make node js the new node */
  2892. for (i = 0; i < ndata; i++)
  2893. { data[js][i] = data[js][i]*mask[js][i] + data[is][i]*mask[is][i];
  2894. mask[js][i] += mask[is][i];
  2895. if (mask[js][i]) data[js][i] /= mask[js][i];
  2896. }
  2897. free(data[is]);
  2898. free(mask[is]);
  2899. data[is] = data[nnodes-inode];
  2900. mask[is] = mask[nnodes-inode];
  2901. /* Fix the distances */
  2902. distid[is] = distid[nnodes-inode];
  2903. for (i = 0; i < is; i++)
  2904. distmatrix[is][i] = distmatrix[nnodes-inode][i];
  2905. for (i = is + 1; i < nnodes-inode; i++)
  2906. distmatrix[i][is] = distmatrix[nnodes-inode][i];
  2907. distid[js] = -inode-1;
  2908. for (i = 0; i < js; i++)
  2909. distmatrix[js][i] = metric(ndata,data,data,mask,mask,weight,js,i,0);
  2910. for (i = js + 1; i < nnodes-inode; i++)
  2911. distmatrix[i][js] = metric(ndata,data,data,mask,mask,weight,js,i,0);
  2912. }
  2913. /* Free temporarily allocated space */
  2914. free(data[0]);
  2915. free(mask[0]);
  2916. free(data);
  2917. free(mask);
  2918. free(distid);
  2919. return result;
  2920. }
  2921. /* ******************************************************************** */
  2922. static
  2923. int nodecompare(const void* a, const void* b)
  2924. /* Helper function for qsort. */
  2925. { const Node* node1 = (const Node*)a;
  2926. const Node* node2 = (const Node*)b;
  2927. const double term1 = node1->distance;
  2928. const double term2 = node2->distance;
  2929. if (term1 < term2) return -1;
  2930. if (term1 > term2) return +1;
  2931. return 0;
  2932. }
  2933. /* ---------------------------------------------------------------------- */
  2934. static
  2935. Node* pslcluster (int nrows, int ncolumns, double** data, int** mask,
  2936. double weight[], double** distmatrix, char dist, int transpose)
  2937. /*
  2938. Purpose
  2939. =======
  2940. The pslcluster routine performs single-linkage hierarchical clustering, using
  2941. either the distance matrix directly, if available, or by calculating the
  2942. distances from the data array. This implementation is based on the SLINK
  2943. algorithm, described in:
  2944. Sibson, R. (1973). SLINK: An optimally efficient algorithm for the single-link
  2945. cluster method. The Computer Journal, 16(1): 30-34.
  2946. The output of this algorithm is identical to conventional single-linkage
  2947. hierarchical clustering, but is much more memory-efficient and faster. Hence,
  2948. it can be applied to large data sets, for which the conventional single-
  2949. linkage algorithm fails due to lack of memory.
  2950. Arguments
  2951. =========
  2952. nrows (input) int
  2953. The number of rows in the gene expression data matrix, equal to the number of
  2954. genes.
  2955. ncolumns (input) int
  2956. The number of columns in the gene expression data matrix, equal to the number of
  2957. microarrays.
  2958. data (input) double[nrows][ncolumns]
  2959. The array containing the gene expression data.
  2960. mask (input) int[nrows][ncolumns]
  2961. This array shows which data values are missing. If
  2962. mask[i][j] == 0, then data[i][j] is missing.
  2963. weight (input) double[n]
  2964. The weights that are used to calculate the distance. The length of this vector
  2965. is ncolumns if genes are being clustered, and nrows if microarrays are being
  2966. clustered.
  2967. transpose (input) int
  2968. If transpose==0, the rows of the matrix are clustered. Otherwise, columns
  2969. of the matrix are clustered.
  2970. dist (input) char
  2971. Defines which distance measure is used, as given by the table:
  2972. dist=='e': Euclidean distance
  2973. dist=='b': City-block distance
  2974. dist=='c': correlation
  2975. dist=='a': absolute value of the correlation
  2976. dist=='u': uncentered correlation
  2977. dist=='x': absolute uncentered correlation
  2978. dist=='s': Spearman's rank correlation
  2979. dist=='k': Kendall's tau
  2980. For other values of dist, the default (Euclidean distance) is used.
  2981. distmatrix (input) double**
  2982. The distance matrix. If the distance matrix is passed by the calling routine
  2983. treecluster, it is used by pslcluster to speed up the clustering calculation.
  2984. The pslcluster routine does not modify the contents of distmatrix, and does
  2985. not deallocate it. If distmatrix is NULL, the pairwise distances are calculated
  2986. by the pslcluster routine from the gene expression data (the data and mask
  2987. arrays) and stored in temporary arrays. If distmatrix is passed, the original
  2988. gene expression data (specified by the data and mask arguments) are not needed
  2989. and are therefore ignored.
  2990. Return value
  2991. ============
  2992. A pointer to a newly allocated array of Node structs, describing the
  2993. hierarchical clustering solution consisting of nelements-1 nodes. Depending on
  2994. whether genes (rows) or microarrays (columns) were clustered, nelements is
  2995. equal to nrows or ncolumns. See src/cluster.h for a description of the Node
  2996. structure.
  2997. If a memory error occurs, pslcluster returns NULL.
  2998. ========================================================================
  2999. */
  3000. { int i, j, k;
  3001. const int nelements = transpose ? ncolumns : nrows;
  3002. const int nnodes = nelements - 1;
  3003. int* vector;
  3004. double* temp;
  3005. int* index;
  3006. Node* result;
  3007. temp = malloc(nnodes*sizeof(double));
  3008. if(!temp) return NULL;
  3009. index = malloc(nelements*sizeof(int));
  3010. if(!index)
  3011. { free(temp);
  3012. return NULL;
  3013. }
  3014. vector = malloc(nnodes*sizeof(int));
  3015. if(!vector)
  3016. { free(index);
  3017. free(temp);
  3018. return NULL;
  3019. }
  3020. result = malloc(nelements*sizeof(Node));
  3021. if(!result)
  3022. { free(vector);
  3023. free(index);
  3024. free(temp);
  3025. return NULL;
  3026. }
  3027. for (i = 0; i < nnodes; i++) vector[i] = i;
  3028. if(distmatrix)
  3029. { for (i = 0; i < nrows; i++)
  3030. { result[i].distance = DBL_MAX;
  3031. for (j = 0; j < i; j++) temp[j] = distmatrix[i][j];
  3032. for (j = 0; j < i; j++)
  3033. { k = vector[j];
  3034. if (result[j].distance >= temp[j])
  3035. { if (result[j].distance < temp[k]) temp[k] = result[j].distance;
  3036. result[j].distance = temp[j];
  3037. vector[j] = i;
  3038. }
  3039. else if (temp[j] < temp[k]) temp[k] = temp[j];
  3040. }
  3041. for (j = 0; j < i; j++)
  3042. {
  3043. if (result[j].distance >= result[vector[j]].distance) vector[j] = i;
  3044. }
  3045. }
  3046. }
  3047. else
  3048. { const int ndata = transpose ? nrows : ncolumns;
  3049. /* Set the metric function as indicated by dist */
  3050. double (*metric)
  3051. (int, double**, double**, int**, int**, const double[], int, int, int) =
  3052. setmetric(dist);
  3053. for (i = 0; i < nelements; i++)
  3054. { result[i].distance = DBL_MAX;
  3055. for (j = 0; j < i; j++) temp[j] =
  3056. metric(ndata, data, data, mask, mask, weight, i, j, transpose);
  3057. for (j = 0; j < i; j++)
  3058. { k = vector[j];
  3059. if (result[j].distance >= temp[j])
  3060. { if (result[j].distance < temp[k]) temp[k] = result[j].distance;
  3061. result[j].distance = temp[j];
  3062. vector[j] = i;
  3063. }
  3064. else if (temp[j] < temp[k]) temp[k] = temp[j];
  3065. }
  3066. for (j = 0; j < i; j++)
  3067. if (result[j].distance >= result[vector[j]].distance) vector[j] = i;
  3068. }
  3069. }
  3070. free(temp);
  3071. for (i = 0; i < nnodes; i++) result[i].left = i;
  3072. qsort(result, nnodes, sizeof(Node), nodecompare);
  3073. for (i = 0; i < nelements; i++) index[i] = i;
  3074. for (i = 0; i < nnodes; i++)
  3075. { j = result[i].left;
  3076. k = vector[j];
  3077. result[i].left = index[j];
  3078. result[i].right = index[k];
  3079. index[k] = -i-1;
  3080. }
  3081. free(vector);
  3082. free(index);
  3083. result = realloc(result, nnodes*sizeof(Node));
  3084. return result;
  3085. }
  3086. /* ******************************************************************** */
  3087. static Node* pmlcluster (int nelements, double** distmatrix)
  3088. /*
  3089. Purpose
  3090. =======
  3091. The pmlcluster routine performs clustering using pairwise maximum- (complete-)
  3092. linking on the given distance matrix.
  3093. Arguments
  3094. =========
  3095. nelements (input) int
  3096. The number of elements to be clustered.
  3097. distmatrix (input) double**
  3098. The distance matrix, with nelements rows, each row being filled up to the
  3099. diagonal. The elements on the diagonal are not used, as they are assumed to be
  3100. zero. The distance matrix will be modified by this routine.
  3101. Return value
  3102. ============
  3103. A pointer to a newly allocated array of Node structs, describing the
  3104. hierarchical clustering solution consisting of nelements-1 nodes. Depending on
  3105. whether genes (rows) or microarrays (columns) were clustered, nelements is
  3106. equal to nrows or ncolumns. See src/cluster.h for a description of the Node
  3107. structure.
  3108. If a memory error occurs, pmlcluster returns NULL.
  3109. ========================================================================
  3110. */
  3111. { int j;
  3112. int n;
  3113. int* clusterid;
  3114. Node* result;
  3115. clusterid = malloc(nelements*sizeof(int));
  3116. if(!clusterid) return NULL;
  3117. result = malloc((nelements-1)*sizeof(Node));
  3118. if (!result)
  3119. { free(clusterid);
  3120. return NULL;
  3121. }
  3122. /* Setup a list specifying to which cluster a gene belongs */
  3123. for (j = 0; j < nelements; j++) clusterid[j] = j;
  3124. for (n = nelements; n > 1; n--)
  3125. { int is = 1;
  3126. int js = 0;
  3127. result[nelements-n].distance = find_closest_pair(n, distmatrix, &is, &js);
  3128. /* Fix the distances */
  3129. for (j = 0; j < js; j++)
  3130. distmatrix[js][j] = max(distmatrix[is][j],distmatrix[js][j]);
  3131. for (j = js+1; j < is; j++)
  3132. distmatrix[j][js] = max(distmatrix[is][j],distmatrix[j][js]);
  3133. for (j = is+1; j < n; j++)
  3134. distmatrix[j][js] = max(distmatrix[j][is],distmatrix[j][js]);
  3135. for (j = 0; j < is; j++) distmatrix[is][j] = distmatrix[n-1][j];
  3136. for (j = is+1; j < n-1; j++) distmatrix[j][is] = distmatrix[n-1][j];
  3137. /* Update clusterids */
  3138. result[nelements-n].left = clusterid[is];
  3139. result[nelements-n].right = clusterid[js];
  3140. clusterid[js] = n-nelements-1;
  3141. clusterid[is] = clusterid[n-1];
  3142. }
  3143. free(clusterid);
  3144. return result;
  3145. }
  3146. /* ******************************************************************* */
  3147. static Node* palcluster (int nelements, double** distmatrix)
  3148. /*
  3149. Purpose
  3150. =======
  3151. The palcluster routine performs clustering using pairwise average
  3152. linking on the given distance matrix.
  3153. Arguments
  3154. =========
  3155. nelements (input) int
  3156. The number of elements to be clustered.
  3157. distmatrix (input) double**
  3158. The distance matrix, with nelements rows, each row being filled up to the
  3159. diagonal. The elements on the diagonal are not used, as they are assumed to be
  3160. zero. The distance matrix will be modified by this routine.
  3161. Return value
  3162. ============
  3163. A pointer to a newly allocated array of Node structs, describing the
  3164. hierarchical clustering solution consisting of nelements-1 nodes. Depending on
  3165. whether genes (rows) or microarrays (columns) were clustered, nelements is
  3166. equal to nrows or ncolumns. See src/cluster.h for a description of the Node
  3167. structure.
  3168. If a memory error occurs, palcluster returns NULL.
  3169. ========================================================================
  3170. */
  3171. { int j;
  3172. int n;
  3173. int* clusterid;
  3174. int* number;
  3175. Node* result;
  3176. clusterid = malloc(nelements*sizeof(int));
  3177. if(!clusterid) return NULL;
  3178. number = malloc(nelements*sizeof(int));
  3179. if(!number)
  3180. { free(clusterid);
  3181. return NULL;
  3182. }
  3183. result = malloc((nelements-1)*sizeof(Node));
  3184. if (!result)
  3185. { free(clusterid);
  3186. free(number);
  3187. return NULL;
  3188. }
  3189. /* Setup a list specifying to which cluster a gene belongs, and keep track
  3190. * of the number of elements in each cluster (needed to calculate the
  3191. * average). */
  3192. for (j = 0; j < nelements; j++)
  3193. { number[j] = 1;
  3194. clusterid[j] = j;
  3195. }
  3196. for (n = nelements; n > 1; n--)
  3197. { int sum;
  3198. int is = 1;
  3199. int js = 0;
  3200. result[nelements-n].distance = find_closest_pair(n, distmatrix, &is, &js);
  3201. /* Save result */
  3202. result[nelements-n].left = clusterid[is];
  3203. result[nelements-n].right = clusterid[js];
  3204. /* Fix the distances */
  3205. sum = number[is] + number[js];
  3206. for (j = 0; j < js; j++)
  3207. { distmatrix[js][j] = distmatrix[is][j]*number[is]
  3208. + distmatrix[js][j]*number[js];
  3209. distmatrix[js][j] /= sum;
  3210. }
  3211. for (j = js+1; j < is; j++)
  3212. { distmatrix[j][js] = distmatrix[is][j]*number[is]
  3213. + distmatrix[j][js]*number[js];
  3214. distmatrix[j][js] /= sum;
  3215. }
  3216. for (j = is+1; j < n; j++)
  3217. { distmatrix[j][js] = distmatrix[j][is]*number[is]
  3218. + distmatrix[j][js]*number[js];
  3219. distmatrix[j][js] /= sum;
  3220. }
  3221. for (j = 0; j < is; j++) distmatrix[is][j] = distmatrix[n-1][j];
  3222. for (j = is+1; j < n-1; j++) distmatrix[j][is] = distmatrix[n-1][j];
  3223. /* Update number of elements in the clusters */
  3224. number[js] = sum;
  3225. number[is] = number[n-1];
  3226. /* Update clusterids */
  3227. clusterid[js] = n-nelements-1;
  3228. clusterid[is] = clusterid[n-1];
  3229. }
  3230. free(clusterid);
  3231. free(number);
  3232. return result;
  3233. }
  3234. /* ******************************************************************* */
  3235. Node* treecluster (int nrows, int ncolumns, double** data, int** mask,
  3236. double *weight, int transpose, char dist, char method, double** distmatrix)
  3237. /*
  3238. Purpose
  3239. =======
  3240. The treecluster routine performs hierarchical clustering using pairwise
  3241. single-, maximum-, centroid-, or average-linkage, as defined by method, on a
  3242. given set of gene expression data, using the distance metric given by dist.
  3243. If successful, the function returns a pointer to a newly allocated Tree struct
  3244. containing the hierarchical clustering solution, and NULL if a memory error
  3245. occurs. The pointer should be freed by the calling routine to prevent memory
  3246. leaks.
  3247. Arguments
  3248. =========
  3249. nrows (input) int
  3250. The number of rows in the data matrix, equal to the number of genes.
  3251. ncolumns (input) int
  3252. The number of columns in the data matrix, equal to the number of microarrays.
  3253. data (input) double[nrows][ncolumns]
  3254. The array containing the data of the vectors to be clustered.
  3255. mask (input) int[nrows][ncolumns]
  3256. This array shows which data values are missing. If mask[i][j]==0, then
  3257. data[i][j] is missing.
  3258. weight (input) double array[n]
  3259. The weights that are used to calculate the distance.
  3260. transpose (input) int
  3261. If transpose==0, the rows of the matrix are clustered. Otherwise, columns
  3262. of the matrix are clustered.
  3263. dist (input) char
  3264. Defines which distance measure is used, as given by the table:
  3265. dist=='e': Euclidean distance
  3266. dist=='b': City-block distance
  3267. dist=='c': correlation
  3268. dist=='a': absolute value of the correlation
  3269. dist=='u': uncentered correlation
  3270. dist=='x': absolute uncentered correlation
  3271. dist=='s': Spearman's rank correlation
  3272. dist=='k': Kendall's tau
  3273. For other values of dist, the default (Euclidean distance) is used.
  3274. method (input) char
  3275. Defines which hierarchical clustering method is used:
  3276. method=='s': pairwise single-linkage clustering
  3277. method=='m': pairwise maximum- (or complete-) linkage clustering
  3278. method=='a': pairwise average-linkage clustering
  3279. method=='c': pairwise centroid-linkage clustering
  3280. For the first three, either the distance matrix or the gene expression data is
  3281. sufficient to perform the clustering algorithm. For pairwise centroid-linkage
  3282. clustering, however, the gene expression data are always needed, even if the
  3283. distance matrix itself is available.
  3284. distmatrix (input) double**
  3285. The distance matrix. If the distance matrix is zero initially, the distance
  3286. matrix will be allocated and calculated from the data by treecluster, and
  3287. deallocated before treecluster returns. If the distance matrix is passed by the
  3288. calling routine, treecluster will modify the contents of the distance matrix as
  3289. part of the clustering algorithm, but will not deallocate it. The calling
  3290. routine should deallocate the distance matrix after the return from treecluster.
  3291. Return value
  3292. ============
  3293. A pointer to a newly allocated array of Node structs, describing the
  3294. hierarchical clustering solution consisting of nelements-1 nodes. Depending on
  3295. whether genes (rows) or microarrays (columns) were clustered, nelements is
  3296. equal to nrows or ncolumns. See src/cluster.h for a description of the Node
  3297. structure.
  3298. If a memory error occurs, treecluster returns NULL.
  3299. ========================================================================
  3300. */
  3301. { Node* result = NULL;
  3302. const int nelements = (transpose==0) ? nrows : ncolumns;
  3303. const int ldistmatrix = (distmatrix==NULL && method!='s') ? 1 : 0;
  3304. if (nelements < 2) return NULL;
  3305. /* Calculate the distance matrix if the user didn't give it */
  3306. if(ldistmatrix)
  3307. { distmatrix =
  3308. distancematrix(nrows, ncolumns, data, mask, weight, dist, transpose);
  3309. if (!distmatrix) return NULL; /* Insufficient memory */
  3310. }
  3311. switch(method)
  3312. { case 's':
  3313. result = pslcluster(nrows, ncolumns, data, mask, weight, distmatrix,
  3314. dist, transpose);
  3315. break;
  3316. case 'm':
  3317. result = pmlcluster(nelements, distmatrix);
  3318. break;
  3319. case 'a':
  3320. result = palcluster(nelements, distmatrix);
  3321. break;
  3322. case 'c':
  3323. result = pclcluster(nrows, ncolumns, data, mask, weight, distmatrix,
  3324. dist, transpose);
  3325. break;
  3326. }
  3327. /* Deallocate space for distance matrix, if it was allocated by treecluster */
  3328. if(ldistmatrix)
  3329. { int i;
  3330. for (i = 1; i < nelements; i++) free(distmatrix[i]);
  3331. free (distmatrix);
  3332. }
  3333. return result;
  3334. }
  3335. /* ******************************************************************* */
  3336. static
  3337. void somworker (int nrows, int ncolumns, double** data, int** mask,
  3338. const double weights[], int transpose, int nxgrid, int nygrid,
  3339. double inittau, double*** celldata, int niter, char dist)
  3340. { const int nelements = (transpose==0) ? nrows : ncolumns;
  3341. const int ndata = (transpose==0) ? ncolumns : nrows;
  3342. int i, j;
  3343. double* stddata = calloc(nelements,sizeof(double));
  3344. int** dummymask;
  3345. int ix, iy;
  3346. int* index;
  3347. int iter;
  3348. /* Maximum radius in which nodes are adjusted */
  3349. double maxradius = sqrt(nxgrid*nxgrid+nygrid*nygrid);
  3350. /* Set the metric function as indicated by dist */
  3351. double (*metric)
  3352. (int, double**, double**, int**, int**, const double[], int, int, int) =
  3353. setmetric(dist);
  3354. /* Calculate the standard deviation for each row or column */
  3355. if (transpose==0)
  3356. { for (i = 0; i < nelements; i++)
  3357. { int n = 0;
  3358. for (j = 0; j < ndata; j++)
  3359. { if (mask[i][j])
  3360. { double term = data[i][j];
  3361. term = term * term;
  3362. stddata[i] += term;
  3363. n++;
  3364. }
  3365. }
  3366. if (stddata[i] > 0) stddata[i] = sqrt(stddata[i]/n);
  3367. else stddata[i] = 1;
  3368. }
  3369. }
  3370. else
  3371. { for (i = 0; i < nelements; i++)
  3372. { int n = 0;
  3373. for (j = 0; j < ndata; j++)
  3374. { if (mask[j][i])
  3375. { double term = data[j][i];
  3376. term = term * term;
  3377. stddata[i] += term;
  3378. n++;
  3379. }
  3380. }
  3381. if (stddata[i] > 0) stddata[i] = sqrt(stddata[i]/n);
  3382. else stddata[i] = 1;
  3383. }
  3384. }
  3385. if (transpose==0)
  3386. { dummymask = malloc(nygrid*sizeof(int*));
  3387. for (i = 0; i < nygrid; i++)
  3388. { dummymask[i] = malloc(ndata*sizeof(int));
  3389. for (j = 0; j < ndata; j++) dummymask[i][j] = 1;
  3390. }
  3391. }
  3392. else
  3393. { dummymask = malloc(ndata*sizeof(int*));
  3394. for (i = 0; i < ndata; i++)
  3395. { dummymask[i] = malloc(sizeof(int));
  3396. dummymask[i][0] = 1;
  3397. }
  3398. }
  3399. /* Randomly initialize the nodes */
  3400. for (ix = 0; ix < nxgrid; ix++)
  3401. { for (iy = 0; iy < nygrid; iy++)
  3402. { double sum = 0.;
  3403. for (i = 0; i < ndata; i++)
  3404. { double term = -1.0 + 2.0*uniform();
  3405. celldata[ix][iy][i] = term;
  3406. sum += term * term;
  3407. }
  3408. sum = sqrt(sum/ndata);
  3409. for (i = 0; i < ndata; i++) celldata[ix][iy][i] /= sum;
  3410. }
  3411. }
  3412. /* Randomize the order in which genes or arrays will be used */
  3413. index = malloc(nelements*sizeof(int));
  3414. for (i = 0; i < nelements; i++) index[i] = i;
  3415. for (i = 0; i < nelements; i++)
  3416. { j = (int) (i + (nelements-i)*uniform());
  3417. ix = index[j];
  3418. index[j] = index[i];
  3419. index[i] = ix;
  3420. }
  3421. /* Start the iteration */
  3422. for (iter = 0; iter < niter; iter++)
  3423. { int ixbest = 0;
  3424. int iybest = 0;
  3425. int iobject = iter % nelements;
  3426. iobject = index[iobject];
  3427. if (transpose==0)
  3428. { double closest = metric(ndata,data,celldata[ixbest],
  3429. mask,dummymask,weights,iobject,iybest,transpose);
  3430. double radius = maxradius * (1. - ((double)iter)/((double)niter));
  3431. double tau = inittau * (1. - ((double)iter)/((double)niter));
  3432. for (ix = 0; ix < nxgrid; ix++)
  3433. { for (iy = 0; iy < nygrid; iy++)
  3434. { double distance =
  3435. metric (ndata,data,celldata[ix],
  3436. mask,dummymask,weights,iobject,iy,transpose);
  3437. if (distance < closest)
  3438. { ixbest = ix;
  3439. iybest = iy;
  3440. closest = distance;
  3441. }
  3442. }
  3443. }
  3444. for (ix = 0; ix < nxgrid; ix++)
  3445. { for (iy = 0; iy < nygrid; iy++)
  3446. { if (sqrt((ix-ixbest)*(ix-ixbest)+(iy-iybest)*(iy-iybest))<radius)
  3447. { double sum = 0.;
  3448. for (i = 0; i < ndata; i++)
  3449. { if (mask[iobject][i]==0) continue;
  3450. celldata[ix][iy][i] +=
  3451. tau * (data[iobject][i]/stddata[iobject]-celldata[ix][iy][i]);
  3452. }
  3453. for (i = 0; i < ndata; i++)
  3454. { double term = celldata[ix][iy][i];
  3455. term = term * term;
  3456. sum += term;
  3457. }
  3458. if (sum>0)
  3459. { sum = sqrt(sum/ndata);
  3460. for (i = 0; i < ndata; i++) celldata[ix][iy][i] /= sum;
  3461. }
  3462. }
  3463. }
  3464. }
  3465. }
  3466. else
  3467. { double closest;
  3468. double** celldatavector = malloc(ndata*sizeof(double*));
  3469. double radius = maxradius * (1. - ((double)iter)/((double)niter));
  3470. double tau = inittau * (1. - ((double)iter)/((double)niter));
  3471. for (i = 0; i < ndata; i++)
  3472. celldatavector[i] = &(celldata[ixbest][iybest][i]);
  3473. closest = metric(ndata,data,celldatavector,
  3474. mask,dummymask,weights,iobject,0,transpose);
  3475. for (ix = 0; ix < nxgrid; ix++)
  3476. { for (iy = 0; iy < nygrid; iy++)
  3477. { double distance;
  3478. for (i = 0; i < ndata; i++)
  3479. celldatavector[i] = &(celldata[ixbest][iybest][i]);
  3480. distance =
  3481. metric (ndata,data,celldatavector,
  3482. mask,dummymask,weights,iobject,0,transpose);
  3483. if (distance < closest)
  3484. { ixbest = ix;
  3485. iybest = iy;
  3486. closest = distance;
  3487. }
  3488. }
  3489. }
  3490. free(celldatavector);
  3491. for (ix = 0; ix < nxgrid; ix++)
  3492. { for (iy = 0; iy < nygrid; iy++)
  3493. { if (sqrt((ix-ixbest)*(ix-ixbest)+(iy-iybest)*(iy-iybest))<radius)
  3494. { double sum = 0.;
  3495. for (i = 0; i < ndata; i++)
  3496. { if (mask[i][iobject]==0) continue;
  3497. celldata[ix][iy][i] +=
  3498. tau * (data[i][iobject]/stddata[iobject]-celldata[ix][iy][i]);
  3499. }
  3500. for (i = 0; i < ndata; i++)
  3501. { double term = celldata[ix][iy][i];
  3502. term = term * term;
  3503. sum += term;
  3504. }
  3505. if (sum>0)
  3506. { sum = sqrt(sum/ndata);
  3507. for (i = 0; i < ndata; i++) celldata[ix][iy][i] /= sum;
  3508. }
  3509. }
  3510. }
  3511. }
  3512. }
  3513. }
  3514. if (transpose==0)
  3515. for (i = 0; i < nygrid; i++) free(dummymask[i]);
  3516. else
  3517. for (i = 0; i < ndata; i++) free(dummymask[i]);
  3518. free(dummymask);
  3519. free(stddata);
  3520. free(index);
  3521. return;
  3522. }
  3523. /* ******************************************************************* */
  3524. static
  3525. void somassign (int nrows, int ncolumns, double** data, int** mask,
  3526. const double weights[], int transpose, int nxgrid, int nygrid,
  3527. double*** celldata, char dist, int clusterid[][2])
  3528. /* Collect clusterids */
  3529. { const int ndata = (transpose==0) ? ncolumns : nrows;
  3530. int i,j;
  3531. /* Set the metric function as indicated by dist */
  3532. double (*metric)
  3533. (int, double**, double**, int**, int**, const double[], int, int, int) =
  3534. setmetric(dist);
  3535. if (transpose==0)
  3536. { int** dummymask = malloc(nygrid*sizeof(int*));
  3537. for (i = 0; i < nygrid; i++)
  3538. { dummymask[i] = malloc(ncolumns*sizeof(int));
  3539. for (j = 0; j < ncolumns; j++) dummymask[i][j] = 1;
  3540. }
  3541. for (i = 0; i < nrows; i++)
  3542. { int ixbest = 0;
  3543. int iybest = 0;
  3544. double closest = metric(ndata,data,celldata[ixbest],
  3545. mask,dummymask,weights,i,iybest,transpose);
  3546. int ix, iy;
  3547. for (ix = 0; ix < nxgrid; ix++)
  3548. { for (iy = 0; iy < nygrid; iy++)
  3549. { double distance =
  3550. metric (ndata,data,celldata[ix],
  3551. mask,dummymask,weights,i,iy,transpose);
  3552. if (distance < closest)
  3553. { ixbest = ix;
  3554. iybest = iy;
  3555. closest = distance;
  3556. }
  3557. }
  3558. }
  3559. clusterid[i][0] = ixbest;
  3560. clusterid[i][1] = iybest;
  3561. }
  3562. for (i = 0; i < nygrid; i++) free(dummymask[i]);
  3563. free(dummymask);
  3564. }
  3565. else
  3566. { double** celldatavector = malloc(ndata*sizeof(double*));
  3567. int** dummymask = malloc(nrows*sizeof(int*));
  3568. int ixbest = 0;
  3569. int iybest = 0;
  3570. for (i = 0; i < nrows; i++)
  3571. { dummymask[i] = malloc(sizeof(int));
  3572. dummymask[i][0] = 1;
  3573. }
  3574. for (i = 0; i < ncolumns; i++)
  3575. { double closest;
  3576. int ix, iy;
  3577. for (j = 0; j < ndata; j++)
  3578. celldatavector[j] = &(celldata[ixbest][iybest][j]);
  3579. closest = metric(ndata,data,celldatavector,
  3580. mask,dummymask,weights,i,0,transpose);
  3581. for (ix = 0; ix < nxgrid; ix++)
  3582. { for (iy = 0; iy < nygrid; iy++)
  3583. { double distance;
  3584. for(j = 0; j < ndata; j++)
  3585. celldatavector[j] = &(celldata[ix][iy][j]);
  3586. distance = metric(ndata,data,celldatavector,
  3587. mask,dummymask,weights,i,0,transpose);
  3588. if (distance < closest)
  3589. { ixbest = ix;
  3590. iybest = iy;
  3591. closest = distance;
  3592. }
  3593. }
  3594. }
  3595. clusterid[i][0] = ixbest;
  3596. clusterid[i][1] = iybest;
  3597. }
  3598. free(celldatavector);
  3599. for (i = 0; i < nrows; i++) free(dummymask[i]);
  3600. free(dummymask);
  3601. }
  3602. return;
  3603. }
  3604. /* ******************************************************************* */
  3605. void somcluster (int nrows, int ncolumns, double** data, int** mask,
  3606. const double weight[], int transpose, int nxgrid, int nygrid,
  3607. double inittau, int niter, char dist, double*** celldata, int clusterid[][2])
  3608. /*
  3609. Purpose
  3610. =======
  3611. The somcluster routine implements a self-organizing map (Kohonen) on a
  3612. rectangular grid, using a given set of vectors. The distance measure to be
  3613. used to find the similarity between genes and nodes is given by dist.
  3614. Arguments
  3615. =========
  3616. nrows (input) int
  3617. The number of rows in the data matrix, equal to the number of genes.
  3618. ncolumns (input) int
  3619. The number of columns in the data matrix, equal to the number of microarrays.
  3620. data (input) double[nrows][ncolumns]
  3621. The array containing the gene expression data.
  3622. mask (input) int[nrows][ncolumns]
  3623. This array shows which data values are missing. If
  3624. mask[i][j] == 0, then data[i][j] is missing.
  3625. weights (input) double[ncolumns] if transpose==0;
  3626. double[nrows] if transpose==1
  3627. The weights that are used to calculate the distance. The length of this vector
  3628. is ncolumns if genes are being clustered, or nrows if microarrays are being
  3629. clustered.
  3630. transpose (input) int
  3631. If transpose==0, the rows (genes) of the matrix are clustered. Otherwise,
  3632. columns (microarrays) of the matrix are clustered.
  3633. nxgrid (input) int
  3634. The number of grid cells horizontally in the rectangular topology of clusters.
  3635. nygrid (input) int
  3636. The number of grid cells horizontally in the rectangular topology of clusters.
  3637. inittau (input) double
  3638. The initial value of tau, representing the neighborhood function.
  3639. niter (input) int
  3640. The number of iterations to be performed.
  3641. dist (input) char
  3642. Defines which distance measure is used, as given by the table:
  3643. dist=='e': Euclidean distance
  3644. dist=='b': City-block distance
  3645. dist=='c': correlation
  3646. dist=='a': absolute value of the correlation
  3647. dist=='u': uncentered correlation
  3648. dist=='x': absolute uncentered correlation
  3649. dist=='s': Spearman's rank correlation
  3650. dist=='k': Kendall's tau
  3651. For other values of dist, the default (Euclidean distance) is used.
  3652. celldata (output) double[nxgrid][nygrid][ncolumns] if transpose==0;
  3653. double[nxgrid][nygrid][nrows] if tranpose==1
  3654. The gene expression data for each node (cell) in the 2D grid. This can be
  3655. interpreted as the centroid for the cluster corresponding to that cell. If
  3656. celldata is NULL, then the centroids are not returned. If celldata is not
  3657. NULL, enough space should be allocated to store the centroid data before callingsomcluster.
  3658. clusterid (output), int[nrows][2] if transpose==0;
  3659. int[ncolumns][2] if transpose==1
  3660. For each item (gene or microarray) that is clustered, the coordinates of the
  3661. cell in the 2D grid to which the item was assigned. If clusterid is NULL, the
  3662. cluster assignments are not returned. If clusterid is not NULL, enough memory
  3663. should be allocated to store the clustering information before calling
  3664. somcluster.
  3665. ========================================================================
  3666. */
  3667. { const int nobjects = (transpose==0) ? nrows : ncolumns;
  3668. const int ndata = (transpose==0) ? ncolumns : nrows;
  3669. int i,j;
  3670. const int lcelldata = (celldata==NULL) ? 0 : 1;
  3671. if (nobjects < 2) return;
  3672. if (lcelldata==0)
  3673. { celldata = malloc(nxgrid*nygrid*ndata*sizeof(double**));
  3674. for (i = 0; i < nxgrid; i++)
  3675. { celldata[i] = malloc(nygrid*ndata*sizeof(double*));
  3676. for (j = 0; j < nygrid; j++)
  3677. celldata[i][j] = malloc(ndata*sizeof(double));
  3678. }
  3679. }
  3680. somworker (nrows, ncolumns, data, mask, weight, transpose, nxgrid, nygrid,
  3681. inittau, celldata, niter, dist);
  3682. if (clusterid)
  3683. somassign (nrows, ncolumns, data, mask, weight, transpose,
  3684. nxgrid, nygrid, celldata, dist, clusterid);
  3685. if(lcelldata==0)
  3686. { for (i = 0; i < nxgrid; i++)
  3687. for (j = 0; j < nygrid; j++)
  3688. free(celldata[i][j]);
  3689. for (i = 0; i < nxgrid; i++)
  3690. free(celldata[i]);
  3691. free(celldata);
  3692. }
  3693. return;
  3694. }
  3695. /* ******************************************************************** */
  3696. double clusterdistance (int nrows, int ncolumns, double** data,
  3697. int** mask, double weight[], int n1, int n2, int index1[], int index2[],
  3698. char dist, char method, int transpose)
  3699. /*
  3700. Purpose
  3701. =======
  3702. The clusterdistance routine calculates the distance between two clusters
  3703. containing genes or microarrays using the measured gene expression vectors. The
  3704. distance between clusters, given the genes/microarrays in each cluster, can be
  3705. defined in several ways. Several distance measures can be used.
  3706. The routine returns the distance in double precision.
  3707. If the parameter transpose is set to a nonzero value, the clusters are
  3708. interpreted as clusters of microarrays, otherwise as clusters of gene.
  3709. Arguments
  3710. =========
  3711. nrows (input) int
  3712. The number of rows (i.e., the number of genes) in the gene expression data
  3713. matrix.
  3714. ncolumns (input) int
  3715. The number of columns (i.e., the number of microarrays) in the gene expression
  3716. data matrix.
  3717. data (input) double[nrows][ncolumns]
  3718. The array containing the data of the vectors.
  3719. mask (input) int[nrows][ncolumns]
  3720. This array shows which data values are missing. If mask[i][j]==0, then
  3721. data[i][j] is missing.
  3722. weight (input) double[ncolumns] if transpose==0;
  3723. double[nrows] if transpose==1
  3724. The weights that are used to calculate the distance.
  3725. n1 (input) int
  3726. The number of elements in the first cluster.
  3727. n2 (input) int
  3728. The number of elements in the second cluster.
  3729. index1 (input) int[n1]
  3730. Identifies which genes/microarrays belong to the first cluster.
  3731. index2 (input) int[n2]
  3732. Identifies which genes/microarrays belong to the second cluster.
  3733. dist (input) char
  3734. Defines which distance measure is used, as given by the table:
  3735. dist=='e': Euclidean distance
  3736. dist=='b': City-block distance
  3737. dist=='c': correlation
  3738. dist=='a': absolute value of the correlation
  3739. dist=='u': uncentered correlation
  3740. dist=='x': absolute uncentered correlation
  3741. dist=='s': Spearman's rank correlation
  3742. dist=='k': Kendall's tau
  3743. For other values of dist, the default (Euclidean distance) is used.
  3744. method (input) char
  3745. Defines how the distance between two clusters is defined, given which genes
  3746. belong to which cluster:
  3747. method=='a': the distance between the arithmetic means of the two clusters
  3748. method=='m': the distance between the medians of the two clusters
  3749. method=='s': the smallest pairwise distance between members of the two clusters
  3750. method=='x': the largest pairwise distance between members of the two clusters
  3751. method=='v': average of the pairwise distances between members of the clusters
  3752. transpose (input) int
  3753. If transpose is equal to zero, the distances between the rows is
  3754. calculated. Otherwise, the distances between the columns is calculated.
  3755. The former is needed when genes are being clustered; the latter is used
  3756. when microarrays are being clustered.
  3757. ========================================================================
  3758. */
  3759. { /* Set the metric function as indicated by dist */
  3760. double (*metric)
  3761. (int, double**, double**, int**, int**, const double[], int, int, int) =
  3762. setmetric(dist);
  3763. /* if one or both clusters are empty, return */
  3764. if (n1 < 1 || n2 < 1) return -1.0;
  3765. /* Check the indices */
  3766. if (transpose==0)
  3767. { int i;
  3768. for (i = 0; i < n1; i++)
  3769. { int index = index1[i];
  3770. if (index < 0 || index >= nrows) return -1.0;
  3771. }
  3772. for (i = 0; i < n2; i++)
  3773. { int index = index2[i];
  3774. if (index < 0 || index >= nrows) return -1.0;
  3775. }
  3776. }
  3777. else
  3778. { int i;
  3779. for (i = 0; i < n1; i++)
  3780. { int index = index1[i];
  3781. if (index < 0 || index >= ncolumns) return -1.0;
  3782. }
  3783. for (i = 0; i < n2; i++)
  3784. { int index = index2[i];
  3785. if (index < 0 || index >= ncolumns) return -1.0;
  3786. }
  3787. }
  3788. switch (method)
  3789. { case 'a':
  3790. { /* Find the center */
  3791. int i,j,k;
  3792. if (transpose==0)
  3793. { double distance;
  3794. double* cdata[2];
  3795. int* cmask[2];
  3796. int* count[2];
  3797. count[0] = calloc(ncolumns,sizeof(int));
  3798. count[1] = calloc(ncolumns,sizeof(int));
  3799. cdata[0] = calloc(ncolumns,sizeof(double));
  3800. cdata[1] = calloc(ncolumns,sizeof(double));
  3801. cmask[0] = malloc(ncolumns*sizeof(int));
  3802. cmask[1] = malloc(ncolumns*sizeof(int));
  3803. for (i = 0; i < n1; i++)
  3804. { k = index1[i];
  3805. for (j = 0; j < ncolumns; j++)
  3806. if (mask[k][j] != 0)
  3807. { cdata[0][j] = cdata[0][j] + data[k][j];
  3808. count[0][j] = count[0][j] + 1;
  3809. }
  3810. }
  3811. for (i = 0; i < n2; i++)
  3812. { k = index2[i];
  3813. for (j = 0; j < ncolumns; j++)
  3814. if (mask[k][j] != 0)
  3815. { cdata[1][j] = cdata[1][j] + data[k][j];
  3816. count[1][j] = count[1][j] + 1;
  3817. }
  3818. }
  3819. for (i = 0; i < 2; i++)
  3820. for (j = 0; j < ncolumns; j++)
  3821. { if (count[i][j]>0)
  3822. { cdata[i][j] = cdata[i][j] / count[i][j];
  3823. cmask[i][j] = 1;
  3824. }
  3825. else
  3826. cmask[i][j] = 0;
  3827. }
  3828. distance =
  3829. metric (ncolumns,cdata,cdata,cmask,cmask,weight,0,1,0);
  3830. for (i = 0; i < 2; i++)
  3831. { free (cdata[i]);
  3832. free (cmask[i]);
  3833. free (count[i]);
  3834. }
  3835. return distance;
  3836. }
  3837. else
  3838. { double distance;
  3839. int** count = malloc(nrows*sizeof(int*));
  3840. double** cdata = malloc(nrows*sizeof(double*));
  3841. int** cmask = malloc(nrows*sizeof(int*));
  3842. for (i = 0; i < nrows; i++)
  3843. { count[i] = calloc(2,sizeof(int));
  3844. cdata[i] = calloc(2,sizeof(double));
  3845. cmask[i] = malloc(2*sizeof(int));
  3846. }
  3847. for (i = 0; i < n1; i++)
  3848. { k = index1[i];
  3849. for (j = 0; j < nrows; j++)
  3850. { if (mask[j][k] != 0)
  3851. { cdata[j][0] = cdata[j][0] + data[j][k];
  3852. count[j][0] = count[j][0] + 1;
  3853. }
  3854. }
  3855. }
  3856. for (i = 0; i < n2; i++)
  3857. { k = index2[i];
  3858. for (j = 0; j < nrows; j++)
  3859. { if (mask[j][k] != 0)
  3860. { cdata[j][1] = cdata[j][1] + data[j][k];
  3861. count[j][1] = count[j][1] + 1;
  3862. }
  3863. }
  3864. }
  3865. for (i = 0; i < nrows; i++)
  3866. for (j = 0; j < 2; j++)
  3867. if (count[i][j]>0)
  3868. { cdata[i][j] = cdata[i][j] / count[i][j];
  3869. cmask[i][j] = 1;
  3870. }
  3871. else
  3872. cmask[i][j] = 0;
  3873. distance = metric (nrows,cdata,cdata,cmask,cmask,weight,0,1,1);
  3874. for (i = 0; i < nrows; i++)
  3875. { free (count[i]);
  3876. free (cdata[i]);
  3877. free (cmask[i]);
  3878. }
  3879. free (count);
  3880. free (cdata);
  3881. free (cmask);
  3882. return distance;
  3883. }
  3884. }
  3885. case 'm':
  3886. { int i, j, k;
  3887. if (transpose==0)
  3888. { double distance;
  3889. double* temp = malloc(nrows*sizeof(double));
  3890. double* cdata[2];
  3891. int* cmask[2];
  3892. for (i = 0; i < 2; i++)
  3893. { cdata[i] = malloc(ncolumns*sizeof(double));
  3894. cmask[i] = malloc(ncolumns*sizeof(int));
  3895. }
  3896. for (j = 0; j < ncolumns; j++)
  3897. { int count = 0;
  3898. for (k = 0; k < n1; k++)
  3899. { i = index1[k];
  3900. if (mask[i][j])
  3901. { temp[count] = data[i][j];
  3902. count++;
  3903. }
  3904. }
  3905. if (count>0)
  3906. { cdata[0][j] = median (count,temp);
  3907. cmask[0][j] = 1;
  3908. }
  3909. else
  3910. { cdata[0][j] = 0.;
  3911. cmask[0][j] = 0;
  3912. }
  3913. }
  3914. for (j = 0; j < ncolumns; j++)
  3915. { int count = 0;
  3916. for (k = 0; k < n2; k++)
  3917. { i = index2[k];
  3918. if (mask[i][j])
  3919. { temp[count] = data[i][j];
  3920. count++;
  3921. }
  3922. }
  3923. if (count>0)
  3924. { cdata[1][j] = median (count,temp);
  3925. cmask[1][j] = 1;
  3926. }
  3927. else
  3928. { cdata[1][j] = 0.;
  3929. cmask[1][j] = 0;
  3930. }
  3931. }
  3932. distance = metric (ncolumns,cdata,cdata,cmask,cmask,weight,0,1,0);
  3933. for (i = 0; i < 2; i++)
  3934. { free (cdata[i]);
  3935. free (cmask[i]);
  3936. }
  3937. free(temp);
  3938. return distance;
  3939. }
  3940. else
  3941. { double distance;
  3942. double* temp = malloc(ncolumns*sizeof(double));
  3943. double** cdata = malloc(nrows*sizeof(double*));
  3944. int** cmask = malloc(nrows*sizeof(int*));
  3945. for (i = 0; i < nrows; i++)
  3946. { cdata[i] = malloc(2*sizeof(double));
  3947. cmask[i] = malloc(2*sizeof(int));
  3948. }
  3949. for (j = 0; j < nrows; j++)
  3950. { int count = 0;
  3951. for (k = 0; k < n1; k++)
  3952. { i = index1[k];
  3953. if (mask[j][i])
  3954. { temp[count] = data[j][i];
  3955. count++;
  3956. }
  3957. }
  3958. if (count>0)
  3959. { cdata[j][0] = median (count,temp);
  3960. cmask[j][0] = 1;
  3961. }
  3962. else
  3963. { cdata[j][0] = 0.;
  3964. cmask[j][0] = 0;
  3965. }
  3966. }
  3967. for (j = 0; j < nrows; j++)
  3968. { int count = 0;
  3969. for (k = 0; k < n2; k++)
  3970. { i = index2[k];
  3971. if (mask[j][i])
  3972. { temp[count] = data[j][i];
  3973. count++;
  3974. }
  3975. }
  3976. if (count>0)
  3977. { cdata[j][1] = median (count,temp);
  3978. cmask[j][1] = 1;
  3979. }
  3980. else
  3981. { cdata[j][1] = 0.;
  3982. cmask[j][1] = 0;
  3983. }
  3984. }
  3985. distance = metric (nrows,cdata,cdata,cmask,cmask,weight,0,1,1);
  3986. for (i = 0; i < nrows; i++)
  3987. { free (cdata[i]);
  3988. free (cmask[i]);
  3989. }
  3990. free(cdata);
  3991. free(cmask);
  3992. free(temp);
  3993. return distance;
  3994. }
  3995. }
  3996. case 's':
  3997. { int i1, i2, j1, j2;
  3998. const int n = (transpose==0) ? ncolumns : nrows;
  3999. double mindistance = DBL_MAX;
  4000. for (i1 = 0; i1 < n1; i1++)
  4001. for (i2 = 0; i2 < n2; i2++)
  4002. { double distance;
  4003. j1 = index1[i1];
  4004. j2 = index2[i2];
  4005. distance = metric (n,data,data,mask,mask,weight,j1,j2,transpose);
  4006. if (distance < mindistance) mindistance = distance;
  4007. }
  4008. return mindistance;
  4009. }
  4010. case 'x':
  4011. { int i1, i2, j1, j2;
  4012. const int n = (transpose==0) ? ncolumns : nrows;
  4013. double maxdistance = 0;
  4014. for (i1 = 0; i1 < n1; i1++)
  4015. for (i2 = 0; i2 < n2; i2++)
  4016. { double distance;
  4017. j1 = index1[i1];
  4018. j2 = index2[i2];
  4019. distance = metric (n,data,data,mask,mask,weight,j1,j2,transpose);
  4020. if (distance > maxdistance) maxdistance = distance;
  4021. }
  4022. return maxdistance;
  4023. }
  4024. case 'v':
  4025. { int i1, i2, j1, j2;
  4026. const int n = (transpose==0) ? ncolumns : nrows;
  4027. double distance = 0;
  4028. for (i1 = 0; i1 < n1; i1++)
  4029. for (i2 = 0; i2 < n2; i2++)
  4030. { j1 = index1[i1];
  4031. j2 = index2[i2];
  4032. distance += metric (n,data,data,mask,mask,weight,j1,j2,transpose);
  4033. }
  4034. distance /= (n1*n2);
  4035. return distance;
  4036. }
  4037. }
  4038. /* Never get here */
  4039. return -2.0;
  4040. }