Counting number of occurrences of a string in a file and using it?

Counting number of occurrences of a string in a file and using it?

81
Advanced UserAdvanced User
81

    Aug 15, 2017#1

    I'm trying to write a script which will count the number of occurrences of different strings and put the count values to certain tags in the file.

    Sample text:

    Code: Select all

    <counts>
    <fig-count count="8"/>
    <table-count count="6"/>
    <equation-count count="10"/>
    <page-count count="1"/>
    </counts>
    </article-meta>
    </front>
    <body>
    <p>Nineteen million people have died of AIDS since the discovery of HIV in the 1980s. In 1999 alone, 5.4 million people were newly infected with HIV (ref. <xref ref-type="bibr" rid="B1">1</xref> and <ext-link ext-link-type="url" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.unaids.org/epidemicupdate/report/Epireport.html">http://www.unaids.org/epidemicupdate/report/Epireport.html</ext-link>). (For brevity, HIV-1 is referred to as HIV in this paper.) Sub-Saharan Africa has been hardest hit, with more than 20&#x0025; of the general population HIV-positive in some countries (<xref ref-type="bibr" rid="B2">2</xref>, <xref ref-type="bibr" rid="B3">3</xref>). In comparison, heterosexual epidemics in developed, market-economy countries have not reached such severe levels. Factors contributing to the severity of the epidemic in economically developing countries abound, including economic, health, and social differences such as high levels of sexually transmitted diseases and a lack of prevention programs. However, the staggering rate at which the epidemic has spread in sub-Saharan Africa has not been adequately explained. The rate and severity of this epidemic also could indicate a greater underlying susceptibility to HIV attributable not only to sexually transmitted disease, economics, etc., but also to other more ubiquitous factors such as host genetics (<xref ref-type="bibr" rid="B4">4</xref>, <xref ref-type="bibr" rid="B5">5</xref>).</p>
    <p>To exemplify the contribution of such a host genetic factor to HIV prevalence trends, we consider a well-characterized 32-bp deletion in the host-cell chemokine receptor CCR5, CCR5&#x0394;32. When HIV binds to host cells, it uses the CD4 receptor on the surface of host immune cells together with a coreceptor, mainly the CCR5 and CXCR4 chemokine receptors (<xref ref-type="bibr" rid="B6">6</xref>). Homozygous mutations for this 32-bp deletion offer almost complete protection from HIV infection, and heterozygous mutations are associated with lower pre-AIDS viral loads and delayed progression to AIDS (<xref ref-type="bibr" rid="B7">7</xref>&#x2013;<xref ref-type="bibr" rid="B14">14</xref>). CCR5&#x0394;32 generally is found in populations of European descent, with allelic frequencies ranging from 0 to 0.29 (<xref ref-type="bibr" rid="B13">13</xref>). African and Asian populations studied outside the United States or Europe appear to lack the CCR5&#x0394;32 allele, with an allelic frequency of almost zero (<xref ref-type="bibr" rid="B5">5</xref>, <xref ref-type="bibr" rid="B13">13</xref>). Thus, to understand the effects of a protective allele, we use a mathematical model to track prevalence of HIV in populations with or without CCR5&#x0394;32 heterozygous and homozygous people and also to follow the CCR5&#x0394;32 allelic frequency.</p>
    <sec>
    <title>The Model</title>
    <p>Because we are most concerned with understanding the severity of the epidemic in developing countries where the majority of infection is heterosexual, we consider a purely heterosexual model. To model the effects of the allele in the population, we examine the rate of HIV spread by using an enhanced susceptible-infected-AIDS model of epidemic HIV (for review see ref. <xref ref-type="bibr" rid="B31">31</xref>). Our model compares two population scenarios: a CCR5 wild-type population and one with CCR5&#x0394;32 heterozygotes and homozygotes in addition to the wild type.</p>
    <fig id="F1">
    <label>Figure 1</label>
    <caption>
    <p>A schematic representation of the basic compartmental HIV epidemic model. The criss-cross lines indicate the sexual mixing between different compartments. Each of these interactions has a positive probability of taking place; they also incorporate individual rates of transmission indicated as &#x03BB;, but in full notation is &#x03BB;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic>,</sub> where <italic>i</italic>,<italic>j</italic>,<italic>k</italic> is the phenotype of the infected partner and <italic>&#x00EE;</italic>,<italic>&#xEB30;</italic> is the phenotype of the susceptible partner.</p>
    </caption>
    <graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="pq1813251001">
    </graphic>
    </fig>
    <table-wrap id="T1">
    <label>Table 1</label>
    <caption>
    <p>Children's genotype</p>
    </caption>
    <table>
    <tr>
    <th>Parents</th>
    <th colspan="4">Mother</th>
    </tr>
    <tr>
    <td colspan="5">
    <hr/>
    </td>
    </tr>
    <tr>
    <td>Father</td>
    <td/>
    <td>W&#x002F;W</td>
    <td>W&#x002F;&#x0394;32</td>
    <td>&#x0394;32&#x002F;&#x0394;32</td>
    </tr>
    </table>
    <table-wrap-foot>
    <fn>
    <p>&#x03C7;<sub>1,<italic>j</italic></sub>1,<italic>j</italic> = wild-type children; (W&#x002F;W); &#x03C7;<sub>2,<italic>j</italic></sub>2,<italic>j</italic> = heterozygous children (W&#x002F;&#x0394;32); &#x03C7;<sub>3,<italic>j</italic></sub>3,<italic>j</italic> = homozygous children (&#x0394;32&#x002F;&#x0394;32) of gender <italic>j</italic>. Children's genotypes are determined by using Mendelian inheritance patterns.</p>
    </fn>
    </table-wrap-foot>
    </table-wrap>
    <sec>
    <title>Parameter Estimates for the Model.</title>
    <p>Estimates for rates that govern the interactions depicted in Fig. <xref ref-type="fig" rid="F1">1</xref> were derived from the extensive literature on HIV. Our parameters and their estimates are summarized in Tables <xref ref-type="table" rid="T2">2</xref>&#x2013;<xref ref-type="table" rid="T4">4</xref>. The general form of the equations describing the rates of transition between population classes as depicted in Fig. <xref ref-type="fig" rid="F1">1</xref> are summarized as follows: <disp-formula id="E1">
    <tex-math id="M1">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dS_{i,j}(t)}{dt}={\chi}_{i,j}(t)-{\mu}_{j}S_{i,j}(t)-{\lambda}_{\hat {\imath},\hat {},\hat {k}{\rightarrow}i,j}S_{i,j}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E2">
    <tex-math id="M2">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \hspace{1em}\hspace{1em}\hspace{.167em}\frac{dI_{i,j,A}(t)}{dt}={\lambda}_{\hat {\imath},\hat {},\hat {k}{\rightarrow}i,j}S_{i,j}(t)-{\mu}_{j}I_{i,j,A}(t)-{\gamma}_{i,j,A}I_{i,j,A}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E3">
    <tex-math id="M3">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dI_{i,j,B}(t)}{dt}={\gamma}_{i,j,A}I_{i,j,A}(t)-{\mu}_{j}I_{i,j,B}(t)-{\gamma}_{i,j,B}I_{i,j,B}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E4">
    <tex-math id="M4">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dA(t)}{dt}={\gamma}_{i,j,B} \left( { \,\substack{ ^{3} \\ {\sum} \\ _{i=1} }\, }I_{i,F,B}(t)+I_{i,M,B}(t) \right) -{\mu}_{A}A(t)-{\delta}A(t), $$ \end{document} </tex-math>
    </disp-formula> where, in addition to previously defined populations and rates (with <italic>i</italic> equals genotype, <italic>j</italic> equals gender, and <italic>k</italic> equals stage of infection, either <italic>A</italic> or <italic>B</italic>), &#x03BC;<sub><italic>j</italic></sub>, represents the non-AIDS (natural) death rate for males and females respectively, and &#x03BC;<sub>A</sub> is estimated by the average (&#x03BC;<sub>F</sub> &#x002B; &#x03BC;<sub>M</sub>/2). This approximation allows us to simplify the model (only one AIDS compartment) without compromising the results, as most people with AIDS die of AIDS (&#x03B4;<sub>AIDS</sub>) and very few of other causes (&#x03BC;<sub>A</sub>). These estimates include values that affect infectivity (&#x03BB;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic></sub>), transmission (&#x03B2;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic></sub>), and disease progression.</p>
    <table-wrap id="T2">
    <label>Table 2</label>
    <caption>
    <p>Transmission probabilities</p>
    </caption>
    <table>
    <tr>
    <th rowspan="3">HIV-infected partner (&#x00EE;&#x0131;&#x0131;&#x005E;&#x005E;, &#xEB30;&#xE2D4;&#xE2D4;&#x005E;&#x005E;, &#xEA50;<italic>k</italic><italic>k</italic>&#x005E;&#x005E;)</th>
    <th colspan="4">Susceptible partner (<italic>i</italic>, <italic>j</italic>)</th>
    </tr>
    <tr>
    <td colspan="4">
    <hr/>
    </td>
    </tr>
    <tr>
    <th>(&#xEB30;&#xE2D4;&#xE2D4;&#x005E;&#x005E; to <italic>j</italic>)</th>
    <th>W&#x002F;W</th>
    <th>W&#x002F;&#x0394;32</th>
    <th>&#x0394;32&#x002F;&#x0394;32 </th>
    </tr>
    <tr>
    <td colspan="5">
    <hr/>
    </td>
    </tr>
    <tr>
    <td>Acute&#x002F;primary</td>
    </tr>
    <tr>
    <td>&#x2003;W&#x002F;W or &#x0394;32&#x002F;&#x0394;32</td>
    <td>M to F</td>
    <td>0.040</td>
    <td>0.040</td>
    <td>0.00040 </td>
    </tr>
    <tr>
    <td/>
    <td>F to M</td>
    <td>0.020</td>
    <td>0.020</td>
    <td>0.00020 </td>
    </tr>
    <tr>
    <td>Asymptomatic </td>
    </tr>
    <tr>
    <td>&#x2003;W&#x002F;W or &#x0394;32&#x002F;&#x0394;32</td>
    <td>M to F</td>
    <td>0.0010</td>
    <td>0.0010</td>
    <td>10 &#x00D7; 10<sup>&#x2212;6</sup>
    </td>
    </tr>
    <tr>
    <td/>
    <td>F to M</td>
    <td>0.00025</td>
    <td>0.00025</td>
    <td>2.5 &#x00D7; 10<sup>&#x2212;6</sup>
    </td>
    </tr>
    </table>
    </table-wrap>
    <title>Disease progression.</title>
    <p>We assume three stages of HIV infection: primary (acute, stage A), asymptomatic HIV (stage B), and AIDS. The rates of transition through the first two stages are denoted by &#x03B3;<sub><italic>i</italic>,<italic>j</italic>,<italic>k</italic></sub><italic>i</italic>,<italic>j</italic>,<italic>k</italic>, where <italic>i</italic> represents genotype, <italic>j</italic> is male/female, and <italic>k</italic> represents either stage A or stage B. Transition rates through each of these stages are assumed to be inversely proportional to the duration of that stage; however, other distributions are possible (<xref ref-type="bibr" rid="B31">31</xref>, <xref ref-type="bibr" rid="B44">44</xref>, <xref ref-type="bibr" rid="B45">45</xref>). Although viral loads generally peak in the first 2 months of infection, steady-state viral loads are established several months beyond this (<xref ref-type="bibr" rid="B46">46</xref>). For group A, the primary HIV-infecteds, duration is assumed to be 3.5 months. Based on results from European cohort studies (<xref ref-type="bibr" rid="B7">7</xref>&#x2013;<xref ref-type="bibr" rid="B10">10</xref>), the beneficial effects of the CCR5 W/&#x0394;32 genotype are observed mainly in the asymptomatic years of HIV infection; &#x2248;7 years after seroconversion survival rates appear to be quite similar between heterozygous and homozygous individuals. We also assume that CCR5&#x0394;32/&#x0394;32-infected individuals and wild-type individuals progress similarly, and that men and women progress through each disease stage at the same rate. Given these observations, and that survival after infection may be shorter in untreated populations, we choose the duration time in stage B to be 6 years for wild-type individuals and 8 years for heterozygous individuals. Transition through AIDS, &#x03B4;<sub>AIDS</sub>, is inversely proportional to the duration of AIDS. We estimate this value to be 1 year for the time from onset of AIDS to death. The progression rates are summarized in Table <xref ref-type="table" rid="T3">3</xref>.</p>
    </sec>
    </sec>
    <sec>
    <title>Prevalence of HIV</title>
    <sec>
    <title>Demographics and Model Validation.</title>
    <p>The model was validated by using parameters estimated from available demographic data. Simulations were run in the absence of HIV infection to compare the model with known population growth rates. Infection was subsequently introduced with an initial low HIV prevalence of 0.5&#x0025; to capture early epidemic behavior.</p>
    <p>In deciding on our initial values for parameters during infection, we use Joint United Nations Programme on HIV&#x002F;AIDS national prevalence data for Malawi, Zimbabwe, and Botswana. Nationwide seroprevalence of HIV in these countries varies from &#x2248;11&#x0025; to over 20&#x0025; (<xref ref-type="bibr" rid="B3">3</xref>), although there may be considerable variation within given subpopulations (<xref ref-type="bibr" rid="B2">2</xref>, <xref ref-type="bibr" rid="B49">49</xref>).</p>
    <p>In the absence of HIV infection, the annual percent population growth rate in the model is &#x2248;2.5&#x0025;, predicting the present-day values for an average of sub-Saharan African cities (data not shown). To validate the model with HIV infection, we compare our simulation of the HIV epidemic to existing prevalence data for Kenya and Mozambique (<ext-link ext-link-type="url" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.who.int/emc-hiv/fact-sheets/pdfs/kenya.pdf">http://www.who.int/emc-hiv/fact-sheets/pdfs/kenya.pdf</ext-link> and ref. <xref ref-type="bibr" rid="B51">51</xref>). Prevalence data collected from these countries follow similar trajectories to those predicted by our model (Fig. <xref ref-type="fig" rid="F2">2</xref>). </p>
    <fig id="F2">
    <label>Figure 2</label>
    <caption>
    <p>Model simulation of HIV infection in a population lacking the protective CCR5&#x0394;32 allele compared with national data from Kenya (healthy adults) and Mozambique (blood donors, ref. <xref ref-type="bibr" rid="B17">17</xref>). The simulated population incorporates parameter estimates from sub-Saharan African demographics. Note the two outlier points from the Mozambique data were likely caused by underreporting in the early stages of the epidemic.</p>
    </caption>
    <graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="pq1813251002">
    </graphic>
    </fig>
    </sec>
    <sec>
    <title>Effects of the Allele on Prevalence.</title>
    <p>After validating the model in the wild type-only population, both CCR5&#x0394;32 heterozygous and homozygous people are included. Parameter values for HIV transmission, duration of illness, and numbers of contacts per partner are assumed to be the same within both settings. We then calculate HIV/AIDS prevalence among adults for total HIV/AIDS cases.</p>
    <p>Fig. <xref ref-type="fig" rid="F3">3</xref> shows the prevalence of HIV in two populations: one lacking the mutant CCR5 allele and another carrying that allele. In the population lacking the protective mutation, prevalence increases logarithmically for the first 35 years of the epidemic, reaching 18&#x0025; before leveling off. </p>
    <p>In contrast, when a proportion of the population carries the CCR5&#x0394;32 allele, the epidemic increases more slowly, but still logarithmically, for the first 50 years, and HIV/AIDS prevalence reaches &#x2248;12&#x0025; (Fig. <xref ref-type="fig" rid="F3">3</xref>). Prevalence begins to decline slowly after 70 years.</p>
    </sec>
    </sec>
    <sec>
    <title>HIV Induces Selective Pressure on Genotype Frequency</title>
    <p>To observe changes in the frequency of the CCR5&#x0394;32 allele in a setting with HIV infection as compared with the Hardy-Weinberg equilibrium in the absence of HIV, we follow changes in the total number of CCR5&#x0394;32 heterozygotes and homozygotes over 1,000 years (Fig. <xref ref-type="fig" rid="F4">4</xref>). We initially perform simulations in the absence of HIV infection as a negative control to show there is not significant selection of the allele in the absence of infection.</p>
    </sec>
    Expected output:

    Code: Select all

    <counts>
    <fig-count count="2"/>
    <table-count count="2"/>
    <equation-count count="4"/>
    <page-count count="1"/>
    </counts>
    </article-meta>
    </front>
    <body>
    <p>Nineteen million people have died of AIDS since the discovery of HIV in the 1980s. In 1999 alone, 5.4 million people were newly infected with HIV (ref. <xref ref-type="bibr" rid="B1">1</xref> and <ext-link ext-link-type="url" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.unaids.org/epidemicupdate/report/Epireport.html">http://www.unaids.org/epidemicupdate/report/Epireport.html</ext-link>). (For brevity, HIV-1 is referred to as HIV in this paper.) Sub-Saharan Africa has been hardest hit, with more than 20&#x0025; of the general population HIV-positive in some countries (<xref ref-type="bibr" rid="B2">2</xref>, <xref ref-type="bibr" rid="B3">3</xref>). In comparison, heterosexual epidemics in developed, market-economy countries have not reached such severe levels. Factors contributing to the severity of the epidemic in economically developing countries abound, including economic, health, and social differences such as high levels of sexually transmitted diseases and a lack of prevention programs. However, the staggering rate at which the epidemic has spread in sub-Saharan Africa has not been adequately explained. The rate and severity of this epidemic also could indicate a greater underlying susceptibility to HIV attributable not only to sexually transmitted disease, economics, etc., but also to other more ubiquitous factors such as host genetics (<xref ref-type="bibr" rid="B4">4</xref>, <xref ref-type="bibr" rid="B5">5</xref>).</p>
    <p>To exemplify the contribution of such a host genetic factor to HIV prevalence trends, we consider a well-characterized 32-bp deletion in the host-cell chemokine receptor CCR5, CCR5&#x0394;32. When HIV binds to host cells, it uses the CD4 receptor on the surface of host immune cells together with a coreceptor, mainly the CCR5 and CXCR4 chemokine receptors (<xref ref-type="bibr" rid="B6">6</xref>). Homozygous mutations for this 32-bp deletion offer almost complete protection from HIV infection, and heterozygous mutations are associated with lower pre-AIDS viral loads and delayed progression to AIDS (<xref ref-type="bibr" rid="B7">7</xref>&#x2013;<xref ref-type="bibr" rid="B14">14</xref>). CCR5&#x0394;32 generally is found in populations of European descent, with allelic frequencies ranging from 0 to 0.29 (<xref ref-type="bibr" rid="B13">13</xref>). African and Asian populations studied outside the United States or Europe appear to lack the CCR5&#x0394;32 allele, with an allelic frequency of almost zero (<xref ref-type="bibr" rid="B5">5</xref>, <xref ref-type="bibr" rid="B13">13</xref>). Thus, to understand the effects of a protective allele, we use a mathematical model to track prevalence of HIV in populations with or without CCR5&#x0394;32 heterozygous and homozygous people and also to follow the CCR5&#x0394;32 allelic frequency.</p>
    <sec>
    <title>The Model</title>
    <p>Because we are most concerned with understanding the severity of the epidemic in developing countries where the majority of infection is heterosexual, we consider a purely heterosexual model. To model the effects of the allele in the population, we examine the rate of HIV spread by using an enhanced susceptible-infected-AIDS model of epidemic HIV (for review see ref. <xref ref-type="bibr" rid="B31">31</xref>). Our model compares two population scenarios: a CCR5 wild-type population and one with CCR5&#x0394;32 heterozygotes and homozygotes in addition to the wild type.</p>
    <fig id="F1">
    <label>Figure 1</label>
    <caption>
    <p>A schematic representation of the basic compartmental HIV epidemic model. The criss-cross lines indicate the sexual mixing between different compartments. Each of these interactions has a positive probability of taking place; they also incorporate individual rates of transmission indicated as &#x03BB;, but in full notation is &#x03BB;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic>,</sub> where <italic>i</italic>,<italic>j</italic>,<italic>k</italic> is the phenotype of the infected partner and <italic>&#x00EE;</italic>,<italic>&#xEB30;</italic> is the phenotype of the susceptible partner.</p>
    </caption>
    <graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="pq1813251001">
    </graphic>
    </fig>
    <table-wrap id="T1">
    <label>Table 1</label>
    <caption>
    <p>Children's genotype</p>
    </caption>
    <table>
    <tr>
    <th>Parents</th>
    <th colspan="4">Mother</th>
    </tr>
    <tr>
    <td colspan="5">
    <hr/>
    </td>
    </tr>
    <tr>
    <td>Father</td>
    <td/>
    <td>W&#x002F;W</td>
    <td>W&#x002F;&#x0394;32</td>
    <td>&#x0394;32&#x002F;&#x0394;32</td>
    </tr>
    </table>
    <table-wrap-foot>
    <fn>
    <p>&#x03C7;<sub>1,<italic>j</italic></sub>1,<italic>j</italic> = wild-type children; (W&#x002F;W); &#x03C7;<sub>2,<italic>j</italic></sub>2,<italic>j</italic> = heterozygous children (W&#x002F;&#x0394;32); &#x03C7;<sub>3,<italic>j</italic></sub>3,<italic>j</italic> = homozygous children (&#x0394;32&#x002F;&#x0394;32) of gender <italic>j</italic>. Children's genotypes are determined by using Mendelian inheritance patterns.</p>
    </fn>
    </table-wrap-foot>
    </table-wrap>
    <sec>
    <title>Parameter Estimates for the Model.</title>
    <p>Estimates for rates that govern the interactions depicted in Fig. <xref ref-type="fig" rid="F1">1</xref> were derived from the extensive literature on HIV. Our parameters and their estimates are summarized in Tables <xref ref-type="table" rid="T2">2</xref>&#x2013;<xref ref-type="table" rid="T4">4</xref>. The general form of the equations describing the rates of transition between population classes as depicted in Fig. <xref ref-type="fig" rid="F1">1</xref> are summarized as follows: <disp-formula id="E1">
    <tex-math id="M1">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dS_{i,j}(t)}{dt}={\chi}_{i,j}(t)-{\mu}_{j}S_{i,j}(t)-{\lambda}_{\hat {\imath},\hat {},\hat {k}{\rightarrow}i,j}S_{i,j}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E2">
    <tex-math id="M2">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \hspace{1em}\hspace{1em}\hspace{.167em}\frac{dI_{i,j,A}(t)}{dt}={\lambda}_{\hat {\imath},\hat {},\hat {k}{\rightarrow}i,j}S_{i,j}(t)-{\mu}_{j}I_{i,j,A}(t)-{\gamma}_{i,j,A}I_{i,j,A}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E3">
    <tex-math id="M3">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dI_{i,j,B}(t)}{dt}={\gamma}_{i,j,A}I_{i,j,A}(t)-{\mu}_{j}I_{i,j,B}(t)-{\gamma}_{i,j,B}I_{i,j,B}(t), $$ \end{document} </tex-math>
    </disp-formula>
    <disp-formula id="E4">
    <tex-math id="M4">\documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dA(t)}{dt}={\gamma}_{i,j,B} \left( { \,\substack{ ^{3} \\ {\sum} \\ _{i=1} }\, }I_{i,F,B}(t)+I_{i,M,B}(t) \right) -{\mu}_{A}A(t)-{\delta}A(t), $$ \end{document} </tex-math>
    </disp-formula> where, in addition to previously defined populations and rates (with <italic>i</italic> equals genotype, <italic>j</italic> equals gender, and <italic>k</italic> equals stage of infection, either <italic>A</italic> or <italic>B</italic>), &#x03BC;<sub><italic>j</italic></sub>, represents the non-AIDS (natural) death rate for males and females respectively, and &#x03BC;<sub>A</sub> is estimated by the average (&#x03BC;<sub>F</sub> &#x002B; &#x03BC;<sub>M</sub>/2). This approximation allows us to simplify the model (only one AIDS compartment) without compromising the results, as most people with AIDS die of AIDS (&#x03B4;<sub>AIDS</sub>) and very few of other causes (&#x03BC;<sub>A</sub>). These estimates include values that affect infectivity (&#x03BB;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic></sub>), transmission (&#x03B2;<sub><italic>&#x00EE;</italic>,<italic>&#xEB30;</italic>,<italic>&#xEA50;</italic>&#x2192;<italic>i</italic>,<italic>j</italic></sub>), and disease progression.</p>
    <table-wrap id="T2">
    <label>Table 2</label>
    <caption>
    <p>Transmission probabilities</p>
    </caption>
    <table>
    <tr>
    <th rowspan="3">HIV-infected partner (&#x00EE;&#x0131;&#x0131;&#x005E;&#x005E;, &#xEB30;&#xE2D4;&#xE2D4;&#x005E;&#x005E;, &#xEA50;<italic>k</italic><italic>k</italic>&#x005E;&#x005E;)</th>
    <th colspan="4">Susceptible partner (<italic>i</italic>, <italic>j</italic>)</th>
    </tr>
    <tr>
    <td colspan="4">
    <hr/>
    </td>
    </tr>
    <tr>
    <th>(&#xEB30;&#xE2D4;&#xE2D4;&#x005E;&#x005E; to <italic>j</italic>)</th>
    <th>W&#x002F;W</th>
    <th>W&#x002F;&#x0394;32</th>
    <th>&#x0394;32&#x002F;&#x0394;32 </th>
    </tr>
    <tr>
    <td colspan="5">
    <hr/>
    </td>
    </tr>
    <tr>
    <td>Acute&#x002F;primary</td>
    </tr>
    <tr>
    <td>&#x2003;W&#x002F;W or &#x0394;32&#x002F;&#x0394;32</td>
    <td>M to F</td>
    <td>0.040</td>
    <td>0.040</td>
    <td>0.00040 </td>
    </tr>
    <tr>
    <td/>
    <td>F to M</td>
    <td>0.020</td>
    <td>0.020</td>
    <td>0.00020 </td>
    </tr>
    <tr>
    <td>Asymptomatic </td>
    </tr>
    <tr>
    <td>&#x2003;W&#x002F;W or &#x0394;32&#x002F;&#x0394;32</td>
    <td>M to F</td>
    <td>0.0010</td>
    <td>0.0010</td>
    <td>10 &#x00D7; 10<sup>&#x2212;6</sup>
    </td>
    </tr>
    <tr>
    <td/>
    <td>F to M</td>
    <td>0.00025</td>
    <td>0.00025</td>
    <td>2.5 &#x00D7; 10<sup>&#x2212;6</sup>
    </td>
    </tr>
    </table>
    </table-wrap>
    <title>Disease progression.</title>
    <p>We assume three stages of HIV infection: primary (acute, stage A), asymptomatic HIV (stage B), and AIDS. The rates of transition through the first two stages are denoted by &#x03B3;<sub><italic>i</italic>,<italic>j</italic>,<italic>k</italic></sub><italic>i</italic>,<italic>j</italic>,<italic>k</italic>, where <italic>i</italic> represents genotype, <italic>j</italic> is male/female, and <italic>k</italic> represents either stage A or stage B. Transition rates through each of these stages are assumed to be inversely proportional to the duration of that stage; however, other distributions are possible (<xref ref-type="bibr" rid="B31">31</xref>, <xref ref-type="bibr" rid="B44">44</xref>, <xref ref-type="bibr" rid="B45">45</xref>). Although viral loads generally peak in the first 2 months of infection, steady-state viral loads are established several months beyond this (<xref ref-type="bibr" rid="B46">46</xref>). For group A, the primary HIV-infecteds, duration is assumed to be 3.5 months. Based on results from European cohort studies (<xref ref-type="bibr" rid="B7">7</xref>&#x2013;<xref ref-type="bibr" rid="B10">10</xref>), the beneficial effects of the CCR5 W/&#x0394;32 genotype are observed mainly in the asymptomatic years of HIV infection; &#x2248;7 years after seroconversion survival rates appear to be quite similar between heterozygous and homozygous individuals. We also assume that CCR5&#x0394;32/&#x0394;32-infected individuals and wild-type individuals progress similarly, and that men and women progress through each disease stage at the same rate. Given these observations, and that survival after infection may be shorter in untreated populations, we choose the duration time in stage B to be 6 years for wild-type individuals and 8 years for heterozygous individuals. Transition through AIDS, &#x03B4;<sub>AIDS</sub>, is inversely proportional to the duration of AIDS. We estimate this value to be 1 year for the time from onset of AIDS to death. The progression rates are summarized in Table <xref ref-type="table" rid="T3">3</xref>.</p>
    </sec>
    </sec>
    <sec>
    <title>Prevalence of HIV</title>
    <sec>
    <title>Demographics and Model Validation.</title>
    <p>The model was validated by using parameters estimated from available demographic data. Simulations were run in the absence of HIV infection to compare the model with known population growth rates. Infection was subsequently introduced with an initial low HIV prevalence of 0.5&#x0025; to capture early epidemic behavior.</p>
    <p>In deciding on our initial values for parameters during infection, we use Joint United Nations Programme on HIV&#x002F;AIDS national prevalence data for Malawi, Zimbabwe, and Botswana. Nationwide seroprevalence of HIV in these countries varies from &#x2248;11&#x0025; to over 20&#x0025; (<xref ref-type="bibr" rid="B3">3</xref>), although there may be considerable variation within given subpopulations (<xref ref-type="bibr" rid="B2">2</xref>, <xref ref-type="bibr" rid="B49">49</xref>).</p>
    <p>In the absence of HIV infection, the annual percent population growth rate in the model is &#x2248;2.5&#x0025;, predicting the present-day values for an average of sub-Saharan African cities (data not shown). To validate the model with HIV infection, we compare our simulation of the HIV epidemic to existing prevalence data for Kenya and Mozambique (<ext-link ext-link-type="url" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.who.int/emc-hiv/fact-sheets/pdfs/kenya.pdf">http://www.who.int/emc-hiv/fact-sheets/pdfs/kenya.pdf</ext-link> and ref. <xref ref-type="bibr" rid="B51">51</xref>). Prevalence data collected from these countries follow similar trajectories to those predicted by our model (Fig. <xref ref-type="fig" rid="F2">2</xref>). </p>
    <fig id="F2">
    <label>Figure 2</label>
    <caption>
    <p>Model simulation of HIV infection in a population lacking the protective CCR5&#x0394;32 allele compared with national data from Kenya (healthy adults) and Mozambique (blood donors, ref. <xref ref-type="bibr" rid="B17">17</xref>). The simulated population incorporates parameter estimates from sub-Saharan African demographics. Note the two outlier points from the Mozambique data were likely caused by underreporting in the early stages of the epidemic.</p>
    </caption>
    <graphic xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="pq1813251002">
    </graphic>
    </fig>
    </sec>
    <sec>
    <title>Effects of the Allele on Prevalence.</title>
    <p>After validating the model in the wild type-only population, both CCR5&#x0394;32 heterozygous and homozygous people are included. Parameter values for HIV transmission, duration of illness, and numbers of contacts per partner are assumed to be the same within both settings. We then calculate HIV/AIDS prevalence among adults for total HIV/AIDS cases.</p>
    <p>Fig. <xref ref-type="fig" rid="F3">3</xref> shows the prevalence of HIV in two populations: one lacking the mutant CCR5 allele and another carrying that allele. In the population lacking the protective mutation, prevalence increases logarithmically for the first 35 years of the epidemic, reaching 18&#x0025; before leveling off. </p>
    <p>In contrast, when a proportion of the population carries the CCR5&#x0394;32 allele, the epidemic increases more slowly, but still logarithmically, for the first 50 years, and HIV/AIDS prevalence reaches &#x2248;12&#x0025; (Fig. <xref ref-type="fig" rid="F3">3</xref>). Prevalence begins to decline slowly after 70 years.</p>
    </sec>
    </sec>
    <sec>
    <title>HIV Induces Selective Pressure on Genotype Frequency</title>
    <p>To observe changes in the frequency of the CCR5&#x0394;32 allele in a setting with HIV infection as compared with the Hardy-Weinberg equilibrium in the absence of HIV, we follow changes in the total number of CCR5&#x0394;32 heterozygotes and homozygotes over 1,000 years (Fig. <xref ref-type="fig" rid="F4">4</xref>). We initially perform simulations in the absence of HIV infection as a negative control to show there is not significant selection of the allele in the absence of infection.</p>
    </sec>
    I've written a script (code given below) which does this job but its not that fast for large files.

    How do I make the script more efficient?

    Code: Select all

    var countStirng =  ['</disp-formula>', '</fig>','</table-wrap>'];
    function myReplace1()
    {
    UltraEdit.activeDocument.top();
    var first = countStirng[0];
    UltraEdit.activeDocument.findReplace.find(first);
    i = 0;
    UltraEdit.clearClipboard();
    while (UltraEdit.activeDocument.isFound())
    {
         if (UltraEdit.activeDocument.isFound()) {
              i++;
         }
         
         UltraEdit.activeDocument.findReplace.find(first);
    }if (i == 0){
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<equation-count count="[0-9]+"/>', '<equation-count count="0"/>');
    } else {
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<equation-count count="[0-9]+"/>', '<equation-count count="'+i+'"/>');
    }
    }
    function myReplace2()
    {
    UltraEdit.activeDocument.top();
    var second = countStirng[1];
    UltraEdit.activeDocument.findReplace.find(second);
    i = 0;
    UltraEdit.clearClipboard();
    while (UltraEdit.activeDocument.isFound())
    {
         if (UltraEdit.activeDocument.isFound()) {
              i++;
         }
         
         UltraEdit.activeDocument.findReplace.find(second);
    }if (i == 0){
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<fig-count count="[0-9]+"/>', '<fig-count count="0"/>');
    } else {
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<fig-count count="[0-9]+"/>', '<fig-count count="'+i+'"/>');
    }
    }
    function myReplace3()
    {
    UltraEdit.activeDocument.top();
    var third = countStirng[2];
    UltraEdit.activeDocument.findReplace.find(third);
    i = 0;
    UltraEdit.clearClipboard();
    while (UltraEdit.activeDocument.isFound())
    {
         if (UltraEdit.activeDocument.isFound()) {
              i++;
         }
         
         UltraEdit.activeDocument.findReplace.find(third);
    }if (i == 0){
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<table-count count="[0-9]+"/>', '<table-count count="0"/>');
    } else {
         UltraEdit.activeDocument.top();
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace('<table-count count="[0-9]+"/>', '<table-count count="'+i+'"/>');
    }
    }
    myReplace1();
    myReplace2();
    myReplace3();

    133
    Basic UserBasic User
    133

      Aug 16, 2017#2

      This is a shorter version of your script:

      Code: Select all

      UltraEdit.insertMode();
      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      UltraEdit.activeDocument.hexOff();
      UltraEdit.activeDocument.findReplace.mode=0;
      UltraEdit.activeDocument.findReplace.matchCase=false;
      UltraEdit.activeDocument.findReplace.matchWord=false;
      UltraEdit.activeDocument.findReplace.regExp=false;
      UltraEdit.activeDocument.findReplace.searchDown=true;
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
      UltraEdit.activeDocument.findReplace.preserveCase=false;
      UltraEdit.activeDocument.findReplace.replaceAll=true;
      UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
      var definitions = [['</disp-formula>', 'equation-count'], 
                         ['</fig>', 'fig-count'], 
                         ['</table-wrap>', 'table-count']];
      var definitionsLength = definitions.length;
      for (var i = 0; i < definitionsLength; i++) {
          var searchString = definitions[i][0];
          var resultTag = definitions[i][1];
          var count = 0;
          UltraEdit.activeDocument.top();
          UltraEdit.activeDocument.findReplace.find(searchString);
          while (UltraEdit.activeDocument.isFound()) {
              count++;
              UltraEdit.activeDocument.findReplace.find(searchString);
          }
          UltraEdit.activeDocument.top();
          UltraEdit.activeDocument.findReplace.regExp=true;
          UltraEdit.activeDocument.findReplace.replace('<' + resultTag + ' count="[0-9]+"/>', 
                                                       '<' + resultTag + ' count="' + count + '"/>');
          UltraEdit.activeDocument.findReplace.regExp=false;
      } 
      Your script should work. It just has a lot of redundant code. Besides the extra code, the only real inefficiency is that you're testing isFound() inside the while loop. Because of the while statement, the loop only executes if isFound() is true, so there's no need to test it again.

      81
      Advanced UserAdvanced User
      81

        Aug 16, 2017#3

        Thanks Evergreen. :D
        BTW, does UltraEdit support Javascript DOM parser?

        133
        Basic UserBasic User
        133

          Aug 16, 2017#4

          I can't find any references to it, so I'm pretty sure the answer is no.

          6,602548
          Grand MasterGrand Master
          6,602548

            Aug 16, 2017#5

            Thanks Evergreen for optimizing the code. That was a big help to write the code below which counts the end tags in memory using match method of JavaScript String object which avoids window refreshes making the script even more faster. That is a method I used several times in posted scripts.

            Code: Select all

            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               // Define environment for this script.
               UltraEdit.insertMode();
               if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
               else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
               UltraEdit.activeDocument.selectAll();
               if (UltraEdit.activeDocument.isSel())
               {
                  var sFileContent = UltraEdit.activeDocument.selection;
                  UltraEdit.activeDocument.top();
            
                  UltraEdit.ueReOn();
                  UltraEdit.activeDocument.findReplace.mode=0;
                  UltraEdit.activeDocument.findReplace.matchCase=false;
                  UltraEdit.activeDocument.findReplace.matchWord=false;
                  UltraEdit.activeDocument.findReplace.regExp=true;
                  UltraEdit.activeDocument.findReplace.searchDown=true;
                  UltraEdit.activeDocument.findReplace.searchInColumn=false;
                  UltraEdit.activeDocument.findReplace.preserveCase=false;
                  UltraEdit.activeDocument.findReplace.replaceAll=false;
                  UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
            
                  var asDefinitions = [['</disp-formula>', 'equation-count'], 
                                       ['</fig>', 'fig-count'], 
                                       ['</table-wrap>', 'table-count']];
            
                  for (var nDefinition = 0; nDefinition < asDefinitions.length; nDefinition++)
                  {
                     var rSearch = new RegExp(asDefinitions[nDefinition][0],'g');
                     var asMatches = sFileContent.match(rSearch);
                     var nCount = (asMatches != null) ? asMatches.length : 0;
                     var sSearch = '^(<' + asDefinitions[nDefinition][1] + ' count="^)[0-9]+';
                     var sReplace = '^1' + nCount.toString(10);
                     UltraEdit.activeDocument.findReplace.replace(sSearch,sReplace);
                     UltraEdit.activeDocument.top();
                  }
               }
            }
            
            JavaScript main page on Mozilla Developer Network (MDN) lists on left side all built-in objects. DOM is not listed. But written in second paragraph is:
            MDN wrote:For information about APIs specific to Web pages, please see Web APIs and DOM.
            The document object model is not part of JavaScript. It is part of the web browsers parsing HTML, XHTML and XML files using DOM and which make the DOM object associated with the file loaded in web browser accessible via JavaScript like UltraEdit makes the opened documents, the output window and some features of UltraEdit application accessible via JavaScript.
            don_bradman wrote:Does UltraEdit support Javascript DOM parser?
            The answer is no because JavaScript core has no DOM parser at all.
            Best regards from an UC/UE/UES for Windows user from Austria

            81
            Advanced UserAdvanced User
            81

              Aug 17, 2017#6

              Thank you Mofi for the info.

              BTW, could you mind explaining your scripts below lines:

              Code: Select all

              var rSearch = new RegExp(asDefinitions[nDefinition][0],'g');
              
              var nCount = (asMatches != null) ? asMatches.length : 0;
              
              var sSearch = '^(<' + asDefinitions[nDefinition][1] + ' count="^)[0-9]+';
              
              var sReplace = '^1' + nCount.toString(10);

              6,602548
              Grand MasterGrand Master
              6,602548

                Aug 17, 2017#7

                Code: Select all

                var rSearch = new RegExp(asDefinitions[nDefinition][0],'g');
                This line creates a new JavaScript RegExp object from strings because the method match expects as first parameter a JavaScript regular expression object. JavaScript would implicitly create a JavaScript regular expression object from a string if method match is called with first parameter being a string using default flags.

                On first loop iteration with nDefinition having value 0, the first string is </disp-formula>. The JavaScript RegExp object rSearch is defined after this line like using in match method call as first parameter /<\/disp-formula>/,g

                Code: Select all

                var nCount = (asMatches != null) ? asMatches.length : 0;
                The match method returns a null if it cannot find any string matching the JavaScript regular expression. Otherwise it returns an array of strings matched by the JavaScript regular expression. So when the variable asMatches is not equal null, then it is (points to) a string array. In this case variable nCount is initialized with number of matched strings being equal the number of strings in the array. Otherwise nCount is initialized with 0 as no string was found matching the JavaScript regular expression. This code line could be also written as:

                Code: Select all

                var nCount;
                if (asMatches != null)
                {
                    nCount = asMatches.length;  // Number of array elements is the number of matched strings.
                }
                else
                {
                    nCount = 0;   // No string matched the regular expression.
                }

                Code: Select all

                var sSearch = '^(<' + asDefinitions[nDefinition][1] + ' count="^)[0-9]+';
                The regular expression engine defined for regular expression finds/replaces in file with using find/replace function of UltraEdit is the UltraEdit regular expression engine. This line just creates in memory on first iteration of the loop the UltraEdit tagged regular expression search string: ^(<equation-count count="^)[0-9]+

                The (fixed) string found by regular expression replace enclosed in ^(...^) is back-referenced in the replace string with ^1 to keep it unmodified in file as just the additionally matched number after this string should be replaced by the new count.

                Code: Select all

                var sReplace = '^1' + nCount.toString(10);
                This line just creates in memory the UltraEdit regular expression replace string ^1x whereby I use here x as placeholder for the count number converted explicitly to a string using base 10 (decimal system).

                When a variable of type number is concatenated with a string to a larger string, JavaScript interpreter uses implicitly number method toString with base 10. So it would be also possible to use:

                Code: Select all

                var sReplace = '^1' + nCount;
                But I prefer to call explicitly toString(10) to make it easier for beginners in JavaScript coding to see what happens here.
                Best regards from an UC/UE/UES for Windows user from Austria