Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting large txt file into small ones based on specific content
    primarykey
    data
    text
    <p>I got a large sequence of genome and I need to split it into small .txt files.</p> <p>Sequence looks like this</p> <pre><code>&gt;supercont1.1 of Geomyces destructans 20631-21 AGATTTTCTTAATAACTTGTTCAATGTGTGTTCAAATGATATGCCGTGATGTATGTAGCA TAAACAGATGTAGTAGAAGAGTTTGCAGCAATCGTTGAGTAGTATTGCTTCTGTTGTTGG &gt;supercont1.2 of Geomyces destructans 20631-21 AGATTTTCTTAATAACTTGTTCAATGTGTGTTCAAATGATATGCCGTGATGTATGTAGCA TAAACAGATGTAGTAGAAGAGTTTGCAGCAATCGTTGAGTAGTATTGCTTCTGTTGTTGG TAAACAGATGTAGTAGAAGAGTTTGCAGCAATCGTTGAGTAGTATTGCTTCTGTTGTTGG &gt;supercont1.3 of Geomyces destructans 20631-21 AGATTTT (...) </code></pre> <p>And it should be splitted into small files with names: "1.1-Geomyces-destructans--20631-21", "1.2-Geomyces..." fulfilled with genome data.</p> <p>My code after @JimMischel help looks like: </p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace genom1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string filter = "Textové soubory|*.txt|Soubory FASTA|*.fasta|Všechny soubory|*.*"; private void doit_Click(object sender, EventArgs e) { bar.Value = 0; OpenFileDialog opf = new OpenFileDialog(); // filter for choosing file types opf.Filter = filter; string lineo = "error"; // test if (opf.ShowDialog() == DialogResult.OK) { var lineCount = 0; using (var reader = File.OpenText(opf.FileName)) { while (reader.ReadLine() != null) { lineCount++; } } bar.Maximum = lineCount; bar.Step = 1; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Vyber složku, do které chceš rozdělit načtený soubor: \n\n" + opf.FileName; // dialog desc if (fbd.ShowDialog() == DialogResult.OK) { List&lt;string&gt; lines = new List&lt;string&gt;(); foreach (var line in File.ReadLines(opf.FileName)) { bar.PerformStep(); if (line[0] == '&gt;') { if (lines.Count &gt;= 0) { // write contents of lines list to file //quicker replace for better file name StringBuilder prep = new StringBuilder(line); prep.Replace("&gt;supercont", ""); prep.Replace("of", ""); prep.Replace(" ", "-"); lineo = prep.ToString(); // append or writeall? how to writeall lines without append? //System.IO.File.WriteAllText(fbd.SelectedPath + "\\" + lineo + ".txt", lineo); StreamWriter SW; SW = File.AppendText(fbd.SelectedPath + "\\" + lineo + ".txt"); foreach (string s in lines) { SW.WriteLine(s); } SW.Close(); // and clear the list. lines.Clear(); } } lines.Add(line); } // here, do the last part if (lines.Count &gt;= 0) { // write contents of lines list to file. /* starts being little buggy here... StreamWriter SW; SW = File.AppendText(fbd.SelectedPath + "\\" + lineo + ".txt"); foreach (string s in lines) { SW.WriteLine(s); } SW.Close(); */ } } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload