/EMBOSS-6.4.0/emboss/makenucseq.c

# · C · 213 lines · 107 code · 40 blank · 66 comment · 10 complexity · 766ee8dd5c4e8b979341389efb0d37bc MD5 · raw file

  1. /******************************************************************************
  2. ** @source makeseq.c application
  3. **
  4. ** Create random sequence.
  5. **
  6. ** @author Copyright (C) 2004 Henrikki Almusa, Medicel Oy
  7. ** @@
  8. **
  9. ** This program is free software; you can redistribute it and/or
  10. ** modify it under the terms of the GNU General Public License
  11. ** as published by the Free Software Foundation; either version 2
  12. ** of the License, or (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. ******************************************************************************/
  23. #include "emboss.h"
  24. static AjPStr makenucseq_random_sequence(AjPStr const* seqchar,
  25. ajint scmax, ajint length);
  26. static void makenucseq_default_chars(AjPList* list);
  27. /* @prog makenucseq ***********************************************************
  28. **
  29. ** Creates a random nucleotide sequence. Can use cusp codon distribution to
  30. ** specify sequence composition.
  31. **
  32. ******************************************************************************/
  33. int main(int argc, char **argv)
  34. {
  35. AjPSeqout outseq = NULL;
  36. AjPList list = NULL;
  37. AjPSeq seq = NULL;
  38. AjPStr insert = NULL;
  39. AjPStr seqstr = NULL;
  40. AjPStr* seqr = NULL;
  41. AjPCod codondata = NULL;
  42. ajint start = 0;
  43. ajint length = 0;
  44. ajint amount = 0;
  45. ajint scmax = 0;
  46. ajint extra = 0;
  47. embInit("makenucseq", argc, argv);
  48. codondata= ajAcdGetCodon("codonfile");
  49. insert = ajAcdGetString("insert");
  50. start = ajAcdGetInt("start");
  51. length = ajAcdGetInt("length");
  52. amount = ajAcdGetInt("amount");
  53. outseq = ajAcdGetSeqoutall("outseq");
  54. list = ajListstrNew();
  55. /* this is checked by acd
  56. if(amount <=0 || length <= 0)
  57. ajFatal("Amount or length is 0 or less. "
  58. "Unable to create any sequences"); */
  59. /* if insert, make sure sequence is large enough */
  60. if(ajStrGetLen(insert))
  61. {
  62. length -= ajStrGetLen(insert);
  63. /* start= start <= 1 ? 0 : --start; */ /* checked in acd */
  64. start--;
  65. if(length <= 0)
  66. ajFatal("Sequence smaller than inserted part. "
  67. "Unable to create sequences.");
  68. }
  69. /* make the list of AjPStr to be used in sequence creation */
  70. if(codondata)
  71. {
  72. ajCodGetCodonlist(codondata, list);
  73. /* length is length in nucleotides, random sequence is
  74. made of triplets */
  75. extra = length % 3;
  76. length /= 3;
  77. if(extra)
  78. {
  79. length++;
  80. extra = 0 - (3 - extra);
  81. }
  82. }
  83. else
  84. makenucseq_default_chars(&list);
  85. /* if insert, make sure type is correct */
  86. /* typechecking code is not working, uncomment and test after it is
  87. if(ajStrGetLen(insert))
  88. {
  89. seqstr = ajStrNew();
  90. ajStrAssignC(&seqstr,"purenucleotide");
  91. if(!ajSeqTypeCheckS(&insert,seqstr))
  92. ajFatal("Insert not the same sequence type as sequence itself.");
  93. ajStrDel(&seqstr);
  94. } */
  95. /* array allows fast creation of a sequences */
  96. scmax = ajListstrToarray(list,&seqr);
  97. if(!scmax)
  98. ajFatal("No strings in list. No characters to make the sequence.");
  99. ajRandomSeed();
  100. while(amount-- > 0)
  101. {
  102. seqstr = makenucseq_random_sequence(seqr,scmax,length);
  103. if(ajStrGetLen(insert))
  104. ajStrInsertS(&seqstr,start,insert);
  105. ajStrFmtLower(&seqstr);
  106. seq = ajSeqNew();
  107. ajStrExchangeSetCC(&seqstr,"u","t");
  108. if(extra < 0)
  109. ajStrCutEnd(&seqstr,-extra);
  110. ajSeqAssignSeqS(seq, seqstr);
  111. ajSeqSetNuc(seq);
  112. ajSeqoutWriteSeq(outseq, seq);
  113. ajSeqDel(&seq);
  114. ajStrDel(&seqstr);
  115. }
  116. ajSeqoutClose(outseq);
  117. ajSeqoutDel(&outseq);
  118. ajListstrFreeData(&list);
  119. ajStrDel(&insert);
  120. ajCodDel(&codondata);
  121. AJFREE(seqr);
  122. embExit();
  123. return 0;
  124. }
  125. /* @funcstatic makenucseq_random_sequence *************************************
  126. **
  127. ** Creates string containing random sequence from given character distribution.
  128. **
  129. ** @param [r] seqchar [AjPStr const*] Characters use to make sequence from
  130. ** @param [r] scmax [ajint] lenght of the seqchar string
  131. ** @param [r] length [ajint] Length of the wanted sequence
  132. ** @return [AjPStr] Sequence string
  133. ** @@
  134. ******************************************************************************/
  135. static AjPStr makenucseq_random_sequence(AjPStr const* seqchar,
  136. ajint scmax, ajint length)
  137. {
  138. AjPStr seq = ajStrNew();
  139. ajint idx = 0;
  140. ajint len = length;
  141. while(len-- > 0)
  142. {
  143. idx = (ajint) (ajRandomDouble()*scmax);
  144. ajStrAppendS(&seq,seqchar[idx]);
  145. }
  146. return seq;
  147. }
  148. /* @funcstatic makenucseq_default_chars ***************************************
  149. **
  150. ** Crates equal distribution of characters for completely random sequences.
  151. **
  152. ** @param [w] list [AjPList*] List with character distributions
  153. ** @return [void]
  154. ** @@
  155. ******************************************************************************/
  156. static void makenucseq_default_chars(AjPList* list)
  157. {
  158. int i;
  159. int max;
  160. char *chars;
  161. char seqCharNucPure[] = "ACGTacgt";
  162. int seqCharNucPureLength = 8;
  163. AjPStr tmp;
  164. chars = seqCharNucPure;
  165. max = seqCharNucPureLength;
  166. for(i = 0; i < max; i++)
  167. {
  168. tmp = ajStrNew();
  169. tmp = ajFmtStr("%c",chars[i]);
  170. ajListstrPushAppend(*list,tmp);
  171. }
  172. return;
  173. }